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 ...
随机推荐
- BZOJ1652 [Usaco2006 Feb]Treats for the Cows
蒟蒻许久没做题了,然后连动规方程都写不出了. 参照iwtwiioi大神,这样表示区间貌似更方便. 令f[i, j]表示i到j还没卖出去,则 f[i, j] = max(f[i + 1, j] + v[ ...
- win7下android开发环境搭建(win7 64位)
win7下android开发环境搭建(win7 64位) 一.安装 JDK 下载JDK最新版本,下载地址如下: http://www.oracle.com/technetwork/java/jav ...
- Python Django 之 Template 模板语言简介
一.什么事模板语言 html+逻辑控制语句 二.模板语言的作用 帮助前端处理后端发来的数据,方便前端展示(杂糅渲染) 三.模板语言语法 1.{{变量}} 变量使用双大括号{{}} 2.万能的句点号. ...
- sql server中的go
1. 作用:向 SQL Server 实用工具发出一批 Transact-SQL 语句结束的信号.2. 语法:一批 Transact-SQL 语句GO如Select 1Select 2Select 3 ...
- SharePoint 2013的100个新功能之网站管理(一)
一:设置盘 网站操作现在被替换为新的(设置)盘子.一些新的操作像添加一个应用.添加一个页面或设计管理器被添加而像创建网站则从菜单中移除了. 二:移除以其他用户身份登录 在SharePoint 2013 ...
- oracle获取执行计划及优缺点 详解
一.获取执行计划的6种方法(详细步骤已经在每个例子的开头注释部分说明了):1. explain plan for获取: 2. set autotrace on : 3. statistics_leve ...
- mysql 的 java 连接库
mysql 的 java 连接库 解压缩mysql-connector-java-5.1.30.zip 将要使用的是mysql-connector-java-5.1.30-bin-g.jar和mysq ...
- ARM_Instruction_Set_Encoding_hacking(ARM指令集编码格式解读)
ARM指令集编码格式解读 说明: 1.本文参考的书籍<ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition>中的Cha ...
- 【c++基础】accumulate
accumulate // accumulate example #include <iostream> // std::cout #include <functional> ...
- HDU 2094:产生冠军(拓扑排序)
产生冠军 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...