線形回帰#
モデル#
線形回帰(linear regression)は、予測の目的変数
ここで
期待値は0:
分散は一定:
異なった誤差項は無相関:
ならば
サンプルサイズが
と表記することができる。
パラメータの推定#
一般的に線形回帰ではパラメータの推定に最小二乗法(least squares method)という方法が使われる。
これは誤差関数
として定義し、この二乗誤差を最小にするパラメータ(最小二乗推定量 ordinary least square’s estimator: OLSE)
を採用するという方法。
二乗誤差
Show code cell source
import matplotlib.pyplot as plt
import numpy as np
def square(error):
return error ** 2
errors = np.linspace(-1, 1, 100)
squared_errors = [square(error) for error in errors]
fig, ax = plt.subplots()
ax.plot(errors, squared_errors)
_ = ax.set(xlabel='error', ylabel='squared error')

誤差二乗和は
であるから、二乗誤差の傾きがゼロになる点は
と表すことができる。
これを整理して
これの両辺を2で割ると(あるいは誤差関数の定義の際に
これを
となり、最小二乗推定量
実装#
numpyでは、行列やベクトルの積は@
という演算子で書くことができる。そのため、
import numpy as np
beta = np.linalg.inv(X.T @ X) @ X.T @ y
のように書けば上の式とおなじ演算を行うことができる。
データの準備#
乱数を発生させて架空のデータを作る。
ここで
import numpy as np
import pandas as pd
n = 100 # sample size
np.random.seed(0)
x0 = np.ones(shape=(n, ))
x1 = np.random.uniform(0, 10, size=n)
x2 = np.random.normal(3, 1, size=n)
noise = np.random.normal(size=n)
beta = [10, 3, 5] # 真の回帰係数
y = beta[0] * x0 + beta[1] * x1 + beta[2] * x2 + noise
特徴量
Show code cell source
import matplotlib.pyplot as plt
import seaborn as sns
fig, axes = plt.subplots(ncols=2, figsize=(10, 3))
axes[0].scatter(x1, y)
axes[0].set(xlabel='x1', ylabel='y')
axes[1].scatter(x2, y)
axes[1].set(xlabel='x2', ylabel='y')
[Text(0.5, 0, 'x2'), Text(0, 0.5, 'y')]

推定#
これらのデータを使用して推定を行う。
X = np.array([x0, x1, x2]).T
# Xの冒頭5行は以下のようになっている
print(X[0:5])
[[1. 5.48813504 1.83485016]
[1. 7.15189366 3.90082649]
[1. 6.02763376 3.46566244]
[1. 5.44883183 1.46375631]
[1. 4.23654799 4.48825219]]
# 最小二乗法で推定
beta_ = np.linalg.inv(X.T @ X) @ X.T @ y
print(f"""
推定された回帰係数: {beta_.round(3)}
データ生成過程の係数: {beta}
""")
推定された回帰係数: [9.564 2.98 5.119]
データ生成過程の係数: [10, 3, 5]
真の値にそれなりに近い回帰係数が推定できた。
なお、scikit-learnに準拠したfit/predictのメソッドを持つ形でクラスとして定義するなら、以下のようになる(参考: sklearn準拠モデルの作り方 - Qiita)。
# scikit-learnに準拠した形で実装
from sklearn.base import BaseEstimator, RegressorMixin
class LinearRegression(BaseEstimator, RegressorMixin):
def fit(self, X, y):
self.coef_ = np.linalg.inv(X.T @ X) @ X.T @ y
return self
def predict(self, X):
return X @ self.coef_
model = LinearRegression()
model.fit(X, y)
model.coef_
array([9.56372548, 2.97972446, 5.11931302])
予測してみる#
root mean squared error (RMSE)
を使って予測値を評価してみる。
# 予測値を算出
y_pred = model.predict(X)
# 予測誤差を評価
from sklearn.metrics import mean_squared_error
rmse = mean_squared_error(y, y_pred, squared=False)
print(f"RMSE: {rmse:.3f}")
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[8], line 6
4 # 予測誤差を評価
5 from sklearn.metrics import mean_squared_error
----> 6 rmse = mean_squared_error(y, y_pred, squared=False)
8 print(f"RMSE: {rmse:.3f}")
File /usr/local/lib/python3.10/site-packages/sklearn/utils/_param_validation.py:194, in validate_params.<locals>.decorator.<locals>.wrapper(*args, **kwargs)
191 func_sig = signature(func)
193 # Map *args/**kwargs to the function signature
--> 194 params = func_sig.bind(*args, **kwargs)
195 params.apply_defaults()
197 # ignore self/cls and positional/keyword markers
File /usr/local/lib/python3.10/inspect.py:3186, in Signature.bind(self, *args, **kwargs)
3181 def bind(self, /, *args, **kwargs):
3182 """Get a BoundArguments object, that maps the passed `args`
3183 and `kwargs` to the function's signature. Raises `TypeError`
3184 if the passed arguments can not be bound.
3185 """
-> 3186 return self._bind(args, kwargs)
File /usr/local/lib/python3.10/inspect.py:3175, in Signature._bind(self, args, kwargs, partial)
3173 arguments[kwargs_param.name] = kwargs
3174 else:
-> 3175 raise TypeError(
3176 'got an unexpected keyword argument {arg!r}'.format(
3177 arg=next(iter(kwargs))))
3179 return self._bound_arguments_cls(self, arguments)
TypeError: got an unexpected keyword argument 'squared'
予測値と実測値の散布図を描くと次のようになった。
Show code cell source
fig, ax = plt.subplots()
ax.plot(y_pred, y_pred, color='gray', alpha=.7)
ax.scatter(y_pred, y, alpha=.7)
_ = ax.set(xlabel='Predicted', ylabel='Actual')
[Text(0.5, 0, 'Predicted'), Text(0, 0.5, 'Actual')]
