from IPython.display import display, Image
y = w1 * x1 + w2 * x2 + b (w1, w2가 계수)
import mglearn
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler, PolynomialFeatures
from sklearn.linear_model import Ridge # 릿지회귀
import pandas as pd
# 한글
import matplotlib
from matplotlib import font_manager, rc
font_loc = "C:/Windows/Fonts/malgunbd.ttf"
font_name = font_manager.FontProperties(fname=font_loc).get_name()
matplotlib.rc('font', family=font_name)
matplotlib.rcParams['axes.unicode_minus'] = False
%matplotlib inline
### 데이터 셋 준비
boston = load_boston() # 데이터 셋 불러오기
print(type(boston.target), type(boston.data))
print(boston.target.shape, boston.data.shape)
df_boston = pd.DataFrame(boston.data,columns=boston.feature_names)
df_boston['target'] = pd.Series(boston.target)
df_boston.head()
<class 'numpy.ndarray'> <class 'numpy.ndarray'> (506,) (506, 13)
CRIM | ZN | INDUS | CHAS | NOX | RM | AGE | DIS | RAD | TAX | PTRATIO | B | LSTAT | target | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0.00632 | 18.0 | 2.31 | 0.0 | 0.538 | 6.575 | 65.2 | 4.0900 | 1.0 | 296.0 | 15.3 | 396.90 | 4.98 | 24.0 |
1 | 0.02731 | 0.0 | 7.07 | 0.0 | 0.469 | 6.421 | 78.9 | 4.9671 | 2.0 | 242.0 | 17.8 | 396.90 | 9.14 | 21.6 |
2 | 0.02729 | 0.0 | 7.07 | 0.0 | 0.469 | 7.185 | 61.1 | 4.9671 | 2.0 | 242.0 | 17.8 | 392.83 | 4.03 | 34.7 |
3 | 0.03237 | 0.0 | 2.18 | 0.0 | 0.458 | 6.998 | 45.8 | 6.0622 | 3.0 | 222.0 | 18.7 | 394.63 | 2.94 | 33.4 |
4 | 0.06905 | 0.0 | 2.18 | 0.0 | 0.458 | 7.147 | 54.2 | 6.0622 | 3.0 | 222.0 | 18.7 | 396.90 | 5.33 | 36.2 |
df_boston.loc[:, 'CRIM':'LSTAT'].shape
(506, 13)
X = df_boston.loc[:, 'CRIM':'LSTAT'] # 입력 데이터
y = boston.target
print("정규화,확장 전 데이터 셋 : ", X.shape, y.shape)
정규화,확장 전 데이터 셋 : (506, 13) (506,)
normalize_X = MinMaxScaler().fit_transform(X) # 입력 데이터 정규화
ex_X = PolynomialFeatures(degree=2, include_bias=False).fit_transform(normalize_X) # 데이터 feature 추가 생성
print("정규화, 추가 생성 : ", ex_X.shape, y.shape)
정규화, 추가 생성 : (506, 104) (506,)
from sklearn.linear_model import LinearRegression
X_train, X_test, y_train, y_test = train_test_split(ex_X, y, random_state=42)
lr = LinearRegression().fit(X_train, y_train)
print("훈련 데이터 세트 점수 : {:.2f}".format(lr.score(X_train, y_train)))
print("테스트 데이터 세트 점수 : {:.2f}".format(lr.score(X_test, y_test)))
훈련 데이터 세트 점수 : 0.94 테스트 데이터 세트 점수 : 0.78
# from sklearn.linear_model import Ridge
ridge = Ridge().fit(X_train, y_train)
print(ridge)
print("훈련 세트 점수 : {:.2f}".format(ridge.score(X_train, y_train)))
print("테스트 세트 점수 : {:.2f}".format(ridge.score(X_test, y_test)))
Ridge() 훈련 세트 점수 : 0.87 테스트 세트 점수 : 0.81
ridge10 = Ridge(alpha=10).fit(X_train, y_train)
print(ridge10)
print("훈련 세트 점수 : {:.2f}".format(ridge10.score(X_train, y_train)))
print("테스트 세트 점수 : {:.2f}".format(ridge10.score(X_test, y_test)))
Ridge(alpha=10) 훈련 세트 점수 : 0.77 테스트 세트 점수 : 0.73
ridge01 = Ridge(alpha=0.1).fit(X_train, y_train)
print(ridge01)
print("훈련 세트 점수 : {:.2f}".format(ridge01.score(X_train, y_train)))
print("테스트 세트 점수 : {:.2f}".format(ridge01.score(X_test, y_test)))
Ridge(alpha=0.1) 훈련 세트 점수 : 0.92 테스트 세트 점수 : 0.82
ridge001 = Ridge(alpha=0.01).fit(X_train, y_train)
A. 앞의 예제는 alpha를 10, 0.1, 0.01으로 이용 B. alpha는 모델을 얼마나 많이 규제할지 조절한다.
ridge_p = [10, 5, 1, 0.1, 0.01]
for i in ridge_p:
ridge = Ridge(alpha=i).fit(X_train, y_train)
print("alpha : {}".format(i))
print("훈련 데이터 세트 점수 : {:.2f}".format(ridge.score(X_train, y_train)))
print("테스트 데이터 세트 점수 : {:.2f}".format(ridge.score(X_test, y_test)))
alpha : 10 훈련 데이터 세트 점수 : 0.77 테스트 데이터 세트 점수 : 0.73 alpha : 5 훈련 데이터 세트 점수 : 0.80 테스트 데이터 세트 점수 : 0.76 alpha : 1 훈련 데이터 세트 점수 : 0.87 테스트 데이터 세트 점수 : 0.81 alpha : 0.1 훈련 데이터 세트 점수 : 0.92 테스트 데이터 세트 점수 : 0.82 alpha : 0.01 훈련 데이터 세트 점수 : 0.94 테스트 데이터 세트 점수 : 0.81
display(Image(filename='img/ridge01.png'))
import matplotlib.pyplot as plt
%matplotlib inline
lr = LinearRegression().fit(X_train, y_train)
ridge01 = Ridge(alpha=0.1).fit(X_train, y_train)
ridge001 = Ridge(alpha=0.01).fit(X_train, y_train)
ridge0001 = Ridge(alpha=0.001).fit(X_train, y_train)
fig = plt.figure(figsize=(12,12))
plt.subplot(2, 2, 1)
plt.hlines(0,0, len(lr.coef_))
plt.plot(lr.coef_, 's', label="LinearRegression")
plt.title('LinearRegression')
plt.subplot(2, 2, 2)
plt.hlines(0,0, len(ridge0001.coef_))
plt.plot(ridge0001.coef_, 's', label="Ridge alpha=0.0001")
plt.title('Ridge alpha=0.0001')
plt.subplot(2, 2, 3)
plt.hlines(0,0, len(ridge001.coef_))
plt.plot(ridge001.coef_, 's', label="Ridge alpha=0.001")
plt.title('Ridge alpha=0.001')
plt.subplot(2, 2, 4)
plt.hlines(0,0, len(ridge01.coef_))
plt.plot(ridge01.coef_, 's', label="Ridge alpha=0.01")
plt.title('Ridge alpha=0.01')
Text(0.5, 1.0, 'Ridge alpha=0.01')
fig = plt.figure(figsize=(10,27))
#ax1 = fig.add_subplot(5, 1, 1)
#ax2 = fig.add_subplot(5, 1, 2)
#ax3 = fig.add_subplot(5, 1, 3)
#ax4 = fig.add_subplot(5, 1, 4)
#ax5 = fig.add_subplot(5, 1, 5)
plt.subplot(5, 1, 1)
plt.hlines(0,0, len(ridge001.coef_))
plt.plot(ridge001.coef_, 's', label="Ridge alpha=0.01")
plt.title('Ridge alpha=0.01')
plt.subplot(5, 1, 2)
plt.hlines(0,0, len(ridge01.coef_))
plt.plot(ridge01.coef_, 's', label="Ridge alpha=0.1")
plt.title('Ridge alpha=0.1')
plt.subplot(5, 1, 3)
plt.hlines(0,0, len(ridge.coef_))
plt.plot(ridge.coef_, '^', label="Ridge alpha=1")
plt.title('Ridge alpha=1')
plt.subplot(5, 1, 4)
plt.hlines(0,0, len(ridge10.coef_))
plt.plot(ridge10.coef_, 'v', label="Ridge alpha=10")
plt.title('Ridge alpha=10')
plt.subplot(5, 1, 5)
plt.hlines(0,0, len(ridge001.coef_))
plt.plot(ridge001.coef_, 'r^', label="Ridge alpha=0.01")
plt.plot(ridge01.coef_, 'go', label="Ridge alpha=0.1")
plt.plot(ridge.coef_, 'yv', label="Ridge alpha=1")
plt.plot(ridge10.coef_, 'bs', label="Ridge alpha=10")
plt.title('Ridge alpha=0.01, 0.1, 1, 10')
plt.xlabel("계수 목록")
plt.ylabel("계수 크기")
plt.legend(ncol=2, loc=(0,0.85))
plt.show()
mglearn.plots.plot_ridge_n_samples()
display(Image(filename='img/linear_model02_lasso.png'))
from sklearn.linear_model import Lasso
import numpy as np
lasso = Lasso().fit(X_train, y_train)
print("학습용 데이터 세트 점수 : {:.2f}".format(lasso.score(X_train, y_train)))
print("테스트 데이터 세트 점수 : {:.2f}".format(lasso.score(X_test, y_test)))
학습용 데이터 세트 점수 : 0.27 테스트 데이터 세트 점수 : 0.26
# 특성(feature)가 0이 아닌 것의 개수는?
print("사용한 특성의 수 : {:.2f}".format(np.sum(lasso.coef_ != 0)))
사용한 특성의 수 : 3.00
lasso00001 = Lasso(alpha=0.0001, max_iter=100000).fit(X_train, y_train)
print("학습용 데이터 세트 점수 : ",lasso00001.score(X_train, y_train))
print("테스트 데이터 세트 점수 : ",lasso00001.score(X_test, y_test))
print("사용한 특성의 수 : ", np.sum(lasso00001.coef_ != 0))
학습용 데이터 세트 점수 : 0.9435815252488565 테스트 데이터 세트 점수 : 0.8080525356174253 사용한 특성의 수 : 95
lasso001 = Lasso(alpha=0.01, max_iter=100000).fit(X_train, y_train)
print("학습용 데이터 세트 점수 : ",lasso001.score(X_train, y_train))
print("테스트 데이터 세트 점수 : ",lasso001.score(X_test, y_test))
print("사용한 특성의 수 : ", np.sum(lasso001.coef_ != 0))
학습용 데이터 세트 점수 : 0.8864717420585476 테스트 데이터 세트 점수 : 0.8036004116583615 사용한 특성의 수 : 34
lasso01 = Lasso(alpha=0.1, max_iter=100000).fit(X_train, y_train)
print("학습용 데이터 세트 점수 : ",lasso01.score(X_train, y_train))
print("테스트 데이터 세트 점수 : ",lasso01.score(X_test, y_test))
print("사용한 특성의 수 : ", np.sum(lasso01.coef_ != 0))
학습용 데이터 세트 점수 : 0.7471467575228325 테스트 데이터 세트 점수 : 0.6986891802234085 사용한 특성의 수 : 12
lasso10 = Lasso(alpha=10, max_iter=100000).fit(X_train, y_train)
print("학습용 데이터 세트 점수 : ",lasso10.score(X_train, y_train))
print("테스트 데이터 세트 점수 : ",lasso10.score(X_test, y_test))
print("사용한 특성의 수 : ", np.sum(lasso10.coef_ != 0))
학습용 데이터 세트 점수 : 0.0 테스트 데이터 세트 점수 : -0.03189647654769301 사용한 특성의 수 : 0
Lasso에 alpha를 0.0001, 0.001, 0.01, 0.1, 1, 10에 대한 학습용 데이터와 테스트용 데이터 셋의 결정계수의 값을 확인해 보자. 각각의 모델의 변수가 몇개씩 남는지?
alpha_p = [0.0001, 0.001, 0.01, 0.1, 1, 10]
for p in alpha_p:
lasso = Lasso(alpha=p).fit(X_train, y_train)
tr_score = lasso.score(X_train, y_train)
test_score = lasso.score(X_test, y_test)
print("alpha : {} 학습 : {}, 테스트 : {}".format(p,tr_score, test_score))
print("유효한 feature 개수 : ", np.sum(lasso.coef_ !=0))
alpha : 0.0001 학습 : 0.9374330725382051, 테스트 : 0.7764741268470517 유효한 feature 개수 : 102 alpha : 0.001 학습 : 0.9296864690381805, 테스트 : 0.8141500652221183 유효한 feature 개수 : 76 alpha : 0.01 학습 : 0.8865033777946089, 테스트 : 0.80359225764207 유효한 feature 개수 : 34 alpha : 0.1 학습 : 0.7471467575228325, 테스트 : 0.6986891802234085 유효한 feature 개수 : 12 alpha : 1 학습 : 0.26783778369518485, 테스트 : 0.2599232118344591 유효한 feature 개수 : 3 alpha : 10 학습 : 0.0, 테스트 : -0.03189647654769301 유효한 feature 개수 : 0
C:\Users\toto\anaconda3\lib\site-packages\sklearn\linear_model\_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 1061.5581359471444, tolerance: 3.361037625329815 model = cd_fast.enet_coordinate_descent( C:\Users\toto\anaconda3\lib\site-packages\sklearn\linear_model\_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 864.3796550250048, tolerance: 3.361037625329815 model = cd_fast.enet_coordinate_descent( C:\Users\toto\anaconda3\lib\site-packages\sklearn\linear_model\_coordinate_descent.py:529: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 6.848133364149362, tolerance: 3.361037625329815 model = cd_fast.enet_coordinate_descent(
lasso = Lasso(alpha=1).fit(X_train, y_train)
plt.figure(figsize=(10,6))
plt.plot(lasso00001.coef_, "r^", label="Lasso alpha=0.0001")
plt.plot(lasso001.coef_, 'go', label="Lasso alpha=0.01")
plt.plot(lasso01.coef_, 'yv', label="Lasso alpha=0.1")
plt.plot(lasso10.coef_, "bs", label="Lasso alpha=10")
plt.xlabel("계수 목록")
plt.ylabel("계수 크기")
plt.ylim(-25, 25)
plt.legend(ncol=2, loc=(0,1.05))
plt.show()
교육용으로 작성된 것으로 배포 및 복제시에 사전 허가가 필요합니다.
Copyright 2021 LIM Co. all rights reserved.