Linear Models(3) - Ridge Regression

2021. 3. 7. 15:39[AI]/Machine Learning

Learned Stuff

Key Points

  • One-hot Encoding

  • Feature Selection

    • Kbest

 

  • Ridge Regression
    • Ridge
    • RidgeCV

New Stuff

[One-Hot Encoding]

  • categorical data를 0 또는 1 의 값으로 바꿔준다

 

code

  • Colab에서 돌리고 싶다면 pip install을 해야한다
pip install category_encoders

 

from category_encoders import OneHotEncoder

# X_train / X_test 라는 dataframe 이 있다고 가정

# use_cat_name = True 를 설정하면 encoded 된 column의 이름 앞에 categorical data의 이름이 prefix로 붙는다
encoder = OneHotEncoder(use_cat_names = True)
X_train = encoder.fit_transform(X_train)
X_test = encoder.transform(X_test)

 

[Feature Selection]

  • regression model에 중요한 feature들을 찾는 method

 

  • SelectKBest method
    • target 과 가장 correlated 된 feature들을 찾아준다

 

code

from sklearn.feature_selection import SelectKBest

# X_train / X_test 라는 dataframe 이 있다고 가정

selector = SelectKBest(score_func=, k=)
# score_func : feature & target 간의 관계를 알아보는 함수 
# k : 원하는 feature 의 갯수


X_train_selected = selector.fit_transform(X_train, y_train)
X_test_selected = selector.transform(X_test)
  • score_func types (필요한 function은 import를 따로 해야함)

    • f_classif : ANOVA F-value 를 가지고 관계를 알아본다

    • chi2 : $\chi^2$ value 를 가지고 관계를 알아본다

    • f_regression : F-value를 가지고 관계를 알아본다

 

[Ridge Regression]

  • 기존의 Multiple Regression 의 Cost Function 에서 $\lambda$ (0 ~ $\infty$) $\times$ $\beta_j^2$ 를 추가 한 것

 

  • training data를 덜 적합(fit) 하게 만들어 좀 더 일반화된 model을 만들 수 있다

 

  • Cost Function
    • $Cost_{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]$

 

  • $\lambda$ (또는 alpha)
    • $\lambda$ → 0, $Cost_{ridge}$ = $Cost_{OLS}$
    • $\lambda$ → $\infty$, $Cost_{ridge}$ → 0

 

Ridge

 

code

from sklearn.linear_model import Ridge

# X_train / y_train / X_test / y_test 라는 dataframe 이 있다고 가정

ridge = Ridge(alpha=, normalize=)
# alpha : lambda 값을 의미 (0 ~ 무한대에 사이에서 설정)
# normalize : ridge regression model을 만들기 전 data를 normalize 할 건지 설정

ridge.fit(X_train,y_train)

ridge.coef_ # coefficients (feature 별 기울기) 반환
ridge.intercept_ # model 의 y-intercept (y 절편) 반환
ridge.predict(X_test) # test data에 대한 예측값 반환

 

RidgeCV

 

code

  • Colab에서 RidgeCV.best_score_ 을 확인할려면 pip install 을 해야한다
pip install -U scikit-learn
from sklearn.linear_model import RidgeCV

# X_train / y_train / X_test / y_test 라는 dataframe 이 있다고 가정

alpha_list = np.arange(0,1,0.01)

ridgeCV = RidgeCV(alphas=alpha_list, normalize=, cv=)
# alpha : lambda 값을 의미 (0 ~ 무한대에 사이에서 설정)
# normalize : ridge regression model을 만들기 전 data를 normalize 할 건지 설정
# cv : 몇개의 fold 로 cross validation을 진행할 건지 설정

ridgeCV.fit(X_train,y_train)

ridgeCV.coef_ # coefficients (feature 별 기울기) 반환
ridgeCV.intercept_ # model 의 y-intercept (y 절편) 반환
ridgeCV.alpha_ # Cost Function을 가장 낮게 만드는 lambda 값 반환 (alpha_list 중에 선택)
ridgeCV.best_score_ # best lambda 값으로 만들어진 train data 의 score 반환
ridgeCV.predict(X_test) # test data에 대한 예측값 반환
  • 만약 normalize(정규화) 가 아닌 standardize(표준화)를 수행하고 싶다면 표준화 한뒤 (normalize = False) 를 넣어주면 된다
728x90