#!/usr/bin/env python # -*- coding: utf-8 -*- """ @Time : 2018/5/29 10:40 @Author : xwill @File : 分类.py @Software: PyCharm """ import csv from sklearn import metrics from sklearn.metrics import precision_recall_curve from sklearn.metrics import average_precision_score from sklearn.metrics import accuracy_score import matplotlib.pyplot as plt column, column1, column = [], [], [] with open('jisuan.csv', 'rt') as csvfile: reader = csv.DictReader(csvfile) column = [row['预测值'] for row in reader] with open('jisuan.csv', 'rt') as csvfile: reader = csv.DictReader(csvfile) column1 = [row['判决结果'] for row in reader] with open('jisuan.csv', 'rt') as csvfile: reader = csv.DictReader(csvfile) column2 = [row['Ground-truth'] for row in reader] def f(column1, column2): TP = 0 FP = 0 FN = 0 TN = 0 #print(column1) for i in range(0, len(column1)): #print(int(column1[i])) if int(column1[i]) == 1 and int(column2[i]) == 1: TP = TP + 1 if int(column1[i]) == 1 and int(column2[i]) == 0: FP = FP + 1 if int(column1[i]) == 0 and int(column2[i]) == 1: FN = FN + 1 if int(column1[i]) == 0 and int(column2[i]) == 0: TN = TN + 1 column[i] = float(column[i]) column1[i] = int(column1[i]) column2[i] = int(column2[i]) return TP, TN, FP, FN TP, TN, FP, FN = f(column1, column2) print(TP, TN, FP, FN) print('算出来的准确率指标:', (TP + TN)/(TP + TN + FP + FN)) # 准确率 print('python自带的:', accuracy_score(column2, column1)) P = TP / (TP + FP) print('算出来的查准率指标:', P)#查准率 print('python自带的:', metrics.precision_score(column2, column1)) R = TP / (TP + FN) print('算出来的查全率指标:', R)#查全率 print('python自带的:', metrics.recall_score(column2, column1)) B = 1 FB = ((1 + B**2) * P * R)/((B**2 * P) + R) print('算出来的准确率与召回率的调和平均指标=1:', FB)#准确率与召回率的调和平均指标 print('python自带的:', metrics.f1_score(column2, column1)) B = 0.5 FB = ((1 + B**2) * P * R)/((B**2 * P) + R) print('算出来的准确率与召回率的调和平均指标=0.5:', FB)#准确率与召回率的调和平均指标 print('python自带的:', metrics.fbeta_score(column2, column1, beta=0.5)) B = 2 FB = ((1 + B**2) * P * R)/((B**2 * P) + R) print('算出来的准确率与召回率的调和平均指标=2:', FB)#准确率与召回率的调和平均指标 print('python自带的:', metrics.fbeta_score(column2, column1, beta=2)) # 画图 def pr(column, column1, column2): py, rx, fpx, tpy = [], [], [], [] P1 = R1 = fp = tp = 0 # print(column, 50 / 100) for i in range(0, 101): for j in range(0, len(column2)): if column[j] < i/100: column1[j] = 0 else: column1[j] = 1 # print(column1) TP, TN, FP, FN = f(column1, column2) # r-p图像 if (TP + FP) == 0: P1 = 1 else: P1 = TP / (TP + FP) if (TP + FN) == 0: R1 = 1 else: R1 = TP / (TP + FN) py.append(P1) rx.append(R1) # roc图像 fp = FP / (TP + FN) tp = TP / (TN + FP) fpx.append(fp) tpy.append(tp) return py, rx, fpx, tpy py, rx, fpx, tpy = pr(column, column1, column2) #tpy = tpy[:: -1] print(fpx) print(tpy) plt.subplot(211) plt.plot(py, rx) plt.xlabel("Recall") plt.ylabel('Precision') plt.title("P-R曲线") plt.subplot(212) plt.plot(fpx, tpy) plt.xlabel("fpx") plt.ylabel('tpy') plt.title("ROC曲线") plt.show() #print(py) #print(rx)