1、损失函数和风险函数

(1)损失函数:常见的有 0-1损失函数  绝对损失函数  平方损失函数  对数损失函数

(2)风险函数:损失函数的期望      经验风险:模型在数据集T上的平均损失

  根据大数定律,当N趋向于∞时,经验风险趋向于风险函数

2、模型评估方法

(1)训练误差与测试误差

  训练误差:关于训练集的平均损失

  测试误差:定义模型关于测试集的平均损失。其反映了学习方法对未知测试数据集的预测能力

(2)泛化误差:学到的模型对未知数据的预测能力。其越小,该模型越有效。泛化误差定义为所学习模型的期望风险

(3)过拟合:对已知数据预测得很好,对未知数据预测得很差的现象。原因是将训练样本本身的一些特点当做了所有潜在样本都具有的一般性质,这会造成泛化能力的下降。常用的防止过拟合的办法为正则化。正则化是基于结构化风险最小化策略的实现。

3、模型评估

(1)留出法:直接将数据划分为三个互斥的部分,然后在训练集上训练模型,在验证集上选择模型,最后用测试集上的误差作为泛化误差的估计。

(2)交叉验证法(S折交叉验证法):数据随机划分为S个互不相交且大小相同的子集,利用S-1个子集数据训练模型,利用余下的一个子集测试模型。对S种组合依次重复进行,获取测试误差的均值。

(3)留一法:留出一个样例作为测试集。其缺点就是当数据集比较大时计算量太大

(4)自助法:先从T中随机取出一个样本放入采样集TS中,再把该样本放回T中。经过N次随机采样操作,得到包含N个样本的采样集TS。将TS用作训练集,T-TS用过测试集。

4、性能度量

(1)测试准确率和测试错误率

(2)混淆矩阵

  查准率:P=TP/(TP+FP)  ,即所有预测为正类的结果中,真正的正类的比例

  查全率:R=TP/(TP+FN),即正真的正类中,被分类器找出来的比例

  不同的问题中,判别标准不同。对于推荐系统,更侧重于查准率(即推荐的结果中,用户真正感兴趣的比例);对于医学诊断系统,更侧重于查全率(即疾病被发现的比例)

  2/F1=1/P+1/R

5、ROC曲线

  真正例率:TPR=TP/(TP+FN)

  假正例率:FPR=FP/(TN+FP),刻画的是分类器错认为正类的负实例占所有负实例的比例

  以真正例率为纵轴、假正例率为横轴作图,就得到ROC曲线。在ROC图中,对角线对应于随机猜想模型。点(0,1)对应于理想模型。通常ROC曲线越靠近点(0,1)越好。

6、偏差方差分解

代码如下:

 from sklearn.metrics import zero_one_loss,log_loss
