-
Notifications
You must be signed in to change notification settings - Fork 0
/
Evaluation.py
52 lines (48 loc) · 1.95 KB
/
Evaluation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__ = '机器学习系统评估'
__author__ = 'pika'
__mtime__ = '16-7-2'
__email__ = 'pipisorry@126.com'
# code is far away from bugs with the god animal protecting
I love animals. They taste delicious.
┏┓ ┏┓
┏━┛┻━━━┛┻━━┓
┃ ☃ ┃
┃ ┳┛ ┗┳ ┃
┃ ┻ ┃
┗━┓ ┏━┛
┃ ┗━━━┓
┃ 神兽保佑 ┣┓
┃ 永无BUG!┏┛
┗┓┓┏━━━┳┓┏┛
┃┫┫ ┃┫┫
┗┻┛ ┗┻┛
"""
import numpy as np
def evaluation(predict, actual):
'''
评估系统性能(两类分类和判决):accuracy, precision, recall, F1
'''
# 将predict和actual数组转换成1,0数组
predict = list(predict)
actual = list(actual)
labels = list(set(predict))
if actual.count(labels[0]) <= actual.count(labels[1]):
pos = labels[0] # 少数样本作1
else:
pos = labels[1]
predict = [1 if p == pos else 0 for p in predict]
actual = [1 if p == pos else 0 for p in actual]
true_pos = (np.array(predict) + np.array(actual)).tolist().count(2)
true_neg = (np.array(predict) + np.array(actual)).tolist().count(0)
# 计算accuracy, precision, recall, F1
describe = {}
describe['accuracy'] = ((true_pos + true_neg) / len(actual))
describe['precision'] = true_pos / predict.count(1)
describe['recall'] = true_pos / actual.count(1)
describe['F1'] = 2 * (describe['precision'] * describe['recall']) / (describe['precision'] + describe['recall'])
# print("accuracy: {}\nprecision: {}\nrecall: {}\nF1: {}\n".format(describe['accuracy'], describe['precision'],
# describe['recall'], describe['F1']))
return describe