使用sklearn 做分类试验(有附图)
来源:互联网 发布:网络销售股票就是聊天 编辑:程序博客网 时间:2024/06/05 19:14
- # -*- coding: utf-8 -*-
- """
- Created on Sun Apr 19 08:57:13 2017
- @author: sishuyong
- """
- print(__doc__)
- import numpy as np
- from scipy import interp
- import matplotlib.pyplot as plt
- from sklearn import svm, datasets
- from sklearn.metrics import roc_curve, auc
- from sklearn.cross_validation import StratifiedKFold
- ###############################################################################
- # Data IO and generation,导入iris数据,做数据准备
- # import some data to play with
- iris = datasets.load_iris()
- X = iris.data
- y = iris.target
- X, y = X[y != 2], y[y != 2]#去掉了label为2,label只能二分,才可以。
- n_samples, n_features = X.shape
- # Add noisy features
- random_state = np.random.RandomState(0)
- X = np.c_[X, random_state.randn(n_samples, 200 * n_features)]
- ###############################################################################
- # Classification and ROC analysis
- #分类,做ROC分析
- # Run classifier with cross-validation and plot ROC curves
- #使用6折交叉验证,并且画ROC曲线
- cv = StratifiedKFold(y, n_folds=6)
- classifier = svm.SVC(kernel='linear', probability=True,
- random_state=random_state)#注意这里,probability=True,需要,不然预测的时候会出现异常。另外rbf核效果更好些。
- mean_tpr = 0.0
- mean_fpr = np.linspace(0, 1, 100)
- all_tpr = []
- for i, (train, test) in enumerate(cv):
- #通过训练数据,使用svm线性核建立模型,并对测试集进行测试,求出预测得分
- probas_ = classifier.fit(X[train], y[train]).predict_proba(X[test])
- # print set(y[train]) #set([0,1]) 即label有两个类别
- # print len(X[train]),len(X[test]) #训练集有84个,测试集有16个
- # print "++",probas_ #predict_proba()函数输出的是测试集在lael各类别上的置信度,
- # #在哪个类别上的置信度高,则分为哪类
- # Compute ROC curve and area the curve
- #通过roc_curve()函数,求出fpr和tpr,以及阈值
- fpr, tpr, thresholds = roc_curve(y[test], probas_[:, 1])
- mean_tpr += interp(mean_fpr, fpr, tpr) #对mean_tpr在mean_fpr处进行插值,通过scipy包调用interp()函数
- mean_tpr[0] = 0.0 #初始处为0
- roc_auc = auc(fpr, tpr)
- #画图,只需要plt.plot(fpr,tpr),变量roc_auc只是记录auc的值,通过auc()函数能计算出来
- plt.plot(fpr, tpr, lw=1, label='ROC fold %d (area = %0.2f)' % (i, roc_auc))
- #画对角线
- plt.plot([0, 1], [0, 1], '--', color=(0.6, 0.6, 0.6), label='Luck')
- mean_tpr /= len(cv) #在mean_fpr100个点,每个点处插值插值多次取平均
- mean_tpr[-1] = 1.0 #坐标最后一个点为(1,1)
- mean_auc = auc(mean_fpr, mean_tpr) #计算平均AUC值
- #画平均ROC曲线
- #print mean_fpr,len(mean_fpr)
- #print mean_tpr
- plt.plot(mean_fpr, mean_tpr, 'k--',
- label='Mean ROC (area = %0.2f)' % mean_auc, lw=2)
- plt.xlim([-0.05, 1.05])
- plt.ylim([-0.05, 1.05])
- plt.xlabel('False Positive Rate')
- plt.ylabel('True Positive Rate')
- plt.title('Receiver operating characteristic example')
- plt.legend(loc="lower right")
- plt.show()
结果如下图所示
阅读全文
0 0
- 使用sklearn 做分类试验(有附图)
- 使用sklearn中svm做多分类时难点解惑
- 用Sklearn做判别分析(分类)
- python sklearn做文本分类
- 使用Sklearn模型做分类并绘制机器学习模型的ROC曲线
- 使用sklearn做特征工程
- 使用sklearn做特征工程
- 使用sklearn做特征工程
- 使用sklearn做各种回归
- 使用sklearn做特征工程
- 基于scikit-learn(sklearn)做分类--2.分类
- 用Python开始机器学习(4:KNN分类算法) sklearn做KNN算法 python
- 关于sklearn的简单使用1(分类)
- 使用sklearn 实现 Logistics Regression 分类
- 基于scikit-learn(sklearn)做分类--1.切词
- 基于scikit-learn(sklearn)做分类--3.优化--保存模型
- 使用sklearn做单机特征工程
- 使用sklearn做单机特征工程
- jQuery min包的制作方法,压缩javascript的方法比对和方案
- AJAX中出现两次请求,OPTIONS请求和GET请求
- 优秀工程师-UIImagePickerController
- HDU-2094产生冠军
- mysql中engine=innodb和engine=myisam的区别
- 使用sklearn 做分类试验(有附图)
- Jmeter功能(一)检查点
- Java使用winrar压缩和解压缩文件
- list of questions
- HDU-6011 Lotus and Characters
- 2017.10.18 微信抽奖活动
- AngularJS路由的简单配置
- 楠叔的Oracle DBA进阶之路-起航
- vue2.0子路由配置和跳转 vue-router: 嵌套路由