《Hands-On Machine Learning with Scikit-Learn & TensorFlow》第三章 分类

1. 先做5和非5的单分类

SGD分类器实现单分类,并用交叉验证,cross_val_score()得到的是kfold的分类accuracy,且里面进行的是分层抽样,根据分类的类别来进行的分层抽样!cross_val_predict()与cross_val_score()类似,但是输出的不是accuracy,而是分类的结果。再通过conf_result = confusion_matrix(y_train_5, y_train_pred),可以得到混淆矩阵。即利用训练集进行交叉验证后,可以得到训练集的混淆矩阵!

 

 


(1)为什么一定要打乱数据集?

让我们打乱训练集。这可以保证交叉验证的时所有的折叠都差不多(你不会希望某一个折叠数据集类缺少某类数字)。而且,一些学习算法对训练样例的顺序敏感当它们在一行当中得到许多相似的样例,这些算法将会表现得非常差。打乱数据集将保证这种情况不会发生。

评估一个分类器,通常比评估一个回归器要困难的多。所以我们将会花大量的篇幅在这个话题上。有许多量度性能的方法,所以拿来一杯咖啡和准备学习许多新概念和首字母缩略词吧。

使用交叉验证测量准确性

(2)为什么准确率很高?——因为数据是biased,正类只有10%,负类有90%,所以即使全判错,也有90%的准确率!!!!处理偏斜类数据时,准确率无法成为分类器的首要性能指标!!!

将数据集分成了三个折叠,进行交叉验证。二分类的交叉验证,看似准确率还不错!且使用的StratifiedKFold类实现的交叉验证,这个类是进行的分层采样,生成的折包含了各类的相应比例的样例!

评估classifiers性能一个比较好的方法是查看混淆矩阵。 一般的想法是计算A类实例被分类为B类的次数。例如,要知道分类器将5与3混淆的次数,您将查看混淆矩阵的第5行和第3列

要计算混淆矩阵,首先需要有一组预测,以便他们可以与实际目标进行比较。 您可以对测试集进行预测,但现在让它保持不变(请记住,只有在项目的最后才能使用测试集,一旦有了准备启动的分类器)。 相反,您可以使用cross_val_predict()函数:

样本不均衡时单分类的评价指标

正样本的Recall还可以用,但是precision一定不好用了https://blog.csdn.net/zhongjunlang/article/details/79568601 

 

             详见:http://blog.csdn.net/login_sonata/article/details/54288653                   


一、5与非5的单分类

https://github.com/ageron/handson-ml/issues/360 Chapter 3: ValueError: The number of classes has to be greater than one; got 1 class 

出现错误的原因是:y_train_5里面只有一个类别。因为使用了fetch_openml()下载的MNIST,它返回的标签是string类型的,所以的那个定义y_train_5 = (y_train == 5)时,返回值都是false。解决办法是cast y_train到int8格式。y_train = y_train.astype(np.int8)使用这个语句即可。或者y_train_5 = (y_train == '5') y_test_5 = (y_test == '5')

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
When most people hearMachine Learning,” they picture a robot: a dependable butler or a deadly Terminator depending on who you ask. But Machine Learning is not just a futuristic fantasy, it’s already here. In fact, it has been around for decades in some specialized applications, such as Optical Character Recognition (OCR). But the first ML application that really became mainstream, improving the lives of hundreds of millions of people, took over the world back in the 1990s: it was the spam filter. Not exactly a self-aware Skynet, but it does technically qualify as Machine Learning (it has actually learned so well that you seldom need to flag an email as spam anymore). It was followed by hundreds of ML applications that now quietly power hundreds of products and features that you use regularly, from better recommendations to voice search. Where does Machine Learning start and where does it end? What exactly does it mean for a machine to learn something? If I download a copy of Wikipedia, has my computer really “learned” something? Is it suddenly smarter? In this chapter we will start by clarifying what Machine Learning is and why you may want to use it. Then, before we set out to explore the Machine Learning continent, we will take a look at the map and learn about the main regions and the most notable landmarks: supervised versus unsupervised learning, online versus batch learning, instance-based versus model-based learning. Then we will look at the workflow of a typical ML project, discuss the main challenges you may face, and cover how to evaluate and fine-tune a Machine Learning system. This chapter introduces a lot of fundamental concepts (and jargon) that every data scientist should know by heart. It will be a high-level overview (the only chapter without much code), all rather simple, but you should make sure everything is crystal-clear to you before continuing to the rest of the book. So grab a coffee and let’s get started!
When most people hearMachine Learning,” they picture a robot: a dependable butler or a deadly Terminator depending on who you ask. But Machine Learning is not just a futuristic fantasy, it’s already here. In fact, it has been around for decades in some specialized applications, such as Optical Character Recognition (OCR). But the first ML application that really became mainstream, improving the lives of hundreds of millions of people, took over the world back in the 1990s: it was the spam filter. Not exactly a self-aware Skynet, but it does technically qualify as Machine Learning (it has actually learned so well that you seldom need to flag an email as spam anymore). It was followed by hundreds of ML applications that now quietly power hundreds of products and features that you use regularly, from better recommendations to voice search. Where does Machine Learning start and where does it end? What exactly does it mean for a machine to learn something? If I download a copy of Wikipedia, has my computer really “learned” something? Is it suddenly smarter? In this chapter we will start by clarifying what Machine Learning is and why you may want to use it. Then, before we set out to explore the Machine Learning continent, we will take a look at the map and learn about the main regions and the most notable landmarks: supervised versus unsupervised learning, online versus batch learning, instance-based versus model-based learning. Then we will look at the workflow of a typical ML project, discuss the main challenges you may face, and cover how to evaluate and fine-tune a Machine Learning system. This chapter introduces a lot of fundamental concepts (and jargon) that every data scientist should know by heart. It will be a high-level overview (the only chapter without much code), all rather simple, but you should make sure everything is crystal-clear to you before continuing to the rest of the book. So grab a coffee and let’s get started!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值