0
1

More than 3 years have passed since last update.

機械学習 線形回帰

Posted at

○この記事の要点
線形回帰を学習したのでメモ

線形回帰:
・回帰問題の予測を行うアルゴリズム
・y = b + axの直線で表現する
・各データと予測で出す直線の誤差(損失)を最小にするパラメータを求める。
・誤差の測定には、平均二乗誤差を利用(直線と各データの距離の2乗の平均)
・説明変数が大きくなるに連れて、目的変数も大きく(小さく)なる関係性をモデル化できる。
・教師あり学習

○ソースコード(Python)

線形回帰モデル
# 線形回帰
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
%matplotlib inline

# 学習データ
X = [[10.0], [8.0], [13.0], [9.0], [11.0], [14.0], [6.0], [4.0], [12.0], [7.0], [5.0]]
y = [8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68]

# モデルの生成と学習、評価
model = LinearRegression()
model.fit(X, y) # 学習
print(model.coef_) # 回帰直線の傾き
print(model.intercept_) # 回帰直線の切片
print('y = 0.5x + 3')
y_pred = model.predict(X) #予測

# グラフ表示
fig, ax = plt.subplots()
plt.xlabel("X")
plt.ylabel("y")
ax.scatter(X, y, color='blue', marker='s', label='data')
plt.plot(X, y_pred, "r-")

結果
[0.50009091]
3.0000909090909094
y = 0.5x + 3
ダウンロード.png

・データ数が少ないが、それなりにデータを表現していると思う
・回帰問題の予測を行うアルゴリズムには、サポートベクトルマシン、正則化、ニューラルネットワークなどがあるが、このアルゴリズムが一番理解しやすい
・理解しやすいのは、学生のころ1次関数で学んでいるからだと思う。必須教育にもっとこういったアルゴリズムの勉強を追加してほしいと思う

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1