python 中 numpy 模块的 size,shape, len的用法

转载:https://blog.csdn.net/qq_24193303/article/details/80961646

numpy 中有很多类方法可以对数组处理,下面将介绍三种常见的处理数组的方法.

 

1.size的用法

  1.  
    import numpy as np
  2.  
    X=np.array([[1,2,3,4],
  3.  
    [5,6,7,8],
  4.  
    [9,10,11,12]])
  5.  
     
  6.  
    number=X.size # 计算 X 中所有元素的个数
  7.  
    X_row=np.size(X,0) #计算 X 一行元素的个数
  8.  
    X_col=np.size(X,1) #计算 X 一列元素的个数
  9.  
     
  10.  
    print("number:",number)
  11.  
    print("X_row:",X_row)
  12.  
    print("X_col:",X_col)
  13.  
     
  14.  
    <<
  15.  
    number: 12
  16.  
    X_row: 3
  17.  
    X_col: 4

 

 

2.shape的用法

  1.  
    import numpy as np
  2.  
    X=np.array([[1,2,3,4],
  3.  
    [5,6,7,8],
  4.  
    [9,10,11,12]])
  5.  
     
  6.  
    X_dim=X.shape # 以元组形式,返回数组的维数
  7.  
    print("X_dim:",X_dim)
  8.  
    print(X.shape[0]) # 输出行的个数
  9.  
    print(X.shape[1]) #输出列的个数
  10.  
     
  11.  
    <<
  12.  
    X_dim: (3, 4)
  13.  
    3
  14.  
    4

 

3.len的用法

  1.  
    import numpy as np
  2.  
    X=np.array([[1,2,3,4],
  3.  
    [5,6,7,8],
  4.  
    [9,10,11,12]])
  5.  
     
  6.  
    length=len(X) #返回对象的长度 不是元素的个数
  7.  
    print("length of X:",length)
  8.  
     
  9.  
    <<
  10.  
    length of X: 3

 

posted @ 2020-04-10 12:52  .ivan  阅读(3370)  评论(0编辑  收藏  举报