Python的numpy库中将矩阵转换为列表等函数
2017-03-26 20:17
621 查看
这篇文章主要介绍Python的numpy库中的一些函数,做备份,以便查找。
(1)将矩阵转换为列表的函数:numpy.matrix.tolist()
返回list列表
Examples
>>>
(2)将数组转换为列表的函数:numpy.ndarray.tolist()
Notes:(数组能够被重新构造)
The array may be recreated, a=np.array(a.tolist()).
Examples
>>>
(3)numpy.mean()计算矩阵或数组的均值:
Examples
>>>
(4)numpy.std()计算矩阵或数组的标准差:
Examples
>>>
(5)numpy.newaxis为数组增加一个维度:
Examples:
(6)numpy.random.shuffle(index): 打乱数据集(数组)的顺序:
Examples:
(7)计算二维数组某一行或某一列的最大值最小值:
(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.64230761],
[-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.64230761, 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.]])
持续更新中……
参考:https://docs.scipy.org/doc/numpy/reference/
(1)将矩阵转换为列表的函数:numpy.matrix.tolist()
返回list列表
Examples
>>>
>>> x = np.matrix(np.arange(12).reshape((3,4))); x matrix([[ 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.1180339887498949 >>> 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实现增加列的维度 >>> d array([[3], [6], [9]]) >>> d.shape #d的维度成了3行1列(3,1) (3, 1) >>> e=a[:,2,None] #None与np.newaxis实现相同的功能 >>> e array([[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.64230761],
[-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.64230761, 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.]])
持续更新中……
参考:https://docs.scipy.org/doc/numpy/reference/
相关文章推荐
- Python的numpy库中将矩阵转换为列表等函数
- Python的numpy库中将矩阵转换为列表等函数的方法
- python 列表,数组,矩阵两两转换tolist()
- Python的zip函数——将两个列表转换成字典
- python中列表,矩阵,数组之间的转换
- Python中将字典转换为有序列表、无序列表的方法
- Python中将字典转换为列表的方法
- python 把一个m*n的二维矩阵转换成一个m*n行,三列的列表
- python 列表,数组,矩阵两两转换tolist()的实例
- Python中将字典转换为列表的方法
- python之numpy库学习常用函数(矩阵运算)
- asp中将相对路径转换为绝对路径的函数代码
- object-c中将字符串nsstring转换成对应刻调用的函数对象
- python下的数据类型转换的函数
- discuz x2.5中将文章列表或内容页的长日期格式转换成短日期格式(Y-m-d H:i:s)-> (Y-m-d)
- Dive Into Python 学习记录1-函数/模块导入/字典/列表/元组/字符串分割、连接、格式化/映射list/
- php中将网址转换为超链接的函数
- python中列表,元组,字符串如何互相转换
- PYTHON列表常用函数
- Python:列表函数