Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 901
progster Avatar asked May 29 '26 03:05

progster


1 Answers

Use this

x = list(zip(my_forest.feature_importances_,list of features you are using))
x = pandas.DataFrame(x,columns=["Importance","Feature_Name"])
like image 150
Abhishek Sharma Avatar answered May 31 '26 15:05

Abhishek Sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!