scikit-learn 是 Python 非常强大的一个做机器学习的包,今天介绍scikit-learn 里几个常用的分类器

SVM, KNN 和 logistic regression,用来做笑脸识别。

这里用到的是GENKI4K 这个数据库,每张图像先做一个人脸检测与剪切,然后提取HOG特征。这个数据库有 4000 张图,分成4组,做一个 cross validation,取平均值作为最终的识别率:

  1. import string, os, sys
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import scipy.io
  5. import random
  6. from sklearn import neighbors, linear_model, svm
  7. dir = '/GENKI4K/Feature_Data'
  8. print '----------- no sub dir'
  9. # prepare the data
  10. files = os.listdir(dir)
  11. for f in files:
  12. print dir + os.sep + f
  13. file_path=dir+os.sep+files[14]
  14. #print file_path
  15. dic_mat = scipy.io.loadmat(file_path)
  16. data_mat=dic_mat['Hog_Feat']
  17. print 'feature: ', data_mat.shape
  18. #print data_mat.dtype
  19. file_path2=dir+os.sep+files[15]
  20. #print file_path2
  21. dic_label=scipy.io.loadmat(file_path2)
  22. label_mat=dic_label['Label']
  23. file_path3=dir+os.sep+files[16]
  24. print 'fiel 3 path: ', file_path3
  25. dic_T=scipy.io.loadmat(file_path3)
  26. T=dic_T['T']
  27. T=T-1
  28. print T.shape
  29. label=label_mat.ravel()
  30. # Acc=np.zeros((1,4))
  31. Acc=[0,0,0,0]
  32. for i in range (0, 4):
  33. print "the fold %d" % (i+1)
  34. train_ind=[]
  35. for j in range (0, 4):
  36. if j==i:
  37. test_ind=T[j]
  38. else:
  39. train_ind.extend(T[j])
  40. # print len(test_ind), len(train_ind)
  41. # print max(test_ind), max(train_ind)
  42. train_x=data_mat[train_ind, :]
  43. test_x=data_mat[test_ind, :]
  44. train_y=label[train_ind]
  45. test_y=label[test_ind]
  46. # SVM
  47. clf=svm.LinearSVC()
  48. # KNN
  49. # clf = neighbors.KNeighborsClassifier(n_neighbors=15)
  50. # Logistic regression
  51. # clf = linear_model.LogisticRegression()
  52. clf.fit(train_x, train_y)
  53. predict_y=clf.predict(test_x)
  54. Acc[i]=np.mean(predict_y == test_y)
  55. print "Accuracy: %.2f" % (Acc[i])
  56. print "The mean average classification accuracy: %.2f" % (np.mean(Acc))
  57. # SVM 的实验结果
  58. (4, 1000)
  59. the fold 1
  60. Accuracy: 0.89
  61. the fold 2
  62. Accuracy: 0.88
  63. the fold 3
  64. Accuracy: 0.89
  65. the fold 4
  66. Accuracy: 0.90
  67. The mean average classification accuracy: 0.89
  68. # KNN 的实验结果
  69. (4, 1000)
  70. the fold 1
  71. Accuracy: 0.83
  72. the fold 2
  73. Accuracy: 0.84
  74. the fold 3
  75. Accuracy: 0.84
  76. the fold 4
  77. Accuracy: 0.85
  78. The mean average classification accuracy: 0.84
  79. # logistic regression 的实验结果
  80. (4, 1000)
  81. the fold 1
  82. Accuracy: 0.91
  83. the fold 2
  84. Accuracy: 0.91
  85. the fold 3
  86. Accuracy: 0.90
  87. the fold 4
  88. Accuracy: 0.92
  89. The mean average classification accuracy: 0.91

