我有一個numpy的矩陣X_test
等一系列y_test
,其尺寸爲:numpy的:CONCAT /一個附加列追加到原始矩陣
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
的最後一列應該是什麼樣的正確方法?謝謝!
您的y_test修改爲'y_test [None,:]',在開始時添加一個新維度,使其成爲'(1,5)'。 – hpaulj