机器学习:scikit-learn 做笑脸识别 (SVM, KNN, Logisitc regression)
scikit-learn 是 Python 非常强大的一个做机器学习的包,今天介绍scikit-learn 里几个常用的分类器
SVM, KNN 和 logistic regression,用来做笑脸识别。
这里用到的是GENKI4K 这个数据库,每张图像先做一个人脸检测与剪切,然后提取HOG特征。这个数据库有 4000 张图,分成4组,做一个 cross validation,取平均值作为最终的识别率:
import string, os, sys
import numpy as np
import matplotlib.pyplot as plt
import scipy.io
import random
from sklearn import neighbors, linear_model, svm
dir = '/GENKI4K/Feature_Data'
print '----------- no sub dir'
# prepare the data
files = os.listdir(dir)
for f in files:
print dir + os.sep + f
file_path=dir+os.sep+files[14]
#print file_path
dic_mat = scipy.io.loadmat(file_path)
data_mat=dic_mat['Hog_Feat']
print 'feature: ', data_mat.shape
#print data_mat.dtype
file_path2=dir+os.sep+files[15]
#print file_path2
dic_label=scipy.io.loadmat(file_path2)
label_mat=dic_label['Label']
file_path3=dir+os.sep+files[16]
print 'fiel 3 path: ', file_path3
dic_T=scipy.io.loadmat(file_path3)
T=dic_T['T']
T=T-1
print T.shape
label=label_mat.ravel()
# Acc=np.zeros((1,4))
Acc=[0,0,0,0]
for i in range (0, 4):
print "the fold %d" % (i+1)
train_ind=[]
for j in range (0, 4):
if j==i:
test_ind=T[j]
else:
train_ind.extend(T[j])
# print len(test_ind), len(train_ind)
# print max(test_ind), max(train_ind)
train_x=data_mat[train_ind, :]
test_x=data_mat[test_ind, :]
train_y=label[train_ind]
test_y=label[test_ind]
# SVM
clf=svm.LinearSVC()
# KNN
# clf = neighbors.KNeighborsClassifier(n_neighbors=15)
# Logistic regression
# clf = linear_model.LogisticRegression()
clf.fit(train_x, train_y)
predict_y=clf.predict(test_x)
Acc[i]=np.mean(predict_y == test_y)
print "Accuracy: %.2f" % (Acc[i])
print "The mean average classification accuracy: %.2f" % (np.mean(Acc))
# SVM 的实验结果
(4, 1000)
the fold 1
Accuracy: 0.89
the fold 2
Accuracy: 0.88
the fold 3
Accuracy: 0.89
the fold 4
Accuracy: 0.90
The mean average classification accuracy: 0.89
# KNN 的实验结果
(4, 1000)
the fold 1
Accuracy: 0.83
the fold 2
Accuracy: 0.84
the fold 3
Accuracy: 0.84
the fold 4
Accuracy: 0.85
The mean average classification accuracy: 0.84
# logistic regression 的实验结果
(4, 1000)
the fold 1
Accuracy: 0.91
the fold 2
Accuracy: 0.91
the fold 3
Accuracy: 0.90
the fold 4
Accuracy: 0.92
The mean average classification accuracy: 0.91
机器学习:scikit-learn 做笑脸识别 (SVM, KNN, Logisitc regression)的更多相关文章
- 机器学习: Tensor Flow +CNN 做笑脸识别
Tensor Flow 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库.节点(Nodes)在图中表示数学操作,图中的线(edges)则表示在节点间相互联系的多维数据数 ...
- 机器学习: TensorFlow with MLP 笑脸识别
Tensor Flow 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库.节点(Nodes)在图中表示数学操作,图中的线(edges)则表示在节点间相互联系的多维数据数 ...
- 机器学习-scikit learn学习笔记
scikit-learn官网:http://scikit-learn.org/stable/ 通常情况下,一个学习问题会包含一组学习样本数据,计算机通过对样本数据的学习,尝试对未知数据进行预测. 学习 ...
- Scikit Learn: 在python中机器学习
转自:http://my.oschina.net/u/175377/blog/84420#OSC_h2_23 Scikit Learn: 在python中机器学习 Warning 警告:有些没能理解的 ...
- (原创)(三)机器学习笔记之Scikit Learn的线性回归模型初探
一.Scikit Learn中使用estimator三部曲 1. 构造estimator 2. 训练模型:fit 3. 利用模型进行预测:predict 二.模型评价 模型训练好后,度量模型拟合效果的 ...
- (原创)(四)机器学习笔记之Scikit Learn的Logistic回归初探
目录 5.3 使用LogisticRegressionCV进行正则化的 Logistic Regression 参数调优 一.Scikit Learn中有关logistics回归函数的介绍 1. 交叉 ...
- 机器学习: Tensor Flow with CNN 做表情识别
我们利用 TensorFlow 构造 CNN 做表情识别,我们用的是FER-2013 这个数据库, 这个数据库一共有 35887 张人脸图像,这里只是做一个简单到仿真实验,为了计算方便,我们用其中到 ...
- 硬核机器学习干货,手把手教你写KNN!
机器学习相关概念 人工智能.机器学习和深度学习的关系 在探讨算法之前,我们先来谈一谈什么是机器学习.相信大家都听说过AlphaGo:2016年3月,AlphaGo与围棋世界冠军李世石进行围棋人机大战, ...
- Python 3 利用机器学习模型 进行手写体数字识别
0.引言 介绍了如何生成数据,提取特征,利用sklearn的几种机器学习模型建模,进行手写体数字1-9识别. 用到的四种模型: 1. LR回归模型,Logistic Regression 2. SGD ...
随机推荐
- 【例题 6-3 UVA - 442】Matrix Chain Multiplication
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用栈来处理一下表达式就好. 因为括号是一定匹配的.所以简单很多. ab x bc会做abc次乘法. [代码] #include< ...
- swift开发网络篇 - 用户登录POST JSON and header
版权声明:本文为博主原创文章,未经博主允许不得转载. import UIKit import Alamofire class ViewController: UIViewController { va ...
- Nginx+Tomcat+Memcached实现会话保持(MSM)
会话保持的三种方式 Session sticky会话绑定:通过在前端调度器的配置中实现统一session发送至同一后发端服务器 Session cluster会话集群:通过配置Tomcat保持所有To ...
- 爬虫抓取页面数据原理(php爬虫框架有很多 )
爬虫抓取页面数据原理(php爬虫框架有很多 ) 一.总结 1.php爬虫框架有很多,包括很多傻瓜式的软件 2.照以前写过java爬虫的例子来看,真的非常简单,就是一个获取网页数据的类或者方法(这里的话 ...
- Android 从硬件到应用:一步一步向上爬 4 -- 使用 JNI 方法调硬件驱动
Android下,java应用程序通过JNI方法调用硬件抽象层模块,在Android 从硬件到应用:一步一步向上爬 3 -- 硬件抽象层訪问硬件驱动 中我们已经编译好了硬件抽象层模块,以下就要開始为H ...
- #308 (div.2) B. Vanya and Books
1.题目描写叙述:点击打开链接 2.解题思路:本题要求统计数位的个数,简单的试验一下发现有例如以下规律:一个n位数的个数有9*(10^n)个.因此全部n位数的数位是n*9*(10^n)个.因此能够利用 ...
- struts2基本的配置代码
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "- ...
- CVE-2014-3566
https://access.redhat.com/articles/1232123 https://tools.ietf.org/html/draft-ietf-tls-downgrade-scsv ...
- hreadPoolExecutor使用和思考(上)-线程池大小设置与BlockingQueue的三种实现区别
阅读更多 工作中多处接触到了ThreadPoolExecutor.趁着现在还算空,学习总结一下. 前记: jdk官方文档(javadoc)是学习的最好,最权威的参考. 文章分上中下.上篇中主要介绍Th ...
- Chrome源代码结构
首先,开始接触Chrome的童鞋可能有一个疑惑,Chrome和Chromium是同一个东西吗?答案是,Chrome是Google官方的浏览器项目名称,Chromium是Google官方对Chrome开 ...