python中numpy.r

it2026-08-19  7

例子

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(c) print(np.c_[c,a])

 

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

[1 2 3 4 5 6]

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

[[1 4 1] [2 5 2] [3 6 3]]在numpy中,一个列表虽然是横着表示的,但它是列向量。

转载于:https://www.cnblogs.com/CK85/p/10299014.html

最新回复(0)