from sklearn.model_selection import train_test_split,KFold,StratifiedKFold,LeaveOneOut,cross_val_score
from sklearn.datasets import load_digits,load_iris
from sklearn.svm import LinearSVC,SVC
from sklearn.metrics import accuracy_score,precision_score,recall_score,f1_score,classification_report
from sklearn.metrics import confusion_matrix,precision_recall_curve,roc_curve
from sklearn.metrics import mean_absolute_error,mean_squared_error,classification_report
from sklearn.multiclass import OneVsRestClassifier
from sklearn.model_selection import validation_curve,learning_curve,GridSearchCV,RandomizedSearchCV
import matplotlib.pyplot as plt
from sklearn.preprocessing import label_binarize
from sklearn.linear_model import LogisticRegression
import numpy as np
#zero_one_loss
# y_true=[1,1,1,1,1,0,0,0,0,0]
# y_pred=[0,0,0,1,1,1,1,1,0,0]
# print("zero_one_loss<fraction>:",zero_one_loss(y_true,y_pred,normalize=True))
# print("zero_one_loss<num>:",zero_one_loss(y_true,y_pred,normalize=False)) #log_loss
# y_true=[1,1,1,0,0,0]
# y_pred=[[0.1,0.9],
# [0.2,0.8],
# [0.3,0.7],
# [0.7,0.3],
# [0.8,0.2],
# [0.9,0.1]
# ]
# print("log_loss<average>:",log_loss(y_true,y_pred,normalize=True))
# print("log_loss<total>:",log_loss(y_true,y_pred,normalize=False)) #train_test_split
# X=[
# [1,2,3,4],
# [11,12,13,14],
# [21,22,23,24],
# [31,32,33,34],
# [41,42,43,44],
# [51,52,53,54],
# [61,62,63,64],
# [71,72,73,74]
# ]
# Y=[1,1,0,0,1,1,0,0]
# X_train,X_test,Y_train,Y_test=train_test_split(X,Y,test_size=0.4,random_state=0)
# print("X_train=",X_train)
# print("X_test=",X_test)
# print("Y_train=",Y_train)
# print("Y_test=",Y_test)
# X_train,X_test,Y_train,Y_test=train_test_split(X,Y,test_size=0.4,random_state=0,stratify=Y)
# print("X_train=",X_train)
# print("X_test=",X_test)
# print("Y_train=",Y_train)
# print("Y_test=",Y_test) #KFold
# X=np.array([
# [1,2,3,4],
# [11,12,13,14],
# [21,22,23,24],
# [31,32,33,34],
# [41,42,43,44],
# [51,52,53,54],
# [61,62,63,64],
# [71,72,73,74],
# [81,82,83,84]
# ])
# Y=np.array([1,1,0,0,1,1,0,0,1])
#
# folder=KFold(n_splits=3,random_state=0,shuffle=False)
# for train_index,test_index in folder.split(X,Y):
# print("Train Index:",train_index)
# print("Test Index:",test_index)
# print("X_train:",X[train_index])
# print("X_test:",X[test_index])
# print("")
#
# shuffle_folder=KFold(n_splits=3,random_state=0,shuffle=True)
# for train_index,test_index in shuffle_folder.split(X,Y):
# print("Train Index:",train_index)
# print("Test Index:",test_index)
# print("X_train:",X[train_index])
# print("X_test:",X[test_index])
# print("") #StratifiedKFold
# stratified_folder=StratifiedKFold(n_splits=4,random_state=0,shuffle=False)
#as the operation is similar to the above,pass #LeaveOneOut,too easy,pass
# loo=LeaveOneOut(len(Y)) #cross_val_score
# digits=load_digits()
# X=digits.data
# Y=digits.target
#
# result=cross_val_score(LinearSVC(),X,Y,cv=10)
# print("Cross Val Score is:",result) #accuracy_score,pass
# accuracy_score(y_true,y_pred,normalize=True/False) #precision_score,pass
# precision_socre(y_true,y_pred) #recall_score,pass
# recall_score(y_true,y_pred) #f1_score,pass
# f1_score(y_true,y_pred) #fbeta_score,pass
# fbeta_score(y_true,y_pred,beta=num_beta) #classification_report
# y_true=[1,1,1,1,1,0,0,0,0,0]
# y_pred=[0,0,1,1,0,0,0,0,0,0]
# print("Classification Report:\n",classification_report(y_true,y_pred,target_names=["class_0","class_1"])) #confusion_matrix,pass
# confusion_matrix(y_true,y_pred,labels=[0,1]) #precision_recall_curve
# iris=load_iris()
# X=iris.data
# Y=iris.target
# #print(X,'\n',Y)
# Y=label_binarize(Y,classes=[0,1,2])
# n_classes=Y.shape[1]
# # print(n_classes,'\n',Y)
# np.random.seed(0)
# n_samples,n_features=X.shape
# # print(n_samples,'\n',n_features)
# X=np.c_[X,np.random.randn(n_samples,200*n_features)]
# # n_samples,n_features=X.shape
# # print(n_samples,'\n',n_features)
# x_train,x_test,y_train,y_test=train_test_split(X,Y,test_size=0.5,random_state=0)
# clf=OneVsRestClassifier(SVC(kernel='linear',probability=True,random_state=0))
# clf.fit(x_train,y_train)
# y_score=clf.fit(x_train,y_train).decision_function(x_test)
# # print(y_score)
# fig=plt.figure()
# ax=fig.add_subplot(1,1,1)
# precision=dict()
# recall=dict()
# for i in range(n_classes):
# precision[i],recall[i],_=precision_recall_curve(y_test[:,i],y_score[:,i])
# ax.plot(recall[i],precision[i],label="target=%s"%i)
# ax.set_xlabel("Recall Score")
# ax.set_ylabel("Precision Score")
# ax.set_title("P-R")
# ax.legend(loc="best")
# ax.set_xlim(0,1.1)
# ax.set_ylim(0,1.1)
# ax.grid()
# plt.show() #roc_curve,roc_auc_score,pass
# roc_curve(y_test,y_score) #mean_absolute_error,pass
# mean_absolute_error(y_true,y_pred) #mean_squared_error,pass
# mean_squared_error(y_true,y_pred) #validation_curve,pass
# validation_curve(LinearSVC(),X,Y,param_name="C",param_range=np.logspace(-2,2),cv=10,scoring="accuracy") #learning_curve,pass
# train_size=np.linspace(0.1,1.0,endpoint=True,dtype='float')
# learning_curve(LinearSVC(),X,Y,cv=10,scoring="accuracy",train_sizes=train_size) #GridSearcgCV
# digits=load_digits()
# x_train,x_test,y_train,y_test=train_test_split(digits.data,digits.target,test_size=0.25,random_state=0,stratify=digits.target)
# tuned_parameters=[{'penalty':['l1','l2'],'C':[0.01,0.05,0.1,0.5,1,5,10,50,100],'solver':['liblinear'],'multi_class':['ovr']},
# {'penalty':['l2'],'C':[0.01,0.05,0.1,0.5,1,5,10,50,100],'solver':['lbfgs'],'multi_class':['ovr','multinomial']},
# ]
# clf=GridSearchCV(LogisticRegression(tol=1e-6),tuned_parameters,cv=10)
# clf.fit(x_train,y_train)
# print("Best parameters set found:",clf.best_params_)
# print("Grid scores:")
# for params,mean_score,scores in clf.grid_scores_:
# print("\t%0.3f(+/-%0.03f) for %s"%(mean_score,scores.std()*2,params))
#
# print("Optimized Score:",clf.score(x_test,y_test))
# print("Detailed classification report:")
# y_true,y_pred=y_test,clf.predict(x_test)
# print(classification_report(y_true,y_pred)) #RandomizedSearchCV
# RandomizedSearchCV(LogisticRegression(penalty='l2',solver='lbfgs',tol=1e-6,tuned_parameters,cv=10,scoring='accuracy',n_iter=100))

