关于支持向量机(SVM)算法应用——人脸识别python代码修改

原始代码

关于相关库:
sklearn
matplotlib
numpy
scipy
推荐直接搭建Anoconda环境,直接conda install 。。。 即可下载相应库。
搭建Anoconda环境连接: Anaconda完全入门指南

可以直接使用Anoconda自带解释器spyder

from __future__ import print_function    //for using Python3

from time import time
import logging
import matplotlib.pyplot as plt

from sklearn.cross_validation import train_test_split
from sklearn.datasets import fetch_lfw_people
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.decomposition import RandomizedPCA
from sklearn.svm import SVC


print(__doc__)

# Display progress logs on stdout
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')


###############################################################################
# Download the data, if not already on disk and load it as numpy arrays

lfw_people = fetch_lfw_people(min_faces_per_person=70, 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
n_features = X.shape[1]

# the label to predict is the id of the person
y = lfw_people.target
target_names = lfw_people.target_names
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
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.25)


###############################################################################
# Compute a PCA (eigenfaces) on the face dataset (treated as unlabeled
# dataset): unsupervised feature extraction / dimensionality reduction
n_components = 150

print("Extracting the top %d eigenfaces from %d faces"
      % (n_components, X_train.shape[0]))
t0 = time()
pca = RandomizedPCA(n_components=n_components, whiten=True).fit(X_train)
print("done in %0.3fs" % (time() - t0))

eigenfaces = pca.components_.reshape((n_components, h, w))

print("Projecting the input data on the eigenfaces orthonormal basis")
t0 = time()
X_train_pca = pca.transform(X_train)
X_test_pca = pca.transform(X_test)
print("done in %0.3fs" % (time() - t0))


###############################################################################
# Train a SVM cla
  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 机器学习实战Python基于支持向量机SVM)是一种强大的分类器算法SVM是一种监督学习方法,可以用于解决二分类和多分类问题。 SVM的基本思想是找到一个最佳的超平面,将数据分割成不同的类别。超平面被定义为在n维空间中具有n-1维的子空间。这个子空间可以将不同类别的数据点分开,并且尽可能地最大化边界。这就意味着SVM在分类时尽量避免误分类,并且对于新的未知数据具有较好的泛化能力。 在Python中,我们可以使用scikit-learn库中的SVM实现机器学习任务。首先,我们需要导入必要的库和数据集。然后,我们可以对数据集进行预处理,如特征缩放和数据划分。接下来,我们可以创建一个SVM分类器,并使用训练数据进行模型的训练。训练完成后,我们可以使用测试数据进行预测,并评估模型的性能。 SVM还有一些重要的参数需要调节,如C和gamma。C表示惩罚项的权重,用于控制分类器的错误率和边界的平衡。较大的C值会减小错误率,但可能导致边界过拟合。gamma表示径向基函数核的参数,影响分类器的决策边界的灵活性。较大的gamma值会导致边界更加精确地拟合训练数据,但可能导致过拟合。 总的来说,机器学习实战Python基于支持向量机SVM)是一个强大的分类器算法,可以用于解决二分类和多分类问题。在实际应用中,我们需要注意调节参数,使得分类器具有良好的泛化能力。 ### 回答2: 机器学习实战是一本非常实用的书籍,其中详细介绍了如何使用Python编程语言基于支持向量机SVM)进行机器学习实践。 支持向量机是一种强大的监督学习算法,可以用于解决分类和回归问题。该算法通过寻找一个最优的超平面来分割样本空间,使得不同类别的样本尽可能远离超平面。实际上,SVM通过最大化支持向量与超平面的距离,来确保分类的准确性和泛化能力。 在书籍中,作者通过经典的例子和详细的代码示例,展示了如何应用Python编程语言和scikit-learn库来构建和训练SVM模型。读者将学会如何准备数据集,进行特征选择和数据预处理,选择合适的SVM参数以及评估模型的性能。 此外,书中还强调了交叉验证、网格搜索和模型调优等重要概念。这些概念是整个机器学习过程中不可或缺的一部分,能够帮助我们提高模型的准确性和可靠性。 机器学习实战还提供了丰富的示例和应用,涵盖了多个领域,如文本分类、手写数字识别人脸识别等。通过这些实例,读者可以深入理解SVM在实际问题中的应用。 总而言之,机器学习实战是一本非常实用的书籍,提供了丰富的例子和代码,使读者能够快速上手并应用SVM算法解决实际问题。无论是对于初学者还是有一定机器学习经验的人来说,这本书都是一本值得推荐的学习资料。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值