优达(Udacity)-机器学习基础-评估指标

来源:互联网 发布:防蓝光夹片 知乎 编辑:程序博客网 时间:2024/06/01 15:49

评估指标

  • 由要解决的问题选择性能指标,然后测试模型表现。

准确度:某特定类别中我们正确标记并正确识别为此类别的项目或数据点的数量,除以该类别中全部项目或数据点的数量。

  • 准确度的缺陷

准确度会在出现偏斜类时出现问题
有时你宁可猜测某人是无辜的以免被误关进监狱
有时你宁可假设某人是有罪的再进行排除

混淆矩阵

这里写图片描述

左边矩阵上方是实际分类,左方是预测的分类。

  • 决策树混淆矩阵

这里写图片描述

  • 召回率(Recall)

    这个人是Hugo的情况下,我们的算法准确识别Hugo的概率

  • 精确率(Precision)

    假设我们的算法检测到了Hugo,那么此人确实是Hugo的概率

请反复诵读上面两个概念!!!非常重要!!!

这里写图片描述

用另一个人来做例子

这里写图片描述

  • 特征脸方法

True Positive
False Positive
False Negitive

  • 注意:

Flase Negitive 即:假负例
这里的意思是:当我们预测他不是Colin,但实际上他是Colin的个数!!!
所以放在示例里,应该是label为真,而预测为假的个数!!
(这里在后面的迷你项目中很有迷惑性!)

这里写图片描述

  • 召回率的计算公式

这里写图片描述

  • 精确率的计算公式

这里写图片描述

sklearn函数链接:http://blog.csdn.net/zhning12L/article/details/78644699

F1分数

  • F1 分数会同时考虑精确率和召回率,以便计算新的分数。
  • 可将 F1 分数理解为精确率和召回率的加权平均值,其中 F1 分数的最佳值为 1、最差值为 0:

F1 = 2 * (精确率 * 召回率) / (精确率 + 召回率)

  • sklearn代码:
sklearn.metrics.f1_score(y_true, y_pred, labels=None, pos_label=1, average=’binary’, sample_weight=None)

sklearn.metrics.f1_score

回归指标

平均绝对误差

计算方法是,将各个样本的绝对误差汇总,然后根据数据点数量求出平均误差。通过将模型的所有绝对值加起来,可以避免因预测值比真实值过高或过低而抵销误差,并能获得用于评估模型的整体误差指标。

  • sklearn代码:
sklearn.metrics.mean_absolute_error(y_true, y_pred, sample_weight=None, multioutput=’uniform_average’)

均方误差

与绝对误差相比,残差(预测值与真实值的差值)被求平方。

对残差求平方的一些好处是,自动将所有误差转换为正数、注重较大的误差而不是较小的误差以及在微积分中是可微的(可让我们找到最小值和最大值)。

  • sklearn代码:
sklearn.metrics.mean_squared_error(y_true, y_pred, sample_weight=None, multioutput=’uniform_average’)

回归分数函数

  • 除了误差指标之外,scikit-learn还包括了两个分数指标,范围通常从0到1,值0为坏,而值1为最好的表现,看起来和分类指标类似,都是数字越接近1.0分数就越好。

  • 其中之一是R2分数

,用来计算真值预测的可决系数。在 scikit-learn 里,这也是回归学习器默认的分数方法。

  • 另一个是可释方差分数

要记住的重点是,回归的默认指标是“分数越高越好

越高的分数表明越好的表现。而当我们用到前面讲的误差指标时,我们要改变这个设定。

R2分数:(sklearn.metrics.r2_score)

sklearn.metrics.r2_score(y_true, y_pred, sample_weight=None, multioutput=’uniform_average’)[source]

可释方差分数:(sklearn.metrics.explained_variance_score)

sklearn.metrics.explained_variance_score(y_true, y_pred, sample_weight=None, multioutput=’uniform_average’)

评估指标迷你项目

#!/usr/bin/python# -*- coding: UTF-8 -*-"""    Starter code for the evaluation mini-project.    Start by copying your trained/tested POI identifier from    that which you built in the validation mini-project.    This is the second step toward building your POI identifier!    Start by loading/formatting the data..."""import pickleimport syssys.path.append("../tools/")from feature_format import featureFormat, targetFeatureSplitdata_dict = pickle.load(open("../final_project/final_project_dataset.pkl", "r") )### add more features to features_list!features_list = ["poi", "salary"]data = featureFormat(data_dict, features_list)labels, features = targetFeatureSplit(data)### your code goes here from sklearn import treeclf = tree.DecisionTreeClassifier()clf = clf.fit (features , labels)pre = clf.predict(features)from sklearn.metrics import accuracy_scoreacc = accuracy_score(labels,pre)print "all:",acc#使用sklearn.cross_validation 中的 train_test_split 验证# 将 30% 的数据用于测试,并设置 random_state 参数为 42#求更新后的准确率from sklearn.model_selection import train_test_splitfeatures_train, features_test, labels_train, labels_test = train_test_split(features, labels, test_size=0.3,random_state=42)clf2 = clf.fit(features_train, labels_train)pre2 = clf2.predict(features_test)acc2 = accuracy_score(labels_test,pre2)print "30%:",acc2print "pre2:",pre2print "data_test members:",len(pre2)# 如果测试集中所有人的识别符都被预测为 0(非 POI),其准确率会是多少?import numpy as nppre3 = np.zeros(len(features_test))acc3 = accuracy_score(labels_test,pre3)print "acc3:",acc3# True Positive 数量counter = 0for index,value in enumerate(pre2):    if value == labels[index] == 1:        counter ==1print "The number of True Positive:",counter#精确率是多少from sklearn.metrics import precision_scoreprecision = precision_score(labels_test,pre2)print precision#召回率from sklearn.metrics import recall_scorerecall = recall_score(labels_test,pre2)print recall#有多少 True Positive?(TN,FP,FN)predict = [0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1]real_label = [0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0]tp,tn,fp,fn=0,0,0,0for index,value in enumerate(real_label):    if value == 1:        if predict[index] == 1:            tp += 1        else:            fn +=1    else:        if predict[index] == 1:            fp += 1        else:            tn += 1print "True Positive:",tpprint "True Negatives:",tnprint "False Positive:",fpprint "False Negatives:",fn#这个分类器的精确率是多少?cl_precision = precision_score(real_label,predict)print cl_precision#这个分类器的召回率是多少?cl_recall = recall_score(real_label,predict)print cl_recall
原创粉丝点击