python大战机器学习——模型评估、选择与验证的更多相关文章

  1. python大战机器学习——支持向量机

    支持向量机(Support Vector Machine,SVM)的基本模型是定义在特征空间上间隔最大的线性分类器.它是一种二类分类模型,当采用了核技巧之后,支持向量机可以用于非线性分类. 1)线性可 ...

  2. python大战机器学习——数据降维

    注:因为公式敲起来太麻烦,因此本文中的公式没有呈现出来,想要知道具体的计算公式,请参考原书中内容 降维就是指采用某种映射方法,将原高维空间中的数据点映射到低维度的空间中 1.主成分分析(PCA) 将n ...

  3. Python大战机器学习——基础知识+前两章内容

    一  矩阵求导 复杂矩阵问题求导方法:可以从小到大,从scalar到vector再到matrix. x is a column vector, A is a matrix d(A∗x)/dx=A d( ...

  4. python大战机器学习——集成学习

    集成学习是通过构建并结合多个学习器来完成学习任务.其工作流程为: 1)先产生一组“个体学习器”.在分类问题中,个体学习器也称为基类分类器 2)再使用某种策略将它们结合起来. 通常使用一种或者多种已有的 ...

  5. python大战机器学习——聚类和EM算法

    注:本文中涉及到的公式一律省略(公式不好敲出来),若想了解公式的具体实现,请参考原著. 1.基本概念 (1)聚类的思想: 将数据集划分为若干个不想交的子集(称为一个簇cluster),每个簇潜在地对应 ...

  6. python大战机器学习——数据预处理

    数据预处理的常用流程: 1)去除唯一属性 2)处理缺失值 3)属性编码 4)数据标准化.正则化 5)特征选择 6)主成分分析 1.去除唯一属性 如id属性,是唯一属性,直接去除就好 2.处理缺失值 ( ...

  7. python大战机器学习——半监督学习

    半监督学习:综合利用有类标的数据和没有类标的数据,来生成合适的分类函数.它是一类可以自动地利用未标记的数据来提升学习性能的算法 1.生成式半监督学习 优点:方法简单,容易实现.通常在有标记数据极少时, ...

  8. python大战机器学习——人工神经网络

    人工神经网络是有一系列简单的单元相互紧密联系构成的,每个单元有一定数量的实数输入和唯一的实数输出.神经网络的一个重要的用途就是接受和处理传感器产生的复杂的输入并进行自适应性的学习,是一种模式匹配算法, ...

  9. python进行机器学习(四)之模型验证与参数选择

    一.模型验证 进行模型验证的一个重要目的是要选出一个最合适的模型,对于监督学习而言,我们希望模型对于未知数据的泛化能力强,所以就需要模型验证这一过程来体现不同的模型对于未知数据的表现效果. 这里我们将 ...

随机推荐

  1. [BZOJ1396&2865]识别子串

    bzoj1396 bzoj2865 dbzoj1396 dbzoj2865 题面 XX在进行字符串研究的时候,遇到了一个十分棘手的问题. 在这个问题中,给定一个字符串\(S\),与一个整数\(K\), ...

  2. Swift错误处理

    相对于可选中运用值的存在与缺失来表达函数的成功与失败,错误处理可以推断失败的原因,并传播至程序的其他部分. throws关键词 一个函数可以通过在声明中添加throws关键词来抛出错误消息. func ...

  3. MySQL on Azure高可用性设计 DRBD - Corosync - Pacemaker - CRM (二)

    在上一篇文章中描述了MySQL HA on Azured 设计思路,本篇文章中将描述具体的部署,每个组件的安装和配置. 整体的设计架构如下: 下面将是所有组件的安装配置过程,所有的虚拟机是CentOS ...

  4. SVN使用技巧和参考文档总结

    以下文章为网上收集: myEclipse 8.5下SVN环境的搭建(重点推荐) SVN建立版本库,配置用户和权限 Tortoise SVN使用方法,简易图解 版本控制软件SVN使用方法详解 学习笔记 ...

  5. 写一个c程序辨别系统是16位or32位

    方法: 32位处理器就是一次只能处理32位,也就是4个字节的数据,虚拟地址空间的最大大小是4G,而64位处理一次就能处理64位,即8个字节的数据,最大虚拟地址空间的最大大小是16T.最明显的是指针大小 ...

  6. ViewPage+Fragment(仿微信切换带通知)

    第一步 : 布局文件 activity_main.xml <?xml version="1.0" encoding="utf-8"?> <Li ...

  7. JAVA相关资料

    http://ifeve.com/java-multi-threading-concurrency-interview-questions-with-answers/ http://www.cnblo ...

  8. 17. CTF综合靶机渗透(十)

    靶机描述:欢迎来到超级马里奥主机!这个虚拟机是对真实世界场景场景的模拟.目标是在VM中找到2个标志.根是不够的(对不起!)VM可以以多种方式开发,但请记住枚举是关键.挑战的程度是中等的.感谢VDBAN ...

  9. Go语言学习记录之一(返回指针与返回值的区别)

    先来一个返回指针的测试,结果跟想象一样 type A map[int]string type B struct { A c int } func main() { b := B{make(A), 10 ...

  10. 拦截导弹 (NYOJ—79) 最长字串问题 (NYOJ—17)

    这是到动态规划的题目,属于有顺序的0 1 背包问题: 代码: #include<stdio.h> #include<string.h> ][]; //d[i][j] ]; in ...