<jupyter_start><jupyter_text>Метрики машинного обучения <NAME>рhttps://ermakovpetr.ru  Метрики классификации  true positive, false positive, true negative, false negative<jupyter_code>from sklearn.metrics import confusion_matrix
y_true = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
y_pred = [0, 0, 0, 0, 1, 0, 1, 1, 1, 1]

confusion_matrix(y_true, y_pred)
import pandas as pd
pd.DataFrame(confusion_matrix(y_true, y_pred))
pd.DataFrame([['tn', 'fp'], ['fn', 'tp']])<jupyter_output><empty_output><jupyter_text>precision / recall Precision, Positive Predictive Value Sensitivity, True Positive Rate, Recall, Hit Rate<jupyter_code>from sklearn.metrics import precision_score, recall_score

y_true = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
y_pred = [0, 0, 0, 0, 1, 0, 1, 1, 1, 1]

precision_score(y_true, y_pred)
recall_score(y_true, y_pred)
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import precision_recall_curve
import numpy as np

%matplotlib inline
from matplotlib import pyplot as plt


X, y = make_classification(n_samples=1000, random_state=42)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

predict = LogisticRegression().fit(X_train, y_train).predict_proba(X_test)
precision, recall, thresholds = precision_recall_curve(y_test, predict[:,1])


plt.figure(figsize=(6,6))
plt.plot(recall, precision, label='%s Pre-Re' % ('LogisticRegression'))
plt.xlim([-0.1, 1.1])
plt.ylim([-0.1, 1.1])
plt.xlabel('recall')
plt.ylabel('precision')
plt.legend(loc=0, fontsize='small')
plt.show()
precision, recall, thresholds = precision_recall_curve(y_test, np.random.random(y_test.shape))


plt.figure(figsize=(6,6))
plt.plot(recall, precision, label='%s Pre-Re' % ('LogisticRegression'))
plt.xlim([-0.1, 1.1])
plt.ylim([-0.1, 1.1])
plt.xlabel('recall')
plt.ylabel('precision')
plt.legend(loc=0, fontsize='small')
plt.show()<jupyter_output><empty_output><jupyter_text>f-beta f1<jupyter_code>from sklearn.metrics import f1_score

y_true = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
y_pred = [0, 0, 0, 0, 1, 0, 1, 1, 1, 1]

f1_score(y_true, y_pred)<jupyter_output><empty_output><jupyter_text>accuracy<jupyter_code>from sklearn.metrics import accuracy_score

y_true = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
y_pred = [0, 0, 0, 0, 1, 0, 1, 1, 1, 1]

accuracy_score(y_true, y_pred)
# Спойлер














from sklearn.metrics import accuracy_score

y_true = [0]*10000+[1]*100
y_pred = [0]*10099+[1]*1

accuracy_score(y_true, y_pred), \
f1_score(y_true, y_pred)<jupyter_output><empty_output><jupyter_text>FPR False Positive Rate (FPR, fall-out, false alarm rate)   ROC-AUC<jupyter_code>predict[:,1]
from sklearn.metrics import roc_curve
fpr, tpr, thresholds = roc_curve(y_test, predict[:,1])


plt.figure(figsize=(6,6))
plt.plot(fpr, tpr, label='%s ROC' % ('LogisticRegression'))
plt.xlim([-0.1, 1.1])
plt.ylim([-0.1, 1.1])
plt.xlabel('fpr')
plt.ylabel('tpr')
plt.legend(loc=0, fontsize='small')
plt.show()
from sklearn.metrics import roc_curve
fpr, tpr, thresholds = roc_curve(y_test, np.random.random(y_test.shape))


plt.figure(figsize=(6,6))
plt.plot(fpr, tpr, label='%s ROC' % ('LogisticRegression'))
plt.xlim([-0.1, 1.1])
plt.ylim([-0.1, 1.1])
plt.xlabel('fpr')
plt.ylabel('tpr')
plt.legend(loc=0, fontsize='small')
plt.show()
https://habr.com/ru/post/267217/<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16486090775988288, 'fraction_numerical': 0.048609077598828695, 'mean_word_length': 6.40997830802603, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '153163', 'license': 'MIT', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1416, 'n_words': 302, 'path': '1_term/made_2019_ml/seminars/week04_metrics/metrics_classification.ipynb', 'repo_name': 'Ilyabasharov/made_mail.ru', 'size': '3420'}