2016-07-04 40 views
1

嗨我想創建一個2列的.csv:隨機森林模型的功能重要性和該功能的名稱。而且可以肯定的是數值和變量名之間的匹配是正確的導出功能重要性csv從隨機森林

這是一個例子,但我不能導出爲.csv correclty

test_features = test[["area","product", etc.]].values 

# Create the target 
target = test["churn"].values 

pred_forest = my_forest.predict(test_features) 

# Print the score of the fitted random forest 
print(my_forest.score(test_features, target)) 


importance = my_forest.feature_importances_ 


pd.DataFrame({"IMP": importance, "features":test_features }).to_csv('forest_0407.csv',index=False) 
+0

這是如何失敗?這對我來說看起來有點可疑,因爲你試圖將功能重要性與功能重要性匹配,這是不正確的,因爲功能重要性是列 – EdChum

+0

我很困惑,因爲我打印「重要性」我只能看到一個數組,但我不確定哪些功能匹配,因爲我想檢查名稱和值。消息錯誤是這樣的: 例外:數據必須是一維的 – progster

+0

試試這個功能爲'test.columns.tolist()'。 – shivsn

回答

1

使用此

x = list(zip(my_forest.feature_importances_,list of features you are using)) 
x = pandas.DataFrame(x,columns=["Importance","Feature_Name"])