zoukankan      html  css  js  c++  java
  • Python

    The shape attribute for numpy arrays returns the dimensions of the array. If Y has n rows and m columns, then Y.shape is (n,m). So Y.shape[0] is n.

    In [46]: Y = np.arange(12).reshape(3,4)
    
    In [47]: Y
    Out[47]: 
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11]])
    
    In [48]: Y.shape
    Out[48]: (3, 4)
    
    #行数 In [
    49]: Y.shape[0] Out[49]: 3

    #列数
    In [49]: Y.shape[1]
    Out[49]: 4
     

    numpy.full(shape, fill_value, dtype = None, order = ‘C’) : Return a new array with the same shape and type as a given array filled with a fill_value.

    # Python Programming illustrating 
    # numpy.full method 
    
    import numpy as np 
    
    a = np.full([2, 2], 67, dtype = int) 
    print("
    Matrix a : 
    ", a) 
    
    c = np.full([3, 3], 10.1) 
    print("
    Matrix c : 
    ", c) 

    output:

    Matrix a : 
     [[67 67]
     [67 67]]
    
    Matrix c : 
     [[ 10.1  10.1  10.1]
     [ 10.1  10.1  10.1]
     [ 10.1  10.1  10.1]]

     

    x = x[ : , 1]

    取x矩阵的第二列的所有行

    选择矩阵a的第一列到倒数第二列:

    a = a[:, :-1]

     

    删除矩阵a的最后一列:

    my_array = np.delete(my_array, -1, axis=1)

    删除第一列:

    my_array = np.delete(my_array, 0, axis=1)

    Numpy中的矩阵合并

    列合并/扩展:np.column_stack()

    行合并/扩展:np.row_stack()

    >>> import numpy as np
    >>> a = np.arange(9).reshape(3,-1)
    >>> a
    
    array([[0, 1, 2],
           [3, 4, 5],
           [6, 7, 8]])
    >>> b = np.arange(10, 19).reshape(3, -1)
    >>> b
    
    array([[10, 11, 12],
           [13, 14, 15],
           [16, 17, 18]])
    >>> top = np.column_stack((a, np.zeros((3,3))))
    >>> top
    
    array([[ 0.,  1.,  2.,  0.,  0.,  0.],
           [ 3.,  4.,  5.,  0.,  0.,  0.],
           [ 6.,  7.,  8.,  0.,  0.,  0.]])
    >>> bottom = np.column_stack((np.zeros((3,3)), b))
    >>> bottom
    
    array([[  0.,   0.,   0.,  10.,  11.,  12.],
           [  0.,   0.,   0.,  13.,  14.,  15.],
           [  0.,   0.,   0.,  16.,  17.,  18.]])
    >>> np.row_stack((top, bottom))
    
    array([[  0.,   1.,   2.,   0.,   0.,   0.],
           [  3.,   4.,   5.,   0.,   0.,   0.],
           [  6.,   7.,   8.,   0.,   0.,   0.],
           [  0.,   0.,   0.,  10.,  11.,  12.],
           [  0.,   0.,   0.,  13.,  14.,  15.],
           [  0.,   0.,   0.,  16.,  17.,  18.]])
  • 相关阅读:
    《Docker技术入门与实践》Docker入门4-使用Dockerfile创建镜像
    《Docker技术入门与实践》Docker入门3-网络基础配置
    《Docker技术入门与实践》Docker入门2-数据管理
    Git管理多个SSH密钥,Git多帐号配置
    《Docker技术入门与实践》Docker入门
    java获取汉字笔画数
    NSBundle、UIImageView、uibutton
    动画帧的使用
    结构体的转换
    IOS字符串的处理例子
  • 原文地址:https://www.cnblogs.com/Bella2017/p/11622636.html
Copyright © 2011-2022 走看看