데이터 & AI/코칭스터디10기<Beyond AI BASIC 2023>

4.피처엔지니어링 - 피처 중요도와 피처선택

뭉실뭉실뜬구름 2023. 5. 12. 13:44
728x90

SUMMARY:

1. 피처 중요도란?

● Model-specific vs Model-agnostic

 

2. Boosting Tree 피처 중요도

 

 

3. Permutation 피처 중요도

 

 

4. 피처 선택이란?

■ Filter Method ■ Wrapper Method ■ Embedded Method

 

 

 

 

 

 

 

1. 피처 중요도란?

  • 피처 중요도 : 타겟 변수를 예측하는 데 얼마나 유용한 지에 따라 피처에 점수를 할당해서 중요도를 측정하는 방법.
  • 피처 중요도는 크게 두 가지 방법으로 나눠서 생각할 수 있다
  •  
                                  Model-specific

                                         Model-agnostic
※ 머신러닝 모델 자체에서 피처 중요도 계산이 가능하다면 쓰는 방법 ex) LightGBM 모델의 경우에는 피처가 트리를 만드는 데 몇 번이나 사용됐는지를 계산함으로써 피처의 중요도를 측정할 수 있습니다. ※모델에서 제공하는 기능에 의존하지 않고 모델을 학습한 후에 적용되는 피처 계산 방법

 

 

2. Boosting Tree 피처 중요도(Model-Specific Method)

 

2.1 LightGBM 피처 중요도

  • LightGBM 피처 중요도 함수 :
  • Training된 LightGBM 모델 클래스에 feature_importance(importance_type) 함수로 피처 중요도 계산기능 제공
  •  인자의 importance_type 값에 'split' 또는 'gain' 사용 가능, 디폴트는 'split'

               ■ split : numbers of times the feature is used in a model

               ■ gain : total gains of splits which use the feature

 

         

2.2 XGBoost 피처 중요도

  • Training된 XGBoost 모델 클래스에 get_score(importance_type) 함수로 피처 중요도 계산 기능 제공
  • 인자의 importance_type, 디폴트는 weight

■ ‘weight’: the number of times a feature is used to split the data across all trees.

■ ‘gain’: the average gain across all splits the feature is used in.

■ ‘cover’: the average coverage across all splits the feature is used in.

■ ‘total_gain’: the total gain across all splits the feature is used in.

■ ‘total_cover’: the total coverage across all splits the feature is used in.

 

2.2 CatBoost 피처 중요도

  • Training된 CatBoost 모델 클래스에 get_feature_importance(type) 함수로 피처 중요도 계산 기능 제공
  • 인자의 type, 디폴트는 Featureimportance

■ FeatureImportance: Equal to PredictionValuesChange for non-ranking metrics and LossFunctionChange for ranking metrics

■ ShapValues: A vector with contributions of each feature to the prediction for every input object and the expected value of the model prediction for the object

■ Interaction: The value of the feature interaction strength for each pair of features.

■ PredictionDiff: A vector with contributions of each feature to the RawFormulaVal difference for each pair of objects.

 

3. Permutation 피처 중요도(Model-Agnostic Method)

  • Measure the importance of a feature by calculating the increase in the model's prediction error after permuting the feature.
  • 위에 문장을 하나하나 분석해보면
  • Feature importance is a measure of how important a feature is to a machine learning model. It can be calculated by measuring the increase in the model's prediction error after permuting the feature.
  • Permuting a feature means randomly shuffling the values of the feature. This can be done to see how much the model's predictions are affected by the feature.
  • If permuting a feature causes a large increase in the model's prediction error, then the feature is important to the model. This is because the model is relying on the feature to make its predictions.

중요도를 하는 방법은 이러하다

출처 : https://christophm.github.io/interpretable-ml-book/feature-importance.html

해석해보자

  1. 모델을 훈련합니다.
  2. 각 특성에 대해 특성 값을 무작위로 섞습니다.
  3. 모델을 사용하여 섞인 데이터에 대한 예측을 합니다.
  4. 모델의 순수 오류와 섞인 데이터에 대한 예측의 오류를 비교합니다.
  5. 각 특성에 대해 오류의 차이를 계산합니다.
  6. 오류의 차이가 큰 특성이 모델에 중요합니다.

적용해보자(출처 -https://scikit-learn.org/stable/modules/generated/sklearn.inspection.permutation_importance.html)

>>> from sklearn.linear_model import LogisticRegression
>>> from sklearn.inspection import permutation_importance
>>> X = [[1, 9, 9],[1, 9, 9],[1, 9, 9],
...      [0, 9, 9],[0, 9, 9],[0, 9, 9]]
>>> y = [1, 1, 1, 0, 0, 0]
>>> clf = LogisticRegression().fit(X, y)
>>> result = permutation_importance(clf, X, y, n_repeats=10,
...                                 random_state=0)
>>> result.importances_mean
array([0.4666..., 0.       , 0.       ])
>>> result.importances_std
array([0.2211..., 0.       , 0.       ])

 

4. 피처 선택(Feature Selection)이란 ?

  • 머신러닝 모델에서 사용할 피처를 선택하는 과정
  • 머신러닝 모델이 타겟 변수를 예측하는데 유용한 피처와 유용하지 않은 피처를 구분해서 유용한 피처를 선택하는 과정
  • 피처 선택을 통해 모델의 복잡도를 낮춤으로써 오버피팅 방지 및 모델의 속도 향상 가능

 

Filter method

○ Filter type methods select variables regardless of the model. They are based only on general features like the correlation with the variable to predict. Filter methods suppress the least interesting variables. The other variables will be part of a classification or a regression model used to classify or to predict data. These methods are particularly effective in computation time and robust to overfitting

  • 필터링 방법은 모델 성능에 기여하지 않는 특성을 제거하여 특성을 선택하는 방법 중 하나입니다. 특성의 분산, 상관 관계, 중요도와 같은 특성의 통계적 특성에 따라 특성을 선택합니다.

Wrapper method

○ Wrapper methods evaluate subsets of variables which allows, unlike filter approaches, to detect the possible interactions amongst variables.

  • 래퍼 메서드는 특성 간의 상관 관계를 계산한 다음 상관 관계가 높은 특성을 선택합니다. 상관 관계가 높은 특성은 서로 관련이 있기 때문에 모델에 중요하지 않을 수 있습니다.

Embedded method

○ Embedded methods have been recently proposed that try to combine the advantages of both previous methods. A learning algorithm takes advantage of its own variable selection process and performs feature selection and classification simultaneously

  • 특징 선택의 래퍼 방법은 특징 엔지니어링 및 모델 훈련을 모두 포함하는 특징 선택 방법입니다. 이러한 방법은 특징 엔지니어링 및 모델 훈련의 이점을 결합하여 특징 선택의 성능을 향상시키기 위해 고안되었습니다.
728x90