1from sklearn.model_selection import GridSearchCV
2cv = GridSearchCV(rfc,parameters,cv=5)
3cv.fit(train_features,train_label.values.ravel())
1# Find number of features for cumulative importance of 95%# Add 1 because Python is zero-indexedprint('Number of features for 95% importance:', np.where(cumulative_importances > 0.95)[0][0] + 1)Number of features for 95% importance: 6
1# List of features sorted from most to least importantsorted_importances = [importance[1] for importance in feature_importances]sorted_features = [importance[0] for importance in feature_importances]# Cumulative importancescumulative_importances = np.cumsum(sorted_importances)# Make a line graphplt.plot(x_values, cumulative_importances, 'g-')# Draw line at 95% of importance retainedplt.hlines(y = 0.95, xmin=0, xmax=len(sorted_importances), color = 'r', linestyles = 'dashed')# Format x ticks and labelsplt.xticks(x_values, sorted_features, rotation = 'vertical')# Axis labels and titleplt.xlabel('Variable'); plt.ylabel('Cumulative Importance'); plt.title('Cumulative Importances');
1def display(results):
2 print(f'Best parameters are: {results.best_params_}')
3 print("\n")
4 mean_score = results.cv_results_['mean_test_score']
5 std_score = results.cv_results_['std_test_score']
6 params = results.cv_results_['params']
7 for mean,std,params in zip(mean_score,std_score,params):
8 print(f'{round(mean,3)} + or -{round(std,3)} for the {params}')