<jupyter_start><jupyter_text>Regression with extra trees<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

from sklearn import ensemble, datasets, metrics, model_selection
boston = datasets.load_boston()
print(boston.DESCR)
X = pd.DataFrame(boston.data, columns=boston.feature_names)
y = boston.target
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, train_size=0.7)

print('train samples:', len(X_train))
print('test samples', len(X_test))
df_train = pd.DataFrame(y_train, columns=['target'])
df_train['type'] = 'train'

df_test = pd.DataFrame(y_test, columns=['target'])
df_test['type'] = 'test'

df_set = df_train.append(df_test)

_ = sns.displot(df_set, x="target" ,hue="type", kind="kde", log_scale=False)
model = ensemble.ExtraTreesRegressor(n_estimators=50)
model.fit(X_train, y_train)
predicted = model.predict(X_test)

fig, ax = plt.subplots()
ax.scatter(y_test, predicted)

ax.set_xlabel('True Values')
ax.set_ylabel('Predicted')
_ = ax.plot([0, y.max()], [0, y.max()], ls='-', color='red')
residual = y_test - predicted

fig, ax = plt.subplots()
ax.scatter(y_test, residual)
ax.set_xlabel('y')
ax.set_ylabel('residual')

_ = plt.axhline(0, color='red', ls='--')
_ = sns.displot(residual, kind="kde");
print("r2 score: {}".format(metrics.r2_score(y_test, predicted)))
print("mse: {}".format(metrics.mean_squared_error(y_test, predicted)))
print("rmse: {}".format(np.sqrt(metrics.mean_squared_error(y_test, predicted))))
print("mae: {}".format(metrics.mean_absolute_error(y_test, predicted)))<jupyter_output>r2 score: 0.8591610187676444
mse: 11.966178
rmse: 3.459216385252591
mae: 2.1858421052631574
{'fraction_non_alphanumeric': 0.1644236395552955, 'fraction_numerical': 0.03978935049736688, 'mean_word_length': 7.55, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '34022', 'license': 'MIT', 'n_tokens_mistral': 717, 'n_tokens_neox': 670, 'n_words': 122, 'path': 'notebooks/supervised/regression/ensemble/regression_extra_trees.ipynb', 'repo_name': 'rueedlinger/machine-learning-snippets-', 'size': '1709'}