Python的numpy库中将矩阵转换为列表等函数的⽅法
这篇⽂章主要介绍Python的numpy库中的⼀些函数,做备份,以便查找。(1)将矩阵转换为列表的函数:numpy.matrix.tolist()返回list列表Examples>>>
>>> x = np.matrix(np.arange(12).reshape((3,4))); xmatrix([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])>>> x.tolist()
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]
(2)将数组转换为列表的函数:numpy.ndarray.tolist()Notes:(数组能够被重新构造)
The array may be recreated, a=np.array(a.tolist()).Examples>>>
>>> a = np.array([1, 2])>>> a.tolist()[1, 2]
>>> a = np.array([[1, 2], [3, 4]])>>> list(a)
[array([1, 2]), array([3, 4])]>>> a.tolist()[[1, 2], [3, 4]]
(3)numpy.mean()计算矩阵或数组的均值:Examples>>>
>>> a = np.array([[1, 2], [3, 4]]) #对所有元素求均值>>> np.mean(a)2.5
>>> np.mean(a, axis=0) #对每⼀列求均值array([ 2., 3.])
>>> np.mean(a, axis=1) #对每⼀⾏求均值array([ 1.5, 3.5])
(4)numpy.std()计算矩阵或数组的标准差:Examples>>>
>>> a = np.array([[1, 2], [3, 4]]) #对所有元素求标准差 >>> np.std(a)
1.11803398874949
>>> np.std(a, axis=0) #对每⼀列求标准差array([ 1., 1.])
>>> np.std(a, axis=1) #对每⼀⾏求标准差array([ 0.5, 0.5])
(5)numpy.newaxis为数组增加⼀个维度:Examples:
>>> a=np.array([[1,2,3],[4,5,6],[7,8,9]]) #先输⼊3⾏2列的数组a>>> b=a[:,:2]
>>> b.shape #当数组的⾏与列都⼤于1时,不需增加维度(3, 2)
>>> c=a[:,2]
>>> c.shape #可以看到,当数组只有⼀列时,缺少列的维度(3,)>>> c
array([3, 6, 9])
>>> d=a[:,2,np.newaxis] #np.newaxis实现增加列的维度>>> darray([[3], [6], [9]])
>>> d.shape #d的维度成了3⾏1列(3,1)(3, 1)
>>> e=a[:,2,None] #None与np.newaxis实现相同的功能>>> earray([[3], [6], [9]])
>>> e.shape(3, 1)
(6)numpy.random.shuffle(index): 打乱数据集(数组)的顺序:Examples:
>>> index = [i for i in range(10)] >>> index
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> np.random.shuffle(index) >>> index
[7, 9, 3, 0, 4, 1, 5, 2, 8, 6]
(7)计算⼆维数组某⼀⾏或某⼀列的最⼤值最⼩值:
>>> import numpy as np
>>> a = np.arange(15).reshape(5,3) #构造⼀个5⾏3列的⼆维数组 >>> a
array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11], [12, 13, 14]])
>>> b = a[:,0].min() ##取第0列的最⼩值,其他列同理 >>> b 0
>>> c = a[0,:].max() ##取第0⾏的最⼤值,其他⾏同理 >>> c 2
(8)向数组中添加列:np.hstack()
n = np.array(np.random.randn(4,2)) n
Out[153]:
array([[ 0.17234 , -0.01480043], [-0.33356669, -1.33565616], [-1.11680009, 0.230761], [-0.51233174, -0.10359941]])
l = np.array([1,2,3,4]) l
Out[155]: array([1, 2, 3, 4])
l.shape
Out[156]: (4,)
可以看到,n是⼆维的,l是⼀维的,如果直接调⽤np.hstack()会出错:维度不同。
n = np.hstack((n,l))
ValueError: all the input arrays must have same number of dimensions
解决⽅法是将l变为⼆维的,可以⽤(5)中的⽅法:
n = np.hstack((n,l[:,np.newaxis])) ##注意:在使⽤np.hstack()时必须⽤()把变量括起来,因为它只接受⼀个变量 n
Out[161]:
array([[ 0.17234 , -0.01480043, 1. ], [-0.33356669, -1.33565616, 2. ], [-1.11680009, 0.230761, 3. ], [-0.51233174, -0.10359941, 4. ]])
下⾯讲⼀下如何按列往⼀个空列表添加值:
n = np.array([[1,2,3,4,5,6],[11,22,33,44,55,66],[111,222,333,444,555,666]]) ##产⽣⼀个三⾏六列容易区分的数组 n
Out[166]:
array([[ 1, 2, 3, 4, 5, 6], [ 11, 22, 33, 44, 55, 66],
[111, 222, 333, 444, 555, 666]])
sample = [[]for i in range(3)] ##产⽣三⾏⼀列的空列表 Out[172]: [[], [], []]
for i in range(0,6,2): ##每间隔⼀列便添加到sample中 sample = np.hstack((sample,n[:,i,np.newaxis]))
sample Out[170]:
array([[ 1., 3., 5.], [ 11., 33., 55.], [ 111., 333., 555.]])
持续更新中……
以上这篇Python的numpy库中将矩阵转换为列表等函数的⽅法就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。