Decision_function:scores,predict以及其他
机器学习的评估
PR曲线用于positive类数据占比比较小,或者你更加在意false postion(相比于false negative);其他情况采用ROC曲线;比如Demo中手写体5的判断,因为只有少量5,所以从ROC上面来看分类效果不错,但是从PR曲线可以看到分类器效果不佳。
y_scores = sgd_clf.decision_function([some_digit])
decision_function代表的是参数实例到各个类所代表的超平面的距离;在梯度下滑里面特有的(随机森林里面没有decision_function),这个返回的距离,或者说是分值;后续的对于这个值的利用方式是指定阈值来进行过滤:
>>> y_scores = sgd_clf.decision_function([some_digit])
>>> y_scores
array([ 161855.74572176])
>>> threshold = 0
>>> y_some_digit_pred = (y_scores > threshold)
array([ True], dtype=bool)
>>> threshold = 200000
>>> y_some_digit_pred = (y_scores > threshold)
>>> y_some_digit_pred
array([False], dtype=bool)
通过上面例子看到了,通过decision_function可以获得一种"分值",这个分值的几何意义就是当前点到超平面(hyperplane)的距离;然后,你可以利用这个分值来和某个阈值做比较(距离的阈值),超过阈值则通过,低于阈值则不通过。再举一个例子:
>>> sgd_clf.fit(X_train, y_train) # y_train, not y_train_5
>>> sgd_clf.predict([some_digit])
array([ 5.])
some_digit_scores=sdg_clf.decision_function([some_digit])
some_digit_scores
array([[-311402.62954431, -363517.28355739, -446449.5306454 ,
-183226.61023518, -414337.15339485, 161855.74572176,
-452576.39616343, -471957.14962573, -518542.33997148,
-536774.63961222]])
sgd_clf.fit(X_train, y_train)这个梯度下降算法学习的对象是说有手写训练样本以及0-9的分类标签,基于学习的模型调用decision_function之后,获取是[some_digit]所有的标签到超平面的距离,其中只有5是正值,所以如果调用predict的话返回的就是5。但是,如果我们训练的分类器是二元分类器(True,false),那么情况又不同:
y_train_5 =(y_train==5)
>>> sgd_clf.fit(X_train,y_train_5) # y_train, not y_train_5
>>> sgd_clf.predict([some_digit])
array([ True])
因为y_train_5这个标签集合只有True和False两种标签,所以训练之后的模型预测的知识True和false;所以到底是二元分类还是多元分类完全取决于训练的时候的标签集。
predict:用于分类模型的预测分类;
fit:对于线性回归的模型学习,称之为"拟合";
y_probas_forest = cross_val_predict(forest_clf, X_train, y_train_5, cv=3, method="predict_proba")
cross_val_predict是交叉获取分类概率(注意,这里的method参数设置为"predict_proba",代表返回值返回的是预期分类的概率)
参考:
http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html
?这有一个问题其实每太搞懂,就是scores和predict的关系到底什么,cross_val_score的机制和cross_val_predit之间的差别是什么,文中代码如下:
from sklearn.ensemble import RandomForestClassifier
forest_clf = RandomForestClassifier(random_state=42)
y_probas_forest = cross_val_predict(forest_clf, X_train, y_train_5, cv=3,
method="predict_proba")
But to plot a ROC curve, you need scores, not probabilities. A simple solution is to
use the positive class's probability as the score:
y_scores_forest = y_probas_forest[:, 1] # score = proba of positive class
fpr_forest, tpr_forest, thresholds_forest = roc_curve(y_train_5,y_scores_forest)
不过这里的代码可以看出一些端倪:
from sklearn.ensemble import RandomForestClassifier
forest_clf=RandomForestClassifier(random_state=42)
y_probas_forest=cross_val_predict(forest_clf, X_train, y_train_5, cv=3, method="predict_proba")
print y_probas_forest
[[0.9 0.1] [1. 0. ] [1. 0. ] ... [1. 0. ] [0.9 0.1] [1. 0. ]]
y_scores_forest=y_probas_forest[:, 1]
print y_scores_forest
[0.1 0. 0. ... 0. 0.1 0. ]
你可以看到,scores是probas的二维数组的第二维的值。那么问题来了,作为cross_val_predict里面的数据,二维数组中这个二维到底是什么?这个二维数组其实代表的是各个分类的概率,对于二分类而言,就是为negative的概率以及position概率;对于scores其实就是为position的分类信息。那就意味着如果N个分类(classification),那么就是N维数组了。
另外对于森林分类器里面有一个method的参数,例子中值是"predict_proba",这个代表着预测各个分类的概率;他还有很多其他选项:
predict:代表的是预测的分类,就是会挑选概率最大的分类返回;
predict_log_proba:算法和predic_proba是一样的,但是最后对于结果会取对数运算,目的是放大值,避免在概率的相乘中会产生一些极小值,然后会因为舍入问题导致误差;另外一些机器算法(比如散度KL)本身就是基于对数运算的。最后,贝叶斯的分类算法需要通过对数运算(log)来实现稳定性;
对于cv=3,代表采用三折交叉验证,即将数据随机分为三份(或者尽量保持数据的均匀分布性),每次拿其中的一份来做测试集(另外两份做训练集),然后将三次的结果(每个测试样本各个分类的概率)做一下平均值;
参考
https://www.reddit.com/r/MLQuestions/comments/5lzv9o/sklearn_why_predict_log_proba/
https://baike.baidu.com/item/%E5%AF%B9%E6%95%B0%E5%85%AC%E5%BC%8F
Decision_function:scores,predict以及其他的更多相关文章
- [LeetCode] Predict the Winner 预测赢家
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...
- [Swift]LeetCode486. 预测赢家 | Predict the Winner
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...
- Predict the Winner LT486
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...
- Minimax-486. Predict the Winner
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...
- 动态规划-Predict the Winner
2018-04-22 19:19:47 问题描述: Given an array of scores that are non-negative integers. Player 1 picks on ...
- 486. Predict the Winner
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...
- LeetCode Predict the Winner
原题链接在这里:https://leetcode.com/problems/predict-the-winner/description/ 题目: Given an array of scores t ...
- LN : leetcode 486 Predict the Winner
lc 486 Predict the Winner 486 Predict the Winner Given an array of scores that are non-negative inte ...
- [LeetCode] 486. Predict the Winner 预测赢家
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...
- LC 486. Predict the Winner
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...
随机推荐
- Eclipse偏好设置的导入与导出
偏好导入:File--->Import
- 基于PU-Learning的恶意URL检测——半监督学习的思路来进行正例和无标记样本学习
PU learning问题描述 给定一个正例文档集合P和一个无标注文档集U(混合文档集),在无标注文档集中同时含有正例文档和反例文档.通过使用P和U建立一个分类器能够辨别U或测试集中的正例文档 [即想 ...
- JavaScript学习总结(二)——逻辑Not运算符详解
在JavaScript 中,逻辑NOT运算符与C和Java中的逻辑 NOT 运算符相同,都由感叹号(!)表示.与逻辑 OR 和逻辑 AND 运算符不同的是,逻辑 NOT 运算符返回的一定是 Boole ...
- 微信小程序跨页面获取数据示例
index.wxml <navigator class="navs" url="{{urls}}"> 中国 </navigator> i ...
- node.js express 4.x 安装指南 (找了很久呀,痛苦之路)
问题出现这,版本的问题,还有express4.0以上的版本启动node app.js指令的变更,蛋疼.参考这个
- SQL Server 调优系列进阶篇 - 查询语句运行几个指标值监测
前言 上一篇我们分析了查询优化器的工作方式,其中包括:查询优化器的详细运行步骤.筛选条件分析.索引项优化等信息. 本篇我们分析在我们运行的过程中几个关键指标值的检测. 通过这些指标值来分析语句的运行问 ...
- Android JNI作用及其详解
Android JNI作用及其详解 Java Native Interface (JNI)标准是Java平台的一部分,它允许Java代码和其他语言写的代码进行交互.JNI 是本地编程接口,它使得在 J ...
- Oracle11g 表空间使用监控
SELECT * FROM (select * from (select a.TABLESPACE_NAME TABLESPACE_NAME,(a.total || 'M') AS "SUM ...
- POJ 1007 DNA sorting (关于字符串和排序的水题)
#include<iostream>//写字符串的题目可以用这种方式:str[i][j] &str[i] using namespace std; int main() {int ...
- python面试知识总结
1. 先做自我介绍 2. 做Python几年了?为什么选择Python?3. 学历?大学什么专业?4. 除了Python以外对其他语言有没有了解?5. 你对Python这门语言的看法?6. 在学习Py ...