1.使用sklearn库和matplotlib.pyplot库

import sklearn
import matplotlib.pyplot as plt

2.准备绘图函数的传入参数1.预测的概率值数组2.预测的labels值数组

for i in range(len(y_labeles)):
a = np.argmax(y_labeles[i])
y_pred.append(y_conv.eval(feed_dict={x: np.reshape(mnist.test.images[i], [1, 784]), keep_prob: 0.5}, session=sess)[0][a])
y_labeles_d1.append(correct_prediction.eval(feed_dict={x: np.reshape(mnist.test.images[i], [1, 784]), y_: np.reshape(y_labeles[i], [1, 10]), keep_prob: 0.5}, session=sess))

3.调用sklearn.metrics.roc_curve();

fpr, tpr, thresholds = sklearn.metrics.roc_curve(y_labeles_d1, y_pred)

plt.plot(fpr, tpr, 'b')#生成ROC曲线
plt.legend(loc='lower right')
plt.plot([0, 1], [0, 1], 'r--')
plt.xlim([0, 1])
plt.ylim([0, 1])
plt.ylabel('TP')
plt.xlabel('FP')
plt.show()

4.例子

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
import sklearn
import matplotlib.pyplot as plt mnist = input_data.read_data_sets('data/', one_hot=True) def weight_variable(shape, name):
initial = tf.truncated_normal(shape, stddev=0.1, name=name)
return tf.Variable(initial) def bias_variable(shape, name):
initial = tf.constant(0.1, shape=shape, name=name)
return tf.Variable(initial) def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') x = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder(tf.float32, [None, 10])
x_image = tf.reshape(x, [-1, 28, 28, 1]) W_conv1 = weight_variable([5, 5, 1, 32], name='W_conv1')
b_conv1 = bias_variable([32], name='b_conv1')
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1) W_conv2 = weight_variable([5, 5, 32, 64], name='W_conv2')
b_conv2 = bias_variable([64], name='b_conv2')
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2) W_fc1 = weight_variable([7*7*64, 1024], name='W_fc1')
b_fc1 = bias_variable([1024], name='b_fc1')
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) W_fc2 = weight_variable([1024, 10], name='W_fc2')
b_fc2 = bias_variable([10], name='b_fc2')
y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) cross_entropy = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(logits=y_conv, labels=y_)) # cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) sess = tf.Session()
sess.run(tf.global_variables_initializer()) for i in range(500): batch = mnist.train.next_batch(100) train_step.run(session=sess, feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
if i % 500 == 0 and i != 0:
train_accuracy = accuracy.eval(session=sess, feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0})
print('step %d, training accuracy %g' % (i, train_accuracy)) print("!!!!!")
print('test accuracy %g' % accuracy.eval(session=sess, feed_dict={
x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0
})) saver = tf.train.Saver()
saver.save(sess, './trained_variables.ckpt', global_step=1000) y_labeles = mnist.test.labels
y_pred = []
y_labeles_d1 = [] for i in range(len(y_labeles)):
a = np.argmax(y_labeles[i])
y_pred.append(y_conv.eval(feed_dict={x: np.reshape(mnist.test.images[i], [1, 784]), keep_prob: 0.5}, session=sess)[0][a])
y_labeles_d1.append(correct_prediction.eval(feed_dict={x: np.reshape(mnist.test.images[i], [1, 784]), y_: np.reshape(y_labeles[i], [1, 10]), keep_prob: 0.5}, session=sess)) fpr, tpr, thresholds = sklearn.metrics.roc_curve(y_labeles_d1, y_pred) plt.plot(fpr, tpr, 'b')#生成ROC曲线
plt.legend(loc='lower right')
plt.plot([0, 1], [0, 1], 'r--')
plt.xlim([0, 1])
plt.ylim([0, 1])
plt.ylabel('TP')
plt.xlabel('FP')
plt.show() # with tf.Session() as sess:
# new_saver = tf.train.import_meta_graph('my_test_model-1000.meta')
# new_saver.restore(sess, tf.train.latest_checkpoint('./')) # print(sess.run(W_conv1))

5.效果:

