内容参考:Numpy库常用函数大全
数组的属性
np.ndim :维度
np.shape :各维度的尺度
np.size :元素的个数
np.dtype :元素的类型
np.itemsize :每个元素的大小,以字节为单位 ,每个元素占4个字节
栗子:
import numpy as np
n = np.array([2,5,7,3])
n
array([2, 5, 7, 3])
n.ndim
1
n.shape
(4,)
n.size
4
n.dtype
dtype('int64')
n.itemsize
8
数组的创建
np.arange(n) :元素从0到n-1的ndarray类型
np.empty(shape) :生成空数组
np.ones(shape): 生成全为1的矩阵
np.zeros((shape), ddtype = np.int32) : 生成int32型的全为0的矩阵
np.full(shape, 5): 生成全为5的矩阵
np.eye(n) : 生成单位矩阵
np.ones_like(a) : 按数组a的形状生成全1的数组
np.zeros_like(a): 同理
np.full_like (a, val) : 同理
np.linspace(1,10,4): 根据起止数据等间距地生成数组
np.linspace(1,10,4, endpoint = False):endpoint 表示10是否作为生成的元素
np.logspace(0,2,10,base=10/2) :生成0到2之间10个以10/2为底的等比数列
np.concatenate((arr1,arr2),axis=0/1):数组的拼接,两个数组的形状要一样
扩展小知识:单位矩阵
在矩阵乘法中,有一种矩阵起着特殊的作用,如同数的乘法中的1,这种矩阵被称为单位矩阵。它是个方阵,从左上角到右下角的对角线(称为主对角线)上的元素均为1。除此以外全都为0。
根据单位矩阵的特点,任何矩阵与单位矩阵相乘都等于本身,而且单位矩阵因此独特性在高等数学中也有广泛应用
栗子:
#指定数组的类型
a = np.array([[1,2], [3,4]], dtype=complex)
a
array([[1.+0.j, 2.+0.j],
[3.+0.j, 4.+0.j]])
#np.zeros((shape),dtype = np.int64) 生成int64型的全0矩阵
b = np.zeros((3,4),dtype = np.int64)
b
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]])
b.dtype
dtype('int64')
#np.ones((2,3),dtype = np.int32) 生成int32型的全1矩阵
c = np.ones((2,3),dtype = np.int32)
c
array([[1, 1, 1],
[1, 1, 1]], dtype=int32)
#np.empty(shape) 生成空数组
d = np.empty((3,4))
d
array([[1.69743644e-316, 0.00000000e+000, 0.00000000e+000,
0.00000000e+000],
[0.00000000e+000, 0.00000000e+000, 0.00000000e+000,
0.00000000e+000],
[0.00000000e+000, 0.00000000e+000, 0.00000000e+000,
0.00000000e+000]])
#np.arange(n) 元素从0到n-1的ndarray类型
e = np.arange(5)
e
array([0, 1, 2, 3, 4])
e.dtype
dtype('int64')
#np.full(shape, n): 生成全为n的矩阵
f = np.full((3,5),5)
f
array([[5, 5, 5, 5, 5],
[5, 5, 5, 5, 5],
[5, 5, 5, 5, 5]])
#生成单位矩阵
g = np.eye(4)
g
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
#np.linspace(1,10,4): 根据起止数据等间距地生成数组,包括10在内
h = np.linspace(1,10,4) #(10-1)/(4-1) = 3
h
array([ 1., 4., 7., 10.])
#np.linspace(1,10,4): 根据起止数据等间距地生成数组,endpoint 表示10是否作为生成的元素,不包括10
i = np.linspace(1,10,4,endpoint=False) #(10-1)/4=2.25
i
array([1. , 3.25, 5.5 , 7.75])
#默认以10为底的等比数列
np.logspace(0,9,10)
array([1.e+00, 1.e+01, 1.e+02, 1.e+03, 1.e+04, 1.e+05, 1.e+06, 1.e+07,
1.e+08, 1.e+09])
#把底数变为2
np.logspace(0,9,10,base=2)
array([ 1., 2., 4., 8., 16., 32., 64., 128., 256., 512.])
#np.concatenate() 数组的拼接
arr2 = np.array([[7,5],[6,1]])
arr3 = np.array([[2,3,4],[5,6,7]])
con3 = np.concatenate((arr2,arr3),axis =1 ) #1行相同,列不同,追加列
con3
array([[7, 5, 2, 3, 4],
[6, 1, 5, 6, 7]])
arr4 = np.array([[2,5],[3,6],[8,5]])
con4 = np.concatenate((arr2,arr4),axis = 0) #0列相同,行不同,追加行
con4
array([[7, 5],
[6, 1],
[2, 5],
[3, 6],
[8, 5]])
数组的随机函数
rand(nd0,nd1... )创建随机数数组,浮点数,[0, 1),均匀分布
randn(nd0,nd1... )创建随机函数数组,标准正态分布
randint(low, high, shape )根据shape创建随机整数或整数数组,范围是[low, high)
seed(n) 随机数种子,seed值的有效次数仅为一次,若要保证每次产生的随机数相同,则需要在调用随机数函数之前再次使用相同的seed值
shuffle(a) : 根据数组a的第一轴进行随机排列,改变数组a
permutation(a) : 根据数组a的第一轴进行随机排列, 但是不改变原数组,将生成新数组
choice(a[, size, replace, p]) : 从一维数组a中以概率p抽取元素, 形成size形状新数组,replace表示是否可以重用元素,默认为False
uniform(low, high, size) : 产生均匀分布的数组,起始值为low,high为结束值,size为形状
normal(loc, scale, size) : 产生正态分布的数组, loc为均值,scale为标准差,size为形状
poisson(lam, size) : 产生泊松分布的数组, lam随机事件发生概率,size为形状
栗子:
#rand(nd0,nd1... )创建随机数数组,浮点数,[0, 1),均匀分布,生成各个纬度的数量
a = np.random.rand(1,2,3,4)
a
array([[[[9.18018356e-04, 4.87832688e-01, 8.76879929e-01,
7.67476807e-01],
[2.17625454e-01, 7.78172886e-01, 7.14865908e-02,
5.32607565e-01],
[1.35164249e-01, 9.54127941e-01, 8.70819401e-01,
1.79284373e-01]],
[[3.19823462e-01, 8.67019344e-01, 1.73814796e-01,
3.98941917e-01],
[1.19521004e-01, 9.41800920e-01, 9.31448299e-01,
5.85017892e-01],
[4.39802293e-01, 6.76682232e-01, 3.31631470e-01,
7.45757805e-01]]]])
#randn(nd0,nd1... )创建随机函数数组,标准正态分布
b = np.random.randn(1,2,3,4)
b
array([[[[ 2.17407732, -0.14816932, -0.05172504, 1.34213426],
[-0.32645533, -0.88477566, 1.77405832, -0.10384156],
[-1.73150275, 0.92107874, -1.40861082, -2.65470768]],
[[ 0.70930984, 1.2631603 , 0.85013548, 0.83539502],
[-0.47923866, -0.09688473, -0.76229436, -0.94953195],
[ 1.06997304, -1.80410918, -0.54808506, 1.74663608]]]])
#randint(low, high, shape )根据shape创建随机整数或整数数组,范围是[low, high)
c = np.random.randint(2,10,(4,6))
c
array([[6, 4, 7, 4, 7, 6],
[8, 7, 8, 4, 8, 4],
[5, 7, 4, 6, 9, 5],
[5, 8, 5, 3, 4, 7]])
#seed(n) 随机数种子,seed值的有效次数仅为一次,若要保证每次产生的随机数相同,则需要在调用随机数函数之前再次使用相同的seed值
np.random.seed(4)
d = np.random.randn(5,5)
d
array([[ 0.05056171, 0.49995133, -0.99590893, 0.69359851, -0.41830152],
[-1.58457724, -0.64770677, 0.59857517, 0.33225003, -1.14747663],
[ 0.61866969, -0.08798693, 0.4250724 , 0.33225315, -1.15681626],
[ 0.35099715, -0.60688728, 1.54697933, 0.72334161, 0.04613557],
[-0.98299165, 0.05443274, 0.15989294, -1.20894816, 2.22336022]])
e = np.random.randn(5,5) #np.random.normal(loc=0, scale=1, size)
e
array([[ 0.39429521, 1.69235772, -1.11281215, 1.63574754, -1.36096559],
[-0.65122583, 0.54245131, 0.04800625, -2.35807363, -1.10558404],
[ 0.83783635, 2.08787087, 0.91484096, -0.27620335, 0.7965119 ],
[-1.14379857, 0.50991978, -1.3474603 , -0.0093601 , -0.13070464],
[ 0.80208661, -0.30296397, 1.20200259, -0.19674528, 0.8365287 ]])
np.random.seed(4)
f = np.random.randn(5,5)
f
array([[ 0.05056171, 0.49995133, -0.99590893, 0.69359851, -0.41830152],
[-1.58457724, -0.64770677, 0.59857517, 0.33225003, -1.14747663],
[ 0.61866969, -0.08798693, 0.4250724 , 0.33225315, -1.15681626],
[ 0.35099715, -0.60688728, 1.54697933, 0.72334161, 0.04613557],
[-0.98299165, 0.05443274, 0.15989294, -1.20894816, 2.22336022]])
#shuffle(a) 根据数组a的第一轴进行随机排列,改变数组a
import numpy as np
a = np.random.randint(1,100,(3,4))
a
array([[50, 33, 40, 87],
[37, 64, 35, 79],
[41, 42, 11, 67]])
np.random.shuffle(a) #不可赋值
a
array([[41, 42, 11, 67],
[37, 64, 35, 79],
[50, 33, 40, 87]])
#permutation(a) 根据数组a的第一轴进行随机排列, 但是不改变原数组,将生成新数组
c = np.random.randint(100,200,(3,4))
c
array([[169, 144, 116, 192],
[119, 131, 186, 190],
[154, 159, 193, 189]])
d = np.random.permutation(c)
d
array([[119, 131, 186, 190],
[169, 144, 116, 192],
[154, 159, 193, 189]])
c
array([[169, 144, 116, 192],
[119, 131, 186, 190],
[154, 159, 193, 189]])
#choice(a[, size, replace, p]) 从一维数组a中以概率p抽取元素, 形成size形状新数组,replace表示是否可以重用元素,默认为False,choice默认可以重复。
e = np.random.randint(100,200,(5,))
e
array([114, 136, 174, 158, 132])
f = np.random.choice(e,(2,6))
f
array([[136, 114, 158, 158, 136, 174],
[174, 114, 174, 132, 136, 158]])
#把replace设定为False则随机选取的元素不能重复
g = np.random.choice(e,(2,2),replace=False) #(2,2)2*2必须比e的个数少
g
array([[136, 114],
[132, 158]])
#uniform(low, high, size) 产生均匀分布的数组,起始值为low,high为结束值,size为形状
h = np.random.uniform(1,5,(3))
h
array([4.37271996, 1.96223774, 4.5393332 ])
#normal(loc, scale, size) 产生正态分布的数组, loc为均值,scale为标准差,size为形状
i = np.random.normal(10,5,(3,4)) #均值是10,方差是5
i
array([[ 6.25376239, 12.70219689, 9.66993908, 20.41170146],
[ 8.09375048, 10.12743271, 3.93302745, 3.43204455],
[ 8.02339524, 7.56522517, 4.25262042, 8.27390636]])
#poisson(lam, size) 产生泊松分布的数组, lam随机事件发生概率,size为形状
j = np.random.poisson(3,(2,5))
j
array([[5, 4, 4, 3, 3],
[2, 1, 2, 5, 1]])
数组的类型转换
arr.astype()
栗子:
#将整数转为浮点数
In [8]: arr = np.array([1,2,3,4,5])
In [9]: arr
Out[9]: array([1, 2, 3, 4, 5])
In [10]: arr.dtype
Out[10]: dtype('int64')
In [11]: float_arr = arr.astype(np.float64)
In [12]: float_arr.dtype
Out[12]: dtype('float64')
In [13]: float_arr
Out[13]: array([1., 2., 3., 4., 5.])
#将浮点数转化为整数(去除了小数部分)
In [14]: arr = np.array([1.23,3.21,4.32,5.67775])
In [15]: arr.dtype
Out[15]: dtype('float64')
In [16]: arr.astype(np.int32)
Out[16]: array([1, 3, 4, 5], dtype=int32)
#将字符串转为数值
In [17]: arr = np.array(['1.2','2.34',' 4.3242'])
In [18]: arr
Out[18]: array(['1.2', '2.34', '4.3242'], dtype='<U6')
In [19]: arr.astype(float)
Out[19]: array([1.2 , 2.34 , 4.3242])
数组的索引/切片/级联/切分/副本
切片同list
np.hstack:水平级联
np.vstack :垂直级联
np.split
np.vsplit :垂直切分
np.hsplit :水平切分
栗子:
#索引
In [39]: import numpy as np
In [40]: nd1 = np.array([1,3,5,7,9,0])
In [41]: nd1[2]
Out[41]: 5
In [42]: nd1[-1]
Out[42]: 0
In [43]: nd1[2]
Out[43]: 5
In [44]: nd1[2] = 100
In [45]: nd1[-1] = 1024
#切片
In [46]: nd1
Out[46]: array([ 1, 3, 100, 7, 9, 1024])
In [47]: nd1[0:2]
Out[47]: array([1, 3])
In [48]: nd1[-3:-1]
Out[48]: array([7, 9])
In [49]: nd1[-3:]
Out[49]: array([ 7, 9, 1024])
In [50]: nd1
Out[50]: array([ 1, 3, 100, 7, 9, 1024])
In [51]: nd1[::-1]
Out[51]: array([1024, 9, 7, 100, 3, 1])
#np.hstack与np.vstack,水平级联与垂直级联,处理自己,进行维度的变更
In [52]: nd2 = np.vstack(nd1)
In [53]: nd2
Out[53]:
array([[ 1],
[ 3],
[ 100],
[ 7],
[ 9],
[1024]])
In [54]: np.hstack(nd2)
Out[54]: array([ 1, 3, 100, 7, 9, 1024])
In [55]: nd3 = np.random.randint(0,10,size = (4,3,5))
In [56]: nd3
Out[56]:
array([[[6, 2, 1, 6, 9],
[6, 6, 7, 7, 7],
[9, 2, 5, 6, 8]],
[[4, 3, 6, 3, 5],
[4, 6, 0, 1, 0],
[1, 9, 3, 2, 8]],
[[8, 2, 9, 2, 6],
[4, 6, 2, 8, 6],
[5, 9, 3, 4, 3]],
[[2, 0, 4, 5, 3],
[8, 2, 0, 9, 1],
[5, 9, 8, 6, 2]]])
In [57]: np.hstack(nd3)
Out[57]:
array([[6, 2, 1, 6, 9, 4, 3, 6, 3, 5, 8, 2, 9, 2, 6, 2, 0, 4, 5, 3],
[6, 6, 7, 7, 7, 4, 6, 0, 1, 0, 4, 6, 2, 8, 6, 8, 2, 0, 9, 1],
[9, 2, 5, 6, 8, 1, 9, 3, 2, 8, 5, 9, 3, 4, 3, 5, 9, 8, 6, 2]])
In [58]: nd3.shape
Out[58]: (4, 3, 5)
#与级联类似,三个函数完成切分工作:np.split,np.vsplit,np.hsplit
In [59]: np.split(nd3,4)
Out[59]:
[array([[[6, 2, 1, 6, 9],
[6, 6, 7, 7, 7],
[9, 2, 5, 6, 8]]]), array([[[4, 3, 6, 3, 5],
[4, 6, 0, 1, 0],
[1, 9, 3, 2, 8]]]), array([[[8, 2, 9, 2, 6],
[4, 6, 2, 8, 6],
[5, 9, 3, 4, 3]]]), array([[[2, 0, 4, 5, 3],
[8, 2, 0, 9, 1],
[5, 9, 8, 6, 2]]])]
In [60]: np.split(nd3,[1,2])
Out[60]:
[array([[[6, 2, 1, 6, 9],
[6, 6, 7, 7, 7],
[9, 2, 5, 6, 8]]]), array([[[4, 3, 6, 3, 5],
[4, 6, 0, 1, 0],
[1, 9, 3, 2, 8]]]), array([[[8, 2, 9, 2, 6],
[4, 6, 2, 8, 6],
[5, 9, 3, 4, 3]],
[[2, 0, 4, 5, 3],
[8, 2, 0, 9, 1],
[5, 9, 8, 6, 2]]])]
In [61]: nd4 = np.random.randint(0,100,size = (4,5))
In [62]: nd4
Out[62]:
array([[72, 16, 25, 8, 21],
[60, 0, 22, 74, 22],
[58, 74, 48, 69, 78],
[99, 38, 18, 84, 47]])
In [63]: np.hsplit(nd4,[1,2])
Out[63]:
[array([[72],
[60],
[58],
[99]]), array([[16],
[ 0],
[74],
[38]]), array([[25, 8, 21],
[22, 74, 22],
[48, 69, 78],
[18, 84, 47]])]
In [64]: np.vsplit(nd4,[1,2])
Out[64]:
[array([[72, 16, 25, 8, 21]]),
array([[60, 0, 22, 74, 22]]),
array([[58, 74, 48, 69, 78],
[99, 38, 18, 84, 47]])]
In [65]: np.split(nd4,[1,2],axis=0)
Out[65]:
[array([[72, 16, 25, 8, 21]]),
array([[60, 0, 22, 74, 22]]),
array([[58, 74, 48, 69, 78],
[99, 38, 18, 84, 47]])]
#创建副本,和原来数组地址一样
In [66]: nd5 = nd4.copy()
In [68]: id(nd4)
Out[68]: 140512145734352
In [69]: id(nd5)
Out[69]: 140512145799568
数组的维度变换
.reshape(shape) : 不改变数组元素,返回一个shape形状的数组,原数组不变
.resize(shape) : 与.reshape()功能一致,但修改原数组
.swapaxes(ax1,ax2) : 将数组n个维度中两个维度进行调换,不改变原数组
.flatten() : 对数组进行降维,返回折叠后的一维数组,原数组不变
栗子:
#reshape(shape) 不改变数组元素,返回一个shape形状的数组,原数组不变
import numpy as np
a = np.arange(20)
a.reshape([5,4])
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19]])
a #a还是原来的a
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19])
#resize(shape) 与.reshape()功能一致,但修改原数组
a.resize([4,5])
a #a为a.resize后的数组
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])
#swapaxes(ax1,ax2) 将数组n个维度中两个维度进行调换,不改变原数组
a.swapaxes(1,0) #将a的维度转换,由原来的4行5列变为5行4列
array([[ 0, 5, 10, 15],
[ 1, 6, 11, 16],
[ 2, 7, 12, 17],
[ 3, 8, 13, 18],
[ 4, 9, 14, 19]])
#flatten() 对数组进行降维,返回折叠后的一维数组,原数组不变
a.flatten()
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19])
a #不改变原来的a数组
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])
b = a.flatten() #可赋值
b
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19])
数组的运算
np.abs(a) np.fabs(a) : 取各元素的绝对值
np.sqrt(a) : 计算各元素的平方根
np.square(a): 计算各元素的平方
np.log(a) np.log10(a) np.log2(a) : 计算各元素的自然对数、10、2为底的对数
np.ceil(a) np.floor(a) : 计算各元素的ceiling 值, floor值(ceiling向上取整,floor向下取整)
np.rint(a) : 各元素 四舍五入
np.modf(a) : 将数组各元素的小数和整数部分以两个独立数组形式返回
np.exp(a) : 计算各元素的指数值
np.sign(a) : 计算各元素的符号值 1(+),0,-1(-)
np.maximum(a, b) np.fmax() : 比较(或者计算)元素级的最大值
np.minimum(a, b) np.fmin() : 取最小值
np.mod(a, b) : 元素级的模运算
np.copysign(a, b) : 将b中各元素的符号赋值给数组a的对应元素
数组的统计函数
sum(a, axis = None) : 依给定轴axis计算数组a相关元素之和,axis为整数或者元组
mean(a, axis = None) : 同理,计算平均值
average(a, axis =None, weights=None) : 依给定轴axis计算数组a相关元素的加权平均值
std(a, axis = None) :同理,计算标准差
var(a, axis = None): 计算方差
eg: np.mean(a, axis =1) : 对数组a的第二维度的数据进行求平均
a = np.arange(15).reshape(3, 5)
np.average(a, axis =0, weights =[10, 5, 1]) : 对a第一各维度加权求平均,weights中为权重,注意要和a的第一维匹配
min(a) max(a) : 计算数组a的最小值和最大值
argmin(a) argmax(a) : 计算数组a的最小、最大值的下标(注:是一维的下标)
unravel_index(index, shape) : 根据shape将一维下标index转成多维下标
ptp(a) : 计算数组a最大值和最小值的差
median(a) : 计算数组a中元素的中位数(中值)
np.argmax(a) –> 0
np.unravel_index( np.argmax(a), a.shape) –> (0,0)
数组的any,all,where
栗子:
In [80]: a = np.array([1,2,3])
In [81]: b = np.array([1,2,1])
#any()任意个相等返回True
In [86]: (a == b).any()
Out[86]: True
#all()全部个相等返回True
In [87]: (a == b).all()
Out[87]: False
#三个参数numpy.where(condition,x,y):满足condition条件,输出x,不满足输出y
In [90]: np.where([[True, False], [True, True]],
...: [[1, 2], [3, 4]],
...: [[9, 8], [7, 6]])
...:
#False的时候,x不满足条件,所以输出y 8
Out[90]: array([[1, 8],
[3, 4]])
#一个参数numpy.where(array):输出array中‘真’值的坐标(‘真‘也可以理解为非零)
In [91]: np.where([[0, 1], [1, 0]])
Out[91]: (array([0, 1]),
array([1, 0]))
In [92]: x=np.array([[0,1,2],[3,4,5],[6,7,8]])
In [93]: np.where(x)
#0为假所以不输出,从坐标(0,1)开始
Out[93]: (array([0, 0, 1, 1, 1, 2, 2, 2]),
array([1, 2, 0, 1, 2, 0, 1, 2]))
数组的梯度函数
np.gradient(f) : 计算数组f中元素的梯度,当f为多维时,返回每个维度梯度
扩展小知识:梯度
连续值之间的变化率,即斜率(求导)
栗子:
In [1]: import numpy as np
In [2]: a = np.random.randint(1,50,(6))
In [3]: a
Out[3]: array([17, 37, 23, 36, 22, 44])
In [4]: np.gradient(a)
Out[4]: array([20. , 3. , -0.5, -0.5, 4. , 22. ])
In [5]: b = np.random.randint(1,50,(3,6))
In [6]: b
Out[6]:
array([[22, 36, 37, 11, 33, 48],
[16, 19, 24, 39, 37, 19],
[28, 20, 47, 15, 24, 36]])
In [7]: np.gradient(b)
Out[7]:
[array([[ -6. , -17. , -13. , 28. , 4. , -29. ],
[ 3. , -8. , 5. , 2. , -4.5, -6. ],
[ 12. , 1. , 23. , -24. , -13. , 17. ]]),
array([[ 14. , 7.5, -12.5, -2. , 18.5, 15. ],
[ 3. , 4. , 10. , 6.5, -10. , -18. ],
[ -8. , 9.5, -2.5, -11.5, 10.5, 12. ]])]
矩阵操作
栗子:
In [94]: nd4
Out[94]:
array([[72, 16, 25, 8, 21],
[60, 0, 22, 74, 22],
[58, 74, 48, 69, 78],
[99, 38, 18, 84, 47]])
#加,每个数加5
In [95]: nd4+5
Out[95]:
array([[ 77, 21, 30, 13, 26],
[ 65, 5, 27, 79, 27],
[ 63, 79, 53, 74, 83],
[104, 43, 23, 89, 52]])
#乘,每个数乘2
In [96]: nd4*2
Out[96]:
array([[144, 32, 50, 16, 42],
[120, 0, 44, 148, 44],
[116, 148, 96, 138, 156],
[198, 76, 36, 168, 94]])
In [97]: nd1 = np.array([[2,3],[1,6]])
In [98]: nd2 = np.array([[-1,2],[2,9]])
#两个矩阵相乘,每个对应位相乘
In [99]: nd1*nd2
Out[99]:
array([[-2, 6],
[ 2, 54]])
#矩阵积
In [100]: np.dot(nd1,nd2)
Out[100]:
array([[ 4, 31],
[11, 56]])
数组的广播机制
ndarray广播机制的两条规则
规则一:为缺失的维度补1
规则二:假定缺失元素用已有值填充
栗子:
# 广播机制的前提,维度必须有一维对应
In [101]: nd4
Out[101]:
array([[72, 16, 25, 8, 21],
[60, 0, 22, 74, 22],
[58, 74, 48, 69, 78],
[99, 38, 18, 84, 47]])
In [102]: nd3 = np.array([1,2,5,8,4])
In [103]: nd4+nd3
Out[103]:
array([[ 73, 18, 30, 16, 25],
[ 61, 2, 27, 82, 26],
[ 59, 76, 53, 77, 82],
[100, 40, 23, 92, 51]])
In [104]: nd5 = np.array([-2,3.14,7.3,5.6])
In [105]: nd4
Out[105]:
array([[72, 16, 25, 8, 21],
[60, 0, 22, 74, 22],
[58, 74, 48, 69, 78],
[99, 38, 18, 84, 47]])
In [106]: nd5 = np.random.randint(0,10,size = (4,1))
In [107]: nd5
Out[107]:
array([[0],
[0],
[5],
[2]])
In [108]: nd4+nd5
Out[108]:
array([[ 72, 16, 25, 8, 21],
[ 60, 0, 22, 74, 22],
[ 63, 79, 53, 74, 83],
[101, 40, 20, 86, 49]])
In [109]: nd3
Out[109]: array([1, 2, 5, 8, 4])
In [110]: index = np.array([0,0,0,1,2,4,4,4,3])
In [111]: nd3[index]
Out[111]: array([1, 1, 1, 2, 5, 4, 4, 4, 8])
随机创建图片
import matplotlib.pyplot as plt
import numpy as np
pic = np.random.randint(0,200,(100,200,3))
plt.imshow(pic)
pic
下载.png
网友评论