1.数组索引
arr = np.arange(10)
print(arr[1])
2.取偶数
print(arr[::2])
print(arr[-1])
3.二维数组索引
arr2 = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]])
print(arr2)
print(arr2[1, 3]) # 9
print(arr2[1, 2:]) # 8,9,10
print(arr2[1:, :2]) # 6,7,11,12
#前面元祖放行,后面元祖放列
print(arr2[(0, 0, 1, 1), (0, 1, 3, 4)])
print(arr2[(0, 2), (0, 3)]) # 1,14
4.bool值取值:
mask = np.array([1, 0, 1], dtype=np.bool)
print(mask)
print(arr2[mask, 2])
mask1 = np.array([0, 1, 1], dtype=np.bool)
mask2 = np.array([0, 1, 0, 1, 0], dtype=np.bool)
print(arr2[mask1, mask2]) # 注意:一一对应取法:类似于元祖取法
5.数组变换形态
arr = np.arange(12)
arr2 = arr.reshape(3, 4)
print(‘数组展平后’, arr2.ravel())
6.数组组合
arr1 = np.arange(12).reshape(3, 4)
arr3 = 3 * arr1
print(arr3)
print(‘横向组合:\n’, np.hstack((arr3, arr2)))
print(‘纵向组合:\n’, np.vstack((arr3, arr2)))
7.concatenate
print(‘横向组合:\n’, np.concatenate((arr1, arr2), axis=1)) # 1表示横向
print(‘纵向组合:\n’, np.concatenate((arr1, arr2), axis=0)) # 0表示纵向
8.数组分割
arr = np.arange(16).reshape(4, 4)
print(arr)
print(‘横向分割\n’,np.hsplit(arr,2))
print(‘纵向分割\n’,np.vsplit(arr,2))
print(np.split(arr,2,axis=0))#纵向切割
print(np.split(arr,2,axis=1))#横向切割
数组是numpy.ndarray类型
9.矩阵创建,矩阵是numpy.matrix类型
matr = np.mat(“1 2 3;4 5 6;7 8 9”)
print(type(matr))
matr2 = np.matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(matr2)
arr1 = np.eye(3)
arr2 = 3 * np.eye(3)
print(arr1)
print(arr2)
print(np.bmat(‘arr1 arr2;arr2 arr1’))
matrix2 = matr + matr2
matrix1 = matr - matr2
print(matrix2)
print(matrix1)
print(matrmatr2)
print(np.multiply(matr,matr2))
10.矩阵转置
print(matr.T)
11.矩阵求逆
print(matr.I)
12.矩阵运算
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
print(x+y)#加
print(x-y)#减
print(xy)#乘
print(‘幂运算:\n’,x**y)
print(‘除法运算:\n’,x/y)
#比较运算
print(x>y)
print(x<y)
print(x==y)
print(x!=y)
print(x>=y)
print(x<=y)
a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
#取大于5的元素
b = a[a > 5]
print(b)
#取偶数
c = a[a % 2 == 0]
print©
13.unfunc的广播机制
条件a:n*m;
b:一行(元素个数等于a的列数)或者一列(行数等于a的行数);
arr3 = np.array([[0, 0, 0], [1, 1, 1], [2, 2, 2]])
arr4 = np.array([1, 2, 3]).reshape((3, 1))
print(arr4)
print(arr3 + arr4)
arr5 = np.array(5)
print(arr3 + arr5)
14.numpy读文件
arr=np.arange(100).reshape((10,10))
np.save(‘arr’,arr)
.npy二进制文件,存放的是一个数组;存储的时候,文件可以不声明后缀
arr2=np.arange(100).reshape((20,5))
np.savez(‘arraz’,arr,arr2)
返回.npz文件
15.读取文件
data = np.load(‘arr.npy’)
print(data)
data1 = np.load(‘arraz.npz’)
print(data1[‘arr_0’])
print(data1[‘arr_1’])
16.文本文件读取:.txt, .csv
np.save(‘arr.txt’, arr, fmt=’%d’, delimiter=’,’)
delimiter必须得有
data2 = np.loadtxt(‘arr.txt’, delimiter=’,’)
print(data2)
17.常用统计函数
arr = np.random.randint(1, 10, size=10)
print(arr)
arr.sort()
print(‘排序后’, arr)
arr1 = np.random.randint(1, 10, size=(3, 3))
arr1.sort(axis=0)
print(arr1)
arr1.sort(axis=1)
print(arr1)
arr6 = np.random.randint(1, 10, size=10)
print(arr6)
print(arr6.argsort()) # 返回下标
print(arr[arr.argsort()[:5]])
18.lexsort():根据某一列数组排序
a = np.array([2, 3, 6, 4, 5])
b = np.array([50, 30, 40, 20, 10])
c = np.array([400, 300, 600, 100, 200])
d = np.lexsort((a, b, c))
print(d)
print(list(zip(a[d], b[d], c[d])))
arr7 = np.array([‘小红’, ‘小明’, ‘小强’, ‘小红’, ‘小明’, ‘小强’, ‘小红’, ‘小红’])
19.去重:set
print(‘去重后数组为:’, np.unique(arr7))
20.重复
arr8 = np.arange(10).reshape((5, 2))
print(np.tile(arr8, 3)) # 针对整体数组进行重复
print(np.tile(arr7, 3))
21.对元素进行重复:repeat
print(arr7.repeat(2,axis=0))
#统计函数
22.所有元素求和
print(np.sum(arr3))
print(arr3.sum())
23.纵向求和
print(arr3.sum(axis=0))
24.横向求和
print(arr3.sum(axis=1))
25.平均值
print(np.mean(arr3))
print(arr3.mean(axis=0))
26.方差
print(arr3.var(axis=0))
print(arr3.std(axis=0))
27.最小值
print(arr3.min())
28.最大值
print(arr3.max())
29.最小值下标
print(arr3.argmin())
30.最大值下标
print(arr3.argmax())
31.累计和
print(arr3.cumsum())
32.累计积
print(arr3.cumprod())