Logistic Regression을 이용한 이진분류 파이썬 코드

  • 거의 모든 X변수는 정규분포를 띄고 있음
    Kaggle에서 Instant Gratification이라는 이름의 대회 데이터셋으로 Logistic Regression을 시도
glm_train_test

Logistic Regression을 이용한 이진분류 예제

  • 거의 모든 X변수는 정규분포를 띄고 있음
  • Kaggle에서 Instant Gratification이라는 이름의 대회 데이터셋
  • AUROC로 평가지표, Kaggle Kernel의 유효성을 확인하기 위한 가상 데이터셋
  • Logistic Regression은 정확도가 거의 찍는수준이며, PCA QDA LDA등의 방법이 우선됨
In [10]:
import pandas_profiling
import numpy as np
import pandas as pd
import seaborn as sns
In [41]:
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
In [81]:
from sklearn.metrics import roc_auc_score
from sklearn.metrics import roc_curve
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
In [79]:
from matplotlib import pyplot
In [4]:
train = pd.read_csv("../inputs/train.csv")
test = pd.read_csv("../inputs/test.csv")
In [89]:
sns.distplot(train['hazy-emerald-cuttlefish-unsorted'])
Out[89]:
<matplotlib.axes._subplots.AxesSubplot at 0x2c0359dc1d0>
In [29]:
train_x, test_x, train_y, test_y = train_test_split(train.drop(['target', 'id'], axis = 1), train['target'], test_size = 0.2)
In [35]:
clf = LogisticRegression(solver='lbfgs',multi_class='multinomial').fit(train_x, train_y)
In [52]:
test_predicted = clf.predict(test_x)

지표 확인

In [53]:
confusion_matrix(test_y, test_predicted)
Out[53]:
array([[13695, 12555],
       [12390, 13789]], dtype=int64)
In [54]:
test_predicted_prob = clf.predict_proba(test_x)
In [76]:
roc_auc_score(test_y, test_predicted_prob[:,1])
Out[76]:
0.5346831815395473

ROC Curve 그리기

In [82]:
fpr, tpr, thresholds = roc_curve(test_y, test_predicted_prob[:,1])
In [86]:
pyplot.plot([0, 1], [0, 1], linestyle='--')
pyplot.plot(fpr, tpr, marker='.')
pyplot.show()
  • ROC 커브는 대각선에 가깝기 때문에, fit이 되지 않았음을 알수있음

답글 남기기