#!/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)的实现相关推荐

  1. Python,OpenCV基于支持向量机SVM的手写数字OCR

    Python,OpenCV基于支持向量机SVM的手写数字OCR 1. 效果图 2. SVM及原理 2. 源码 2.1 SVM的手写数字OCR 2.2 非线性SVM 参考 上一节介绍了基于KNN的手写数 ...

  2. Udacity机器人软件工程师课程笔记(二十二) - 物体识别 - 色彩直方图,支持向量机SVM

    物体识别 1.HSV色彩空间 如果要进行颜色检测,HSV颜色空间是当前最常用的. HSV(Hue, Saturation, Value)是根据颜色的直观特性由A. R. Smith在1978年创建的一 ...

  3. 支持向量机(SVM)简介

    支持向量机(support vector machine, SVM):是监督学习中最有影响力的方法之一.类似于逻辑回归,这个模型也是基于线性函数wTx+b的.不同于逻辑回归的是,支持向量机不输出概率, ...

  4. R语言分类模型:逻辑回归模型LR、决策树DT、推理决策树CDT、随机森林RF、支持向量机SVM、Rattle可视化界面数据挖掘、分类模型评估指标(准确度、敏感度、特异度、PPV、NPV)

    R语言分类模型:逻辑回归模型LR.决策树DT.推理决策树CDT.随机森林RF.支持向量机SVM.Rattle可视化界面数据挖掘.分类模型评估指标(准确度.敏感度.特异度.PPV.NPV) 目录

  5. R语言e1071包中的支持向量机:构建nu-classification类型的支持向量机SVM并分析不同nu值惩罚下模型分类螺旋线型(sprials)线性不可分数据集的表现

    R语言e1071包中的支持向量机:构建nu-classification类型的支持向量机SVM并分析不同nu值惩罚下模型分类螺旋线型(sprials)线性不可分数据集的表现 目录

  6. R语言e1071包中的支持向量机:仿真数据(螺旋线性不可分数据集)、简单线性核的支持向量机SVM(模型在测试集上的表现、可视化模型预测的结果、添加超平面区域与原始数据标签进行对比分析)、如何改进核函数

    R语言e1071包中的支持向量机:仿真数据(螺旋线性不可分数据集).简单线性核的支持向量机SVM(模型在测试集上的表现.可视化模型预测的结果.添加超平面区域与原始数据标签进行对比分析).如何改进核函数 ...

  7. 支持向量机SVM模型中C和gamma参数分别是什么?对模型有什么影响?

    支持向量机SVM模型中C和gamma参数分别是什么?对模型有什么影响? SVM模型有两个非常重要的参数C与gamma. C的本质是正则化系数. C值是惩罚系数或者叫惩罚因子,表征的是模型对于误差的容忍 ...

  8. 影像组学视频学习笔记(11)-支持向量机(SVM)(理论)、Li‘s have a solution and plan.

    本笔记来源于B站Up主: 有Li 的影像组学系列教学视频 本节(11)主要介绍: SVM支持向量机(理论) 支持向量机 (support vector machine, SVM) 号称是鲁棒性(rob ...

  9. 机器学习-第六章 支持向量机(SVM)

    机器学习-第六章 支持向量机(SVM) D系鼎溜关注 2020.02.09 21:19:41字数 1,131阅读 458 6.1 间隔与支持向量 开倍速观看视频之后,对课本所说的会更加了解. 支持向量 ...

  10. 支持向量机SVM 参数选择

    http://ju.outofmemory.cn/entry/119152 http://www.cnblogs.com/zhizhan/p/4412343.html 支持向量机SVM是从线性可分情况 ...

最新文章

  1. CSS/font-size和line-height属性继承的研究
  2. 阿里云存储_OSS对象存储
  3. BlockChain:《区块链技术在医疗领域应用分析》—中投顾问《2016-2020年区块链技术深度调研及投资前景预测报告》听课笔记
  4. css3 画半圆和1/4圆
  5. 常见问题_智能切膜机常见问题
  6. 有关软件工程的问题的分析和讨论及课后的作业3
  7. 外设驱动库开发笔记38:RTD热电阻测温驱动
  8. 手rm-linux联网后自动dhcp,Linux操作系统下DHCP简单设置
  9. IDEA创建Maven Web 项目
  10. 11.30 如何取得当事人的银行账号?
  11. 一文完成vosviewer共现网络,使用知网及web of science导出文献
  12. LinuxCNC Stepconf设置教程
  13. 制作网页所需的一些简单ps技巧
  14. c++ 工厂模式简介和应用场景
  15. SolidWorks如何提取stp格式装配体中的零件图
  16. 第一讲_SQP添加与查询语句
  17. 家具行业APP定制开发需具备哪些功能
  18. java中的notify和notifyAll有什么区别?
  19. 两个栈共享一块存储空间新解
  20. NB-IoT的DRX、eDRX、PSM三个模式怎么用?通俗解释,看完就懂!

热门文章

  1. tesseract4.0.0 中文语言包_一份TensorFlow2.0中文教程
  2. appender log4j 扩展_Log4j扩展使用--输出地Appender
  3. java写hive自定义函数_hive自定义函数的实现和执行
  4. 乱码 讯飞 语音识别_一段讯飞、百度等语音识别API无法识别的语音最终解决办法...
  5. python中的with open读取表格文件_python 使用 with open() as 读写文件
  6. python自动输入账号密码_Python如何基于selenium实现自动登录博客园
  7. 中兴没有云服务器_中国移动携手中兴通讯推进5G网络云建设
  8. 哥大计算机专业 世界排名,哥伦比亚大学计算机科学硕士排名第16(2020年TFE Times排名)...
  9. php 邮件发送是html 没样式_使用python发送邮件
  10. php 类常量用法,php类常量用法实例分析