๐ค ์ ๋ฒ ํฌ์คํ ์์ ์ฐ๋ฆฌ๋ Multiple Regression ๋คํญํ๊ท๋ชจ๋ธ์ ๋ํด ๊ณต๋ถํ๋ค.
๐ค ๊ทธ๋ฆฌ๊ณ Ridge ๋ชจ๋ธ ์๊ฐ ์๊ฐ์ SLR ๋ชจ๋ธ์ Ridge ๊ท์ ๋ฅผ ์ ํด Ridge์ ํจ๊ณผ๋ฅผ ์คํ์ผ๋ก ์ฆ๋ช ํ๊ณ , ํ๋ฐ๋ถ์ MLR ๋ชจ๋ธ์ ๊ดํด์๋ ์ ๊น ์ธ๊ธํ์๋ค
> MLR ๋คํญํ๊ท๋ชจ๋ธ์์๋ ์ฌ๋ฌ ๋ณ์๋ค์ด feature๋ก ์์ฉํ๋ค. ์ด ๋ ridge ๊ท์ ๋ฅผ ํตํด ๋ ์ค์ํ feature์ ์ํฅ๋ ฅ์ ๊ฐ์์ํจ๋ค. SLR ๋ชจ๋ธ๊ณผ ๋ค๋ฅด๊ฒ ridge ํจ๊ณผ๊ฐ ํ์ฐํ ๋๊ปด์ง ์ ๋ฐ์ ์๋ค! (feature๊ฐ ๋ง์ผ๋ฏ๋ก)
> ๊ทธ๋ผ ์ฐ๋ฆฌ๋ ridge ์ ์ฉ ํ โ ๊ฐ feature์ coefficients ๊ณ์์ ๋ณํ๋ฅผ ์ดํด๋ณผ ๊ฒ์ด๋ฉฐ, ๊ทธ ๊ฒฐ๊ณผ โกridge ํ ๋ชจ๋ธ์ ์ฑ๋ฅ์ด ์ด๋ ์ ๋ ๊ฐ์ ๋์๋ ์ง ์์น๋ก ํ์ธํด๋ณด๊ณ ์ ํ๋ค.
์์!
โ ๊ฐ feature์ coefficients ๊ณ์์ ๋ณํ
1> data ์ค๋น / train, test ๋ถ๋ฆฌ / features, target ๋ถ๋ฆฌ
#<MLR ๋ชจ๋ธ - ์ต์ ์ alpha์ฐพ๊ณ ์ด๋ ๊ฐ feature๋ณ coef ๋ณํ ๋ณด๊ธฐ>
path = 'https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DA0101EN-SkillsNetwork/labs/Data%20files/module_5_auto.csv'
df = pd.read_csv(path)
df.to_csv('module_5_auto.csv')
df=df._get_numeric_data()
train = df.sample(frac=0.75,random_state=1)
test = df.drop(train.index)
train.dropna(inplace=True)
test.dropna(inplace=True)
target = 'price'
## 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]
2> MLR ๋ชจ๋ธ
#mlr model
#20 features
from sklearn.linear_model import LinearRegression
model_lr = LinearRegression()
model_lr.fit(X_train, y_train)
3> RidgeCV๋ก ์ต์ ์ ridge ๊ท์ ๋ชจ๋ธ ์ฐพ๊ณ RidgeCV ์ ์ฉ ์ ๊ณผ ํ coefficients ํ์ธํ๊ธฐ
#ridgeCV
alphas = np.arange(1, 200, 1)
ridge_mlr = RidgeCV(alphas=alphas, cv=10)
#fitting
ridge_mlr.fit(X_train, y_train)
print(ridge_mlr.coef_, ridge_mlr.intercept_, ridge_mlr.alpha_)
print(ridge_mlr.best_score_)
#[-7.56491516e+00 -7.56491516e+00 2.78681974e+02 -1.06845870e+01
# 8.61802076e+01 -7.29448106e+01 1.27669085e+02 2.37005747e+02
# 2.26131771e+00 8.68613041e+01 8.27692719e+02 -2.19957856e+03
# 3.19741534e+02 5.01485386e+01 1.73810064e+00 -4.85339063e+01
# 2.47655001e+02 7.95687055e+02 7.05511891e+01 -7.05511891e+01] -49415.03407219711 5
# plot MLR coefficients
coefficients = pd.Series(model_lr.coef_, X_train.columns)
plt.figure(figsize=(10,5))
coefficients.sort_values().plot.barh()
plt.show()
print(model_lr.coef_.mean())
#1251.874366308613
print(model_lr.coef_.var())
#63032739.06265261
# plot Ridge coefficients
coefficients = pd.Series(ridge_mlr.coef_, X_train.columns)
plt.figure(figsize=(10,5))
coefficients.sort_values().plot.barh()
plt.show()
print(ridge_mlr.coef_.mean())
#35.722544617003535
print(ridge_mlr.coef_.var())
#323993.716524918
4> coefficients ๋น๊ต ๋ถ์
- (์) Ridge ์ ์ฉ ์ / (์๋) Ridge ์ ์ฉ ํ -
โ๐ป ridge ์ ์ฉ ์ ๊ณ์ ๊ฐ์ด ์๋์ ์ผ๋ก ์ ์ฉ ํ๋ณด๋ค ํผ์ ํ์ธํ ์ ์๋ค. ์ฆ width์ length์ target ๊ฒฐ์ ๋ ฅ ์ํฅ์ด ํ feature์ ๋นํด ์๋์ ์ผ๋ก ํฐ๋ฐ, ์ด๋ก ์ธํด ์๋์ ์ผ๋ก ๋ค๋ฅธ feature๊ฐ target ๊ฒฐ์ ์ ์ํฅ์ ๊ฑฐ์ ๋ชป ๋ฏธ์น๊ณ ์๋ค. ridge ์ ์ฉ ํ ์๋์ ์ผ๋ก ํฐ ์ํฅ์ ๋ฏธ์น๋ width์ length ๊ณ์๊ฐ ํ ๊ฐ์ํ์ผ๋ฉฐ, stroke ๊ฒฐ์ ๋ ฅ์ด ์๋์ ์ผ๋ก ์ปค์ง๊ฒ ์๋๊ฐ ์๊ฐํ ์ ์์ง๋ง, ๋ชจ๋ ๊ณ์๋ค์ ํ๊ท ์ ๋ฐ์ง๋ฉด 1251์์ 35๋ก ํฐ ๊ฐ์๋ฅผ ๋ณด์์ ์ ์ ์๋ค. ์ฆ l2 ๊ท์ ๋ฅผ ํตํด ๋ชจ๋ feature์ target ๊ฒฐ์ ๋ ฅ์ ์ด๋ ์ ๋ ํจ๊ณผ๋ฅผ ๊ฐ์์ํด์ ์ ์ ์๋ค.
โ๐ป ridge ์ ์ฉ ์ ๋ถ์ฐ ๊ฐ์ 63032739์ด๊ณ , ridge ์ ์ฉ ํ ๋ถ์ฐ ๊ฐ์ 323993์ผ๋ก ์์ฒญ๋ ๊ณ์๊ฐ ๋ถ์ฐ์ ๊ฐ์๋ฅผ ๋ณด์ธ๋ค. ์ด๋ ๊ณ์๊ฐ ๊ฐ์ ํฐ ํธ์ฐจ๋ฅผ ๊ท์ ๋ก ์ธํด ํ ์ค์๋ค๋ ๋ป์ด ๋๋ฉฐ, ๊ณง ์๋์ ์ผ๋ก ๋ ๋ง์ feature๋ค์ด target ๊ฒฐ์ ๋ ฅ์ ์ด๋ ์ ๋ ์ํฅ๋ ฅ์ ๋ณด์ด๊ธฐ ์์ํ์์ ๋ปํ๋ฉฐ, ์ด๋ ๊ณง ์ผ๋ถ feature๋ก๋ง target๊ฐ์ด ๊ฒฐ์ ๋๋ค๊ณ ๋งํ ์ ์๋ ํ์์ ์ด๋ ์ ๋ ๋ง์๋ค๊ณ ๋ถ์ํ ์ ์๊ฒ ๋ค!
๐ค๐ป feature selection์ ํจ๊ณผ - ์ฐ๋ฆฌ๋ ridge๋ฅผ ์ ์ฉํ ๊ฒฐ๊ณผ stroke๋ผ๋ feature๊ฐ ๊ฒฐ์ ์ ์ธ feature์์ ํ์ธํ๊ณ , ์ด๋ ๊ณง ridge model์ ํตํด ์ด๋ feature๊ฐ ๊ฒฐ์ ์ ์ธ ์ง ๋งํด์ฃผ๊ณ ์๋ค. (๋ฌผ๋ก ridge ์ ์ฉ ์ ์๋ ์ด๋ค ์ข ๋ฅ๋ฅผ feature๋ก ์ ํ ์ง ์ ์ ์์ง๋ง ๊ธฐ์กด training set์ ๋ง์ถฐ์ง ๊ณผ์ ํฉ์ ๊ฐ๋ฅ์ฑ์ด ์๋ค. ๋ฐ๋ผ์ ์ ๋ขฐ์ฑ์ด ์๋์ ์ผ๋ก ๋จ์ด์ง๋ค. ์คํ๊ฒฐ๊ณผ๋ฅผ ๋ณด์์ ๋๋ ๋๋๋ฌ์ง feature ์ข ๋ฅ๊ฐ ๋ฌ๋ผ์ก์์ ํ์ธ ๊ฐ๋ฅ!) ๋ํ ์ฐ๋ฆฌ๊ฐ ridge๋ฅผ ์ฌ์ฉํ๋ ์ด์ ๋ ๊ธฐ์กด training set๋ณด๋ค ์์ผ๋ก ์๋กญ๊ฒ ๋ค์ด์ฌ test data์ ๋ํ ์ข์ ์์ธก ์ฑ๋ฅ์ ๋ณด์ด๊ธฐ ์ํจ์ด๊ธฐ ๋๋ฌธ์ด๋ค. ๋ฐ๋ผ์ l2 ๊ท์ ๋ ๊ณ์๊ฐ์ ๊ฐ์์์ผ ๋๋ฌด ๋ณต์กํ ๋ชจ๋ธ์ด ๋์ง ์๊ฒ๋ ํ๊ณ , ์ด๋ ์ ๋ ์์ธก์ ์๋๊ฒ ๋ชจ๋ธ์ ๋ง๋ค์ด์ค๋ค. (l1 ๊ท์ ๋ ์ํฅ๋ ฅ์ด ์๋์ ์ผ๋ก ์ ์ feature ์์ 0์ผ๋ก ๋ง๋ค์ด๋ฒ๋ฆฐ๋ค. ๋ฐ๋ผ์ feature selection์ด ๋ชฉ์ ์ด๋ผ๋ฉด LASSO๊ฐ ๋ ๋ซ๋ค๊ณ ํ๋จ๋๋ค. ํ์ ํฌ์คํ ์์ )
โกridge ํ ๋ชจ๋ธ์ ์ฑ๋ฅ์ด ์ด๋ ์ ๋ ๊ฐ์ ๋์๋ ์ง ์์น๋ก ํ์ธ
5> test data๋ก ๋ชจ๋ธ ์ฑ๋ฅ ์์ธกํ๊ธฐ (MLR & ridge)
#predicting
y_test_pred_MLR = model_lr.predict(X_test)
print(r2_score(y_test, y_test_pred_MLR), mean_squared_error(y_test, y_test_pred_MLR))
#0.8977890607619405 5466979.56131857
#predicting
y_test_pred_mlr = ridge_mlr.predict(X_test)
print(r2_score(y_test, y_test_pred_mlr), mean_squared_error(y_test, y_test_pred_mlr))
#0.8991869412385929 5392210.812963509
๐๐ป alpha ๊ท์ ๊ฐ์ด 5์ผ ๋ ์ต๊ณ ์ ์ฑ๋ฅ์ ๋ณด์ฌ์ฃผ์๊ณ , MLR ๊ธฐ์กด ๋ชจ๋ธ๋ณด๋ค ์ํญ์ด์ง๋ง ๊ฒฐ์ ๊ณ์๊ฐ, MSE ๋ชจ๋ ์ฑ๋ฅ์ด ์ข์์ก์ผ๋ฉฐ, ์ด ๊ฒฐ๊ณผ๋ฅผ ํตํด ์ฐ๋ฆฌ๋ l2 regulariztion์ ํจ๊ณผ๋ฅผ ๋ณด์๋ค๊ณ ๋งํ ์ ์๊ฒ ๋ค!
- ๋ง์ง๋ง์ผ๋ก ridge์ ๋ํด ์ด๋ค user๊ฐ ๋จ๊ธด '๋๋ฌด ์ ๋ฆฌ๊ฐ ์ ๋' ํ ๋ง๋๋ฅผ ๋ณต๋ถํ์ฌ ๊ฐ์ ธ์ด๐๐ป -
'If our underlying data follows a relatively simple model, and the model we use is too complex for the task, what we are essentially doing is we are putting too much weight on any possible change or variance in the data. Our model is overreacting and overcompensating for even the slightest change in our data(๊ณผ์ ํฉ). People in the field of statistics and machine learning call this phenomenon overfitting. When you have features in your dataset that are highly linearly correlated with other features, turns out linear models will be likely to overfit(์ ์์์์ ํนํ width๊ฐ ๋๋ฌด ์๋์ ์ผ์ ๋๋ก overfit์ ๊ฐ๋ฅ์ฑ์ ๋ณด์์ - ํฌ๊ฒ ์ํฅ์ ์ฃผ๋ feature๊ฐ ์๋ค๋ฉด ์ด๋ก ์ธํด ์กฐ๊ธ์ feature ๋ณํ์๋ target์ ๋ฏผ๊ฐํ๊ฒ ๋ฐ์ํจ - ๋ฐ๋ผ์ ์ด๋ ์ ๋ ์ด feature์ ์ํฅ๋ ฅ์ ์ ํ๊ฒ ๋ง๋ค ํ์๊ฐ ์์). Ridge Regression, avoids over fitting by adding a penalty to models that have too large coefficients(penalty ์ค ๊ฒฐ๊ณผ width์ ์ํฅ๋ ฅ ํ์ ํ ๊ฐ์).'
* overfitting/underfitting ๊ด๋ จ ํฌ์คํ ์ ์๋ ์ฐธ์กฐ ↓↓↓↓
* ์ฐธ๊ณ ์ถ์ฒ) https://stats.stackexchange.com/questions/251708/when-to-use-ridge-regression-and-lasso-regression-what-can-be-achieved-while-us
๋๊ธ