Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
710 B
5
Indexable
Never
from sklearn.ensemble import RandomForestRegressor

X_rf = X_case1.dropna()
X_rf = pd.get_dummies(X_rf)

# Parameters in RandomForestRegressor function can be optimize through random search or grid search
# but I will skip it for now
rf_model = RandomForestRegressor(max_depth=10)
rf_model.fit(X_rf.drop('ret', axis=1), X_rf['ret'])

features_test = X_case1.columns
importances = rf_model.feature_importances_
indices = np.argsort(importances)[-9:]  # top 10 features
plt.title('Feature Importances')
plt.barh(range(len(indices)), importances[indices], color='b', align='center')
plt.yticks(range(len(indices)), [features_test[i] for i in indices])
plt.xlabel('Relative Importance')
plt.show()