[sklearn] 알고리즘이 분류, 회귀, 이상치탐지 중 어떤건가?

아주 희박한 확률로, model 객체를 받았는데, 이게 분류 (classifier) 모델인지, 회귀 (regressor) 모델인지, 이상치 (outlier_detector) 인지를 프로그램적으로 판단해야할 때가 있다.

 

그럴 때를 위해 sklearn에는 is_classifier, is_regressor, is_outlier_detector 총 3가지의 validation 툴이 존재한다.

from sklearn.base import is_classifier, is_regressor, is_outlier_detector
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor, IsolationForest

models = [RandomForestClassifier, RandomForestRegressor, IsolationForest]

for m in models:
    print(m.__name__, is_classifier(m), is_regressor(m), is_outlier_detector(m))

위와 같이 각각 RandomForestClassifier, RandomForestRegressor, IsolationForest를 테스트 해보면, (물론 우리는 이름만 봐도 알 수 있다).

RandomForestClassifier True False False
RandomForestRegressor  False True False
IsolationForest        False False True

이상, 간단하게 알고리즘의 타입을 찾는 방법에 대해 알아보았다.

본 글은 필자가 답변한 How to check if sklearn model is classifier or regressor 을 가다듬은 것입니다.

 

'Python > Scikit-Learn' 카테고리의 다른 글

[sklearn] One Hot Encoding 되돌리기  (0) 2021.01.24
[debugging] Expected 2D array, got 1D array instead  (0) 2019.11.15