2016-03-29 164 views
-3

我需要將我的分類模型的labels_train(這是一個字典列表[{Id:0, Prediction:2},{Id:1, Prediction:4},....])轉換爲數組。如何將字典轉換爲數組

#Classification model: 
from sklearn.linear_model import LogisticRegression 
class_model = LogisticRegression() 

#Fitting the classifier to the training set. 
classifier= class_model.fit(X_train_pca, labels_train) 
score= classifier.score(X_test_pca, labels_test) 
print(score) 

當我運行這個codeThank你了:)我得到這個錯誤:

TypeError: unorderable types: dict()>dict() 

X_train_pca因此,一個數組我想我應該轉換lables_train還到一個數組,但我不因爲數組與列表不同,所以不知道該怎麼做。

+1

提供輸入,預期的輸出,從實際代碼分離的問題,它的性質,告訴我們你已經嘗試過的情況,並定義「陣列」(即numpy的陣列或Python列表? )。 – timgeb

回答

1

試試這個

class_model.fit(X_train_pca, [i['Prediction'] for i in labels_train]) 
+0

這個工程!我試過了,但在另一種情況下:-D –

+0

非常感謝! – Mary