Python - Y.shape[n,m];Numpy中的矩阵合并

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("\nMatrix a : \n", a) 

c = np.full([3, 3], 10.1) 
print("\nMatrix c : \n", 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.]])

 

posted @ 2019-10-04 17:33  爱学英语的程序媛  阅读(518)  评论(0编辑  收藏  举报