sklearn实现人脸识别lda特征提取
时间: 2023-07-06 17:21:38 浏览: 108
首先,我们需要导入必要的库:
```python
import numpy as np
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn.datasets import fetch_lfw_people
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import classification_report
```
接着,我们需要获取人脸数据集,这里我选择了LFW(Labeled Faces in the Wild)数据集:
```python
lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4)
```
然后,我们需要准备好数据集,将数据集分为训练集和测试集,并将数据集进行LDA特征提取:
```python
X = lfw_people.data
y = lfw_people.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
lda = LDA(n_components=100)
X_train_lda = lda.fit_transform(X_train, y_train)
X_test_lda = lda.transform(X_test)
```
在进行LDA特征提取后,我们可以使用SVM进行分类:
```python
clf = SVC(kernel='rbf', class_weight='balanced')
clf.fit(X_train_lda, y_train)
y_pred = clf.predict(X_test_lda)
print(classification_report(y_test, y_pred, target_names=lfw_people.target_names))
```
最后,我们可以输出分类报告,得到人脸识别的结果。
阅读全文