#encoding:utf-8
import pandas as pd
import numpy as np
from sklearn import datasets,linear_model
from sklearn.metrics import roc_curve,auc
import pylab as pl
from matplotlib.pyplot import plot def confusionMatrix(predicted,actual,threshold):
if len(predicted)!=len(actual):return -1
tp = 0.0
fp = 0.0
tn = 0.0
fn = 0.0
for i in range(len(actual)):
if actual[i]>0.5:
if predicted[i]>threshold:
tp += 1.0
else:
fn += 1.0
else:
if predicted[i]<threshold:
tn += 1.0
else:
fp += 1.0
rtn = [fp,fn,fp,tn]
return rtn
#获取数据
rockdata = open('sonar.all-data')
xList = []
labels = []
#将标签转换成数值,M转换成1.0,R转换为0.0
for line in rockdata:
row = line.strip().split(",")
if(row[-1] =='M'):
labels.append(1.0)
else:
labels.append(0.0)
row.pop()
floatRow = [float(num) for num in row]
xList.append(floatRow)
print labels
#获取数据的行数,通过对3的求余,将数据划分为2个子集,1/3的测试集,2/3的训练集
indices = range(len(xList))
xListTest = [xList[i] for i in indices if i%3==0]
xListTrain = [xList[i] for i in indices if i%3!=0]
labelsTest = [labels[i] for i in indices if i%3==0]
labelsTrain = [labels[i] for i in indices if i%3!=0]
#将列表转换成数组
xTrain = np.array(xListTrain)
yTrain = np.array(labelsTrain)
xTest = np.array(xListTest)
yTest = np.array(labelsTest)
#预测模型
rocksVMinesModel = linear_model.LinearRegression()
#训练数据
rocksVMinesModel.fit(xTrain,yTrain)
# 预测训练数据
trainingPredictions = rocksVMinesModel.predict(xTrain)
print ("---------",trainingPredictions[0:5],trainingPredictions[-6:-1])
#生成训练数据的混淆矩阵
confusionMatTrain = confusionMatrix(trainingPredictions,yTrain,0.5)
print confusionMatTrain
#预测测试数据
testPredictions = rocksVMinesModel.predict(xTest)
#生成测试数据的混淆矩阵
confusionTest = confusionMatrix(testPredictions,yTest,0.5)
print confusionTest
#通过roc_curve函数计算fpt,tpr,并计算roc_auc,AUC越高代表越好
fpr,tpr,thresholds = roc_curve(yTrain,trainingPredictions)
roc_auc = auc(fpr,tpr)
print roc_auc
#生成训练集上的ROC曲线
#plot roc curve
pl.clf()#清楚图形,初始化图形的时候需要
pl.plot(fpr,tpr,label='ROC curve (area=%0.2f)' %roc_auc)#画ROC曲线
pl.plot([0,1],[0,1],'k-')#生成对角线
pl.xlim([0.0,1.0])#X轴范围
pl.ylim([0.0,1.0])#Y轴范围
pl.xlabel('False Positive Rate')#X轴标签显示
pl.ylabel('True Positive Rate')#Y轴标签显示
pl.title('In sample ROC rocks versus mines')#标题
pl.legend(loc="lower left")#图例位置
pl.show() #生成测试集上的ROC曲线
fpr,tpr,thresholds = roc_curve(yTest,testPredictions)
roc_auc = auc(fpr,tpr)
print roc_auc
#plot roc curve
pl.clf()
pl.plot(fpr,tpr,label='ROC curve (area=%0.2f)' %roc_auc)
pl.plot([0,1],[0,1],'k-')
pl.xlim([0.0,1.0])
pl.ylim([0.0,1.0])
pl.xlabel('False Positive Rate')
pl.ylabel('True Positive Rate')
pl.title('In sample ROC rocks versus mines')
pl.legend(loc="lower right")
pl.show() 训练集上的ROC曲线

测试集上的ROC曲线

												

最新文章

  1. 反编译ILSpy 无法显式调用运算符或访问器 错误处理方法 转
  2. 35. Binary Tree Level Order Traversal &amp;&amp; Binary Tree Level Order Traversal II
  3. SQL Server:在事务中回滚TRUNCATE操作
  4. Android中visibility属性VISIBLE、INVISIBLE、GONE的区别
  5. ncs安装及初次运行
  6. Android开发环境下关于如何导出手机通讯录数据库【Written By KillerLegend】
  7. JavaScript库开发者们的规则
  8. Git使用之基于SSH的Gitserver的client配置(下篇)
  9. REST总结
  10. 2017-02-23 switch case 循环语句
  11. Django的form表单之文件上传
  12. linux下一键安装redis并设置为后台进程及开机启动
  13. IOS UITextView自适应高度
  14. idea中@Data标签getset不起作用
  15. 【PLM】【PDM】60页PPT终于说清了PDM和PLM的区别;智造时代,PLM系统10大应用趋势!
  16. webpack4 系列教程(十五):开发模式与webpack-dev-server
  17. 处理器 趣事 CPU/GPU/TPU/DPU/BPU
  18. 【转】java面试题
  19. matplotlib 刻度,坐标轴不可见
  20. PAT 1038 统计同成绩学生(20)(代码)

热门文章

  1. web 汇率
  2. macosx下apache的默认用户为daemon
  3. 【C语言】求旋转数组的最小数字,输入一个递增排序的数组的一个旋转,输出其最小元素
  4. img与特殊布局下对浏览器渲染的剖析
  5. LAMP环境如何配置多个域名访问
  6. AWS系列-EC2实例选择镜像
  7. 下载Qt安装包
  8. vue+webpack2实现路由的懒加载
  9. CentOS7 minimal下MySQL安装
  10. Win32控制台中使用定时器的方法