2级精度召回曲线
from sklearn import svm, datasets
from sklearn.model_selection import train_test_split
import numpy as np
iris = datasets.load_iris()
X = iris.data
y = iris.target
# 增加噪音特征
random_state = np.random.RandomState(0)
n_samples, n_features = X.shape
X = np.c_[X, random_state.randn(n_samples, 200 * n_features)]
# 限制在前两个类别上,并且将数据分类为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X[y < 2], y[y < 2],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? test_size=.5,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? random_state=random_state)
# 创建一个简单的分类器
classifier = svm.LinearSVC(random_state=random_state)
classifier.fit(X_train, y_train)
y_score = classifier.decision_function(X_test)
from sklearn.metrics import average_precision_score
average_precision = average_precision_score(y_test, y_score)
print('Average precision-recall score: {0:0.2f}'.format(
? ? ? average_precision))
from sklearn.metrics import plot_precision_recall_curve
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
disp = plot_precision_recall_curve(classifier, X_test, y_test)
disp.ax_.set_title('2级精度召回曲线: '
? ? ? ? ? ? ? ? ? 'AP={0:0.2f}'.format(average_precision))