로지스틱 회귀와 ROC 곡선

scikit-learn으로 배우는 지도 학습

George Boorman

Core Curriculum Manager, DataCamp

이진 분류를 위한 로지스틱 회귀

  • 로지스틱 회귀는 분류 문제에 사용됨

  • 로지스틱 회귀는 확률을 출력

  • 확률이 $ \ p>0.5인 경우:

    • 데이터는 1로 레이블링 됨
  • 확률이 $ \ p<0.5$인 경우:

    • 데이터는 0로 레이블링 됨
scikit-learn으로 배우는 지도 학습

선형 결정 경계

scatter plot of feature1 vs feature 2, with a straight line decision boundary for predicting churn running left to right

scikit-learn으로 배우는 지도 학습

scikit-learn의 로지스틱 회귀

from sklearn.linear_model import LogisticRegression

logreg = LogisticRegression()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
logreg.fit(X_train, y_train)
y_pred = logreg.predict(X_test)
scikit-learn으로 배우는 지도 학습

확률 예측

y_pred_probs = logreg.predict_proba(X_test)[:, 1]

print(y_pred_probs[0])
[0.08961376]
scikit-learn으로 배우는 지도 학습

확률 임계값

  • 기본 로지스틱 회귀 임계값 = 0.5

  • 로지스틱 회귀에 국한되지 않음

    • KNN 분류기도 임계값이 존재
  • 임계값을 바꾸면 어떻게 될까요?

scikit-learn으로 배우는 지도 학습

ROC 곡선

true positive rate vs false positive rate with a dotted line running bottom left to top right

scikit-learn으로 배우는 지도 학습

ROC 곡선

zero threshold highlighted in the top right

scikit-learn으로 배우는 지도 학습

ROC 곡선

threshold of 1 also highlighted in the bottom left

scikit-learn으로 배우는 지도 학습

ROC 곡선

both thresholds highlighted

scikit-learn으로 배우는 지도 학습

ROC 곡선

Dots curving up and to the right above the dotted line, representing different thresholds

scikit-learn으로 배우는 지도 학습

ROC 곡선

line curving up and to the right above the dotted line, representing different thresholds

scikit-learn으로 배우는 지도 학습

ROC 그래프 그리기

from sklearn.metrics import roc_curve

fpr, tpr, thresholds = roc_curve(y_test, y_pred_probs)
plt.plot([0, 1], [0, 1], 'k--') plt.plot(fpr, tpr) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title('Logistic Regression ROC Curve') plt.show()
scikit-learn으로 배우는 지도 학습

ROC 그래프 그리기

roc curve plot for churn data set, with a line moving up and to the right from the bottom left

scikit-learn으로 배우는 지도 학습

ROC AUC

roc curve plot for churn data set, with a line moving up and to the right from the bottom left, with p=0.67 annotated

scikit-learn으로 배우는 지도 학습

scikit-learn의 ROC AUC

from sklearn.metrics import roc_auc_score

print(roc_auc_score(y_test, y_pred_probs))
0.6700964152663693
scikit-learn으로 배우는 지도 학습

연습해 봅시다!

scikit-learn으로 배우는 지도 학습

Preparing Video For Download...