<jupyter_start><jupyter_text>Using a random forest to select important features for regression<jupyter_code>import numpy as np
import sklearn as sk
import sklearn.datasets as skd
import sklearn.ensemble as ske
import matplotlib.pyplot as plt
%matplotlib inline
data = skd.load_boston()
reg = ske.RandomForestRegressor()
X = data['data']
y = data['target']
reg.fit(X, y)
fet_ind = np.argsort(reg.feature_importances_)[::-1]
fet_imp = reg.feature_importances_[fet_ind]
fig, ax = plt.subplots(1, 1, figsize=(8, 3))
labels = data['feature_names'][fet_ind]
pd.Series(fet_imp, index=labels).plot('bar', ax=ax)
ax.set_title('Features importance')
fig, ax = plt.subplots(1, 1)
ax.scatter(X[:, -1], y)
ax.set_xlabel('LSTAT indicator')
ax.set_ylabel('Value of houses (k$)')
from sklearn import tree
tree.export_graphviz(reg.estimators_[0],
                     'tree.dot')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13789237668161436, 'fraction_numerical': 0.010089686098654708, 'mean_word_length': 5.922480620155039, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '70111', 'license': 'MIT', 'n_tokens_mistral': 337, 'n_tokens_neox': 328, 'n_words': 77, 'path': 'Section02/.ipynb_checkpoints/06_random_forest-checkpoint.ipynb', 'repo_name': 'PacktPublishing/Statistical-Methods---Applied-Mathematics-in-Data-Science', 'size': '892'}