scikit-learn画ROC图的更多相关文章

  1. (原创)(三)机器学习笔记之Scikit Learn的线性回归模型初探

    一.Scikit Learn中使用estimator三部曲 1. 构造estimator 2. 训练模型:fit 3. 利用模型进行预测:predict 二.模型评价 模型训练好后,度量模型拟合效果的 ...

  2. MATLAB画ROC曲线,及计算AUC值

    根据决策值和真实标签画ROC曲线,同时计算AUC的值 步骤: 根据决策值和真实标签画ROC曲线,同时计算AUC的值: 计算算法的决策函数值deci 根据决策函数值deci对真实标签y进行降序排序,得到 ...

  3. 使用Mysql Workbench 画E-R图

    MySQL Workbench 是一款专为MySQL设计的ER/数据库建模工具.你可以用MySQL Workbench设计和创建新的数据库图示,建立数据库文档,以及进行复杂的MySQL 迁移.这里介绍 ...

  4. 用rose画UML图(用例图,活动图)

    用rose画UML图(用例图,活动图) 首先,安装rose2003,电脑从win8升到win10以后,发现win10并不支持rose2003的安装,换了rose2007以后,发现也不可以. 解决途径: ...

  5. python中matplotlib画折线图实例(坐标轴数字、字符串混搭及标题中文显示)

    最近在用python中的matplotlib画折线图,遇到了坐标轴 "数字+刻度" 混合显示.标题中文显示.批量处理等诸多问题.通过学习解决了,来记录下.如有错误或不足之处,望请指 ...

  6. 相机拍的图,电脑上画的图,word里的文字,电脑屏幕,手机屏幕,相机屏幕显示大小一切的一切都搞明白了!

    相机拍的图,电脑上画的图,word里的文字,电脑屏幕,手机屏幕,相机屏幕显示大小一切的一切都搞明白了! 先说图片X×dpi=点数dotX是图片实际尺寸,简单点,我们只算图片的高吧,比如说拍了张图片14 ...

  7. SAS 画折线图PROC GPLOT

    虽然最后做成PPT里的图表会被要求用EXCEL画,但当我们只是在分析的过程中,想看看数据的走势,直接在SAS里画会比EXCEL画便捷的多. 修改起来也会更加的简单,,不用不断的修改程序然后刷新EXCE ...

  8. Windows8.1画热度图 - 坑

    想要的效果 如上是silverlight版本.原理是设定一个调色板,为256的渐变色(存在一个png文件中,宽度为256,高度为1),然后针对要处理的距离矩阵图形,取图片中每个像素的Alpha值作为索 ...

  9. 使用网站websequencediagrams在线画时序图

    在线画时序图的网站:https://www.websequencediagrams.com/ 该网站提供拖拉图形和编写脚本代码2个方式来制作时序图,同时提供多种显示风格. 实例: 1.脚本代码: ti ...

随机推荐

  1. a 便签实现 下载

    如果想通过纯前端技术实现文件下载,直接把a标签的href属性设置为文件路径即可,如下: <a href="https://cdn.shopify.com/s/files/1/1545/ ...

  2. SQL简介及MySQL的安装目录详解

    一,SQL简介 1,数据库定义语言(DDL) ①create:用于创建数据库.表.索引.视图等: ②alter:用于修改数据库.表.索引.视图等: ③drop:用于删除数据库.表.索引.视图.用户等. ...

  3. Ubuntu14.04 + Text-Detection-with-FRCN(CPU)

    操作系统: yt@yt-MS-:~$ cat /etc/issue Ubuntu LTS \n \l Python版本: yt@yt-MS-:~$ python --version Python pi ...

  4. 一步步Cobol 400 上手自学入门教程03 - 数据部

    数据部的作用 程序中涉及到的全部数据(输入.输出.中间)都要在此定义,对它们的属性进行说明.主要描述以下属性: 数据类型(数值/字符)和存储形式(长度) 数据项之间的关系(层次和层号) 文件与记录的关 ...

  5. python 生成器 迭代器

    阅读目录 一 递归和迭代 二 什么是迭代器协议 三 python中强大的for循环机制 四 为何要有for循环 五 生成器初探 六 生成器函数 七 生成器表达式和列表解析 八 生成器总结 一 递归和迭 ...

  6. 独立部署GlusterFS+Heketi实现Kubernetes共享存储

    目录 环境 glusterfs配置 安装 测试 heketi配置 部署 简介 修改heketi配置文件 配置ssh密钥 启动heketi 生产案例 heketi添加glusterfs 添加cluste ...

  7. ASP.NET Core 中使用 Hangfire 定时启动 Scrapyd 爬虫

    用 Scrapy 做好的爬虫使用 Scrapyd 来管理发布启动等工作,每次手动执行也很繁琐;考虑可以使用 Hangfire 集成在 web 工程里. Scrapyd 中启动爬虫的请求如下: curl ...

  8. CDN基本工作过程

    看了一些介绍CDN的文章,感觉这篇是讲的最清楚的. 使用CDN会极大地简化网站的系统维护工作量,网站维护人员只需将网站内容注入CDN的系统,通过CDN部署在各个物理位置的服务器进行全网分发,就可以实现 ...

  9. JDK的windows和Linux版本之下载(图文详解)

    不多说,直接上干货! 简单说下,Eclipse需要Jdk,MyEclipse有自带的Jdk,除非是版本要求 http://www.oracle.com/technetwork/java/javase/ ...

  10. 【转】谷歌三大核心技术(一)The Google File System中文版

      The Google File System中文版 译者:alex 摘要 我们设计并实现了Google GFS文件系统,一个面向大规模数据密集型应用的.可伸缩的分布式文件系统.GFS虽然运行在廉价 ...