机器学习:scikit-learn 做笑脸识别 (SVM, KNN, Logisitc regression)的更多相关文章

  1. 机器学习: Tensor Flow +CNN 做笑脸识别

    Tensor Flow 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库.节点(Nodes)在图中表示数学操作,图中的线(edges)则表示在节点间相互联系的多维数据数 ...

  2. 机器学习: TensorFlow with MLP 笑脸识别

    Tensor Flow 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库.节点(Nodes)在图中表示数学操作,图中的线(edges)则表示在节点间相互联系的多维数据数 ...

  3. 机器学习-scikit learn学习笔记

    scikit-learn官网:http://scikit-learn.org/stable/ 通常情况下,一个学习问题会包含一组学习样本数据,计算机通过对样本数据的学习,尝试对未知数据进行预测. 学习 ...

  4. Scikit Learn: 在python中机器学习

    转自:http://my.oschina.net/u/175377/blog/84420#OSC_h2_23 Scikit Learn: 在python中机器学习 Warning 警告:有些没能理解的 ...

  5. (原创)(三)机器学习笔记之Scikit Learn的线性回归模型初探

    一.Scikit Learn中使用estimator三部曲 1. 构造estimator 2. 训练模型:fit 3. 利用模型进行预测:predict 二.模型评价 模型训练好后,度量模型拟合效果的 ...

  6. (原创)(四)机器学习笔记之Scikit Learn的Logistic回归初探

    目录 5.3 使用LogisticRegressionCV进行正则化的 Logistic Regression 参数调优 一.Scikit Learn中有关logistics回归函数的介绍 1. 交叉 ...

  7. 机器学习: Tensor Flow with CNN 做表情识别

    我们利用 TensorFlow 构造 CNN 做表情识别,我们用的是FER-2013 这个数据库, 这个数据库一共有 35887 张人脸图像,这里只是做一个简单到仿真实验,为了计算方便,我们用其中到 ...

  8. 硬核机器学习干货,手把手教你写KNN!

    机器学习相关概念 人工智能.机器学习和深度学习的关系 在探讨算法之前,我们先来谈一谈什么是机器学习.相信大家都听说过AlphaGo:2016年3月,AlphaGo与围棋世界冠军李世石进行围棋人机大战, ...

  9. Python 3 利用机器学习模型 进行手写体数字识别

    0.引言 介绍了如何生成数据,提取特征,利用sklearn的几种机器学习模型建模,进行手写体数字1-9识别. 用到的四种模型: 1. LR回归模型,Logistic Regression 2. SGD ...

随机推荐

  1. 高级Java工程师必备 ----- 深入分析 Java IO (三)

    概述 Java IO即Java 输入输出系统.不管我们编写何种应用,都难免和各种输入输出相关的媒介打交道,其实和媒介进行IO的过程是十分复杂的,这要考虑的因素特别多,比如我们要考虑和哪种媒介进行IO( ...

  2. ZOJ-1649 Rescue BFS (HDU 1242)

    看题传送门: ZOJ http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 HDU http://acm.hdu.edu. ...

  3. 11、DMA操作说明

    先理解cache的作用CPU在访问内存时,首先判断所要访问的内容是否在Cache中,如果在,就称为“命中(hit)”,此时CPU直接从Cache中调用该内容:否则,就 称为“ 不命中”,CPU只好去内 ...

  4. 【z08】乌龟棋

    描述 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩家控制一个乌龟棋子从起点出发走到终点 ...

  5. markdown + vim

    https://www.jianshu.com/p/24aefcd4ca93https://github.com/isnowfy/python-vim-instant-markdownhttps:// ...

  6. css3背景透明文字不透明

    在 FF/Chrome 等较新的浏览器中可以使用css属性background-color的rgba轻松实现背景透明,而文字保持不透明.而IE6/7/8浏览器不支持rgba,只有使用IE的专属滤镜fi ...

  7. MS SQL Server的STRING_SPLIT和STRING_AGG函数

    在较新版本的SQL中,出现有2个函数,STRING_SPLIT和STRING_AGG,前者是把带有分隔的字符串转换为表,而后者却是把表某一表转换为以某种字符分隔的字符串. 如下面: DECLARE @ ...

  8. [RxJS] Use RxJS concatMap to map and concat high order observables

    Like switchMap and mergeMap, concatMap is a shortcut for map() followed by a concatAll(). In this le ...

  9. PHP 根据对象属性进行对象数组的排序(usort($your_data, "cmp");)(inside the class: usort($your_data, array($this, "cmp")))

    PHP 根据对象属性进行对象数组的排序(usort($your_data, "cmp");)(inside the class: usort($your_data, array($ ...

  10. Android 添加常驻图标到状态栏

    / * * 如果没有从状态栏中删除ICON,且继续调用addIconToStatusbar,则不会有任何变化.如果将notification中的resId设置不同的图标,则会显示不同的图标 * / p ...