<jupyter_start><jupyter_text>Multi-output example for susi.SOMRegressor<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_california_housing
from sklearn.metrics import r2_score
from sklearn.multioutput import MultiOutputRegressor
from sklearn.preprocessing import StandardScaler

# --- for running the script without pip
import sys
sys.path.append("../")
# ---

import susi
from susi.SOMPlots import plot_estimation_map<jupyter_output><empty_output><jupyter_text>Read in data<jupyter_code>cali = fetch_california_housing()
n_datapoints: int = 100
X = cali.data[:n_datapoints]
y = np.array([cali.target[:n_datapoints], cali.target[:n_datapoints]]).T

# preprocessing
scaler = StandardScaler()
X = scaler.fit_transform(X)<jupyter_output><empty_output><jupyter_text>Regression<jupyter_code>som = susi.SOMRegressor(
    n_rows=35,
    n_columns=35,
    n_iter_unsupervised=2500,
    n_iter_supervised=2500)

mor = MultiOutputRegressor(estimator=som, n_jobs=2)
# NBVAL_IGNORE_OUTPUT
mor.fit(X, y)
y_pred = mor.predict(X)
# NBVAL_IGNORE_OUTPUT

print("R2 (both variables): {0:.1f} %".format(r2_score(y, y_pred)*100))
print("R2 (first variable): {0:.1f} %".format(r2_score(y[:, 0], y_pred[:, 0])*100))
print("R2 (first variable): {0:.1f} %".format(r2_score(y[:, 1], y_pred[:, 1])*100))<jupyter_output>R2 (both variables): 77.5 %
R2 (first variable): 79.5 %
R2 (first variable): 75.5 %<jupyter_text>Plots<jupyter_code>plt.hist([est[0]-est[1] for est in y_pred])
plt.xlabel("Difference between estimation 1 and 2")
plt.ylabel("N")
plt.show()
plt.figure(figsize=(5, 5))
plt.scatter(y[:, 0], y_pred[:, 0], alpha=0.5)
plt.scatter(y[:, 1], y_pred[:, 1], alpha=0.5)

# define plot limits
min_lim = min(y.min(), y_pred.min())
max_lim = max(y.max(), y_pred.max())
plt.xlim(min_lim-1, max_lim+1)
plt.xlim(min_lim-1, max_lim+1)

plt.xlabel("True value")
plt.ylabel("Estimation")

plt.show()<jupyter_output><empty_output><jupyter_text>Plot estimation map<jupyter_code>for i in range(2):
    print(f"Estimator no. {i}:")
    som_tmp = mor.estimators_[i]
    estimation_map = np.squeeze(som_tmp.get_estimation_map())
    plot_estimation_map(
        estimation_map, cbar_label="Estimation", cmap="viridis_r")
    plt.show()<jupyter_output>Estimator no. 0:
{'fraction_non_alphanumeric': 0.1554290718038529, 'fraction_numerical': 0.03239929947460595, 'mean_word_length': 6.491803278688525, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '179310', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 978, 'n_tokens_neox': 921, 'n_words': 166, 'path': 'examples/SOMRegressor_multioutput.ipynb', 'repo_name': 'felixriese/susi', 'size': '2284'}