<jupyter_start><jupyter_text>ROC 커브<jupyter_code>from sklearn.datasets import make_classification
from sklearn.metrics import *
X, y = make_classification(n_samples=10000, weights=[0.9, 0.1], random_state=1)
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC

model1 = LogisticRegression().fit(X, y)
y_hat1 = model1.predict(X)

model2 = SVC(gamma=0.0026, C=10, probability=True).fit(X, y)
y_hat2 = model2.predict(X)
print(confusion_matrix(y, y_hat1)) # logistic regression 모델 성능
print(confusion_matrix(y, y_hat2)) # SVC 모델 성능
print(classification_report(y, model1.predict(X)))
print(classification_report(y, model2.predict(X)))<jupyter_output>precision    recall  f1-score   support

          0       0.96      0.99      0.98      8947
          1       0.93      0.62      0.74      1053

avg / total       0.95      0.95      0.95     10000<jupyter_text>둘의 성능지표 값은 거의 동일하다.<jupyter_code>from sklearn.metrics import roc_curve

fpr1, tpr1, thresholds1 = roc_curve(y, model1.decision_function(X))
fpr2, tpr2, thresholds1 = roc_curve(y, model2.decision_function(X))
plt.plot(fpr1, tpr1, label="Logistic Regression")
plt.plot(fpr2, tpr2, label="Kernel SVM")
plt.legend()
plt.plot([0, 1], [0, 1], 'k--', label="random guess")
plt.xlabel('False Positive Rate (Fall-Out)')
plt.ylabel('True Positive Rate (Recall)')
plt.title('Receiver operating characteristic example')
plt.show()<jupyter_output><empty_output><jupyter_text>- 근데 ROC 커브를 그리면 두 모델이 다르게 나온다.- 전체적인 성능은 초록색(Kernel SVM)이 좋다.<jupyter_code>N = 500

plt.subplot(211)
plt.plot(model1.decision_function(X[:N]) * 0.1 + 1)
plt.plot(y[:N], 'o', alpha=0.5)
plt.title("Logistic Regression")

plt.subplot(212)
plt.plot(model2.decision_function(X[:N]) + 1)
plt.plot(y[:N], 'o', alpha=0.5)
plt.title("Kernel SVM")
plt.xlabel("sample number")

plt.tight_layout()
plt.show()<jupyter_output><empty_output><jupyter_text>Kernel SVM는 threshhold가 위에서 아래로 왔다갔다해도 값이 0에서 1로 바뀔일이 Logistic보다 적다.<jupyter_code>from sklearn.naive_bayes import GaussianNB
from sklearn.datasets import load_iris
from sklearn.preprocessing import label_binarize
from sklearn.metrics import roc_curve

iris = load_iris()
X = iris.data
y = label_binarize(iris.target, [0, 1, 2])

fpr = [None] * 3
tpr = [None] * 3
thr = [None] * 3

for i in range(3):
    model = GaussianNB().fit(X, y[:, i])
    fpr[i], tpr[i], thr[i] = roc_curve(y[:, i], model.predict_proba(X)[:, 1])
    plt.plot(fpr[i], tpr[i])

plt.xlabel('False Positive Rate (Fall-Out)')
plt.ylabel('True Positive Rate (Recall)')
plt.show()
model.predict_proba(X)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14528593508500773, 'fraction_numerical': 0.044435857805255025, 'mean_word_length': 4.993055555555555, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '576543', 'license': 'MIT', 'n_tokens_mistral': 1185, 'n_tokens_neox': 1127, 'n_words': 220, 'path': 'machinelearning/classification/basic/ROC.ipynb', 'repo_name': 'Junhojuno/TIL', 'size': '2588'}