numpy常用接口

txt文件地址:
1.txt文件导入数据

#导入numpy库
import numpy

#导入一个txt文件,该文件行内容以","分割。
world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",", dtype=str)
print(type(world_alcohol))
print(world_alcohol)

2 .构建向量和矩阵

#The numpy.array() function can take a list or list of lists as input. When we input a list, we get a one-dimensional array as a result:
vector = numpy.array([5, 10, 15, 20])
#When we input a list of lists, we get a matrix as a result:
matrix = numpy.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]])
print(vector)
print(matrix)

3.查看矩阵的规格

#We can use the ndarray.shape property to figure out how many elements are in the array
vector = numpy.array([1, 2, 3, 4])
print(vector.shape)
#For matrices, the shape property contains a tuple with 2 elements.
matrix = numpy.array([[5, 10, 15], [20, 25, 30]])
print(matrix.shape)

4.查看矩阵元素类型

#Each value in a NumPy array has to have the same data type
#NumPy will automatically figure out an appropriate data type when reading in data or converting lists to arrays. 
#You can check the data type of a NumPy array using the dtype property.
numbers = numpy.array([1, 2, 3, 4])
print(numbers)
print(numbers.dtype)

5.访问特定行,列的值

uruguay_other_1986 = world_alcohol[1,4]
third_country = world_alcohol[2,2]
print(uruguay_other_1986)
print(third_country)

6.取向量的n个数据

vector = numpy.array([5, 10, 15, 20])
print(vector[0:3])  

7.取矩阵的的某一列数据


matrix = numpy.array([
                    [5, 10, 15], 
                    [20, 25, 30],
                    [35, 40, 45]
                 ])
print(matrix[:,1])

8.取矩阵的n列数据

matrix = numpy.array([
                    [5, 10, 15], 
                    [20, 25, 30],
                    [35, 40, 45]
                 ])
print(matrix[:,0:2])

9.取矩阵的n行数据

matrix = numpy.array([
                    [5, 10, 15], 
                    [20, 25, 30],
                    [35, 40, 45]
                 ])
print(matrix[1:3,0:2])

10.判断矩阵中是否有特定的值

matrix = numpy.array([
                    [5, 10, 15], 
                    [20, 25, 30],
                    [35, 40, 45]
                 ])
print(matrix == 25)

11.根据判断特定值的布尔矩阵找到对应行数据

matrix = numpy.array([
                [5, 10, 15], 
                [20, 25, 30],
                [35, 40, 45]
             ])
second_column_25 = (matrix[:,1] == 25)
print(second_column_25)
print(matrix[second_column_25, :])

12.与或操作

vector = numpy.array([5, 10, 15, 20])
equal_to_ten_or_five = (vector == 10) | (vector == 5)
print(equal_to_ten_or_five)

13.修改特定值

matrix = numpy.array([
            [5, 10, 15], 
            [20, 25, 30],
            [35, 40, 45]
         ])
second_column_25 = matrix[:,1] == 25
print(second_column_25)
matrix[second_column_25, 1] = 10
print(matrix)

14.类型转换

#We can convert the data type of an array with the ndarray.astype() method.
vector = numpy.array(["1", "2", "3"])
print(vector.dtype)
print(vector)
vector = vector.astype(float)
print(vector.dtype)
print(vector)

15.行求和

matrix = numpy.array([
                [5, 10, 15], 
                [20, 25, 30],
                [35, 40, 45]
             ])
matrix.sum(axis=1)

16.列求和

matrix = numpy.array([
                [5, 10, 15], 
                [20, 25, 30],
                [35, 40, 45]
             ])
matrix.sum(axis=0)

17对导入的txt数据 求和,求平均数

#replace nan value with 0
world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",")
#print world_alcohol
is_value_empty = numpy.isnan(world_alcohol[:,4])
#print(is_value_empty)

world_alcohol[is_value_empty, 4] = '0'

alcohol_consumption = world_alcohol[:,4]
alcohol_consumption = alcohol_consumption.astype(float)

total_alcohol = alcohol_consumption.sum()
average_alcohol = alcohol_consumption.mean()
print(total_alcohol)
print(average_alcohol)

18.矩阵的随机生成

import numpy as np
print(np.arange(15))
a = np.arange(15).reshape(3, 5)
print(a)

19.查看矩阵的规格,维度,类型,元素个数

print(a.shape)
print(a.ndim)
print(a.dtype.name)
print(a.size)

