1、什幺是多分类？

2、构建多个二分类器进行分类

```import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm,datasets
from itertools import cycle
from sklearn import svm, datasets
from sklearn.metrics import roc_curve, auc
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import label_binarize
from sklearn.multiclass import OneVsRestClassifier
from scipy import interp
# 导入鸢尾花数据集
X = iris.data  # X.shape==(150, 4)
y = iris.target  # y.shape==(150, )
# 二进制化输出
y = label_binarize(y, classes=[0, 1, 2])  # shape==(150, 3)
n_classes = y.shape[1]  # n_classes==3
#np.r_是按列连接两个矩阵，就是把两矩阵上下相加，要求列数相等。
#np.c_是按行连接两个矩阵，就是把两矩阵左右相加，要求行数相等。
# 添加噪音特征，使问题更困难
random_state = np.random.RandomState(0)
n_samples, n_features = X.shape  # n_samples==150, n_features==4
X = np.c_[X, random_state.randn(n_samples, 200 * n_features)]  # shape==(150, 84)```

```# 打乱数据集并切分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.5,
random_state=0)
# X_train.shape==(75, 804), X_test.shape==(75, 804), y_train.shape==(75, 3), y_test.shape==(75, 3)
# 学习区分某个类与其他的类
classifier = OneVsRestClassifier(svm.SVC(kernel='linear', probability=True,
random_state=random_state))
y_score = classifier.fit(X_train, y_train).decision_function(X_test)```

predict()：返回预测标签、

predict_proba()：返回预测属于某标签的概率

decision_function()：返回样本到分隔超平面的有符号距离来度量预测结果的置信度

`print(classifier.estimators_)`

```[SVC(C=1.0, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma='scale', kernel='linear',
max_iter=-1, probability=True,
random_state=RandomState(MT19937) at 0x7F480F316A98, shrinking=True,
tol=0.001, verbose=False),
SVC(C=1.0, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma='scale', kernel='linear',
max_iter=-1, probability=True,
random_state=RandomState(MT19937) at 0x7F480F316CA8, shrinking=True,
tol=0.001, verbose=False),
SVC(C=1.0, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma='scale', kernel='linear',
max_iter=-1, probability=True,
random_state=RandomState(MT19937) at 0x7F480F316DB0, shrinking=True,
tol=0.001, verbose=False)]```

`y_true=np.where(y_test==1)[1]`

array([2, 1, 0, 2, 0, 2, 0, 1, 1, 1, 2, 1, 1, 1, 1, 0, 1, 1, 0, 0, 2, 1, 0, 0, 2, 0, 0, 1, 1, 0, 2, 1, 0, 2, 2, 1, 0, 1, 1, 1, 2, 0, 2, 0, 0, 1, 2, 2, 2, 2, 1, 2, 1, 1, 2, 2, 2, 2, 1, 2, 1, 0, 2, 1, 1, 1, 1, 2, 0, 0, 2, 1, 0, 0, 1])

```#这里重新定义标签，1代表当前标签，0代表其他标签
y0=[0 if i==0 else 1 for i in y_true]
print(y0)
print(classifier.estimators_[0].fit(X_train,y0).predict(X_test))```

[0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0]

[0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 0 1 0 1 0 0]

[0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 1 0 0 1 1 0 0 1 1 0 1 1 0 0 1 0 0 1 0 0 0 1 0 0 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 1 1 0]

https://www.cnblogs.com/xiximayou/p/13682052.html

```from sklearn.metrics import roc_curve, auc
# 为每个类别计算ROC曲线和AUC
fpr = dict()
tpr = dict()
roc_auc = dict()
n_classes=3
for i in range(n_classes):
fpr[i], tpr[i], _ = roc_curve(y_test[:, i], y_score[:, i])
roc_auc[i] = auc(fpr[i], tpr[i])
# fpr[0].shape==tpr[0].shape==(21, ), fpr[1].shape==tpr[1].shape==(35, ), fpr[2].shape==tpr[2].shape==(33, )
# roc_auc {0: 0.9118165784832452, 1: 0.6029629629629629, 2: 0.7859477124183007}
plt.figure()
lw = 2
for i in range(n_classes):
plt.plot(fpr[i], tpr[i], color='darkorange',
lw=lw, label='ROC curve (area = %0.2f)' % roc_auc[i])
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.legend(loc="lower right")
plt.show()```

3、多分类评价指标？

#### 宏平均 Macro-average

Macro F1：将n分类的评价拆成n个二分类的评价，计算每个二分类的F1 score，n个F1 score的平均值即为Macro F1。

#### 微平均 Micro-average

Micro F1：将n分类的评价拆成n个二分类的评价，将n个二分类评价的TP、FP、TN、FN对应相加，计算评价准确率和召回率，由这2个准确率和召回率计算的F1 score即为Micro F1。

```TP=cnf_matrix[1][1] #预测为正的真实标签为正
FP=cnf_matrix[0][1] #预测为正的真实标签为负
FN=cnf_matrix[1][0] #预测为负的真实标签为正
TN=cnf_matrix[0][0] #预测为负的真实标签为负
accuracy=(TP+TN)/(TP+FP+FN+TN)
precision=TP/(TP+FP)
recall=TP/(TP+FN)
f1score=2 * precision * recall/(precision + recall)```

