Skip to content

Latest commit

 

History

History
128 lines (58 loc) · 1.17 KB

linear_regression.md

File metadata and controls

128 lines (58 loc) · 1.17 KB
import numpy as np
import matplotlib.pyplot as plt
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)
X_b = np.c_[np.ones((100, 1)), X]
X_b
theta_best = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y)
theta_best
array([[3.87608605],
       [2.97262453]])
X_new = np.array([[0], [2]])
X_new_b = np.c_[np.ones((2, 1)), X_new]
y_predict =  X_new_b.dot(theta_best)
plt.plot(X , y , 'ro')
plt.plot(X_new, y_predict, "b-")

plt.show()

png

from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression()
lin_reg.fit(X, y)
lin_reg.intercept_, lin_reg.coef_
(array([3.87608605]), array([[2.97262453]]))
lin_reg.predict(X_new)
array([[3.87608605],
       [9.82133511]])
theta_best_svd, residuals, rank, s = np.linalg.lstsq(X_b, y, rcond=1e-6)
theta_best_svd
array([[3.87608605],
       [2.97262453]])
np.linalg.pinv(X_b).dot(y)
array([[3.87608605],
       [2.97262453]])