sklearn学习笔记1
Image recognition with Support Vector Machines
- #our dataset is provided within scikit-learn
- #let's start by importing and printing its description
- import sklearn as sk
- import numpy as np
- import matplotlib.pyplot as plt
- from sklearn.datasets import fetch_olivetti_faces
- faces = fetch_olivetti_faces()
- print(faces.DESCR)
Modified Olivetti faces dataset. The original database was available from (now defunct)
The version retrieved here comes in MATLAB format from the personal web page of Sam Roweis:
There are ten different images of each of 40 distinct subjects. For some subjects, the images were taken at different times, varying the lighting, facial expressions (open / closed eyes, smiling / not smiling) and facial details (glasses / no glasses). All the images were taken against a dark homogeneous background with the subjects in an upright, frontal position (with tolerance for some side movement). The original dataset consisted of 92 x 112, while the Roweis version consists of 64x64 images.
- print(faces.keys())
- print(faces.images.shape)
- print(faces.data.shape)
- print(faces.target.shape)
- print(np.max(faces.data))
- print(np.min(faces.data))
- print(np.mean(faces.data))
Before learning, let’s plot some faces.
- def print_faces(images, target, top_n):
- #set up the figure size in inches
- fig = plt.figure(figsize=(12, 12))
- fig.subplots_adjust(left = 0, right = 1, bottom = 0, top = 1, hspace = 0.05,wspace = 0.05)
- for i in range(top_n):
- #plot the images in a matrix of 20x20
- p = fig.add_subplot(20, 20, i + 1, xticks = [], yticks = [])
- p.imshow(images[i], cmap = plt.cm.bone)
- #label the image with target value
- p.text(0, 14, str(target[i]))
- p.text(0, 60, str(i))
If we print the first 20 images, we can see faces from two faces.(但是不知道为什么,打印不出图片)
- print_faces(faces.images, faces.target, 20)
Training a Support Vector machine
Import the SVC class from the sklearn.svm module:
- from sklearn.svm import SVC
- To start, we will use the simplest kernel, the linear one
- svc_1 = SVC(kernel = 'linear')
Before continuing, we will split our dataset into training and testing datasets.
- from sklearn.cross_validation import train_test_split
- X_train, X_test, y_train, y_test = train_test_split(faces.data,faces.target, test_size = 0.25, random_state = 0)
And we will define a function to evaluate K-fold cross-validation.
- from sklearn.cross_validation import cross_val_score, KFold
- from scipy.stats import sem
- def evaluate_cross_validation(clf, X, y, K):
- #create a k-fold cross validation iterator
- cv = KFold(len(y), K, shuffle=True, random_state=0)
- #by default the score used is the one return by score method of the estimator (accuracy)
- scores = cross_val_score(clf, X, y, cv = cv)
- print(scores)
- print(("Mean score: {0: .3f} (+/-{1: .3f})").format(np.mean(scores), sem(scores)))
- evaluate_cross_validation(svc_1, X_train, y_train, 5)
- [ 0.93333333 0.86666667 0.91666667 0.93333333 0.91666667]
- Mean score: 0.913 (+/- 0.012)
We will also define a function to perform training on the training set and evaluate the performance on the testing set.
- from sklearn import metrics
- def train_and_evaluate(clf, X_train, X_test, y_train, y_test):
- clf.fit(X_train, y_train)
- print("Accuracy on training set:")
- print(clf.score(X_train, y_train))
- print("Accuracy on testing set:")
- print(clf.score(X_test, y_test))
- y_pred = clf.predict(X_test)
- print("Classification Report:")
- print(metrics.classification_report(y_test, y_pred))
- print("Confusion Matrix:")
- print(metrics.confusion_matrix(y_test, y_pred))
- train_and_evaluate(svc_1, X_train, X_test, y_train, y_test)
Accuracy on training set: 1.0 Accuracy on testing set: 0.99
classify the faces as people with and without glasses
First thing to do is to defne the range of the images that show faces wearing glasses.
The following list shows the indexes of these images:
- # the index ranges of images of people with glasses
- glasses = [
- (10, 19), (30, 32), (37, 38), (50, 59), (63, 64),
- (69, 69), (120, 121), (124, 129), (130, 139), (160, 161),
- (164, 169), (180, 182), (185, 185), (189, 189), (190, 192),
- (194, 194), (196, 199), (260, 269), (270, 279), (300, 309),
- (330, 339), (358, 359), (360, 369)
- ]
Then we'll defne a function that from those segments returns a new target array that marks with 1 for the faces with glasses and 0 for the faces without glasses (our new target classes):
- def create_target(segments):
- # create a new y array of target size initialized with zeros
- y = np.zeros(faces.target.shape[0])
- # put 1 in the specified segments
- for (start, end) in segments:
- y[start:end + 1] = 1
- return y
- target_glasses = create_target(glasses)
So we must perform the training/testing split again.
- X_train, X_test, y_train, y_test = train_test_split(faces.data, target_glasses, test_size=0.25, random_state=0)
Now let's create a new SVC classifer, and train it with the new target vector using the following command:
- svc_2 = SVC(kernel='linear')
If we check the performance with cross-validation by the following code:
- evaluate_cross_validation(svc_2, X_train, y_train, 5)
- [ 1. 0.95 0.98333333 0.98333333 0.93333333]
- Mean score: 0.970 (+/- 0.012)
We obtain a mean accuracy of 0.970 with cross-validation if we evaluate on our testing set.
- train_and_evaluate(svc_2, X_train, X_test, y_train, y_test)
- Accuracy on training set:
- 1.0
- Accuracy on testing set:
- 0.99
- Classification Report:
- precision recall f1-score support
- 0.0 1.00 0.99 0.99 67
- 1.0 0.97 1.00 0.99 33
- avg / total 0.99 0.99 0.99 100
- Confusion Matrix:
- [[66 1]
- [ 0 33]]
Could it be possible that our classifer has learned to identify peoples' faces associated with glasses and without glasses precisely? How can we be sure that this is not happening and that if we get new unseen faces, it will work as expected? Let's separate all the images of the same person, sometimes wearing glasses and sometimes not. We will also separate all the images of the same person, the ones with indexes from 30 to 39, train by using the remaining instances, and evaluate on our new 10 instances set. With this experiment we will try to discard the fact that it is remembering faces, not glassed-related features.
- X_test = faces.data[30:40]
- y_test = target_glasses[30:40]
- print(y_test.shape[0])
- select = np.ones(target_glasses.shape[0])
- select[30:40] = 0
- X_train = faces.data[select == 1]
- y_train = target_glasses[select == 1]
- print(y_train.shape[0])
- svc_3 = SVC(kernel='linear')
- train_and_evaluate(svc_3, X_train, X_test, y_train, y_test)
- 10
- 390
- Accuracy on training set:
- 1.0
- Accuracy on testing set:
- 0.9
- Classification Report:
- precision recall f1-score support
- 0.0 0.83 1.00 0.91 5
- 1.0 1.00 0.80 0.89 5
- avg / total 0.92 0.90 0.90 10
- Confusion Matrix:
- [[5 0]
- [1 4]]
From the 10 images, only one error, still pretty good results, let's check out which one was incorrectly classifed. First, we have to reshape the data from arrays to 64 x 64 matrices:
- y_pred = svc_3.predict(X_test)
- eval_faces = [np.reshape(a, (64, 64)) for a in X_test]
Then plot with our print_faces function:
- print_faces(eval_faces, y_pred, 10)
The image number 8 in the preceding fgure has glasses and was classifed as no glasses. If we look at that instance, we can see that it is different from the rest of the images with glasses (the border of the glasses cannot be seen clearly and the person is shown with closed eyes), which could be the reason it has been misclassifed.
sklearn学习笔记1的更多相关文章
- sklearn学习笔记之简单线性回归
简单线性回归 线性回归是数据挖掘中的基础算法之一,从某种意义上来说,在学习函数的时候已经开始接触线性回归了,只不过那时候并没有涉及到误差项.线性回归的思想其实就是解一组方程,得到回归函数,不过在出现误 ...
- sklearn学习笔记3
Explaining Titanic hypothesis with decision trees decision trees are very simple yet powerful superv ...
- sklearn学习笔记2
Text classifcation with Naïve Bayes In this section we will try to classify newsgroup messages using ...
- sklearn学习笔记
用Bagging优化模型的过程:1.对于要使用的弱模型(比如线性分类器.岭回归),通过交叉验证的方式找到弱模型本身的最好超参数:2.然后用这个带着最好超参数的弱模型去构建强模型:3.对强模型也是通过交 ...
- sklearn学习笔记(一)——数据预处理 sklearn.preprocessing
https://blog.csdn.net/zhangyang10d/article/details/53418227 数据预处理 sklearn.preprocessing 标准化 (Standar ...
- sklearn学习笔记之岭回归
岭回归 岭回归是一种专用于共线性数据分析的有偏估计回归方法,实质上是一种改良的最小二乘估计法,通过放弃最小二乘法的无偏性,以损失部分信息.降低精度为代价获得回归系数更为符合实际.更可靠的回归方法,对病 ...
- sklearn学习笔记之开始
简介 自2007年发布以来,scikit-learn已经成为Python重要的机器学习库了.scikit-learn简称sklearn,支持包括分类.回归.降维和聚类四大机器学习算法.还包含了特征 ...
- sklearn学习笔记(1)--make_blobs函数及相应参数简介
make_blobs方法: sklearn.datasets.make_blobs(n_samples=100,n_features=2,centers=3, cluster_std=1.0,cent ...
- Google TensorFlow深度学习笔记
Google Deep Learning Notes Google 深度学习笔记 由于谷歌机器学习教程更新太慢,所以一边学习Deep Learning教程,经常总结是个好习惯,笔记目录奉上. Gith ...
随机推荐
- Java EE 编程中路径
版权声明:未经博主允许,不得转载 首先我们要限定一个范围,是一个项目,或是以个访问地址..就先以一个项目为限定的范围 前述: 学过物理学的都知道相对运动和绝对运动, 虽然是相似的概念,但这里的要简单得 ...
- SQLite数据库在多线程写锁文件的解决办法
参考了很多SQLITE数据库多线程的解决办法 我自己写了一个SQLITEHELPER 来解决这个问题 希望大家多多指教 调用的时候 SQLLiteDBHelper _SQLLiteDBHelper ...
- 一个文件夹可以link 到另外一个文件夹
Creates a symbolic link. MKLINK [[/D] | [/H] | [/J]] Link Target /D Creates a directory symboli ...
- OGRE启动过程详解(OGRE HelloWorld程序原理解析)
本文介绍 OGRE 3D 1.9 程序的启动过程,即从程序启动到3D图形呈现,背后有哪些OGRE相关的代码被执行.会涉及的OGRE类包括: Root RenderSystem RenderWindow ...
- C#编程语言与面向对象——继承
现实生活中的事物都归属于一定的类别,比如,狮子是一种(IS_A)动物,为了在计算机中模拟这种关系,面向对象的语言引入了继承(inherit)特性. 构成继承关系的两个类中,Animal称为父类(par ...
- web安全之文件上传漏洞
成因: 当文件上传时,若服务端脚本语言未对上传的文件进行严格验证和过滤,若恶意用户上传恶意的 脚本文件时,就有可能控制整个网站甚至是服务器,这就是文件上传漏洞. 权限: 1. 后台权限:登陆了后台,可 ...
- Javascript.//DOM
文档对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展标志语言的标准编程接口.Document Object Model的历史可以追溯至1990年代后期微 ...
- linux用户和用户组的基本操作
1.用户组操作 -创建用户组 # groupadd 组名 说明:新创建的组id默认从500开始,也可以通过[-g]选项指定组id,指定组id后新创建的组id会从指定的id后依次创建. -删除用户组 # ...
- Single Number
Given an array of integers, every element appears twice except for one. Find that single one.Your al ...
- esxi 升级
开启ssh esxcli software vib install -d="/vmfs/volumes/53034105-a8b88330-c096-40f2e993407b/update- ...