【发布时间】:2017-08-09 20:24:41
【问题描述】:
我有一个 numpy 矩阵 X_test 和一个系列 y_test,它们的维度是:
print(X_test.shape)
print(y_test.shape)
(5, 9)
(5,)
然后我尝试将y_test 添加为X_test 的最后一列,如下所示:
np.concatenate((X_test, y_test), axis = 1)
但出现以下错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-53-2edea4d89805> in <module>()
24
---> 25 print(np.concatenate((X_test, y_test), axis = 1))
ValueError: all the input arrays must have same number of dimensions
我也试过了:
np.append((X_test, y_test), 1)
但也有错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-54-f3d5e5ec7978> in <module>()
---> 26 print(np.append((X_test, y_test), 1))
/usr/local/lib/python3.4/dist-packages/numpy/lib/function_base.py in append(arr, values, axis)
5139
5140 """
-> 5141 arr = asanyarray(arr)
5142 if axis is None:
5143 if arr.ndim != 1:
/usr/local/lib/python3.4/dist-packages/numpy/core/numeric.py in asanyarray(a, dtype, order)
581
582 """
--> 583 return array(a, dtype, copy=False, order=order, subok=True)
584
585
ValueError: could not broadcast input array from shape (5,9) into shape (5)
我在这里错过了什么?将y_test 添加为矩阵X_test 的最后一列的正确方法应该是什么?谢谢!
【问题讨论】:
标签: python-3.x numpy matrix