支持向量机(SVM)的实现

#!/usr/bin/env python
#-*-coding:utf-8-*-
#支持向量积的使用,建立超平面
from sklearn import svm

x=[[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 svm

np.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_function

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__)

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=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))

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()

















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值