본문 바로가기
Data Science/Machine Learning

[Scikit-learn] sklearn.feature_selection.SelectFrom Model

by Queen2 2022. 9. 23.
728x90
반응형

√ Class구성요소

이 기능의 목적은 여러 피처들을 다 고려하기에는 시간이 많이 들고 비효율적이기 때문에, 모델에 적합한 피처들을 골라줘! 라는 모델인데요. 주요한 구성요소를 살펴보겠습니다

 

class sklearn.feature_selection.SelectFromModel(estimator, *, threshold=None, prefit=False, norm_order=1,
	max_features=None, importance_getter='auto')

estimator : 어떤 모델을 사용하냐 (Logistic Regression 등등)

threshold: median, mean 등 기타 임계값이 되는 수치

 

 

 

(source: scikit-learn.org)
>>> from sklearn.feature_selection import SelectFromModel
>>> from sklearn.linear_model import LogisticRegression
>>> X = [[ 0.87, -1.34,  0.31 ],
...      [-2.79, -0.02, -0.85 ],
...      [-1.34, -0.48, -2.55 ],
...      [ 1.92,  1.48,  0.65 ]]
>>> y = [0, 1, 0, 1]
>>> selector = SelectFromModel(estimator=LogisticRegression()).fit(X, y)
>>> selector.estimator_.coef_
array([[-0.3252302 ,  0.83462377,  0.49750423]])

>>> selector.threshold_
0.55245...

>>> selector.get_support()
array([False,  True, False])

>>> selector.transform(X)
array([[-1.34],
       [-0.02],
       [-0.48],
       [ 1.48]])

 

√ 메서드 종류

 

fit_transform(input samples, target values) => 변환된 array 반환

get_feature_names_out([input features]) => 선별된 피처들의 이름  string 반환

get_params(deep = True) => Estimator의 파라미터를 dict 형태로 반환

get_support(indices=False) => 선별된 피처들을 True/False 형태로 선별 여부를 반환함

 

특히 get_support 메서드는 캐글에서 종종 볼 수 있는데요 

 

from sklearn.feature_selection import SelectFromModel

sfm = SelectFromModel(ridge, threshold=threshold).fit(X, y)
print(f"Features selected by SelectFromModel: {feature_names[sfm.get_support()]}")
>> Features selected by SelectFromModel: ['s1' 's5']

sklearn 홈페이지에 있는 예시처럼 모델이 피처를 잘 선택했는지, 어떤 피처를 사용했는지 보기 위해

종종 사용하는 것 같습니다

728x90
반응형

댓글