20.初始化矩阵

print(np.zeros((3,4)))
print(np.ones((2,3,4), dtype=np.int32))

21.根据起始值 结束值 步长 构建矩阵

#To create sequences of numbers
#>=起始值 <结束值 步长
np.arange(10, 30, 5)

22.重新指定矩阵规格

print(np.arange(12).reshape(4,3))

23.构建随机矩阵

print(np.random.random((2,3)))

24.根据起始值,结束值,总元素个数构建矩阵

#<=起始值  >=结束值 总个数(平均取值)
print(np.linspace(0, 2*pi, 100))

25.对矩阵进行加减乘操作

#the product operator * operates elementwise in NumPy arrays
a = np.array([20,30,40,50])
b = np.arange(4)
print(a)
print(b)

c = a-b
print(c)

print(b**2)

print(a<35)

26.矩阵点乘运算

#The matrix product can be performed using the dot function or method
A = np.array( [[1,1],
               [0,1]] )

B = np.array( [[2,0],
               [3,4]] )
print(A)
print(B)
print(20*'-')

#对应位置相乘
print(A*B)
print(20*'-')

#矩阵相乘
print(A.dot(B))
print(20*'-')

#矩阵相城
print(np.dot(A, B))

27.乘方和根号运算

import numpy as np
B = np.arange(3)
print(B)

#求e的n次幂
print(np.exp(B))

#求根号
print(np.sqrt(B))

28.取整,矩阵转换向量,转置等运算

#Return the floor of the input
#random:本身是-1到+1之间的随机值; floor:向下取整
a = np.floor(10*np.random.random((3,4)))
print(a)
print(a.shape)
print(20*'-')

## flatten the array
#把矩阵转换为一个向量
print(a.ravel())

a.shape = (6, 2)
print(a)
print(20*'-')

#求矩阵转置
print(a.T)
print(20*'-')

print(a.resize((2,6)))
print(a)

#If a dimension is given as -1 in a reshaping operation, the other dimensions are automatically calculated:
#行确定后,列会自动计算
print(a.reshape(3,-1))

29.矩阵的拼接

a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2)))
print(a)
print('---')
print(b)
print('---')

#按行进行拼接矩阵
print(np.hstack((a,b)))
print(20*'-')
#按列拼接
print(np.vstack((a,b)))

30.矩阵的分割

a = np.floor(10*np.random.random((2,12)))
print(a)
print(20*'-')

#横着切
print(np.hsplit(a,3))

#指定位置进行切 列号
print(np.hsplit(a,(3,4)))   # Split a after the third and the fourth column
print(20*'-')

#竖着切
a = np.floor(10*np.random.random((12,2)))
print(a)
np.vsplit(a,3)

31.浅拷贝

#Simple assignments make no copy of array objects or of their data.
#等号:指向同一块内存,值和规格都一样。 浅拷贝
a = np.arange(12)
b = a
# a and b are two names for the same ndarray object
print(b is a)

b.shape = 3,4
print(a.shape)

print(id(a))
print(id(b))

32.数据拷贝

#The view method creates a new array object that looks at the same data.
#view:共用一堆数据值
c = a.view()
print(c is a)

c.shape = 2,6
print(a.shape)

c[0,4] = 1234
print(a)

33.深拷贝

#The copy method makes a complete copy of the array and its data.
#copy:两者没有关系 深拷贝
d = a.copy() 
print(d is a)

d[0,0] = 9999
print(d)
print(a)

34.找出行最大值

import numpy as np
data = np.sin(np.arange(20)).reshape(5,4)
print(data)

#按列进行计算,找出最大值的位置
ind = data.argmax(axis=0)
print(ind)

#通过位置,拿到行最大值
data_max = data[ind, range(data.shape[1])]
print(data_max)
print(all(data_max == data.max(axis=0)))

35.矩阵行,列数据扩展

a = np.arange(0, 40, 10)
print(a)

#行 列都扩展n倍数量
b = np.tile(a, (2, 2)) 
print(b)

36.矩阵大小排序

a = np.array([[4, 3, 5], [1, 2, 1]])
print(a)

#按行进行计算,大小排序
b = np.sort(a, axis=1)
print(b)

print(20*'-')
a = np.array([4, 3, 1, 2])

#求最小值的索引[最小值(值为索引值)排序]
j = np.argsort(a)
print(j)

#打印值
print(a[j])
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值