๋ชฉํ
- ๋ฒ์ฃผํ(Categorical) ์๋ฃ๋ฅผ ๋ค๋ฃจ๊ธฐ ์ํ One-hot encoding ๊ธฐ๋ฒ ์ดํด
- Ridge ํ๊ท๋ฅผ ํตํ ํน์ฑ ์ ํ(Feature selection)๊ณผ์ ์ดํด('์ผ๋ฐํ'์ ๊ด์ ์์)
- ์ ๊ทํ(Regularization)์ ์ํ Ridge ํ๊ท ๋ชจ๋ธ์ ์ดํด
One-hot encoding
์ด์ฒ๋ผ ๋ฐ์ดํฐ์ ๋ฌธ์์ด(string)์ด ์กด์ฌํ ๋, ์ฌ๊ธฐ์ City ์ปฌ๋ผ์ ๋์ ์ง์ญ์ ๋ํ๋ด๋ ๋ณ์๋ ๋ฒ์ฃผํ ๋ณ์
๋ฒ์ฃผํ ์๋ฃ๋ ๋ช
๋ชฉํ(nominal; ์์๊ฐ ์์)๊ณผ ์์ํ(ordinal; ์์๊ฐ ์์)๋ก ๋๋์ด์ง
์์ ์์์์ ๋์๋ ๋๊ณ ๋ฎ์์ด ์๊ณ ์ฐ์ ์์์ด ๋จ์ง ๊ตฌ๋ถ๋ง ๋๊ธฐ ๋๋ฌธ์ ๋ช
๋ชฉํ
one-hot encoding ๋ชจ์๋
pandas์ get_dummies ์ด์ฉ
df_oh = pd.get_dummies(df, prefix=['City'])
df_oh
์ฌ๊ธฐ์ ๋์๊ฐ 3๊ฐ์ง๋ก ๋๋๋๋ฐ ๋ถํ์ํ ์์๋ฅผ ์์ค ๋ฐฉ๋ฒ์ ์ด์ฉ(์์ ๋์ ๊ฐ๋ : ์ธ์ฒ๊ณผ ์์ธ๋ง ์ ํด์ ธ๋ ๋ ๊ฐ์ด 0์ธ ๊ฒฝ์ฐ๊ฐ ๋ถ์ฐ์ด ๋ ํ ๋)
df_dum = pd.get_dummies(df, prefix=['City'], drop_first=True)
df_dum
์์ ๊ฐ์ด ์ปฌ๋ผ์ 2๊ฐ๋ง ๋ง๋ค์ด๋ ๋ถ์ฐ ๋ํ ํํํ ์ ์์
Category_encoders ์ด์ฉ
๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ฉด ์์์ ๋ฒ์ฃผํ ๋ฐ์ดํฐ์๋ง one-hot encoding์ ์ ์ฉ
!pip install category_encoders
## import OneHotEncoder
from category_encoders import OneHotEncoder
## ์ํซ ์ธ์ฝ๋ฉ
encoder = OneHotEncoder(use_cat_names = True)
X_train = encoder.fit_transform(X_train)
X_test = encoder.transform(X_test) # target์ธ y์ ๋ํด์๋ ํ ํ์๊ฐ ์๋ค!(๋น์ฐํ๊ฑฐ)
ํน์ฑ ์ ํ(Feature selection)
ํน์นด์ดํฐ ์ฃผํ ๊ฐ๊ฒฉ ๋ฐ์ดํฐ๋ฅผ ์์๋ก ์ค๋ช
# import house data
df = pd.read_csv('https://ds-lecture-data.s3.ap-northeast-2.amazonaws.com/kc_house_data/kc_house_data.csv')
# np.percentile ์ฌ์ฉํด ์ด์์น ์ ๊ฑฐ
df = df[(df['price'] >= np.percentile(df['price'], 5)) & (df['price'] <= np.percentile(df['price'], 95))]
# ์ด๋ ๊ฒ ์ด์์น ์ ๊ฑฐํ๋ ์ ์ฒ๋ฆฌ ๊ธฐ์ตํด๋๊ธฐ
- ํน์ฑ๊ณตํ(Feature Engineering)์ด๋ task์ ์ ํฉํ ํน์ฑ์ ๋ง๋ค์ด๋ด๋ ๊ณผ์
- ์ค๋ฌด์์ ๊ฐ์ฅ ๋ง์ ์๊ฐ์ด ์์๋๋ ์์ ์ค ํ๋
- sklearn์์๋ ๋ชจ๋ธ์ ์ ํฉํ ํน์ฑ์ ๊ณจ๋ผ์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ ๊ณต(SelectKBest)
์์ ์์ ๋ฐ์ดํฐ์ feature ์ค๋ช ์์ฝ
Feature description
- id - Unique ID for each home sold
- date - Date of the home sale
- price - Price of each home sold
- bedrooms - Number of bedrooms
- bathrooms - Number of bathrooms, where .5 accounts for a room with a toilet but no shower
- sqft_living - Square footage of the apartments interior living space
- sqft_lot - Square footage of the land space
- floors - Number of floors
- waterfront - A dummy variable for whether the apartment was overlooking the waterfront or not
- view - An index from 0 to 4 of how good the view of the property was
- condition - An index from 1 to 5 on the condition of the apartment,
- grade - An index from 1 to 13, where 1-3 falls short of building construction and design, 7 has an average level of construction and design, and 11-13 have a high quality level of construction and design.
- sqft_above - The square footage of the interior housing space that is above ground level
- sqft_basement - The square footage of the interior housing space that is below ground level
- yr_built - The year the house was initially built
- yr_renovated - The year of the house’s last renovation
- zipcode - What zipcode area the house is in
- lat - Lattitude
- long - Longitude
- sqft_living15 - The square footage of interior housing living space for the nearest 15 neighbors
- sqft_lot15 - The square footage of the land lots of the nearest 15 neighbors
ํน์ฑ(์ปฌ๋ผ)์ ๊ณ ๋ฅผ ์ ์๋ ๊ฐ์ง์๋ nCk์ sum
์ฆ, ํน์ฑ์ ๊ณ ๋ฅด๋ ๋ฐฉ๋ฒ์ ๋๋ฌด ๋ง์
ํด์ ์ฌ์ฉํ ์ ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ SelectKBest
# target(Price)์ ๊ฐ์ฅ correlated ๋ features ๋ฅผ k๊ฐ ๊ณ ๋ฅด๋ ๊ฒ์ด ๋ชฉํ์
๋๋ค.
## f_regresison, SelectKBest
from sklearn.feature_selection import f_regression, SelectKBest
## selctor ์ ์ํฉ๋๋ค.
selector = SelectKBest(score_func=f_regression, k=10) # ํน์ฑ๋ค์ ์ ์๋ f_regression์ ํตํด ๋์ค๊ฒ, k=10์ผ๋ก 10๊ฐ๋ฅผ ๊ณจ๋ผ๋ฌ๋ผ๋ ์๋ฏธ)
## ํ์ต๋ฐ์ดํฐ์ fit_transform
X_train_selected = selector.fit_transform(X_train, y_train) # train set์ fitํ๊ณ
## ํ
์คํธ ๋ฐ์ดํฐ๋ transform
X_test_selected = selector.transform(X_test) # test data์ ๋ํด์๋ transform๋ง ํ๋ฉด ๋๋ค. ์ด๋ฐ ๊ณผ์ ์ ๋์ ์ตํ๋์.
์ ํํ ํน์ฑ ํ์ธ
all_names = X_train.columns
## selector.get_support()
selected_mask = selector.get_support()
## ์ ํ๋ ํน์ฑ๋ค
selected_names = all_names[selected_mask]
## ์ ํ๋์ง ์์ ํน์ฑ๋ค
unselected_names = all_names[~selected_mask]
print('Selected names: ', selected_names)
print('Unselected names: ', unselected_names)
Selected names: Index(['bedrooms', 'bathrooms', 'sqft_living', 'floors', 'view', 'grade', 'sqft_above', 'lat', 'sqft_living15', 'rooms'], dtype='object')
Unselected names: Index(['sqft_lot', 'condition', 'sqft_basement', 'yr_built', 'yr_renovated', 'zipcode', 'long', 'sqft_lot15'], dtype='object')
๊ทธ๋ ๋ค๋ฉด ์๋ฌธ! k๋ฅผ ๋ช๊ฐ๋ก ์ค์ ํ๋ ๊ฒ ์ข์๊น??? (์ด ๋ถ๋ถ์ ์ฐธ๊ณ ๋ง ํ ๊ฒ)
# features๋ฅผ ๋ช ๊ฐ ์ ์ฑ
ํ๋ ๊ฒ์ด ์ข์์ง ์์ ๋ด
์๋ค.
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, r2_score
training = []
testing = []
ks = range(1, len(X_train.columns)+1)
# 1 ๋ถํฐ ํน์ฑ ์ ๋งํผ ์ฌ์ฉํ ๋ชจ๋ธ์ ๋ง๋ค์ด์ MAE ๊ฐ์ ๋น๊ต ํฉ๋๋ค.
for k in range(1, len(X_train.columns)+ 1):
print(f'{k} features')
selector = SelectKBest(score_func=f_regression, k=k)
X_train_selected = selector.fit_transform(X_train, y_train)
X_test_selected = selector.transform(X_test)
all_names = X_train.columns
selected_mask = selector.get_support()
selected_names = all_names[selected_mask]
print('Selected names: ', selected_names)
model = LinearRegression()
model.fit(X_train_selected, y_train)
y_pred = model.predict(X_train_selected)
mae = mean_absolute_error(y_train, y_pred)
training.append(mae)
y_pred = model.predict(X_test_selected)
mae = mean_absolute_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
testing.append(mae)
print(f'Test MAE: ${mae:,.0f}')
print(f'Test R2: {r2} \n')
plt.plot(ks, training, label='Training Score', color='b')
plt.plot(ks, testing, label='Testing Score', color='g')
plt.ylabel("MAE ($)")
plt.xlabel("Number of Features")
plt.title('Validation Curve')
plt.legend()
plt.show()
์์ ๊ฐ์ด for๋ฌธ์ ์ด์ฉํด์ MAE๊ฐ์ ๋ฎ์ถ๊ณ score๊ฐ ๋์์ง๋ ๋๊น์ง๋ก ๋ฐ๋ณตํด๋ณผ ์ ์๋ค.(์ด์ฐจํผ k๋ฅผ ์ค์ ํ๋ฉด ์์์ ์ ์ ๋์ ๋ณ์๋ถํฐ ๊ณจ๋ผ์ฃผ๋๊น)
Ridge Regression(๋ฅํ ํ๊ท) ๊ฐ๋ ์ค์
- Ridge ํ๊ท์ ๊ธฐ๋ณธ์ ๊ธฐ์กด ๋ค์คํ๊ท์ ์ train data์ ๋ ์ ํฉ์ด ๋๋๋ก ๋ง๋๋ ๊ฒ.
$$\beta_{ridge} : argmin[\sum_{i=1}^n(y_i - \beta_0 - \beta_1x_{i1}-\dotsc-\beta_px_{ip})^2 + \lambda\sum_{j=1}^p\beta_j^2]$$
n: ์ํ์, p: ํน์ฑ์, $$\lambda$$: ํ๋ ํ๋ผ๋ฏธํฐ(ํจ๋ํฐ)
์ฐธ๊ณ : alpha, lambda, regularization parameter, penalty term ๋ชจ๋ ๊ฐ์ ์๋ฏธ๋ก ์ดํด
- Ridge ํ๊ท๋ฅผ ์ฌ์ฉํ๋ ์ด์ : ๊ณผ์ ํฉ์ ์ค์ด๊ธฐ ์ํด์(๋ชจ๋ธ์ ๋ณต์ก๋๋ฅผ ์ค์ด๊ธฐ ์ํด์; ํน์ฑ์ ๊ฐฏ์๋ฅผ ์ค์ด๊ฑฐ๋ ๋ชจ๋ธ์ ๋จ์ํ ๋ชจ์์ผ๋ก ์ ํฉํ๋ ๊ฒ)
- ๋ชจ๋ธํ์ต์ ์์ด ํธํฅ(bias)์ ๋ถ์ฐ(variance)์ ๊ด์ ์์ ๋ณธ๋ค๋ฉด Ridge ํ๊ท๋ ํธํฅ(bias)์ ์กฐ๊ธ ๋์ด๋ ๋์ , ๋ถ์ฐ(variance)์ ์ค์ด๋ ๋ฐฉ๋ฒ์ผ๋ก ์ ๊ทํ(Regularization)์ ์ํํ๊ฒ ๋ฉ๋๋ค. ์ฌ๊ธฐ์ ์ ๊ทํ๋, ๋ชจ๋ธ์ ๋ณํํ์ฌ ๊ณผ์ ํฉ์ ์ํํด์ ์ผ๋ฐํ ์ฑ๋ฅ์ ๋์ฌ์ฃผ๋ ๊ฒ์ ๋งํฉ๋๋ค.
$$\lambda → 0์ ํ๊ฒ ๋๋ฉด, \beta_{ridge} → \beta_{OLS} (์ฆ, ๊ธฐ์กด ๋ค์ค์ ํํ๊ท๋ ๊ฐ์)$$
$$\lambda → ∞์ ํ๊ฒ ๋๋ฉด, \beta_{ridge} → 0(ํ์ต ์์ฒด๊ฐ ์๋๋๊น ํ๊ท๊ณ์๊ฐ 0์ ๊ฐ๊ฒ ๋จ)$$
์๋์ ๊ฐ์ ๋ฐ์ดํฐ๊ฐ ์๋ค.
์ ํํ๊ท๋ฅผ ์งํ ํ์ ๋
์ ์ ๋๋ค๊ฐ(์ํ๊ฐ)์ ์ฆ๊ฐ์ํค๋ฉด์ ์ ์ฉ
์์ ๊ทธ๋ํ๋ฅผ ํตํด alpha=0์ผ ๋ OLS(๊ธฐ์กด ์ ํํ๊ท)์ ๊ฐ์ ๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๊ณ , alpha๊ฐ ์ปค์ง ์๋ก ์ง์ ์ ๊ธฐ์ธ๊ธฐ๊ฐ 0์ ์๋ ดํ๋ฉด์ ํ๊ท ๊ธฐ์ค๋ชจ๋ธ(baseline)๊ณผ ๊ฐ์์ง๋ ๊ฒ์ ํ์ธ
๊ทธ๋ ๋ค๋ฉด ์ด alpha๋ฅผ ์ต์ ์ผ๋ก ์ค ์ ์๋ ๋ฐฉ๋ฒ์ ์์๊น?
RidgeCV๋ฅผ ํตํ ์ต์ ์ ํจ๋ํฐ(alpha, lambda) ๊ฒ์ฆ
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, normalize=True, cv=3)
ridge.fit(ans[['x']], ans['y'])
print("alpha: ", ridge.alpha_)
print("best score: ", ridge.best_score_)
alpha: 0.2
best score: 0.4389766255562206
์ฆ, alpha๊ฐ 0.2์ผ ๋๊ฐ ์ ์ผ score๊ฐ ๋๊ฒ๋ ํด์ฃผ๋ ํจ๋ํฐ(Ridge regression์ ์ด์์น์ ๊ทธ๋๋ง ๋๊ฐํ๊ฒ ํด์ค๋ค.)
์ข๋ ๋ณด๋์ค
RidgeCV์์ CV๋ Cross Validation์ผ๋ก '๊ต์ฐจ ๊ฒ์ฆ'์ ์๋ฏธํ๋ค.
from sklearn.preprocessing import PolynomialFeatures
๋ฐ์ดํฐ์ ๊ฐ ์นผ๋ผ์ ๋ํด์ ๋คํญ์ํํ์ ์นผ๋ผ์ ๋ํด์ค๋ค. ์ฆ, ๋ณ์์ ๋ํ ๋คํญ์์ผ๋ก ๋ชจ๋ธ์ ์ข๋ ๋ณต์กํ๊ฒ ํผํ ํ๊ณ ์ถ์ ๋ ์ฌ์ฉ
from sklearn.pipeline import make_pipeline
๋ชจ๋ธ์ fit, transformํ๋ ์ฌ์ดํท๋ฐ์ ๋ณํ๊ธฐ๋ค์ ์ด์ด์ ํ๋ฒ์ ์คํํ ์ ์๊ฒ ํด์ฃผ๋ ํจ์
'๐ฟ Data > ๋ถํธ์บ ํ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[TIL]27.Section2_sprint1 challenge (0) | 2021.12.24 |
---|---|
[TIL]26.Logistic Regression(๋ก์ง์คํฑ ํ๊ท; ๋ถ๋ฅ) (0) | 2021.12.22 |
[TIL]24.๋ค์ค์ ํํ๊ท(Multiple Linear Regression) (0) | 2021.12.21 |
[TIL]23.Simple Regression(๋จ์ํ๊ท) (0) | 2021.12.18 |
[TIL]22.Section Challenge ๋ฐ ๋ณต์ต (0) | 2021.12.17 |