在numpy中与R函数cbind相当的是什么?

9

我希望实现以下行为:

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上找到的选项(concatenate、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

这不是cbind的作用。 - carlo
不是吗?cbind是做什么的? - stackoverflax
2个回答

15

通过继续搜索,找到以下答案https://dev59.com/92oy5IYBdhLWcg3wiegp#8505658

np.c_[x,y]

提供

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

我将把是否应该将此问题作为重复内容删除的决定留给StackOverflow风格专家。


3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接