<jupyter_start><jupyter_text>Visualization Precision and Recall<jupyter_code>import numpy as np
from sklearn import datasets
import matplotlib.pyplot as plt
digits = datasets.load_digits()

X = digits.data
y = digits.target.copy()

# 把十个数字分类问题转化成二分类问题. 把数据集变成一个极度偏斜的数据集
y[digits.target==9] = 1
y[digits.target!=9] = 0
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=666)
from sklearn.linear_model import LogisticRegression

log_reg = LogisticRegression()
log_reg.fit(X_train, y_train)
decision_scores = log_reg.decision_function(X_test)
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
precisions = []
recalls = []
# 对于不同的 thresholds, 精准率和召回率的变化
thresholds = np.arange(np.min(decision_scores), np.max(decision_scores), 0.1)
for threshold in thresholds:
    y_predict = np.array(decision_scores >= threshold, dtype='int')
    precisions.append(precision_score(y_test, y_predict))
    recalls.append(recall_score(y_test, y_predict))
    
plt.plot(thresholds, precisions)
plt.plot(thresholds, recalls)<jupyter_output><empty_output><jupyter_text>- 可以看出，开始时，threshold 很小，此时精确率很高，但是召回率很低- 随着 threshold 的升高，精确率上升，而召回率降低  1. Precision-Recall 曲线<jupyter_code>plt.plot(precisions, recalls)<jupyter_output><empty_output><jupyter_text>2. scikit-learn 中的Precision-Recall 曲线<jupyter_code>from sklearn.metrics import precision_recall_curve

precisions, recalls, thresholds = precision_recall_curve(y_test, decision_scores)
precisions.shape
recalls.shape
thresholds.shape<jupyter_output><empty_output><jupyter_text>- thresholds 有144个值，比 precisions 和 recalls 少了一个- 因为当 thresholds 最大时，此时精准率为 1，召回率为 0，此时没有对应的 threshold<jupyter_code>plt.plot(thresholds, precisions[:-1])
plt.plot(thresholds, recalls[:-1])<jupyter_output><empty_output><jupyter_text>- 观察到做出的图和上面我们自己的图不同- 这是因为在 scikit-learn 中，自动的选取了它认为最重要的部分数据<jupyter_code>plt.plot(precisions, recalls)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10172064777327935, 'fraction_numerical': 0.009109311740890687, 'mean_word_length': 8.414285714285715, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 22, 'https://': 0, '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': '88979', 'license': 'MIT', 'n_tokens_mistral': 872, 'n_tokens_neox': 841, 'n_words': 124, 'path': 'ML-Base-MOOC/chapt-7 Logistic Regression/06-Visualization Precision and Recall.ipynb', 'repo_name': 'silence1016/machine-learning', 'size': '1976'}