다항회귀
선형회귀 모델을 다항회귀로 변환
* linear_reg10.py
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1,2,3,4,5])
y = np.array([4,2,1,3,7])
plt.scatter(x, y) # 산포도
plt.show()
선형회귀 모델
from sklearn.linear_model import LinearRegression
x = x[:, np.newaxis] # 입력을 matrix로 주어야함으로 차원 확대
print(x)
'''
[[1]
[2]
[3]
[4]
[5]]
'''
model = LinearRegression().fit(x, y) # 선형회귀 모델
y_pred = model.predict(x) # 예측값
print(y_pred)
# [2. 2.7 3.4 4.1 4.8]
plt.scatter(x, y) # 산포도
plt.plot(x, y_pred, c='red') # 추세선 그래프
plt.show()
다항식 특징을 추가
# 비선형인 경우 다항식 특징을 추가해서 작업한다.
from sklearn.preprocessing import PolynomialFeatures
poly = PolynomialFeatures(degree=3, include_bias = False) # degree : 열 개수, include_bias : 편향
print(poly)
x2 = poly.fit_transform(x) # 특징 행렬 생성
print(x2)
'''
[[ 1. 1. 1.]
[ 2. 4. 8.]
[ 3. 9. 27.]
[ 4. 16. 64.]
[ 5. 25. 125.]]
----제곱------>
'''
model2 = LinearRegression().fit(x2, y) # 선형회귀 모델
y_pred2 = model2.predict(x2) # 예측값
print(y_pred2)
# [4.04285714 1.82857143 1.25714286 2.82857143 7.04285714]
plt.scatter(x, y) # 산포도
plt.plot(x, y_pred2, c='red') # 추세선 그래프
plt.show()
선형회귀 모델을 다항회귀로 변환
: 다항식 추가. 특징행렬 생성.
* linear_reg11.py
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.metrics._regression import mean_squared_error, r2_score
x = np.array([258, 270, 294, 320, 342, 368, 396, 446, 480, 586])[:, np.newaxis]
print(x)
'''
[[258]
[270]
[294]
[320]
[342]
[368]
[396]
[446]
[480]
[586]]
'''
y = np.array([236, 234, 253, 298, 314, 342, 360, 368, 391, 390])
# 비교목적으로 일반회귀 모델 클래스와 다항식 모델 클래스
lr = LinearRegression()
pr = LinearRegression()
polyf = PolynomialFeatures(degree=2) # 특징 행렬 생성
x_quad = polyf.fit_transform(x)
print(x_quad)
'''
[[1.00000e+00 2.58000e+02 6.65640e+04]
[1.00000e+00 2.70000e+02 7.29000e+04]
[1.00000e+00 2.94000e+02 8.64360e+04]
'''
lr.fit(x, y)
x_fit = np.arange(250, 600, 10)[:, np.newaxis]
print(x_fit)
'''
[[250]
[260]
[270]
[280]
[290]
'''
y_lin_fit = lr.predict(x_fit)
print(y_lin_fit)
# [250.63869122 256.03244588 261.42620055 ...
pr.fit(x_quad, y)
y_quad_fit = pr.predict(polyf.fit_transform(x_fit))
print(y_quad_fit)
# [215.50100168 228.03388862 240.11490613 ...
# 시각화
plt.scatter(x, y, label='train points')
plt.plot(x_fit, y_lin_fit, label='linear fit', linestyle='--', c='red')
plt.plot(x_fit, y_quad_fit, label='quadratic fit', linestyle='-', c='blue')
plt.legend()
plt.show()
print()
# MSE(평균 제곱오차)와 R2(결정계수) 확인
y_lin_pred = lr.predict(x)
print('y_lin_pred :\n', y_lin_pred)
'''
[254.95369495 261.42620055 274.37121174 288.39497387 300.26123414
314.28499627 329.38750933 356.35628266 374.69504852 431.86884797]
'''
y_quad_pred = pr.predict(x_quad)
print('y_quad_pred :\n', y_quad_pred)
'''
[225.56346079 240.11490613 267.26572086 293.74195218 313.75904653
334.59594733 353.61955374 378.77882554 389.43443486 389.12615204]
'''
print('train MSE 비교 : 선형모델은 %.3f, 다항모델은 %.3f'%(mean_squared_error(y, y_lin_pred), mean_squared_error(y, y_quad_pred)))
# train MSE 비교 : 선형모델은 570.885, 다항모델은 58.294
print('train 결정계수 비교 : 선형모델은 %.3f, 다항모델은 %.3f'%(r2_score(y, y_lin_pred), r2_score(y, y_quad_pred)))
# train 결정계수 비교 : 선형모델은 0.831, 다항모델은 0.983
'BACK END > Deep Learning' 카테고리의 다른 글
[딥러닝] SVM (0) | 2021.03.16 |
---|---|
[딥러닝] 로지스틱 회귀 (0) | 2021.03.15 |
[딥러닝] 단순선형 회귀, 다중선형 회귀 (0) | 2021.03.11 |
[딥러닝] 선형회귀 (0) | 2021.03.10 |
[딥러닝] 공분산, 상관계수 (0) | 2021.03.10 |