sklearn的precision_score, recall_score, f1_score使用
来源:互联网 发布:北京华贸广场caffe 编辑:程序博客网 时间:2024/06/08 03:16
1 使用numpy计算true positives等
import numpy as npy_true = np.array([0, 1, 1, 0, 1, 0])y_pred = np.array([1, 1, 1, 0, 0, 1])# true positiveTP = np.sum(np.multiply(y_true, y_pred))print(TP)# false positiveFP = np.sum(np.logical_and(np.equal(y_true, 0), np.equal(y_pred, 1)))print(FP)# false negativeFN = np.sum(np.logical_and(np.equal(y_true, 1), np.equal(y_pred, 0)))print(FN)# true negativeTN = np.sum(np.logical_and(np.equal(y_true, 0), np.equal(y_pred, 0)))print(TN)输出结果:
2211
2 使用tensorflow计算true positives等
import tensorflow as tfsess = tf.Session()y_true = tf.constant([0, 1, 1, 0, 1, 0])y_pred = tf.constant([1, 1, 1, 0, 0, 1])# true positiveTP = tf.reduce_sum(tf.multiply(y_true, y_pred))print(sess.run(TP))# false positiveFP = tf.reduce_sum(tf.cast(tf.logical_and(tf.equal(y_true, 0), tf.equal(y_pred, 1)), tf.int32))print(sess.run(FP))# false negativeFN = tf.reduce_sum(tf.cast(tf.logical_and(tf.equal(y_true, 1), tf.equal(y_pred, 0)), tf.int32))print(sess.run(FN))# true negativeTN = tf.reduce_sum(tf.cast(tf.logical_and(tf.equal(y_true, 0), tf.equal(y_pred, 0)), tf.int32))print(sess.run(TN))输出结果:
2211
3 使用sklearn的metrics模块计算precision,recall和f1-score
3.1 数据是list类型
from sklearn.metrics import precision_score, recall_score, f1_scorey_true = [0, 1, 1, 0, 1, 0]y_pred = [1, 1, 1, 0, 0, 1]p = precision_score(y_true, y_pred, average='binary')r = recall_score(y_true, y_pred, average='binary')f1score = f1_score(y_true, y_pred, average='binary')print(p)print(r)print(f1score)输出结果:
0.50.6666666666670.571428571429
3.2 数据是ndarray类型
from sklearn.metrics import precision_score, recall_score, f1_scoreimport numpy as npy_true = np.array([[0, 1, 1], [0, 1, 0]])y_pred = np.array([[1, 1, 1], [0, 0, 1]])y_true = np.reshape(y_true, [-1])y_pred = np.reshape(y_pred, [-1])p = precision_score(y_true, y_pred, average='binary')r = recall_score(y_true, y_pred, average='binary')f1score = f1_score(y_true, y_pred, average='binary')print(p)print(r)print(f1score)输出结果:
0.50.6666666666670.571428571429
阅读全文
0 2
- sklearn的precision_score, recall_score, f1_score使用
- sklearn.metrics.precision_score 中 unknow is not supported 问题
- sklearn.metrics中的评估方法(accuracy_score,recall_score,roc_curve,roc_auc_score,confusion_matrix)
- sklearn.metrics中的评估方法介绍(accuracy_score, recall_score, roc_curve, roc_auc_score, confusion_matrix)
- 使用sklearn
- sklearn.preprocessing.PolynomialFeatures类的使用
- sklearn.preprocessing.PolynomialFeatures类的使用
- sklearn.preprocessing.PolynomialFeatures类的使用
- 有关使用sklearn LogisticRegression出现的 DeprecationWarning:
- 机器学习--sklearn的常见使用
- sklearn 模块间的搭配使用
- 关于sklearn的简单使用1(分类)
- sklearn 中 make_blobs模块的使用
- python sklearn模型的保存与使用
- python中sklearn的朴素贝叶斯方法(sklearn.naive_bayes.GaussianNB)的简单使用
- sklearn常用工具箱使用
- sklearn-基础使用
- sklearn使用实例
- HDU
- 代理模式--原理及实现
- cpu如何知道内存中一条指令的大小从而使cs:ip寄存器更改自己的值
- linux sed命令详解
- 机器视觉——旋转矩阵的计算(二)
- sklearn的precision_score, recall_score, f1_score使用
- WordPress 自定义链接
- 暴力(Cake,HDU 5355)
- DOM中的一些概念
- UDS的CAN刷新软件
- Codis2迁移到Codis3
- Navigation Drawer的基本用法(SlidingMenu的替代品)
- Kotlin的Spring之旅(二):AOP(面向切面编程)
- 电商时代已经要过去了。接下来是零售