<jupyter_start><jupyter_text>Fantastic features and where to find them (code)<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
from sklearn import datasets

sns.set_style("whitegrid")

from IPython.core.display import display, HTML
display(HTML("<style>.container { width:90% !important; }</style>"))
data = [
    (datasets.load_boston, "reg"), 
    (datasets.load_breast_cancer, "cls"), 
    (datasets.load_diabetes, "reg"), 
    (datasets.load_digits, "cls"),
    (datasets.load_iris, "cls"),
    (datasets.load_linnerud, "reg"),
    (datasets.load_wine, "cls"),
]
importances = []
for f, target in data:
    X, y = f(return_X_y=True)
    model = RandomForestRegressor() if target == "reg" else RandomForestClassifier()
    model.fit(X, y)
    feat_nb = model.feature_importances_
    feat_nb = np.sort(feat_nb, axis=-1)[::-1]
    importances.append(feat_nb)
importances_df = pd.DataFrame(importances, index=["boston", "cancer", "diabetes", "digits", "iris", "linnerud", "wine"])
importances_df.T.plot(figsize=(30, 14))
importances_df.cumsum(1).T.plot(figsize=(30, 14))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1458670988654781, 'fraction_numerical': 0.01053484602917342, 'mean_word_length': 5.5, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '467897', 'license': 'Apache-2.0', 'n_tokens_mistral': 465, 'n_tokens_neox': 444, 'n_words': 94, 'path': '_notebooks/2021-04-24-fantastic-features.ipynb', 'repo_name': 'conormm/blog', 'size': '1234'}