2016-12-25 44 views
0

我想向矩陣中添加一個額外的列,以便使用某些機器學習算法預測某些功能。向矩陣添加額外的功能np.Concatenate錯誤:只能將長度爲1的數組轉換爲Python標量

我的trainSet有8899行和11個維度。

我想要做的就是添加額外的維度distance(見代碼)。

但我得到了一個錯誤:

only length-1 arrays can be converted to Python scalars 

temp_train_long/lat(8899L,)

X_train = df_train.as_matrix() 
temp_train_long=(X_train[:,3] - X_train[:,7])**2#long 
temp_train_lat = (X_train[:,4] - X_train[:,8])**2#lat 
distance = np.sqrt(temp_train_long + temp_train_lat) 
np.concatenate(X_train, distance.T) 
+0

不要在long,lat上使用歐幾里德距離。地球不平坦。 –

+0

感謝您的評論,但它不能解決我的問題 –

回答

0

審查concatenate文檔

串連((A1,A2,...),軸= 0)

該函數需要2個參數。首先是一個列表或元組,您想要加入的數組。第二個是一個數字,表示該軸。並且它返回一個新的數組。它不適用。

X_train = df_train.as_matrix() 

因此,這是2D(8899,N)中,n大於9.根據pd文檔這是一個numpy的array不是numpy的matrix(這是重要的)

temp_train_long=(X_train[:,3] - X_train[:,7])**2#long 
temp_train_lat = (X_train[:,4] - X_train[:,8])**2#lat 

兩個一維陣列( 8899,)

distance = np.sqrt(temp_train_long + temp_train_lat) 

另外(8899,)。 distance.T什麼都不做;未在形狀

np.concatenate(X_train, distance.T) 

改變你給它2個參數,一個是2D陣列,另外,在axis慢是一維數組。

你可能想

new_train = np.concatenate((X_train, distance[:,None]), axis=1) 

2陣列中的一個元組,軸是標量。 distance數組已變成2d 1列數組。

相關問題