Upgrade to Pro — share decks privately, control downloads, hide ads and more …

線形回帰の実装

Avatar for kaise kaise
May 27, 2024
210

 線形回帰の実装

単回帰から重回帰の実装,おまけとして多項式回帰についてまとめた.

Avatar for kaise

kaise

May 27, 2024
Tweet

Transcript

  1. 答え. 2.8 実装してみよう class SimpleLinearRegression(): def fit(self, x, y): var_x,

    cov = np.cov(x, y)[0] bar_x = x.mean() bar_y = y.mean() self.a = cov / var_x self.b = bar_y - self.a * bar_x def predict(self, x): y = self.a * x + self.b return y
  2. 3.7 実装してみよう 答え. class LinearRegression(): def fit(self, X, y): X

    = np.insert(X, 0, 1, axis=1) self.w = np.linalg.inv(X.T @ X) @ X.T @ y def predict(self, X): X = np.insert(X, 0, 1, axis=1) return X @ self.w