머신러닝

원핫인코딩, 특성 선택, 릿지 회귀( Ridge Regression )

ROSEV 2021. 8. 4. 19:09

데이터 유형에 대한 참고 사진

범주형 자료 - 원핫인코딩

  • 원핫인코딩이란 모델링을 할 때 범주형 변수가 있다면 0과 1등으로 표현하는 것을 말합니다.
  • 즉 도시라는 feature에서 '서울', '부산, '대구'가 있다면 이를 0과 1 등으로 표현합니다.

 

명목형 변환

 

  • 범주형 자료에도, 순서가 없는 위의 사례같은 명목형과, 순서가 있는 순서형으로 분류됩니다.
  • 주의 할점은 도시와 같이 대소 관계의 의미가 없는데, 아래와 같이 순서형으로 하면 안된다는 것입니다.

순서형 변환

원한인코딩 주의점

  • 카테고리가 너무 많으면(high cardinality) 너무 많은 컬럼이 생성되므로, 적합하지 않을 수 있습니다.

 

Python의 Pandas를 이용한 원핫인코딩

## get_dummies를 이용한 원핫인코딩

import pandas as pd

df_dummy = pd.get_dummies(df, prefix=['원핫인코딩할 속성']

 

여기서 가변수(dummy)란 위에서 말한 독립변수(ex.성별)를 0과 1로 표현하는 것을 의미합니다. 

 

Python의 category_encoders를 이용한 원핫인코딩

## category_encoders 이용한 원핫인코딩

from category_encoders import OneHotEncoder

## 원핫 인코딩
encoder = OneHotEncoder(cols=[...]) # 원핫인코딩 할 속성 선택 

df = encoder.fit_transform(df) # 원핫인코딩 적용

 

공식문서 : https://contrib.scikit-learn.org/category_encoders/

 

Category Encoders — Category Encoders 2.2.2 documentation

Category Encoders A set of scikit-learn-style transformers for encoding categorical variables into numeric with different techniques. While ordinal, one-hot, and hashing encoders have similar equivalents in the existing scikit-learn version, the transforme

contrib.scikit-learn.org


특성 선택(Feature selection)

  • 특성 선택 방법은 3가지로 나뉩니다.  
  1. 단변량 분석 
  2. 모델 기반 특성 선택
  3. 반복적 특성 선택

단변량분석

  • 단변량 분석이란 각각의 특성과 종속변수(Target)와의 관계를 계산하여, 관련성이 높다면 특성을 선택하는 것을 의미합니다.
  • 여기서 관련성이 높다는 의미는 분산분석을 하여, F통계량을 통해 p-value 값을 확인하여 낮게 나타난 변인을 선택한다는 것입니다.

 

Python의 scikit-learn에서 단변량 분석

# target와 가장 correlated 된 features 를 k개 고르는 것이 목표입니다.

## f_regresison, SelectKBest
from sklearn.feature_selection import f_regression, SelectKBest, SelectPercentile

## selctor 정의합니다. 
### 파라미터는 회귀분석 시 f_regression 분류 시 f_classif(기본값)을 사용

selector = SelectKBest(score_func=f_regression, k=10)

## 학습데이터에 fit_transform 
X_train_selected = selector.fit_transform(X_train, y_train)
x_test_selected = select.transform(x_test)

 

# SelectPercentile을 통해 특성을 지정된 비율만큼 선택

select = SelectPercentile(percentile=50).fit(x_train, y_train) # percentile: 50%만 선택

x_train_select = select.transform(x_train) # 적용

x_test_selected = select.transform(x_test)

모델 기반 특성 선택(feature selection based model)

  • 모델기반특성선택이란 지도학습 머신러닝 모델을 이용하여 중요도를 평가하고, 그 중 높은 속성을 선택하는 것을 말합니다.
  • 머신러닝 모델 중 속성의 중요도를 측정할 수 있는 것은 decision tree와 Lasso입니다.
  • 기존 단변량 분석과 다르게 속성들끼리의 관계도 반영할 수 있습니다.

 

Python의 scikit-learn에서 모델기반특성선택

from sklearn.feature_selection import SelectFromModel
from sklearn.ensemble import RandomForestClassifier

# 중요도가 지정한 임계치(threshold)보다 큰 모든 특성 선택하는 모델 구성

# 절반 가량의 특성이 선택될수 있도록 중간값을 임계치로 사용
RF = RandomForestClassifier(n_estimators=100, random_state=42)
select1 = SelectFromModel(RF, threshold='median')

# 특성 선택
select1.fit(X_train, y_train)
X_train_selected1 = select1.transform(X_train)

반복적 특성 선택

  • 반복적 특성선택이란 특성의 수가 각각 다른 모델을 만들어, 성능이 높은 것을 택하는 방법이다.
  • 특성을 선택하는 접근 방법은 두가지가 있다.
  • 첫번째 특성을 하나도 선택하지 않은채로 시작하여 하나씩 추가 하여 만드는 방법
  • 두번째 특성 모두 넣고, 하나씩 제거해 나가는 방법 

 

Python의 scikit-learn에서 모델기반특성선택

# 재귀적 특성 방법이며, 이는 2번째 방법인 모든 특성을 넣고 중요도가 낮은 특성을 제거해 나가는 방식

from sklearn.feature_selection import RFE


RF = RandomForestClassifier(n_estimators=100, random_state=42)

# 특성 선택
select = RFE(RF, n_features_to_select=40)

select.fit(X_train, y_train)
X_train_selected = select.transform(X_train)

 

# https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=ssdyka&logNo=221292406449
# 윗분 블로그에 좋은 설명과 코드 예시가 있어서, 참고해보시길 바랍니다.

# coding: utf-8

import numpy as np

import matplotlib.pyplot as plt

from sklearn.datasets import load_breast_cancer

from sklearn.preprocessing import StandardScaler

from sklearn.model_selection import train_test_split

from sklearn.ensemble.forest import RandomForestClassifier

from sklearn.feature_selection import RFE



cancer = load_breast_cancer()



# 고정된 난수 발생

rng = np.random.RandomState(42)

noise = rng.normal(size=(len(cancer.data), 50))



# 데이터에 노이즈 특성 추가

# 처음 30 개는 원본이고 다음 50개는 노이즈로 구성

X_w_noise = np.hstack([cancer.data, noise])

X_train, X_test, y_train, y_test = train_test_split(X_w_noise, cancer.target, random_state=0, test_size=.5)



# StandardScaler를 사용해 각 틍성의 분산이 1이 되도록 스케일 조정

standard_scaler = StandardScaler()

standard_scaler.fit(cancer.data)



X_scaled = standard_scaler.transform(cancer.data)



select = RFE(RandomForestClassifier(n_estimators=100, random_state=42), n_features_to_select=40)

select.fit(X_train, y_train)



mask = select.get_support()

print(mask)



plt.matshow(mask.reshape(1, -1), cmap='gray_r')

plt.xlabel("특성 번호")

plt.show()

 

 


능선회귀 ( Rideg Regression )

  • 릿지회귀란, 선형회귀모델을 사용할 때 과적합이 생길때가 있는데, 이때 과적합 문제를 줄이기 위헤 사용하는 것이 릿지 회귀입니다.
  • 즉 편향을 조금 더헤주고, 분산을 줄이는 것을 목표로 맨 오른쪽의 페널티항을 통해 계수를 규제하는 방법입니다.
  • 페널티항이 높을 수록, 계수가 0에 가깝게 형성됩니다.
기하학적으로 설명하자면 모든 특성의 기울기를 0에 가깝게 만들어(Regulation), 과적합을 피하면서 Variance가 작은 것을 추구

 

alpha값에 따른 회귀선의 기울기

 

 

  • 위 사진을 통해 페널티(alpha값)이 높을 수록 선이 평평해지는 모습을 볼 수 있습니다.
  • 페널티값(alpha) 을 얼마나 주어야 하는가에 대한 것은 직접 조절해보는 방법 밖에 없습니다.
  • 참고 동영상 : https://youtu.be/pJCcGK5omhE

 

Python의 scikit-learn에서 Rideg Regression

from mglearn.datasets import load_extended_boston
from sklearn.linear_model import Ridge, LinearRegression
from sklearn.model_selection import train_test_split
import pandas as pd

df = pd.read_csv('파일경로/데이터.csv')

x_train, x_test, y_train, y_test = train_test_split(df, target, random_state=42, test_size=0.25)

ridge = Ridge()

ridge.fit(x_train, y_train)

# 분산분석을 하는 이유는 ? 왜 분산과 평균이 달라야 p-value가 낮아지는가 ?

# 릿지회귀에서 페널티항이 어떻게 계수에 영향을 끼치는가 계산적인 부분이 이해가 가지않음

# 양적변수, 질적변수, 이산형변수, 연속형 변수에 대해

# 교통사고의 횟수를 모델링 단에서 어떤 변수로 쓰이는지, 연속적이지 않은 데이터