資源簡(jiǎn)介
python3代碼,調(diào)用SVM實(shí)現(xiàn)人臉識(shí)別,并根據(jù)python2.7代碼,進(jìn)行勘誤。
代碼片段和文件信息
#?!/usr/bin/env?python
#?-*-?coding:?utf-8?-*-
#?Author:?Tanghong
#?在python2.x版本中要使用Python3.x的特性可以使用__future__模塊導(dǎo)入相應(yīng)的接口,減少對(duì)當(dāng)前低版本影響
#?from?__future__?import?print_function
#?計(jì)時(shí),程序運(yùn)行時(shí)間
from?time?import?time
#?打印程序進(jìn)展時(shí)的一些信息
import?logging
#?最后識(shí)別出來(lái)的人臉通過(guò)繪圖打印出來(lái)
import?matplotlib.pyplot?as?plt
from?PIL?import?Image
from?scipy?import?ndimage
#?當(dāng)import?一個(gè)模塊比如如下模塊cross_validation時(shí),會(huì)有刪除橫線,表示該模塊在當(dāng)前版本可能已經(jīng)被刪除在新版本中改為model_selection模塊
#?DeprecationWarning:?This?module?was?deprecated?in?version?0.18?in?favor?of?the?model_selection
#?module?into?which?all?the?refactored?classes?and?functions?are?moved.
#?Also?note?that?the?interface?of?the?new?CV?iterators?are?different?from?that?of?this?module.
#?This?module?will?be?removed?in?0.20.“This?module?will?be?removed?in?0.20.“?DeprecationWarning)
#?from?sklearn.cross_validation?import?train_test_split
from?sklearn.model_selection?import?train_test_split
from?sklearn.datasets?import?fetch_lfw_people
#?grid_search已經(jīng)被移除
#?from?sklearn.grid_search?import?GridSearchCV
from?sklearn.model_selection?import?GridSearchCV
from?sklearn.metrics?import?classification_report
#?Class?RandomizedPCA?is?deprecated;?RandomizedPCA?was?deprecated?in?0.18?and?will?be?removed?in?0.20.
#?Use?PCA(svd_solver=‘randomized‘)?instead.?The?new?implementation?DOES?NOT?store?whiten?‘‘components_‘‘.
#?Apply?transform?to?get?them.
from?sklearn.decomposition?import?PCA
from?sklearn.svm?import?SVC
#?導(dǎo)入混淆矩陣模塊confusion_matrix()
from?sklearn.metrics?import?confusion_matrix
print(__doc__)
#?Display?progress?logs?on?stdout程序進(jìn)展的信息打印出來(lái)
logging.basicConfig(level=logging.INFO?format=‘%(asctime)s?%(message)s‘)
###############################################################################
#?Download?the?data?if?not?already?on?disk?and?load?it?as?numpy?arrays
#?下載人臉庫(kù)?http://vis-www.cs.umass.edu/lfw/
lfw_people?=?fetch_lfw_people(min_faces_per_person=80?resize=0.4)
#?introspect?the?images?arrays?to?find?the?shapes?(for?plotting)
n_samples?h?w?=?lfw_people.images.shape
#?for?machine?learning?we?use?the?2?data?directly?(as?relative?pixel
#?positions?info?is?ignored?by?this?model)
#?獲取特征向量矩陣
X?=?lfw_people.data
#?特征向量的維度(列數(shù))或者稱(chēng)特征點(diǎn)的個(gè)數(shù)
n_features?=?X.shape[1]
#?the?label?to?predict?is?the?id?of?the?person
#?返回每一組的特征標(biāo)記
y?=?lfw_people.target
target_names?=?lfw_people.target_names
#?返回多少類(lèi)(多少行),也就是多少個(gè)人進(jìn)行人臉識(shí)別
n_classes?=?target_names.shape[0]
print(“Total?dataset?size:“)
print(“n_samples:?%d“?%?n_samples)
print(“n_features:?%d“?%?n_features)
print(“n_classes:?%d“?%?n_classes)
###############################################################################
#?Split?into?a?training?set?and?a?test?set?using?a?stratified?k?fold
#?split?into?a?training?and?testing?set
#?將數(shù)據(jù)集拆分成四個(gè)部分
X_train?X_test?y_train?y_test?=?train_test_split(X?y?test_size=0.25)
###############################################################################
#?PCA降維方法,減少特征
評(píng)論
共有 條評(píng)論