progster
progster

Reputation: 937

exporting feature importance to csv from random forest

Hi I would like to create a .csv with 2 columns: the feature importance of a random forest model and the name of that feature. And to be sure that the match between numeric value and variable name is correct

Here it's an example but I cannot export to .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)

Upvotes: 3

Views: 2701

Answers (1)

Abhishek Sharma
Abhishek Sharma

Reputation: 2039

Use this

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

Upvotes: 2

Related Questions