svm多分类python和评价指标
时间: 2023-08-06 20:07:49 浏览: 49
上面已经介绍了SVM多分类的两种方法,接下来介绍一些SVM多分类的常用评价指标。
1. 准确率(Accuracy):预测正确的样本数占总样本数的比例。
```python
from sklearn.metrics import accuracy_score
y_true = [0, 1, 2, 3]
y_pred = [0, 1, 1, 3]
accuracy_score(y_true, y_pred)
```
2. 宏平均精确率、召回率和F1值(Macro-average precision, recall and F1-score):分别计算每个类别的精确率、召回率和F1值,然后对所有类别的指标求平均。适用于各个类别的样本数量差不多的情况。
```python
from sklearn.metrics import precision_score, recall_score, f1_score
y_true = [0, 1, 2, 3]
y_pred = [0, 1, 1, 3]
precision_score(y_true, y_pred, average='macro')
recall_score(y_true, y_pred, average='macro')
f1_score(y_true, y_pred, average='macro')
```
3. 微平均精确率、召回率和F1值(Micro-average precision, recall and F1-score):将所有类别的预测结果合并起来,然后计算精确率、召回率和F1值。适用于各个类别的样本数量差异很大的情况。
```python
from sklearn.metrics import precision_score, recall_score, f1_score
y_true = [0, 1, 2, 3]
y_pred = [0, 1, 1, 3]
precision_score(y_true, y_pred, average='micro')
recall_score(y_true, y_pred, average='micro')
f1_score(y_true, y_pred, average='micro')
```
4. 加权平均精确率、召回率和F1值(Weighted-average precision, recall and F1-score):计算每个类别的精确率、召回率和F1值,然后根据各个类别的样本数量加权求平均。适用于各个类别的样本数量差不多的情况。
```python
from sklearn.metrics import precision_score, recall_score, f1_score
y_true = [0, 1, 2, 3]
y_pred = [0, 1, 1, 3]
precision_score(y_true, y_pred, average='weighted')
recall_score(y_true, y_pred, average='weighted')
f1_score(y_true, y_pred, average='weighted')
```
需要注意的是,对于不平衡的多分类问题,仅使用准确率作为评价指标可能会给出误导性的结果。此时可以考虑使用混淆矩阵(Confusion Matrix)来得到更全面的评价结果。