선형회귀Linear Regression 모델은 선형회귀식을 활용한 모델입니다. 선형 회귀 모델을 훈련한다는 것은 훈련 데이터에 잘 맞는 모델 파라미터, 즉 회귀계수를 찾는 것입니다.
# 데이터 생성
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0) # 시드값 고정
w0 = 5 # y절편
w1 = 2 # 회귀계수
noise = np.random.randn(100, 1) # 노이즈
x = 4 * np.random.rand(100, 1) # 0~4 사이의 균등분포 난수 100개
y = w1*x + w0 + noise # y 값
plt.scatter(x, y);
#plt.show();
# 데이터 훈련
from sklearn.linear_model import LinearRegression
linear_reg_model = LinearRegression() # 선형 회귀 모델
linear_reg_model.fit(x, y) # 모델 훈련
print('y절편(w0):', linear_reg_model.intercept_)
print('회귀계수(w1):', linear_reg_model.coef_)
# 회귀선 확인
y_pred = linear_reg_model.predict(x) # x에 대한 예측값
plt.scatter(x, y) # 산점도
plt.plot(x, y_pred ) # 회귀선 그리기
plt.show();