<jupyter_start><jupyter_text>Regression with AutoML (auto-sklearn)<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

from sklearn import datasets, metrics, model_selection, preprocessing, pipeline

import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)

import autosklearn.regression<jupyter_output><empty_output><jupyter_text>Load the data set<jupyter_code>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)<jupyter_output><empty_output><jupyter_text>**Note:** We do some restrictions here running time and number of ensembles, because the model fitting would not take much longer. So this is just an example how you could run AutoML.<jupyter_code>model = autosklearn.regression.AutoSklearnRegressor(time_left_for_this_task=30, ensemble_size=3)
%%capture
# ignore oput from model fit with capture magic command
model.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Print the final ensemble constructed by auto-sklearn<jupyter_code>for m in model.get_models_with_weights():
    print(m)
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.8947682220991282
mse: 7.507093475386857
rmse: 2.739907566942151
mae: 1.9723889601858038
{'fraction_non_alphanumeric': 0.1389344262295082, 'fraction_numerical': 0.03155737704918033, 'mean_word_length': 7.876363636363636, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '231239', 'license': 'MIT', 'n_tokens_mistral': 946, 'n_tokens_neox': 891, 'n_words': 182, 'path': 'notebooks/automl/regression_with_automl.ipynb', 'repo_name': 'rueedlinger/machine-learning-snippets-', 'size': '2440'}