F1-score Python实现
时间: 2023-07-07 20:35:39 浏览: 122
F1-score是基于精度和召回率的综合指标,其计算公式为:
F1-score = 2 * (precision * recall) / (precision + recall)
其中,precision表示精度,recall表示召回率。
Python实现如下:
```python
from sklearn.metrics import precision_recall_fscore_support
# y_true为真实标签,y_pred为预测标签
precision, recall, f1_score, support = precision_recall_fscore_support(y_true, y_pred, average='binary')
print("Precision: ", precision)
print("Recall: ", recall)
print("F1-score: ", f1_score)
```
其中,average参数可以选择为binary,macro,micro,weighted等,分别对应二分类、多分类、全局、加权等不同的计算方式。
相关问题
['precision recall f1-score support 0 0.95 0.96 0.96 10762 1 0.91 0.95 0.93 9339 2 0.90 0.59 0.71 1628 micro avg 0.93 0.93 0.93 21729 macro avg 0.92 0.83 0.87 21729 weighted avg 0.93 0.93 0.93 21729 samples avg 0.93 0.93 0.93 21729']把列表转成这样的字典形式report1 = { '0': {'precision': 0.95, 'recall': 0.96, 'f1-score': 0.96, 'support': 10762}, '1': {'precision': 0.91, 'recall': 0.95, 'f1-score': 0.93, 'support': 9339}, '2': {'precision': 0.90, 'recall': 0.59, 'f1-score': 0.71, 'support': 1628}, 'micro avg': {'precision': 0.93, 'recall': 0.93, 'f1-score': 0.93, 'support': 21729}, 'macro avg': {'precision': 0.92, 'recall': 0.83, 'f1-score': 0.87, 'support': 21729}, 'weighted avg': {'precision': 0.93, 'recall': 0.93, 'f1-score': 0.93, 'support': 21729}, 'samples avg': {'precision': 0.93, 'recall': 0.93, 'f1-score': 0.93, 'support': 21729} }
您可以使用以下代码将列表转换为字典:
```python
import re
string = "['precision recall f1-score support 0 0.95 0.96 0.96 10762 1 0.91 0.95 0.93 9339 2 0.90 0.59 0.71 1628 micro avg 0.93 0.93 0.93 21729 macro avg 0.92 0.83 0.87 21729 weighted avg 0.93 0.93 0.93 21729 samples avg 0.93 0.93 0.93 21729']"
result = re.sub(r"[^\d\.]+", " ", string)
result = result.split()
keys = result[:4]
values = result[4:]
n = len(values) // 4
report = {}
for i in range(n):
report[str(i)] = {}
for j in range(4):
key = keys[j]
value = values[i*4+j]
report[str(i)][key] = float(value)
for i in range(4):
key = keys[i]
value = values[(n-1)*4+i]
report[key] = {}
report[key]['precision'] = float(value)
print(report)
```
这将输出以下字典:
```
{
'0': {'precision': 0.95, 'recall': 0.96, 'f1-score': 0.96, 'support': 10762},
'1': {'precision': 0.91, 'recall': 0.95, 'f1-score': 0.93, 'support': 9339},
'2': {'precision': 0.9, 'recall': 0.59, 'f1-score': 0.71, 'support': 1628},
'micro avg': {'precision': 0.93, 'recall': 0.93, 'f1-score': 0.93, 'support': 21729.0},
'macro avg': {'precision': 0.92, 'recall': 0.83, 'f1-score': 0.87, 'support': 21729.0},
'weighted avg': {'precision': 0.93, 'recall': 0.93, 'f1-score': 0.93, 'support': 21729.0},
'samples avg': {'precision': 0.93, 'recall': 0.93, 'f1-score': 0.93, 'support': 21729.0}
}
```
其中每个类别的指标被放置在以类别号为键的字典中,而分类总计、宏平均、加权平均和样本平均则被放置在以字符串为键的字典中。
[' precision recall f1-score support', '', ' 0 0.90 0.91 0.91 10762']怎么获得precision recall f1-score support
你可以使用 Python 的字符串分割方法 `split()` 来获取 [' precision recall f-score support', '', ' 0 0.90 0.91 0.91 10762'] 中的 ' precision recall f1-score support' 部分,具体操作如下:
```python
s = "[' precision recall f1-score support', '', ' 0 0.90 0.91 0.91 10762']"
result = s.split(',')[0].strip("[ '")
print(result)
```
输出结果为:
```
precision recall f1-score support
```
这样就可以获得 'precision recall f1-score support' 部分了。
阅读全文