码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-10-04 19:22:08      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:shape   dimens   type   nump   res   none   post   value   ott   

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]]

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.]])

 

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

标签:shape   dimens   type   nump   res   none   post   value   ott   

原文地址:https://www.cnblogs.com/Bella2017/p/11622636.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!