๐ผ ์ ๋ฒ ํฌ์คํ ์์ Ridge ํ๊ท๊ฐ ๋ฌด์์ธ์ง ๊ฐ๋ ์ ๋ํด ์ ํํ ์์๋ณด์๋ค ๐ผ
(L2 Regularization) → Ridge Regression (concepts)
** ์ฐ๋ฆฌ๋ ์ ๋ฒ ํฌ์คํ ์์ Supervised Learning ์ค Regression์ ์ผ์ข ์ธ 'linear regression'์ ๋ํด ํ์ตํ๋ค. โ๏ธ ์ ๊ทธ๋ฆผ์์ ๋ณด๋ค์ํผ linear ์ ํ regression์ผ๋ก๋ ๋ง์ ์ข ๋ฅ์ model์ด ์์์ ํ์ธํ ..
sh-avid-learner.tistory.com
→ ์ด์ Ridge ํ๊ท๋ฅผ ์ค์ python code๋ก ๊ตฌํํด์ ์ผ๋ง๋ model์ด ์ ์์ธก์ ํ๋์ง, ๊ณผ์ฐ ๊ณผ์ ํฉ์์ ๋ฒ์ด๋ ๋ ๋์ model์ ๋ง๋ค ์ ์๋ ๊ฑด์ง ์ด๋ฒ ํฌ์คํ ์ ํตํด ์์๋ณด๋ ค ํ๋ค
→ Ridge ๋ชจ๋ธ์ ๋ง๋๋ ํจ์๋ ํฌ๊ฒ Ridge์ RidgeCV๋ก ๋๋๋ค. ๊ฐ๊ฐ์ ๋ํด์ ์์๋ณด์
1. Ridge
โค Ridge docu โค
https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Ridge.html
class sklearn.linear_model.Ridge(alpha=1.0, *, fit_intercept=True, normalize='deprecated', copy_X=True, max_iter=None, tol=0.001, solver='auto', positive=False, random_state=None)
๐ 'Linear least squares with l2 regularization. Minimizes the objective function(cost function์ด๋ผ๊ณ ๋ ํ๋ฉฐ ์๋ bold์ฒด) This model solves a regression model where the loss function is the linear least squares function and regularization is given by the l2-norm. Also known as Ridge Regression or Tikhonov regularization. This estimator has built-in support for multi-variate regression(๋ค์ค ํ๊ท์๋ ์ ์ฉ ๊ฐ๋ฅ์๊ธฐ) (i.e., when y is a 2d-array of shape (n_samples, n_targets)'
" ||y - Xw||^2_2(SSR) + alpha * ||w||^2_2(L2-norm) "
- ์ผ๋ถ ์ค์ parameter๋ฅผ ๋ณด๋ฉด .. -
→ alpha) (default = 1.0) 'Regularization strength; must be a positive float. Regularization improves the conditioning of the problem and reduces the variance of the estimates. Larger values specify stronger regularization.'
= concept ํฌ์คํ ์์ ๋ค๋ฃฌ penalty ๋ถ๊ณผ ์ ๋ lambda ๊ฐ์ ๋งํ๋ค
→ max_iter & solver - ๋ด๋ถ data type์ ๋ฐ๋ผ ridge ๊ตฌํ๋ ์ต์ ์ ์ฐ์ฐ๋ฒ์ auto ์๋์ผ๋ก ์ฐพ์์ฃผ๋๋ฐ, ์ด ๋์ ์ฐ์ฐ๋ฒ์ ์์๋ก ์ง์ ํด์ ํด๋น ์ฐ์ฐ๋ฒ์ผ๋ก ridge๋ฅผ ๊ตฌํ๊ฒ ์ค์ ํ ์ ์๋ค. (max_iter๋ ์ด ๋ ์ฐ์ฐ๋ฒ ๋ฐ๋ณต ํ์)
(- ๋ค์ํ ์ข ๋ฅ์ solver๊ฐ ์์ผ๋ ๋๋ฌด ์ฌํ ๋ด์ฉ์ด๋ผ pass - ๐ )
ใalpha ์ฆ๊ฐ์ ๋ฐ๋ฅธ Ridge ์ ํ๋ชจ๋ธ ๋ณํ ์๊ฐํ ๐ ใ
→ ์ฐ๋ฆฌ๋ alpha(์ฆ lambda)๊ฐ์ ๋ฐ๋ผ penalty์ ํฌ๊ณ ์์์ ์ํฅ์ ์ค์ Ridge ๋ชจ๋ธ์ด ๊ธฐ์กด SLR(Simple Linear Regression) ๋ชจ๋ธ์ ๊ณผ์ ํฉ์ฑ์ ํด๊ฒฐํ๊ณ ์, ์ฆ ๋ generalized๋ model์ ๋ง๋ค ์ ์๋ค๊ณ ์ธ๊ธํ๋ค.
→ ์ฐ๋ฆฌ๋ Ridge()๋ฅผ ์ฌ์ฉํ์ฌ ์ฌ๋ฌ alpha๊ฐ์ ์ง์ด๋ฃ๊ณ ๊ฐ alpha๊ฐ์ ๋ฃ์ Ridge ๋ชจ๋ธ์ ๋ชจ๋ ์๊ฐํํ์ฌ ๊ฐ๊ฐ์ ์ฐจ์ด์ ์ ๋น๊ตํด๋ณผ ์ ์๋ค!
1> iris dataset ์ค๋น
2> SLR๋ชจ๋ธ๊ณผ์ ๋น๊ต๋ฅผ ์ํด y์ข ์๋ณ์๋ 'petal length' & x๋ ๋ฆฝ๋ณ์๋ 'sepal length' 1๊ฐ ํ์
3> alpha๊ฐ์ 0๋ถํฐ 20, 40, 60, 80 ๊น์ง ์ด 5๊ฐ์ง์ Ridge model ์ค๋น
(→์ฌ๊ธฐ์ alpha๊ฐ 0์ผ๋๋ penalty๋ฅผ ์์ ๋ถ๊ณผํ์ง ์๊ฒ ๋ค๋ ๋ป์ผ๋ก SLR model์ด ๊ณง Ridge model)
4> ์ด 5๊ฐ์ Ridge model์ ์๊ฐํํ์ฌ ์ก์์ ์ฐจ์ด์ ์ ํ์ธํด๋ณด์
import matplotlib.pyplot as plt
import matplotlib
from sklearn.linear_model import Ridge
import seaborn as sns
import numpy as np
import pandas as pd
#preprocessing
df = sns.load_dataset('iris')
df.drop(columns=['sepal_width','species','petal_width'],inplace=True)
plt.figure(figsize=(6,7))
title_font = {
'fontsize': 16,
'fontweight': 'light'
}
plt.title('Sepal Length vs. Petal Length - SLR vs Ridge', pad=15, fontdict=title_font)
plt.xlabel('Sepal Length', labelpad=10, size=13)
plt.ylabel('Petal Length', labelpad=10, size=13)
plt.scatter(df['sepal_length'], df['petal_length'],color = '#C6492B')
x_lim = plt.xlim()
alphas = np.arange(0,100,20)
for alpha in alphas:
#get a Ridge model
ridge = Ridge(alpha=alpha)
#fitting
ridge.fit(df[['sepal_length']], df['petal_length'])
slope = ridge.coef_
intercept = ridge.intercept_
x = x_lim
#visualization
plt.plot(x, slope*x + intercept, linewidth = 3, label = f'alpha {alpha}')
plt.grid(True, linestyle='--')
plt.legend()
plt.style.use('default')
plt.axhline(df['petal_length'].mean(), 0, 1, color = 'red', linewidth=3)
→ ํด์ 1> alpha๊ฐ ์ฆ๊ฐํ ์๋ก ๊ธฐ์ธ๊ธฐ๊ฐ ๊ฐ์ํ๋ ๊ฒ์ ๋ณผ ์ ์๋ค. ์ด๋ lambda๊ฐ์ ์ฆ๊ฐ์ํจ๋ค๋ฉด cost function์ ์ต์ํํ๊ธฐ ์ํ ๋ชฉ์ ์ผ๋ก ํ๊ท๊ณ์์ ๊ณฑํฉ์ ๊ฐ์์์ผ์ผ ํ๋ฏ๋ก - ํด๋น ๊ธฐ์กด SLR ๋ชจ๋ธ์์์ ํ๊ท๊ณ์์ธ ๊ธฐ์ธ๊ธฐ๊ฐ์ ๊ฒฐ๊ณผ์ ์ผ๋ก ๊ฐ์์ํจ ํจ๊ณผ
→ ํด์ 2> ๊ทธ๋ฌ๋ alpha๋ฅผ ์ ์ ์ฆ๊ฐ์ํฌ์๋ก SLR model์ basline model์ ์ ์ ๊ฐ๊น์์ง๋ ๊ฒ์ ํ์ธํ ์ ์๋ค. ๋ฐ๋ผ์ ์ด๋ ์ ๋์ ์์ ์์ ๋ฉ์ถฐ์ผ ํ๋ค!
๐ ์ฆ, ์ฐ๋ฆฌ๋ ์ต์ ์ alpha๋ฅผ ๊ตฌํ ์ ์์ด์ผ ํ๋ค ๐
→ alpha ํ๋ณด๊ฐ๋ค์ ๋ชจ๋ ridge์ ๋์
ํด ๊ฐ๋ฉด์ cross-validation ๊ธฐ๋ฒ์ผ๋ก ์ต์ ์ alpha๋ฅผ ์ฐพ์์ผ ํ๋ค!
(์ต์ ์ alpha ์ฐพ๋ ๊ธฐ์ค์ evaluation metrics (scoring system)์ ์๊ฑฐํ์ฌ ์ฐพ์)
(*CV ๊ธฐ๋ฒ ๊ด๋ จ ํฌ์คํ ์ ํ๋จ ์ฐธ์กฐ ↓↓↓↓)
Cross-Validation (concepts)
* ๋จธ์ ๋ฌ๋์ ์ํด์ ๋ฌด.์กฐ.๊ฑด. ์์์ผ ํ๋ CROSS-VALIDATION! ๊ฐ๋จํ ๊ฐ๋ ๋ง ์์๋ณดZA * - 2๋ฒ ๊ณผ์ - model selection์์ ์ฃผ๋ก ๋ง์ด ์ฐ์ด๋ cross-validation ๊ธฐ๋ฒ - ๐ง PURPOSE? 'The purpose of cross..
sh-avid-learner.tistory.com
→ ์ฌ๋ฌ alpha ํ๋ณด๊ตฐ๋ค์ ๋ฃ์ด์ ์์์ ์ต์ ์ alpha๋ฅผ ์ฐพ์ Ridge model์ ๋ง๋ค์ด์ฃผ๋ ํจ์๊ฐ ๋ฐ๋ก ์กด์ฌํ๋ค! - RidgeCV
2. RidgeCV
โค RidgeCV docu โค
class sklearn.linear_model.RidgeCV(alphas=(0.1, 1.0, 10.0), *, fit_intercept=True, normalize='deprecated', scoring=None, cv=None, gcv_mode=None, store_cv_values=False, alpha_per_target=False)
๐ 'Ridge regression with built-in cross-validation' - ์ฌ๋ฌ alpha๊ฐ๋ค์ list ํํ๋ก alphas์ ์ง์ด๋ฃ์ผ๋ฉด ํด๋น alpha ์ค ์ต์ ์ alpha๋ฅผ built-in cv๊ธฐ๋ฒ(Leave-One-Out CV)์ ์ฌ์ฉํ์ฌ ์ฐพ๋๋ค. ์ฌ๊ธฐ์ scoring ์ธ์์ ์ํ๋ metrics ์ข ๋ฅ๋ฅผ ์ง์ด๋ฃ์ด ์ต์ ์ alpha๋ฅผ ์ด๋ค ์งํ๋ก ์ฐพ๋ ์ง ์๋์ผ๋ก ๋ฃ์ ์ ์๋ค.
→ cv์ธ์) ๋ช fold์ cross-validation ์ฌ์ฉํ ์ง ๊ฒฐ์ (10์ ๊ถ์ฅ)
→ alphas์ธ์) ๊ฒ์ฆํ๊ณ ์ถ์ alpha๋ค list ํํ๋ก ์ง์ด๋ฃ๊ธฐ
→ scoring ์ธ์) ์ต์ ์ alpha๋ฅผ ๊ตฌํ๊ธฐ ์ํด ์ฌ์ฉํ๋ scroing evaluation metrics
- default๋ None) 'If None, the negative mean squared error if cv is ‘auto’ or None (i.e. when using leave-one-out cross-validation), and r2 score otherwise.' = '์ฆ cv๊ฐ์ด ์์ผ๋ฉด NMSE๋ฅผ ์ฌ์ฉํ๋ cv๊ฐ์ด ๋ช ์๋์ด ์์ผ๋ฉด r2๊ฐ ์ฌ์ฉ'
> ์ฌ๊ธฐ์ NMSE๋ MSE๊ฐ์ ์์(-)๋ฅผ ๊ณฑํ ๊ฒ์ผ ๋ฟ - ์ฆ MSE์ ๋ฐ๋๋ก NMSE๊ฐ ์ปค์ง๋ ๋ฐฉํฅ ์ชฝ์ด ๋ฐ๋์งํจ!
(evaluation metrics ๊ด๋ จ ํฌ์คํ ์ ์๋ ์ฐธ์กฐ ↓↓↓↓)
All About Evaluation Metrics(1/2) → MSE, MAE, RMSE, R^2
** ML ๋ชจ๋ธ์ ์ฑ๋ฅ์ ์ต์ข ์ ์ผ๋ก ํ๊ฐํ ๋ ๋ค์ํ evaluation metrics๋ฅผ ์ฌ์ฉํ ์ ์๋ค๊ณ ํ์! ** (supervised learning - regression problem์์ ๋ง์ด ์ฐ์ด๋ ํ๊ฐ์งํ๋ค) - ๊ณผ์ (5) - ๐ ๊ทธ๋ฌ๋ฉด ์ฐจ๊ทผ์ฐจ..
sh-avid-learner.tistory.com
+) ์ถ๊ฐ๋ก model attributes๋ก best_score_์ด ์๋๋ฐ ํด๋น attribute๋ฅผ ํตํด ์ต์ ์ alpha๋ฅผ ๊ตฌํ ๋ ์ฌ์ฉํ ์ต์ ์ score๊ฐ์ ๋ฐ๋ก ์ถ๋ ฅํ ์ ์๋ค
> ์ iris dataset ์๋ฅผ ๊ทธ๋๋ก ๊ฐ์ ธ์์ ์ด 7๊ฐ์ alpha ์ค ์ต์ ์ alpha๋ฅผ 5-folds CV๋ฅผ ์ด์ฉํด ์ฐพ์๋ณด๋ฉด.. (NMSE scoring system)
(df๋ฅผ train๊ณผ test๋ก ๋๋๊ณ ridge model์ train data๋ฅผ fittingํ๋ค)
train = df.sample(frac=0.75,random_state=1)
test = df.drop(train.index)
target = 'petal_length'
## X_train, y_train, X_test, y_test ๋ฐ์ดํฐ๋ก ๋ถ๋ฆฌ
X_train = train.drop(columns=target)
y_train = train[target]
X_test = test.drop(columns=target)
y_test = test[target]
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.linear_model import RidgeCV
alphas = [0.01, 0.05, 0.1, 0.2, 1.0, 10.0, 100.0]
ridge = RidgeCV(alphas=alphas, cv=5)
#fitting
ridge.fit(X_train, y_train)
print(ridge.coef_, ridge.intercept_, ridge.alpha_)
print(ridge.best_score_)
#[1.85727652] -7.153726228538956 1.0
#0.7520930909807484
> alpha๊ฐ 1์ผ ๋์ด๋ฉฐ, y =1.85727652x -7.153726228538956์ ํด๋นํ๋ ์ง์ ์ด๋ค.
→ ํด๋น alpha๋ค ์ค ํ์ ํด์ ์ต์ ์ Ridge๋ฅผ ๊ตฌํ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ๋ง์ฝ SLR & MLR ๋ชจ๋ธ์์ ๊ณผ์ ํฉ์ด ๋ฐ์ํ๋ค๋ฉด ๋ค์ํ alpha๋ฅผ ์ง์ด๋ฃ์ด ์ ๋ฐ์ฑ์ ๋์ผ ํ์๊ฐ ์๋ค๐
- ์ด ๋ ์ต์ ์ alpha๋ฅผ ์ฐพ๊ธฐ ์ํด ์๋์ผ๋ก ์ฌ๋ฌ alpha์์ ๊ตฌํ๋ ๋์ GridSearchCV๋ฅผ ์ฌ์ฉํ๊ธฐ๋ ํ๋ค (์ถํ ํฌ์คํ ์์ )
> Q.) ๊ทธ๋ฌ๋ฉด ๊ณผ์ฐ ๊ธฐ์กด SLR ๋ชจ๋ธ๋ณด๋ค Ridge ์ ์ฉํ ํ์ ๋ชจ๋ธ ์ฑ๋ฅ์ด ๋ ์ข์์ก์๊น?
→ test set์ ์ ์ฉํด๋ณด์
1) ridge์ผ ๋์ r2_score & MSE
#predicting
y_test_pred = ridge.predict(X_test)
print(r2_score(y_test, y_test_pred), mean_squared_error(y_test, y_test_pred))
#0.7288552126544037 0.7451543211624959
2) SLR ๋ชจ๋ธ์ผ ๋์ r2_score & MSE
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(X_train, y_train)
#predicting
y_test_pred_SLR = model.predict(X_test)
print(r2_score(y_test, y_test_pred_SLR), mean_squared_error(y_test, y_test_pred_SLR))
#0.727547500747554 0.7487481471318764
๐๐ปโ๏ธ ํ์คํ alpha=0 ์ฆ, ๊ธฐ์กด ๋จ์์ ํํ๊ท(SLR) ๋ชจ๋ธ์ผ ๋๋ณด๋ค r2_score๊ฐ์ 0.727์์ 0.729๋ก ์ํญ ์์นํ๊ณ , MSE๊ฐ์ 0.748์์ 0.745๋ก ์ฝ๊ฐ ๊ฐ์ํ์ฌ ์กฐ๊ธ์ด๋ผ๋ ๋ ์ฑ๋ฅ์ด ์ข์ ๋ชจ๋ธ์ ๋ง๋ค์๋ค๊ณ ๊ฒฐ๋ก ์ ๋ด๋ฆด ์ ์์.
์ฆ L2 ๊ท์ ๊ฐ ๋ณธ๋์ ๋ชฉ์ ์ ๋ฌ์ฑํ ๊ฒ์ด๋ค!
(๋ณธ๋ ridge ๊ท์ ๋ ๊ธฐ์กด ๋ชจ๋ธ์ด ๊ณผ์ ํฉ์ ์ผ์ผ์ผ ๋๋ฌด train data์ ๋ง์ถฐ์ ธ ์์ ๋ ์ด๋ฅผ ์ด๋ ์ ๋ ๊ท์ ๋ฅผ ํผํ๊ณ ์ ํ๊ธฐ ์ํด ์ฌ์ฉํ๋ค. ๋ฐ๋ผ์ ๋คํญํ๊ท๋ชจ๋ธ(์ ํ๋ง๊ณ ๋ ๋คํญ๋ชจ๋ธ)๋ก ๊ณผ์ ํฉ์ ์ผ์ผํฌ ๊ฒฝ์ฐ ridge ๊ท์ ๋ฅผ ์ฌ์ฉํ๋ case๊ฐ ๋ง์ ๋ฐ ๊ณผ์ฐ PR ๋ชจ๋ธ์ ๊ท์ ๋ฅผ ๊ฐํ ๊ฒฐ๊ณผ ์ฑ๋ฅ์ด ์ข์์ง๋ ์ง๋ ๋ค์ ํฌ์คํ ์์ ์คํํด๋ณผ ์์ !)
- Ridge ๊ธฐ์ด ๋! -
(์ด์ ๋ค๋ฅธ L1 Regularization์ผ๋ก ๋ง๋ LASSO์ ๋น๊ตํด๋ณด์)
* ์ถ์ฒ) https://www.mygreatlearning.com/blog/what-is-ridge-regression/
'Machine Learning > Models (with codes)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Polynomial Regression Model (0) | 2022.04.24 |
---|---|
Logistic Regression Model (concepts) (0) | 2022.04.24 |
(L2 Regularization) โ Ridge Regression (concepts) (0) | 2022.04.19 |
Multiple Linear Regression Model (concepts+w/code) (0) | 2022.04.17 |
Simple Linear Regression Model (w/scikit-learn) (0) | 2022.04.16 |
๋๊ธ