ROC曲线：

FPR = FP / ( FP +TN)

TPR = TP / ( TP+ FN)

AUC：就是roc曲线和横坐标围城的面积。

```# 计算微平均ROC曲线和AUC
fpr["micro"], tpr["micro"], _ = roc_curve(y_test.ravel(), y_score.ravel())
roc_auc["micro"] = auc(fpr["micro"], tpr["micro"])
# 计算宏平均ROC曲线和AUC
# 首先汇总所有FPR
all_fpr = np.unique(np.concatenate([fpr[i] for i in range(n_classes)]))
# 然后再用这些点对ROC曲线进行插值
mean_tpr = np.zeros_like(all_fpr)
for i in range(n_classes):
mean_tpr += interp(all_fpr, fpr[i], tpr[i])
# 最后求平均并计算AUC
mean_tpr /= n_classes
fpr["macro"] = all_fpr
tpr["macro"] = mean_tpr
roc_auc["macro"] = auc(fpr["macro"], tpr["macro"])
# 绘制所有ROC曲线
plt.figure()
lw = 2
plt.plot(fpr["micro"], tpr["micro"],
label='micro-average ROC curve (area = {0:0.2f})'
''.format(roc_auc["micro"]),
color='deeppink', linestyle=':', linewidth=4)
plt.plot(fpr["macro"], tpr["macro"],
label='macro-average ROC curve (area = {0:0.2f})'
''.format(roc_auc["macro"]),
color='navy', linestyle=':', linewidth=4)
colors = cycle(['aqua', 'darkorange', 'cornflowerblue'])
for i, color in zip(range(n_classes), colors):
plt.plot(fpr[i], tpr[i], color=color, lw=lw,
label='ROC curve of class {0} (area = {1:0.2f})'
''.format(i, roc_auc[i]))
plt.plot([0, 1], [0, 1], 'k--', lw=lw)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Some extension of Receiver operating characteristic to multi-class')
plt.legend(loc="lower right")
plt.show()```

```from sklearn.metrics import confusion_matrix
classes=[0,1,2]
y_my_test=np.where(y_test==1)[1]
y_my_score=np.zeros(y_my_test.shape)
for i in range(len(classes)):
y_my_score[np.where(y_score[:,i]==1)]=i
confusion = confusion_matrix(y_my_test, y_my_score)# 绘制热度图
plt.imshow(confusion, cmap=plt.cm.Greens)
indices = range(len(confusion))
plt.xticks(indices, classes)
plt.yticks(indices, classes)
plt.colorbar()
plt.xlabel('y_pred')
plt.ylabel('y_true')

# 显示数据
for first_index in range(len(confusion)):
for second_index in range(len(confusion[first_index])):
plt.text(first_index, second_index, confusion[first_index][second_index])

# 显示图片
plt.show()```

```from sklearn.metrics import classification_report
t = classification_report(y_my_test, y_my_score, target_names=['0', '1', '2'])```

```              precision    recall  f1-score   support
0       0.52      0.71      0.60        21
1       0.60      0.40      0.48        30
2       0.73      0.79      0.76        24
accuracy                           0.61        75
macro avg       0.62      0.64      0.61        75
weighted avg       0.62      0.61      0.60        75```

`t = classification_report(y_my_test, y_my_score, target_names=['0', '1', '2'],output_dict=True)`

{‘0’: {‘precision’: 0.5172413793103449, ‘recall’: 0.7142857142857143, ‘f1-score’: 0.6000000000000001, ‘support’: 21}, ‘1’: {‘precision’: 0.6, ‘recall’: 0.4, ‘f1-score’: 0.48, ‘support’: 30}, ‘2’: {‘precision’: 0.7307692307692307, ‘recall’: 0.7916666666666666, ‘f1-score’: 0.76, ‘support’: 24}, ‘accuracy’: 0.6133333333333333, ‘macro avg’: {‘precision’: 0.6160035366931919, ‘recall’: 0.6353174603174603, ‘f1-score’: 0.6133333333333334, ‘support’: 75}, ‘weighted avg’: {‘precision’: 0.6186737400530504, ‘recall’: 0.6133333333333333, ‘f1-score’: 0.6032000000000001, ‘support’: 75}}

```import sklearn
for i in range(len(classes)):
precision=sklearn.metrics.precision_score(y_test[:,i], y_score[:,i], labels=None, pos_label=1, average='binary',
sample_weight=None)
print("{} precision:{}".format(i,precision))```

```from sklearn.metrics import precision_score
print(precision_score(y_test, y_score, average="micro"))```

average可选参数micro、macro、weighted ``` ```

https://zhuanlan.zhihu.com/p/59862986

https://blog.csdn.net/hfutdog/article/details/88079934

https://blog.csdn.net/wf592523813/article/details/95202448

https://blog.csdn.net/vivian_ll/article/details/99627094