<jupyter_start><jupyter_text>We will train 500 trees in wine dataset and rank the 13 features by their respective importance<jupyter_code>from sklearn.ensemble import RandomForestClassifier
import pandas as pd
import numpy as np
from itertools import combinations
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
%matplotlib inline
df_wine = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data',header = None)
df_wine.columns = ['Class label', 'Alcohol',
 'Malic acid', 'Ash',
 'Alcalinity of ash', 'Magnesium',
 'Total phenols', 'Flavanoids',
 'Nonflavanoid phenols',
 'Proanthocyanins',
 'Color intensity', 'Hue',
 'OD280/OD315 of diluted wines',
 'Proline']
feat_labels = df_wine.columns[1:]
forest = RandomForestClassifier(n_estimators=500,random_state=1)
X, y = df_wine.iloc[:,1:].values, df_wine.iloc[:,0].values
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.3,
                                                   random_state = 0,
                                                   stratify = y)
forest.fit(X_train, y_train)
importances = forest.feature_importances_
indices = np.argsort(importances)[::-1]
indices
feat_labels[indices]
for f in range(X_train.shape[1]):
    print("%2d) %-*s %f"%(f+1, 30, feat_labels[indices[f]], importances[indices[f]]))
plt.title("Feature Importance")
plt.bar(range(X_train.shape[1]), importances[indices], align='center')
plt.xticks(range(X_train.shape[1]),feat_labels[indices],rotation = 90)
plt.xlim([-1, X_train.shape[1]])
plt.tight_layout()
plt.show()<jupyter_output><empty_output><jupyter_text>We can conclude that the proline and flavonoid levels, the color intensity, theOD280/OD315 diffraction, and the alcohol concentration of wine are the mostdiscriminative features in the dataset based on the average impurity decrease in the500 decision trees.<jupyter_code>from sklearn.feature_selection import SelectFromModel
sfm = SelectFromModel(forest, threshold=0.1, prefit=True)
X_selected = sfm.transform(X_train)
print("Number of features that meet this threshold critetion - ", X_selected.shape[1])
for f in range(X_selected.shape[1]):
    print("%2d) %-*s %f"%(f+1, 30, feat_labels[indices[f]], importances[indices[f]]))<jupyter_output>1) Proline                        0.185453
 2) Flavanoids                     0.174751
 3) Color intensity                0.143920
 4) OD280/OD315 of diluted wines   0.136162
 5) Alcohol                        0.118529
{'fraction_non_alphanumeric': 0.11605035405192761, 'fraction_numerical': 0.03776553894571204, 'mean_word_length': 4.275933609958506, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '593385', 'license': 'Apache-2.0', 'n_tokens_mistral': 891, 'n_tokens_neox': 803, 'n_words': 204, 'path': '.ipynb_checkpoints/Assessing feature importance with random forests-checkpoint.ipynb', 'repo_name': 'AshishSinha5/mlAlgos', 'size': '2542'}