一文搞定Numpy快速入门
程序员文章站
2022-07-13 09:00:24
...
1 基本数据结构
>>> import numpy as np
>>> ar=np.array([1,2,2,3,9])
>>> print(ar)
[1 2 2 3 9]
>>> print(ar.ndim) #轴数
1
>>> print(ar.shape) #维度,n行m列
(5,)
>>> print(ar.size) #元素个数
5
>>> print(ar.dtype) #元素类型
int32
>>> print(np.arange(5,12,2)) #随机数生成器,(首元,末元,步长)
[ 5 7 9 11]
>>> print(np.linspace(2.0,3.0,num=5)) #均匀间隔随机数生成器,(首元,末元,个数)
[2. 2.25 2.5 2.75 3. ]
>>> print(np.zeros((2,2),dtype=np.int)) #元素为0多维数组,ones类似
[[0 0]
[0 0]]
>>> ar3 = np.array([list(range(5)),list(range(5,10))])
>>> ar4=np.zeros_like(ar3) #元素为0多维复制维度数组,ones_like类似
>>> print(ar4)
[[0 0 0 0 0]
[0 0 0 0 0]]
>>> print(np.eye(5)) #多维单位阵
[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]]
2 索引及切片
>>> ar = np.arange(20) #一维数组
>>> print(ar[4]) #索引
4
>>> print(ar[4:7]) #切片,左闭右开
[4 5 6]
>>> ar = np.arange(16).reshape(4,4) #二维数组
>>> print(ar[:2,1:]) #切片
[[1 2 3]
[5 6 7]]
>>> print(ar[2]) #索引
[ 8 9 10 11]
>>> ar = np.arange(8).reshape(2,2,2) #三维数组
>>> print(ar[0]) #索引
[[0 1]
[2 3]]
>>> print(ar[0][0]) #索引
[0 1]
>>> print(ar[0][0][1]) #索引
1
>>> ar = np.arange(10)
>>> b = ar.copy() #复制,传值
>>> b[7:9] = 200 #附新值
>>> print(ar)
[0 1 2 3 4 5 6 7 8 9]
>>> print(b)
[ 0 1 2 3 4 5 6 200 200 9]
3 随机数
>>> sample = np.random.normal(size=(4,4)) #正态分布随机数
>>> print(sample)
[[ 2.24138154e-01 1.30289275e+00 -1.75193696e+00 -3.72794238e-02]
[ 2.90698158e-01 -1.22481090e+00 -1.61384828e-03 -8.41389311e-01]
[ 2.41947711e+00 2.42897439e-01 1.80080970e+00 -8.58082546e-01]
[-8.78980085e-01 1.62853108e+00 -1.78333195e+00 -5.00738236e-02]]
>>> b = np.random.rand(4) #4个[0,1)的随机浮点数,均匀分布
>>> print(b,type(b))
[0.78363867 0.22277917 0.24412891 0.84483113] <class 'numpy.ndarray'>
>>> c = np.random.rand(2,3) #2x3个[0,1)的随机浮点数,均匀分布
>>> print(c,type(c))
[[0.34339409 0.10286506 0.75058407]
[0.92130664 0.84412402 0.77756031]] <class 'numpy.ndarray'>
>>> b = np.random.randn(4) #4个[0,1)的随机浮点数,正态分布
>>> print(b,type(b))
[ 0.04014725 -1.85238454 -0.31065846 -0.34918658] <class 'numpy.ndarray'>
>>> c = np.random.randn(2,3) #2x3个[0,1)的随机浮点数,正态分布
>>> print(c,type(c))
[[ 1.18949625 -0.85708013 1.23795057]
[ 0.0868969 3.26636273 0.08522144]] <class 'numpy.ndarray'>
>>> print(np.random.randint(2,6,size=5)) #随机整数 (首元,末元,个数)
[4 3 3 2 5]
>>> print(np.random.randint(2,6,(2,3))) #随机整数 (首元,末元,维度)
[[4 3 4]
[5 3 3]]
4 常用函数
>>> ar2 = np.ones((5,2))
>>> print(ar2,'\n',ar2.T) #转置
[[1. 1.]
[1. 1.]
[1. 1.]
[1. 1.]
[1. 1.]]
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
>>> ar6 = np.resize(np.arange(5),(3,4)) #重新定义形状
>>> print(ar6)
[[0 1 2 3]
[4 0 1 2]
[3 4 0 1]]
>>> ar1 = np.arange(10,dtype=float)
>>> print(ar1,ar1.dtype)
[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.] float64
>>> ar2 = ar1.astype(np.int32) #转换元素类型
>>> print(ar2,ar2.dtype)
[0 1 2 3 4 5 6 7 8 9] int32
>>> a = np.arange(5)
>>> b = np.arange(5,9)
>>> ar1 = np.hstack((a,b)) #水平堆叠(按列),注意形状
>>> print(ar1,ar1.shape)
[0 1 2 3 4 5 6 7 8] (9,)
>>> a = np.arange(5)
>>> b = np.arange(5,10)
>>> ar2 = np.vstack((a,b)) #垂直堆叠(按行),注意形状
>>> print(ar2,ar2.shape)
[[0 1 2 3 4]
[5 6 7 8 9]] (2, 5)
>>> ar1 = np.stack((a,b)) #堆叠,轴为0按列
>>> ar2 = np.stack((a,b),axis = 1) #堆叠,轴为1按行
>>> print(ar1,ar1.shape)
[[0 1 2 3 4]
[5 6 7 8 9]] (2, 5)
>>> print(ar2,ar2.shape)
[[0 5]
[1 6]
[2 7]
[3 8]
[4 9]] (5, 2)
>>> ar = np.arange(16).reshape(4,4)
>>> ar1 = np.hsplit(ar,2) #水平拆分(按列),注意形状
>>> print(ar1,type(ar1))
[array([[ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13]]), array([[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15]])] <class 'list'>
>>> ar2 = np.vsplit(ar,4) #垂直拆分(按行),注意形状
>>> print(ar2,type(ar2))
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]]), array([[12, 13, 14, 15]])] <class 'list'>
>>> ar = np.arange(6).reshape(2,3)
>>> print(ar + 10) #加法
[[10 11 12]
[13 14 15]]
>>> print(ar * 2) #乘法
[[ 0 2 4]
[ 6 8 10]]
>>> print(1 / (ar+1)) #除法
[[1. 0.5 0.33333333]
[0.25 0.2 0.16666667]]
>>> print(ar ** 0.5)
[[0. 1. 1.41421356]
[1.73205081 2. 2.23606798]]
>>> print(ar.mean()) #求平均
2.5
>>> print(ar.max()) #最大值
5
>>> print(ar.min()) #最小值
0
>>> print(ar.std()) #标准差
1.707825127659933
>>> print(ar.var()) #方差
2.9166666666666665
>>> print(ar.sum(), np.sum(ar,axis = 0)) #求和,轴为0按列求和
15 [3 5 7]
>>> print(np.sort(np.array([1,4,3,2,5,6]))) #排序
[1 2 3 4 5 6]
上一篇: HBase单节点部署
下一篇: 一文搞定babel转换