日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

支持向量机(SVM)的实现

發布時間:2024/8/23 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 支持向量机(SVM)的实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#!/usr/bin/env python #-*-coding:utf-8-*- #支持向量積的使用,建立超平面 from sklearn import svmx=[[2,0],[1,1],[2,3]]y=[0,0,1] clf=svm.SVC(kernel='linear') #kernel='linear'線性核函數clf.fit(x,y)print(clf)print(clf.support_vectors_) #支持向量 print(clf.support_) #支持向量的位置print(clf.n_support_) #支持向量的個數print(clf.predict([2,0]))#!/usr/bin/env python #-*-coding:utf-8-*- #svm 向量積的使用2 import numpy as np import pylab as pl #畫圖 from sklearn import svmnp.random.seed(0) X=np.r_[np.random.randn(20,2)-[2,2],np.random.randn(20,2)+[2,2]] Y=[0]*20+[1]*20 #兩部分進行賦值clf=svm.SVC(kernel='linear') clf.fit(X,Y)w=clf.coef_[0] a=-w[0]/w[1] xx=np.linspace(-5,5) yy=a*xx-(clf.intercept_[0])/w[1]b=clf.support_vectors_[0] yy_down=a*xx+(b[1]-a*b[0]) b=clf.support_vectors_[-1] yy_up=a*xx+(b[1]-a*b[0])print('w: ',w)print('a: ',a)print('support_vectors_: ',clf.support_vectors_) print('clf.coef_: ',clf.coef_)pl.plot(xx,yy,'k-') pl.plot(xx,yy_down,'k--') pl.plot(xx,yy_up,'k--')pl.scatter(clf.support_vectors_[:,0],clf.support_vectors_[:,1],s=80,facecolors='none') pl.scatter(X[:,0],X[:,1],c=Y,cmap=pl.cm.Paired)pl.axis('tight') pl.show()#!/usr/bin/env python #-*-coding:utf-8-*- #svm 實現人臉識別 from __future__ import print_functionfrom time import time import logging import matplotlib.pyplot as pltfrom 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__)logging.basicConfig(level=logging.INFO,format='%(asctime)s %(message)s') #進展打印 lfw_people=fetch_lfw_people(min_faces_per_person=70,resize=0.4) #加載數據 n_samples,h,w=lfw_people.images.shape #個數圖片,圖片的h,w X=lfw_people.data #特征向量 n_features=X.shape[1] #特征向量的維數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)X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.25) #分類n_components=150print('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))eignfaces=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_train_pca=pca.transform(X_test) print('done in %0.3fs' % (time()-t0))#分類用支持向量機 print('Fitting the classifier to the training set') t0=time() param_grid={'C':[1e3,5e3,1e4,5e4,1e5],'gamma':[0.0001,0.0005,0.001,0.005,0.01,0.1],} #特征值不同比例進行嘗試clf=GridSearchCV(SVC(kernel='rbf',class_weight='auto'),param_grid) #準確率高那個clf=clf.fit(X_train_pca,y_train) print('done in %0.3fs' % (time()-t0)) print('Best extimator found by grid search:') print(clf.best_estimator_)#評估準確率 print("Predicting people's names on the test set") t0=time() y_pred=clf.predict(X_test_pca) #預測 print('done in %0.3fs' % (time()-t0))#print(classification_report(y_test,y_pred,target_names=target_names)) #print(confusion_matrix(y_test,y_pred,labels=range(n_classes))) #顯示正確值與測試值 #def plot_gallery(images,titles,h,w,n_row=3,n_col=4):# 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(())#def title(y_pred,y_test,target_names,i):# pred_name=target_names[y_pred[i]].resplit(' ',1)[-1]# true_name=target_names[y_test[i]].resplit(' ',1)[-1]# return 'predicted: %s\ntrue: %s' % (pred_name,true_name)#prediction_titels=[title(y_pred,y_test,target_names,i) for i in range(y_pred.shape[0])]#plot_gallery(X_test,prediction_titles,h,w)#eigenface_titles=['eigenface %d' % i for i in range(eigenfaces.shape[0])] #plot_gallery(eigenfaces,eigenface_titles.h,w)plt.show()

總結

以上是生活随笔為你收集整理的支持向量机(SVM)的实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。