首页 文章 精选 留言 我的

精选列表

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

使用python绘出常见函数

'''''' ''' mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = False用来解决不能使用汉字问题,需要导入matplotlib ''' import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = False import math x=np.arange(0.05,3,0.05) #设置X坐标轴 y1=[5 for i in x] plt.plot(x,y1,linewidth=2,label=u'常函数:y=5') #常函数 y2=[2*i+1 for i in x] plt.plot(x,y2,linewidth=2,label=u'一次函数:y=2x+1') #二次函数,在$内的内容能正确显示x^2 y3=[1.5*i*i-3*i+1 for i in x] plt.plot(x,y3,linewidth=2,label=u'二次函数:y=1.5$x^2$-3x+1') #幂函数,math,pow(x,y) x是底数 y是指数 y4=[math.pow(i,2) for i in x] plt.plot(x,y4,linewidth=2,label=u'幂函数:y=$x^2$') #指数函数 y5=[math.pow(2,i) for i in x] plt.plot(x,y5,linewidth=2,label=u'指数函数:y=$2^x$') #对数函数,math.log(x,y) y是可以设置的底数 y6=[math.log(i,2) for i in x] plt.plot(x,y6,linewidth=2,label=u'对数函数:y=logx2(x)') #-4pi到4pi之间产生一百个等差值 x1=np.linspace(-4*np.pi,4*np.pi,100) y7=[np.sin(i) for i in x1] y8=[np.cos(i) for i in x1] plt.plot(x1,y7,label='y=sin(x)',c='g',linewidth=2) plt.plot(x1,y8,label='y=cos(x)',c='r',linewidth=2) #突出某条具体的线 # plt.plot([1,1],[-3],5,'--',color='#999999',linestyle=2) #plt.grid(True)是否显示网格线 plt.legend(loc='lower right') plt.grid(True) plt.show()

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

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%,相对于现状流行的深度学习来说精度还是差了一点。

资源下载

更多资源
Mario

Mario

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

Nacos

Nacos

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

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册