Python Numpy模块函数np.c_和np.r_

时间:2021-11-24 05:44:47

    np.r_:是按列连接两个矩阵,就是把两矩阵上下相加,要求列数相等,类似于pandas中的concat()。

    np.c_:是按行连接两个矩阵,就是把两矩阵左右相加,要求行数相等,类似于pandas中的merge()。

 

  

import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.c_[a,b]

print(np.r_[a,b])
print('\n')
print(c)
print('\n')
print(np.c_[c,a])
################################
结果:
[1 2 3 4 5 6]


[[1 4]
 [2 5]
 [3 6]]


[[1 4 1]
 [2 5 2]
 [3 6 3]]

 其它函数

a = np.arange( 10, 30, 5 )
print(a)
# [10 15 20 25]


a = np.arange(15).reshape(3, 5)
print(a)
# [[ 0  1  2  3  4]
#  [ 5  6  7  8  9]
#  [10 11 12 13 14]]

a = np.array([1,2,3,4,5,6,7,8,9]).reshape(3,3)
print(a)
# [[1 2 3]
#  [4 5 6]
#  [7 8 9]]

a = np.zeros((3,4))
print(a)
# [[0. 0. 0. 0.]
#  [0. 0. 0. 0.]
#  [0. 0. 0. 0.]]

a = np.linspace( 0, 2, 9 )
print(a)
# [0.   0.25 0.5  0.75 1.   1.25 1.5  1.75 2.  ]

a = np.ones(3, dtype=np.int32)
print(a)
# [1 1 1]

a = np.ones((2,3), dtype=int)
print(a)
# [[1 1 1]
#  [1 1 1]]