资源简介
此文档使用python语言实现SVM经典机器学习算法,可以帮助初学者更好的掌握。

代码片段和文件信息
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm datasets
from load_data import dataload
def make_meshgrid(x y h=.02):
“““Create a mesh of points to plot in
Parameters
----------
x: data to base x-axis meshgrid on
y: data to base y-axis meshgrid on
h: stepsize for meshgrid optional
Returns
-------
xx yy : ndarray
“““
x_min x_max = x.min() - 1 x.max() + 1
y_min y_max = y.min() - 1 y.max() + 1
xx yy = np.meshgrid(np.arange(x_min x_max h)
np.arange(y_min y_max h))
return xx yy
def plot_contours(ax clf xx yy **params):
“““Plot the decision boundaries for a classifier.
Parameters
----------
ax: matplotlib axes object
clf: a classifier
xx: meshgrid ndarray
yy: meshgrid ndarray
params: dictionary of params to pass to contourf optional
“““
Z = clf.predict(np.c_[xx.ravel() yy.ravel()])
Z = Z.reshape(xx.shape)
out = ax.contourf(xx yy Z **params)
return out
# import some data to play with
iris = datasets.load_iris()
# Take the first two features. We could avoid this by using a two-dim dataset
X = iris.data[: :2]
y = iris.target
# we create an instance of SVM and fit out data. We do not scale our
# data since we want to plot the support vectors
C = 1.0 # SVM regularization parameter
models = (svm.SVC(kernel=‘linear‘ C=C)
svm.LinearSVC(C=C)
svm.SVC(kernel=‘rbf‘ gamma=0.7 C=C)
svm.SVC(kernel=‘poly‘ degree=3 C=C))
models = (clf.fit(X y) for clf in models)
# title for the plots
titles = (‘SVC with linear kernel‘
‘LinearSVC (linear kernel)‘
‘SVC with RBF kernel‘
‘SVC with polynomial (degree 3) kernel‘)
# Set-up 2x2 grid for plotting.
fig sub = plt.subplots(2 2)
plt.subplots_adjust(wspace=0.4 hspace=0.4)
X0 X1 = X[: 0] X[: 1]
xx yy = make_meshgrid(X0 X1)
for clf title ax in zip(models titles sub.flatten()):
plot_contours(ax clf xx yy
cmap=plt.cm.coolwarm alpha=0.8)
ax.scatter(X0 X1 c=y cmap=plt.cm.coolwarm s=20 edgecolors=‘k‘)
ax.set_xlim(xx.min() xx.max())
ax.set_ylim(yy.min() yy.max())
ax.set_xlabel(‘Sepal length‘)
ax.set_ylabel(‘Sepal width‘)
ax.set_xticks(())
ax.set_yticks(())
ax.set_title(title)
plt.show()
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 62 2018-09-08 20:58 SVM\svm\.spyproject\codest
文件 64 2018-09-08 20:58 SVM\svm\.spyproject\encoding.ini
文件 92 2018-09-08 20:58 SVM\svm\.spyproject\vcs.ini
文件 281 2019-03-28 11:52 SVM\svm\.spyproject\workspace.ini
文件 196048 2018-10-25 19:07 SVM\svm\archive.txt
文件 4700 2018-08-28 17:49 SVM\svm\DATA.txt
文件 2490 2018-09-09 15:45 SVM\svm\iris_dataset.py
文件 2703 2018-11-02 15:12 SVM\svm\load_data.py
文件 29322 2018-11-02 16:03 SVM\svm\TestingData.txt
文件 321 2018-09-09 15:45 SVM\svm\train.py
文件 88248 2018-11-02 16:03 SVM\svm\TrainingData.txt
文件 1475 2018-11-02 16:03 SVM\svm\__pycache__\load_data.cpython-36.pyc
文件 4688 2018-09-29 20:07 SVM\svm\人脸识别.py
目录 0 2018-09-08 20:58 SVM\svm\.spyproject
目录 0 2018-11-02 16:03 SVM\svm\__pycache__
目录 0 2018-11-02 15:12 SVM\svm
目录 0 2018-09-08 20:58 SVM
----------- --------- ---------- ----- ----
330494 17
相关资源
- 二级考试python试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
评论
共有 条评论