首页 文章 精选 留言 我的

精选列表

搜索[Python版本],共10000篇文章
优秀的个人博客,低调大师

python svm pca实践二

继上一片的内容,这片来·讲一下sklearn来进行简单的人脸识别,这里用的方法是pca和svm 先导入必要的包和数据集 import numpy as np import matplotlib.pyplot as plt from scipy import stats from sklearn.decomposition import PCA from sklearn.svm import SVC from sklearn import datasets lfw_people = datasets.fetch_lfw_people(min_faces_per_person=70, \ resize=0.4) sklearn的人脸数据集包含5千多个不同人的人脸,但有些人的人脸只包含一张, n_samples, h, w = lfw_people.images.shape print('height and width of images:', h, w) # The images in X have been collapsed into a 1D array # just like for the handwritten digits X = lfw_people.data # X.shape[0] tells you the number of images (faces); # this is the same as n_samples ahove # X.shape[1] gives the number of pixels for each image # or, "features" print('X.shape', X.shape) n_features = X.shape[1] # the label/target to predict is the id of the person -- y is an integer y = lfw_people.target # target_names are actually names target_names = lfw_people.target_names print('target_names.shape', target_names.shape) print('target_names', target_names) # n_classes gives the number of people # Different from the number of faces (n_samples)!! n_classes = target_names.shape[0] print("Total dataset size:") print("n_samples (number of faces): {0}".format(n_samples)) # n_features = 1850, which is 50x37, the dimension of the images. print("n_features (number of pixels): {0}".format(n_features)) print("n_classes (number of people): {0}".format(n_classes)) 通过打印可以看到数据集人脸的尺寸为50x37,为7类共1288张人脸 pca = PCA(n_components=4,whiten = True) X_proj = pca.fit_transform(X[:500]) print("eigen vector",pca.components_) print("...") print('eigen value', pca.explained_variance_[:2]) print(np.var(X_proj[:,0])) print(np.var(X_proj[:,1])) 取500组数据将其降维为4个维度,并进行归一化处理 explained_variance_,它代表降维后的各主成分的方差值。方差值越大,则说明越是重要的主成分 from sklearn import svm def plot_faces(n_features): # nside = 1 X = lfw_people.data # fig, axes = plt.subplots(nside, nside, figsize=(8, 8)) plt.imshow(X[5].reshape(50,37)) plot_faces(n_features= 16) plt.show() 试着打一下其中的一幅图片 Xtrain = lfw_people.data[:1000] Xtest = lfw_people.data[1000:,] ytrain = lfw_people.target[:1000] ytest = lfw_people.target[1000:,] # Xtest = X[select_idx].reshape(1, -1) # test_img = X[select_idx] # ytest = y[select_idx] # n_comp = 50 pca = PCA(n_comp, whiten = True) pca.fit(Xtrain) # pca.fit(Xtest) Xtrain_proj = pca.transform(Xtrain) # projecting test data onto pca axes Xtest_proj = pca.transform(Xtest) print(Xtrain_proj.shape) print(Xtest_proj.shape) # ************************************* The SVM Section ******************************** # instantiating an SVM classifier clf = svm.SVC(gamma=0.001, C=100.) # apply SVM to training data and draw boundaries. clf.fit(Xtrain_proj, ytrain) # Use SVM-determined boundaries to make # a prediction for the test data point. ypred = clf.predict(Xtest_proj) correct = np.sum(ytest == ypred) print(correct/288*100) 接下来之前载入的数据用pca和svm进行训练识别,在1288个数据中取前1000组为训练集,后288个为测试集,pca将维为50维,并用训练集训练的模型对测试集进行预测,最后的测试精度为:81.25%,相对于现状流行的深度学习来说精度还是差了一点。

优秀的个人博客,低调大师

初学Python——集合及其运算

一、集合定义及其功能 集合是一个无序的、不重复的数据组合,和字典列表一样也是一种数据类型。 集合两个最主要的功能:①去重(把一个列表变成集合,就自动去重了) ②关系测试(测试两组数据之间的交、并、差集等关系) 二、关系测试 首先,如何创建集合? 两种方式:(1)直接创建 a=set([1,8,2,3,5,4,5,3]) (2)将列表转化成集合 list1=[5,9,7,3,6,6,7,95,78,65,102] #创建列表 list1=set(list1) #将列表转化成集合 在这里需要说明,集合是无序的,尽管将集合a输出:print(a),得到的结果是 看上去好像自动做了排序,其实不然,只是集合的数据太少了,输出list1就不是这样了: 1.取两个集合的交集: 两种写法:①intersection()方法 print(list1.intersection(a)) print(a.intersection(list1)) 这两种写法没有任何区别,输出结果都是 ②数学表示 print(list_1 & a) 2.取并集: 也是两种方法:①union()方法 print("union:",a.union(list_1)) ②数学表示 print("union:",a|list_1) 输出结果: 3.取差集: 亦是两种方法:①difference()方法 print("difference:",list_1.difference(a)) #差集(list_1减去交集) print("difference:",a.difference(list_1)) #差集(a减去交集) ②数学表示 print("list_1 - a :",list_1-a) print("a - list_1 :",a-list_1) 运行结果: 4.判断是否是父集和子集的关系: issubset()方法和issuperset()方法 print("issubset:",list_1.issubset(a)) #判断list_1是不是a的子集 print("issuperset:",a.issuperset(list_1)) #判断a是不是List_1的父集 当然,肯定都不是,再创建一个集合试一试 list_2=set([1,3,5]) print(list_2.issubset(a)) #判断list_2是不是a的子集,是则返回True print(a.issuperset(list_2)) #判断a是不是list_2的父集,不是返回False 运行结果: 5.对称差集: 对称差集是指两个集合的并集减去交集 ①symmetric_difference()方法 print("symmetric_difference:",a.symmetric_difference(list_1)) ②数学表示 print("a^list_1:",a^list_1) 运行结果: 6.判断是不是相交 list_3=set([50,30,60]) print("isdisjoint:",a.isdisjoint(list_3)) #判断两个集合之间的关系是不是相交,如果相交返回False print("isdisjoint:",a.isdisjoint(list_2)) 运行结果: 三、集合的操作 1.添加数据 添加一项: a.add("abc") #添加一项 添加多项: a.update([20,30,90]) 2.删除数据 ①remove()方法 a.remove("abc") #删除字符“abc” ②pop()方法 print(a.pop()) #随机删除,并返回删除的值 ③discard()方法 a.discard(30) #删除数据 3.获取集合的长度 print(len(a)) #集合 a 的长度 4.判断数据是否是集合的成员 print(30 in a) #判断30是否是集合a的成员(列表、字典、集合、字符串都可以用这种方法来判断) print(30 not in a) #判断30是否不是集合a的成员

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册
修改密码
忘记密码