<jupyter_start><jupyter_text>単変量線形回帰での外れ値の影響<jupyter_code>import numpy as np
import random

from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score

import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>学習データ生成* $x =0.0, 0.5, 1.0, \cdots, 10.0$* サンプル数: 21* $y = -x + 11 + \varepsilon,\ \varepsilon \sim N(0, 0.5^2)$<jupyter_code>sample_size = 20 + 1
sigma = 0.5

seed = 1234
random.seed(seed)

x = np.array([0.5 *  i for i in range(sample_size)])
y = -x + 11 + [random.gauss(0, sigma)  for i in range(sample_size)]
plt.scatter(x, y)

plt.xlabel('x')
plt.ylabel('y');<jupyter_output><empty_output><jupyter_text>単変量線形回帰モデルの構築<jupyter_code>x = x.reshape(-1, 1)
y = y.reshape(-1, 1)

model = LinearRegression()
model.fit(x, y)
y_fitted = model.predict(x)
print('決定係数: {:.3f}'.format(r2_score(y, y_fitted)))
plt.scatter(x, y, label='fact')
plt.plot(x, y_fitted, color='orange', label='fitted')

plt.xlabel('x')
plt.ylabel('y')
plt.legend();<jupyter_output><empty_output><jupyter_text>学習データの範囲外での予測（外挿）<jupyter_code>x_ext = 10 + np.array([0.2 * i for i in range(11)])
x_ext = x_ext.reshape(-1, 1)

y_ext = model.predict(x_ext)
plt.scatter(x, y, label='fact')
plt.plot(x, y_fitted, color='orange', label='fitted')

plt.plot(x_ext, y_ext, color='orange', label='pred(ext)', linestyle='dashed')

plt.xlabel('x')
plt.ylabel('y')
plt.legend();<jupyter_output><empty_output><jupyter_text>学習データ中の外れ値の影響* 点$(x, y) = (100, 100)$を学習データに追加<jupyter_code># (100, 100)を学習データに追加
x_outlier = np.append(x, [100]).reshape(-1, 1)
y_outlier = np.append(y, [100]).reshape(-1, 1)
plt.scatter(x_outlier, y_outlier)

plt.xlabel('x')
plt.ylabel('y');
model = LinearRegression()
model.fit(x_outlier, y_outlier)
y_fitted = model.predict(x_outlier)
print('決定係数: {:.3f}'.format(r2_score(y_outlier, y_fitted)))
plt.scatter(x_outlier, y_outlier, label='fact')
plt.plot(x_outlier, y_fitted, color='orange', label='fitted')

plt.legend()
plt.xlabel('x')
plt.ylabel('y');
plt.scatter(x_outlier, y_outlier, label='fact')
plt.plot(x_outlier, y_fitted, color='orange', label='fitted')

plt.xlim([0, 11])
plt.ylim([0, 11])
plt.legend()
plt.xlabel('x')
plt.ylabel('y');<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19401631912964643, 'fraction_numerical': 0.03445149592021759, 'mean_word_length': 6.996376811594203, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '461406', 'license': 'MIT', 'n_tokens_mistral': 1160, 'n_tokens_neox': 1064, 'n_words': 163, 'path': 'scikit-learn/SimpleLinearRegression/Outlier.ipynb', 'repo_name': 'starpentagon/python_scripts', 'size': '2206'}