Note ≡


    역주 사이킷런의 VotingClassifier를 사용해 보겠습니다. estimators 매개변수에는 분류기 이름과 객체로 구성된 튜플의 리스트를 입력합니다. 앞에서 만든 MajorityVoteClassifiervote 매개변수에 상관없이 predict_proba 메서드를 실행할 수 있지만 사이킷런의 VotingClassifiervoting='hard'일 경우 predict_proba 메서드를 지원하지 않습니다. ROC AUC를 계산하기 위해서는 예측 확률이 필요하므로 voting='soft'로 지정합니다.

    >>> from sklearn.model_selection import cross_validate
    >>> from sklearn.ensemble import VotingClassifier
    
    >>> vc = VotingClassifier(estimators=[
    ...     ('lr', pipe1), ('dt', clf2), ('knn', pipe3)], voting='soft')
    
    >>> scores = cross_validate(estimator=vc, X=X_train, y=y_train,
    ...                         cv=10, scoring='roc_auc')
    >>> print("ROC AUC: : %0.2f (+/- %0.2f) [%s]"
    ...       % (scores['test_score'].mean(),
    ...          scores['test_score'].std(), 'VotingClassifier'))
    ROC AUC: : 0.98 (+/- 0.05) [VotingClassifier]

    VotingClassifierfit 메서드를 호출할 때 사이킷런 0.23 버전에서 추가된 verbose 매개변수를 True로 지정하여 진행 과정을 출력할 수 있습니다. 여기서는 앞서 만든 vc 객체의 set_params 메서드를 사용하여 verbose 매개변수를 설정하겠습니다.

    >>> vc.set_params(verbose=True)
    
    >>> vc = vc.fit(X_train, y_train)
    [Voting] ....................... (1 of 3) Processing lr, total= 0.0s
    [Voting] ....................... (2 of 3) Processing dt, total= 0.0s
    [Voting] ....................... (3 of 3) Processing knn, total= 0.0s

    voting='soft'일 때 predict 메서드는 predict_proba 메서드에서 얻은 가장 큰 확률의 클래스를 예측으로 삼습니다. predict_proba 메서드는 각 분류기의 클래스 확률을 평균하여 반환합니다.

    >>> vc.predict_proba(X_test[:10])
    array([[0.80858947, 0.19141053],
           [0.80798659, 0.19201341],
           [0.80742142, 0.19257858],
           [0.81176637, 0.18823363],
           [0.81195778, 0.18804222],
           [0.17701319, 0.82298681],
           [0.17670572, 0.82329428],
           [0.17845724, 0.82154276],
           [0.1796252 , 0.8203748 ],
    [0.81076201, 0.18923799]])
    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.