python svm pca实践(一)
好久没写博客了
这里主要用python的sklearn包,来进行简单的svm的分类和pca的降维
svm是常用的分类器,其核心是在分类的时候找到一个最优的超平面,使得所有的样本与超平面之间的距离达到最小。
pca是常用的一种降维的方法,其核心是对去中心化后的数据,求得协方差矩阵,再对协方差矩阵进行特征分解,将最大的几个特征值作为这个样本的的新特征,达到降低数据特征维度的效果
这里用sklearn的digits数据集作为演示数据集
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
# ***use seaborn plotting style defaults
import seaborn as sns; sns.set()
from sklearn import decomposition
from sklearn.decomposition import PCA
from sklearn.datasets import load_digits
#********************* KEY IMPORT OF THIS LECTURE********************************
from sklearn import svm
# loading handwritten digits
dig_data = load_digits()
X = dig_data.data
# y: the values of the digits, or "ground truth"
y = dig_data.target
dig_img = dig_data.images
print(type(X), X.dtype, X.shape)
print(type(dig_img), dig_img.dtype, dig_img.shape)
print(type(y), y.dtype, y.shape)
先导入所需要的包,并下载所需要的数据集
这里主要用到digits数据集中的.data,.target,.images属性
.data 为数据集的图像数据,用有1797个数据以一维的形式呈现,每个数据集的长度为64
.target为数据集的标签数据
.image位数据以8x8的形式呈现
dig_data = load_digits()
X = dig_data.data
y = dig_data.target
# This is basically each array in X
# getting reshaped into (8, 8).
dig_img = dig_data.images
print(type(X), X.dtype, X.shape)
print(type(y), y.dtype, y.shape)
select_idx = 2
# select_idx = 5
# ********************************Separating training data from testing data****************
Xtrain = np.delete(X, select_idx, axis = 0)
ytrain = np.delete(y, select_idx)
# if you don't do .reshape(1, -1), you get a warning.
# B/c the data argument for classifier has to be an array,
# even if it's a one-element array.
Xtest = X[select_idx].reshape(1, -1)
test_img = dig_img[select_idx]
ytest = y[select_idx]
print('Xtrain.shape, ytrain.shape', Xtrain.shape, ytrain.shape)
print('Xtest.shape, ytest.shape', Xtest.shape, ytest.shape)
plt.figure(figsize = (4, 4))
plt.imshow(test_img, cmap = 'binary')
plt.grid('off')
plt.axis('off')
# ************************************* The PCA Section ********************************
n_comp = 10
pca = PCA(n_comp)
# finding pca axes
pca.fit(Xtrain)
# projecting training data onto pca axes
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.
clf.predict(Xtest_proj)
将数据集分成训练集和测试集,将索引为2的数据从整个数据集中剔除作为测试数据集,剩下的数据作为训练数据集,
n_comp为PCA降维后取得维数,
用训练集来训练PCA,再用训练集的模型来对训练集和测试集进行降维,
接着用训练集来训练SVM,并对测试集进行预测
def classify_dig_svm(X, y, dig_img, select_idx, n_comp, plot_test_img = False):
dig_data = load_digits()
X = dig_data.data
y = dig_data.target
dig_img = dig_data.images
Xtrain = np.delete(X, select_idx, axis = 0)
ytrain = np.delete(y, select_idx)
Xtest = X[select_idx].reshape(1, -1)
test_img = dig_img[select_idx]
ytest = y[select_idx]
if plot_test_img == True:
plt.figure(figsize = (4, 4))
plt.imshow(test_img, cmap = 'binary')
plt.grid('off')
plt.axis('off')
n_comp = 10
pca = PCA(n_comp)
pca.fit(Xtrain)
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)
clf.predict(Xtest_proj)
return clf.predict(Xtest_proj)
X = dig_data.data
y = dig_data.target
n_comp = 30
# select_idx =
# classify_dig_svm(X, y, dig_img, select_idx, n_comp, plot_test_img = False)
counter = 0
tot = 1797
for i in range(tot):
if classify_dig_svm(X, y, dig_img, i, n_comp, plot_test_img = False) == y[i]:
counter += 1
rate = counter/tot
print(rate)
classify_dig_svm的方法是对每个数据进行测试看一下训练出来的pca模型和svm模型的准确性
其实效果还好

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
最详细的JavaWeb开发基础之java环境搭建(Mac版)
阅读文本大概需要 5 分钟。 我之前分享过在 Windows 下面配置 Java 环境,这次给大家带来的是 Mac 下面安装配置 Java 环境。首先 Mac 系统已经带有默认的 Java,但是由于使用不方便,这里教大家一个比较方便的方法,并且管理方便。也方便我们后面配置 IDEA, Eclipse。 下面开始我们 Java 环境的安装配置。 1、打开 Java 官网 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 在这里你可以选择你要下载的 java 版本,这里我们以 java1.8 为例来讲解。在下载之前,我们需要先接受协议才能够去下载。 这里说明下,我为什么去选择 linux 版本的 JDK 去下载,而不是 Mac 版本的 JDK,这里主要是因为如果我们下载了 Mac 版本的 JDK 在安装的时候,我们是无法去选择安装目录的,也就是它默认安装之后,你还是要在接着去 Google Mac 下 JDK 默认的安装目录,这样岂不是多次一举。其次,因为 Mac 就...
-
下一篇
初学Python——软件目录结构规范
为什么要设计好目录结构? 可读性高: 不熟悉这个项目的代码的人,一眼就能看懂目录结构,知道程序启动脚本是哪个,测试目录在哪儿,配置文件在哪儿等等。从而非常快速的了解这个项目。 可维护性高: 定义好组织规则后,维护者就能很明确地知道,新增的哪个文件和代码应该放在什么目录之下。这个好处是,随着时间的推移,代码/配置的规模增加,项目结构不会混乱,仍然能够组织良好 目录组织方式 关于如何组织一个较好的Python工程目录结构,已经有一些得到了共识的目录结构。在Stackoverflow的这个问题上,能看到大家对Python目录结构的讨论。 这里面说的已经很好了,我也不打算重新造轮子列举各种不同的方式,这里面我说一下我的理解和体会。 假设你的项目名为foo, 我比较建议的最方便快捷目录结构这样就足够了: Foo/ |-- bin/ | |-- foo | |-- foo/ | |-- tests/ | | |-- __init__.py | | |-- test_main.py | | | |-- __init__.py | |-- main.py | |-- docs/ | |-- conf....
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- 设置Eclipse缩进为4个空格,增强代码规范
- CentOS关闭SELinux安全模块
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- CentOS8编译安装MySQL8.0.19
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- MySQL数据库在高并发下的优化方案
- SpringBoot2更换Tomcat为Jetty,小型站点的福音