Python的numpy库中的shape用法
2017-05-16 18:28
921 查看
shape函数是numpy.core.fromnumeric中的函数,它的功能是读取矩阵的维度。
例:
shape(matrixA) 返回matrixA的(行数,列数)元组
shape(matrixA)
[0] ==》 行数
shape(matrixA)
[1] ==》 列数
shape的输入参数可以使一个实数,一个一维列表(数组),二维数组,也可以是一个矩阵。
实例:
>>> np.shape(1) #实数
()
>>> np.shape([1,2,3]) #一维数组(列表)
(3,)
>>> alist2 = [[1,2],[3,4],[5,6]]
>>> np.shape(alist2) #二维列表
(3, 2)
>>> arr2 = np.array(alist2)
>>> np.shape(arr2)#二维数组
(3, 2)
>>> ma2 = np.mat(alist2)
>>> ma2
matrix([[1, 2],
[3, 4],
[5, 6]])
>>> np.shape(ma2)#二维矩阵
(3, 2)
>>> ml = np.shape(ma2)[0]#获取一维参数(行数)
>>> ml
3
>>>
ml2 = np.shape(ma2)[1]
>>> ml2
2
>>> em = np.eye(ml)#构造对角矩阵(实际上是二维数组)
>>>
em
array([[
1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
>>> em.shape()
Traceback (most recent call last):
File "<pyshell#45>", line 1, in <module>
em.shape()
TypeError: 'tuple' object is not callable
>>> em.shape
(3, 3)
>>> em.shape[0] #获取一维参数
3
例:
shape(matrixA) 返回matrixA的(行数,列数)元组
shape(matrixA)
[0] ==》 行数
shape(matrixA)
[1] ==》 列数
shape的输入参数可以使一个实数,一个一维列表(数组),二维数组,也可以是一个矩阵。
实例:
>>> np.shape(1) #实数
()
>>> np.shape([1,2,3]) #一维数组(列表)
(3,)
>>> alist2 = [[1,2],[3,4],[5,6]]
>>> np.shape(alist2) #二维列表
(3, 2)
>>> arr2 = np.array(alist2)
>>> np.shape(arr2)#二维数组
(3, 2)
>>> ma2 = np.mat(alist2)
>>> ma2
matrix([[1, 2],
[3, 4],
[5, 6]])
>>> np.shape(ma2)#二维矩阵
(3, 2)
>>> ml = np.shape(ma2)[0]#获取一维参数(行数)
>>> ml
3
>>>
ml2 = np.shape(ma2)[1]
>>> ml2
2
>>> em = np.eye(ml)#构造对角矩阵(实际上是二维数组)
>>>
em
array([[
1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
>>> em.shape()
Traceback (most recent call last):
File "<pyshell#45>", line 1, in <module>
em.shape()
TypeError: 'tuple' object is not callable
>>> em.shape
(3, 3)
>>> em.shape[0] #获取一维参数
3
相关文章推荐
- python: numpy--函数 shape用法
- Python:numpy中shape和reshape的用法
- python: numpy--函数 shape用法
- Python-Numpy的tile函数用法
- Python Intro - Numpy array shape
- python 的numpy库中的mean()函数用法
- python numpy中nonzero()的用法
- 【Python】Numpy扩充数组函数之repeat和tile用法
- python numpy中nonzero()的用法
- python numpy 如何获取和更改数组(array)的形状、维数-shape&reshape
- Python之数组(array)使用方法总结与Numpy中的数组用法
- Python:一篇文章掌握Numpy的基本用法
- python numpy.shape 和 numpy.reshape函数
- python numpy中nonzero()的用法
- Python之数组(array)使用方法总结与Numpy中的数组用法
- Python和numpy 中random 用法
- 【Python】Numpy扩充数组函数之repeat和tile用法
- python之numpy的用法
- python numpy矩阵信息,shape,size,dtype
- Python-Numpy的tile函数用法