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.  
    ()
  3.  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM