多分类评价指标python代码
时间: 2023-06-24 22:08:15 浏览: 117
以下是几个常用的多分类评价指标的 Python 代码实现:
1. 准确率(accuracy)
```python
from sklearn.metrics import accuracy_score
y_true = [0, 1, 2, 0, 1, 2]
y_pred = [0, 2, 1, 0, 2, 1]
accuracy = accuracy_score(y_true, y_pred)
print("准确率:", accuracy)
```
2. 精确率(precision)
```python
from sklearn.metrics import precision_score
y_true = [0, 1, 2, 0, 1, 2]
y_pred = [0, 2, 1, 0, 2, 1]
precision = precision_score(y_true, y_pred, average='macro')
print("精确率:", precision)
```
3. 召回率(recall)
```python
from sklearn.metrics import recall_score
y_true = [0, 1, 2, 0, 1, 2]
y_pred = [0, 2, 1, 0, 2, 1]
recall = recall_score(y_true, y_pred, average='macro')
print("召回率:", recall)
```
4. F1-score
```python
from sklearn.metrics import f1_score
y_true = [0, 1, 2, 0, 1, 2]
y_pred = [0, 2, 1, 0, 2, 1]
f1score = f1_score(y_true, y_pred, average='macro')
print("F1-score:", f1score)
```
其中,`average` 参数用于设置多分类情况下如何计算指标。可选值有 `'micro'`、`'macro'`、`'weighted'` 和 `'samples'`。一般情况下,`'macro'` 表示对每个类别分别计算指标,再求平均值;`'micro'` 表示将所有样本看成一个整体计算指标。
阅读全文