python numpty 中shape的用法

python: numpy--函数 shape用法 - CSDN博客

https://blog.csdn.net/u010758410/article/details/71554224

shape函数是numpy.core.fromnumeric中的函数,它的功能是查看矩阵或者数组的维数。

举例说明:

建立一个3×3的单位矩阵e, e.shape为(3,3),表示3行3列,第一维的长度为3,第二维的长度也为3

 

[plain]  view plain  copy
 
 
在CODE上查看代码片 派生到我的代码片
  1. >>> e = eye(3)  
  2. >>> e  
  3. array([[ 1.,  0.,  0.],  
  4.        [ 0.,  1.,  0.],  
  5.        [ 0.,  0.,  1.]])  
  6. >>> e.shape  
  7. (3, 3)  
  1.  
    >>> e = eye( 3)
  2.  
    >>> e
  3.  
    array([[ 1., 0., 0.],
  4.  
    [ 0., 1., 0.],
  5.  
    [ 0., 0., 1.]])
  6.  
    >>> e.shape
  7.  
    ( 3, 3)

建立一个一维矩阵b, b.shape 为矩阵的长度

[plain]  view plain  copy
 
 
在CODE上查看代码片 派生到我的代码片
  1. >>> b =array([1,2,3,4])  
  2. >>> b.shape  
  3. (4,)  
  4. #可以简写  
  5. >>> shape([1,2,3,4])  
  6. (4,)  
  7. >>>   
  1.  
    >>> b =array([ 1, 2, 3, 4])
  2.  
    >>> b.shape
  3.  
    ( 4,)
  4.  
    #可以简写
  5.  
    >>> shape([ 1, 2, 3, 4])
  6.  
    ( 4,)
  7.  
    >>>

 

建立一个4×2的矩阵c, c.shape[1] 为第一维的长度,c.shape[0] 为第二维的长度。

 

[plain]  view plain  copy
 
 
在CODE上查看代码片 派生到我的代码片
  1. >>> c = array([[1,1],[1,2],[1,3],[1,4]])  
  2. >>> c.shape  
  3. (4, 2)  
  4. >>> c.shape[0]  
  5. 4  
  6. >>> c.shape[1]  
  7. 2  
  1.  
    >>> c = array([[ 1, 1],[ 1, 2],[ 1, 3],[ 1, 4]])
  2.  
    >>> c.shape
  3.  
    ( 4, 2)
  4.  
    >>> c.shape[ 0]
  5.  
    4
  6.  
    >>> c.shape[ 1]
  7.  
    2

一个单独的数值,返回值为空

 

 

[plain]  view plain  copy
 
 
在CODE上查看代码片 派生到我的代码片
  1. >>> shape(3)  
  2. ()  
  1.  
    >>> shape( 3)
  2.  
    ()

猜你喜欢

转载自www.cnblogs.com/wanghuadongsharer/p/9577408.html
今日推荐