机器学习项目实战——21SVM算法之人脸识别

现在关于人脸识别的项目一般采用深度学习方法,很少使用SVM的了

整体代码:

import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.datasets import fetch_lfw_people
from sklearn.model_selection  import GridSearchCV
from sklearn.metrics import classification_report
from sklearn.svm import SVC
from sklearn.decomposition import PCA

# 载入数据
lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4)

plt.imshow(lfw_people.images[6],cmap='gray')
plt.show()

# 照片的数据格式
n_samples, h, w = lfw_people.images.shape
print(n_samples)  #1288
print(h)   #50
print(w)   #37

print(lfw_people.data.shape)   #(1288, 1850)

print(lfw_people.target)    #array([5, 6, 3, ..., 5, 3, 5], dtype=int64)

target_names = lfw_people.target_names
print(target_names)

n_classes = lfw_people.target_names.shape[0]

x_train, x_test, y_train, y_test = train_test_split(lfw_people.data, lfw_people.target)

model = SVC(kernel='rbf', class_weight='balanced')
model.fit(x_train, y_train)

predictions = model.predict(x_test)
print(classification_report(y_test, predictions, target_names=lfw_people.target_names))
#






# PCA降维
# 100个维度
n_components = 100

pca = PCA(n_components=n_components, whiten=True).fit(lfw_people.data)

x_train_pca = pca.transform(x_train)
x_test_pca = pca.transform(x_test)

print(x_train_pca.shape)  #(966, 100)

model = SVC(kernel='rbf', class_weight='balanced')
model.fit(x_train_pca, y_train)

predictions = model.predict(x_test_pca)
print(classification_report(y_test, predictions, target_names=target_names))
#





# 调参
param_grid = {'C': [0.1, 1, 5, 10, 100],
              'gamma': [0.0005, 0.001, 0.005, 0.01], }
model = GridSearchCV(SVC(kernel='rbf', class_weight='balanced'), param_grid)
model.fit(x_train_pca, y_train)
print(model.best_estimator_)

predictions = model.predict(x_test_pca)
print(classification_report(y_test, predictions, target_names=target_names))

param_grid = {'C': [0.1, 0.6, 1, 2, 3],
              'gamma': [0.003, 0.004, 0.005, 0.006, 0.007], }
model = GridSearchCV(SVC(kernel='rbf', class_weight='balanced'), param_grid)
model.fit(x_train_pca, y_train)
print(model.best_estimator_)

predictions = model.predict(x_test_pca)
print(classification_report(y_test, predictions, target_names=target_names))





# 画图,3行4列
def plot_gallery(images, titles, h, w, n_row=3, n_col=5):
    plt.figure(figsize=(1.8 * n_col, 2.4 * n_row))
    plt.subplots_adjust(bottom=0, left=.01, right=.99, top=.90, hspace=.35)
    for i in range(n_row * n_col):
        plt.subplot(n_row, n_col, i + 1)
        plt.imshow(images[i].reshape((h, w)), cmap=plt.cm.gray)
        plt.title(titles[i], size=12)
        plt.xticks(())
        plt.yticks(())

# 获取一张图片title
def title(predictions, y_test, target_names, i):
    pred_name = target_names[predictions[i]].split(' ')[-1]
    true_name = target_names[y_test[i]].split(' ')[-1]
    return 'predicted: %s\ntrue:      %s' % (pred_name, true_name)

# 获取所有图片title
prediction_titles = [title(predictions, y_test, target_names, i) for i in range(len(predictions))]

# 画图
plot_gallery(x_test, prediction_titles, h, w)

plt.show()



你可能感兴趣的:(机器学习,机器学习)