Multi-label to achieve the precision and recall rate

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@File : recall.py
@Author : 郭凯锋
@Time : 2020/1/12 17:57
@Software : PyCharm
@Git-Hub : daguonice
@博客园: https://www.cnblogs.com/daguonice/

"""
import pandas as pd
import numpy as np
from sklearn.metrics import recall_score


def right_or_wrong(ypred_ele, ytrue_ele):
if ypred_ele in ytrue_ele:
return True
else:
return False


def get_single_score(y_true, y_pred):
TP = np.sum(np.logical_and(np.equal(y_true, 1), np.equal(y_pred, 1)))
FP = np.sum(np.logical_and(np.equal(y_true, 0), np.equal(y_pred, 1)))
TN = np.sum(np.logical_and(np.equal(y_true, 1), np.equal(y_pred, 0)))
FN = np.sum(np.logical_and(np.equal(y_true, 0), np.equal(y_pred, 0)))
recall = [TP, (TP + FN)]
accuracy = [(TP + TN), len(y_pred)]
return accuracy, recall


def get_multi_score(y_true, y_pred):
list_score = []
for ele in ['相互宝', '健康险', '保险线', '花呗']:
y_true_temp = np.zeros(len(y_true))
y_pred_temp = np.zeros(len(y_pred))
for idx in range(len(y_true)):
if y_pred[idx] == ele:
y_pred_temp[idx] = 1.
if right_or_wrong(ele, y_true[idx]):
y_true_temp[idx] = 1.
accuracy, recall = get_single_score (y_true_temp, y_pred_temp) # becomes percentile
list_score.append ([Accuracy, Recall])
return list_score # returns a list of three-dimensional, are mutually Po, fractional precision and recall of the health insurance, insurance lines


fraction2decimal DEF (Result):
RES = []
for ELE in Result:
list_ele = []
for Lab in ELE:
list_lab = []
for Score in Lab:
IF Score [. 1] == 0:
list_lab.append (0.0)
the else:
list_lab.append (round (Score [0] / Score [. 1], 2))
list_ele.append (list_lab)
res.append (list_ele)
return RES


DEF FUNC (y_true, y_pred):
# y_true是一个一维列表, y_pred是一个二维列表
if len(y_true) != len(y_pred):
raise Exception("The two input lengths are inconsistent")
length = len(y_true)
list_score = get_multi_score(y_true, y_pred)
result = []
temp_list = list_score
for idx in range(length):
list_score = temp_list
if len(y_true[idx]) == 1 and y_pred[idx] not in y_true[idx]:
list_score[1][1][1] += 1
list_score[0][0][1] += 1
result.append(list_score)
elif len(y_true[idx]) == 2 and y_pred[idx] in y_true[idx]:
list_score[0][0][0] += 1
list_score[0][0][1] += 1
list_score [0] [. 1] [0] + =. 1
list_score [0] [. 1] [. 1] + =. 1
result.append (list_score)
elif len (y_true [IDX]) == 2 and y_pred [IDX] Not in y_true [IDX]:
list_score [. 1] [. 1] [. 1] + =. 1
list_score [2] [. 1] [. 1] + =. 1
list_score [0] [0] [. 1] + =. 1
result.append (list_score)
the else:
result.append (list_score)
Result = fraction2decimal (Result)
return Result


IF the __name__ == '__main__':
alist = [ 'are Po', 'are Po', 'are Po', 'health insurance' 'line of insurance '' flower chanting ']
blist = [' health insurance ', [' health insurance ',' mutual Po '], [' health insurance ',' safety line '], [' mutual Po '], [' Insurance line '],[ 'Flower chanting']]
RES = FUNC (for blist, alist)
# Res = pd.DataFrame (res, columns = [ ' mutual treasure', 'health insurance', 'safety line', 'flower chanting'])
# pd.set_option ( 'display.max_columns', None)
# pd.set_option ( 'display.max_rows', None)
# pd.set_option ( 'display.width', 100000)
# pd.set_option ( 'display.unicode.east_asian_width', True)
Print ()
Print (RES)

Guess you like

Origin www.cnblogs.com/daguonice/p/12185479.html