Skip to main content
 首页 » 编程设计

python之numpy 中的 cbind(R 函数)等价物

2025年05月04日25insus

这是我想要的行为:

import numpy as np 
x = np.array([[1,2],[3,4]]) 
y = np.array([5, 6]) 
cbind(x,y) # desired result: np.array([[1,2,5],[3,4,6]]) 

看起来应该很简单,但是我在 http://mathesaurus.sourceforge.net/r-numpy.html 上找到的选项(连接、hstack 等)不起作用,例如:

np.hstack((x,y)) 

给予

Traceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
  File "X/site-packages/numpy/core/shape_base.py", line 288, in hstack 
    return _nx.concatenate(arrs, 1) 
ValueError: all the input arrays must have same number of dimensions 

请您参考如下方法:

更多的谷歌搜索找到了以下答案https://stackoverflow.com/a/8505658/1890660 :

np.c_[x,y] 

给予

array([[1, 2, 5], 
   [3, 4, 6]]) 

我会把它留给 StackOverflow 的设计师来决定我是否应该将整个问题作为重复项删除。