核化主成分分析(Kernel PCA)应用及调参
核化这个概念在很多机器学习方法中都有应用,如SVM,PCA等。在此结合sklearn中的KPCA说说核函数具体怎么来用。
KPCA和PCA都是用来做无监督数据处理的,但是有一点不一样。PCA是降维,把m维的数据降至k维。KPCA恰恰相反,它是把m维的数据升至k维。但是他们共同的目标都是让数据在目标维度中(线性)可分,即PCA的最大可分性。
在sklearn中,kpca和pca的使用基本一致,接口都是一样的。kpca需要指定核函数,不然默认线性核。
首先我们用下面的代码生成一组数据。
import numpy as np
from sklearn.decomposition import PCA, KernelPCA
import matplotlib.pyplot as plt import math
x=[]
y=[]
N = 500
for i in range(N):
deg = np.random.randint(0,360)
if np.random.randint(0,2)%2==0:
x.append([6*math.sin(deg), 6*math.cos(deg)])
y.append(1)
else:
x.append([15*math.sin(deg), 15*math.cos(deg)])
y.append(0)
y = np.array(y)
x = np.array(x)
print('ok')
这些数据可以用下图来表示
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXwAAAD8CAYAAAB0IB+mAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAIABJREFUeJztnW9sHPd5578Pl6aSpRzEXvoMWzGXycE9nHTAGRBh4JBL4ILExckbNwVaJLdyhMCALG5aCHf3JoVe1CggoDhcEBiHkg6LuFa02xh5E9RohAQhizvjXqUU4CaiAl/VRFREu7HIOAdLtCWLfO7Fb3/d4XL+7s7/+X6Awe7OzO78ZmbnO888v+f3PKKqIIQQUn7Gsm4AIYSQdKDgE0JIRaDgE0JIRaDgE0JIRaDgE0JIRaDgE0JIRaDgE0JIRaDgE0JIRaDgE0JIRRjPugFOpqamdGZmJutmEEJIobh06dKWqj4UtF6uBH9mZgZra2tZN4MQQgqFiGyEWY8uHUIIqQgUfEIIqQgUfEIIqQgUfEIIqQgUfEIIqQixCL6IvCwi74jIZce8F0RkU0Te6E1fiGNbhCTC/Dwg0p8+8hFgbAyYmQG6XbNOuw2Mj5vltRpw+LBZZ2rKTIPrE5I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bMwoick1EftY7D4XKVCgiL4vIOyJy2THvQRH5sYj8Y+/1gSzbGBaPfUn8Oim84AO4DOD3AbzusuyfVPWJ3nQ65XYNg+u+iMhRAF8CcAzA0wAWbb2BAvFNx7m4mHVjwtI7zn8B4PMAjgL4cu98FJnf7Z2HooUzvgLz/3fydQCrqvo4gNXe5yLwCg7uC5DwdVJ4wS9TIXWffXkGwKuqekdVfwngKoAn021dZXkSwFVV/YWq3gXwKsz5ICmjqq8DGKyr8QyA87335wH8XqqNGhKPfUmcwgt+AGUppH4EwK8cn2/05hWJPxaRn/YeZQvx2N2jDMfeiQJYEZFLInIqcO3887Cqvt17/88AHs6yMTGQ6HVSCMFPupB6mgy5L7knYL+WAHwKwBMw5+UbmTa22vzH3jXxeQBfE5HPZt2guFATcljksMPEr5ORShymRdKF1NNkmH0BsAngMcfnT/Tm5Yaw+yUifwngbxNuTpzk/thHQVU3e6/viMj3YVxWbv1fReHXIvKIqr4tIo8AeCfrBg2Lqv7avk/qOimEhT8MJSuk/hqAL4nIIRH5JMy+/CTjNoWmdyFavgjTOV0U/h7A4yLySRGZgOk8fy3jNg2FiEyKyP32PYD/hGKdCzdeA3Cy9/4kgL/JsC0jkcZ1UggL348yFVL32hdVXReR7wG4AuAegK+p6m6WbY3IfxeRJ2Aet68BeD7b5oRHVe+JyB8B+BGAGoCXVXU942YNy8MAvi8igLn2/1pVf5htk8IjIt8F8BSAKRG5AeBPAfw5gO+JyHMwmXb/MLsWhsdjX55K+jrhSFtCCKkIpXXpEEII2Q8FnxBCKgIFnxBCKgIFnxBCKgIFnxBCKgIFnxBCKgIFnxBCKgIFnxBCKsL/B7+BXso7KyACAAAAAElFTkSuQmCC" alt="" />
显然,我们选的正样本(蓝)都落在一个半径为6的圆上,负样本(红)全选在一个半径为15的圆上。
这样的数据显然是线性不可分的。如果我们强行要用线性分类器来做,可以对原始数据做一个kpca处理。
如下代码:
kpca = KernelPCA(kernel="rbf", n_components=14)
x_kpca = kpca.fit_transform(x)
我们用rbf核,指定维度为14(为啥是14,后面说),也就是说吧X(2维)中的数据映射到14维的空间中去。
然后我们用一个线性的SVM分类器来对映射后的数据做一个分类,随机取80%做训练集,20%做测试集。
from sklearn import svm
clf = svm.SVC(kernel='linear') clf.fit(x_kpca[:0.8*N],y[:0.8*N])
y0 = y[0.8*N:]
y1 = clf.predict(x_kpca[0.8*N:])
print(np.linalg.norm(y0-y1, 1))
此时,输出的是0,也就是说我们预测出的分类与正确的分类完全吻合,没有一个错误。此时分类器在这个数据集上是完全有效的。
综上所述,通过核化,可以把原本线性不可分的数据映射到高维空间后实现线性可分。
不过这个过程也不完全是那么简单,我们取不同的维度得到的结果可能相差甚远,也就是经典的调参问题。
在此,我测试了几组不同的k取值,得到不同k值条件下分类准确率值。如下图。
可以看出,不同的k值对结果是有影响的。但不同的数据集可能不一样,所以需要进行调参。我这个取14就全ok了。
核化主成分分析(Kernel PCA)应用及调参的更多相关文章
- 深入学习主成分分析(PCA)算法原理(Python实现)
一:引入问题 首先看一个表格,下表是某些学生的语文,数学,物理,化学成绩统计: 首先,假设这些科目成绩不相关,也就是说某一科目考多少分与其他科目没有关系,那么如何判断三个学生的优秀程度呢?首先我们一眼 ...
- python 机器学习中模型评估和调参
在做数据处理时,需要用到不同的手法,如特征标准化,主成分分析,等等会重复用到某些参数,sklearn中提供了管道,可以一次性的解决该问题 先展示先通常的做法 import pandas as pd f ...
- 基于pytorch的CNN、LSTM神经网络模型调参小结
(Demo) 这是最近两个月来的一个小总结,实现的demo已经上传github,里面包含了CNN.LSTM.BiLSTM.GRU以及CNN与LSTM.BiLSTM的结合还有多层多通道CNN.LSTM. ...
- sklearn中SVM调参说明
写在前面 之前只停留在理论上,没有实际沉下心去调参,实际去做了后,发现调参是个大工程(玄学).于是这篇来总结一下sklearn中svm的参数说明以及调参经验.方便以后查询和回忆. 常用核函数 1.li ...
- [调参]CV炼丹技巧/经验
转自:https://www.zhihu.com/question/25097993 我和@杨军类似, 也是半路出家. 现在的工作内容主要就是使用CNN做CV任务. 干调参这种活也有两年时间了. 我的 ...
- 贪玩ML系列之CIFAR-10调参
调参方法:网格调参 tf.layers.conv2d()中的padding参数 取值“same”,表示当filter移出边界时,给空位补0继续计算.该方法能够更多的保留图像边缘信息.当图片较小(如CI ...
- Deep learning网络调参技巧
参数初始化 下面几种方式,随便选一个,结果基本都差不多.但是一定要做.否则可能会减慢收敛速度,影响收敛结果,甚至造成Nan等一系列问题.n_in为网络的输入大小,n_out为网络的输出大小,n为n_i ...
- 机器学习笔记——模型调参利器 GridSearchCV(网格搜索)参数的说明
GridSearchCV,它存在的意义就是自动调参,只要把参数输进去,就能给出最优化的结果和参数.但是这个方法适合于小数据集,一旦数据的量级上去了,很难得出结果.这个时候就是需要动脑筋了.数据量比较大 ...
- 调参、最优化、ml算法(未完成)
最优化方法 调参方法 ml算法 梯度下降gd grid search lr 梯度上升 随机梯度下降 pca 随机梯度下降sgd 贝叶斯调参 lda 牛顿算法 knn 拟牛顿算法 kmeans ...
随机推荐
- WIFI底座
自己贴片的51+WIFI的开发板终于到了..还是贴片的好看 美中不足的是需要改一个电阻的阻值..还有就是由于自己的8266和51单片机一块断电上电,所以如果用的USB线的质量不好就会出现 下载不了程序 ...
- C#中byte[]类型转换为其它类型
我们这里就举byte[]类型和long类型的转换,其它数据类型用BitConverter类以此类推: /// <summary> /// 字节数组转换为long类型 /// </su ...
- CF954I Yet Another String Matching Problem 并查集、FFT
传送门 题意:给出两个由小写$a$到$f$组成的字符串$S$和$T$($|S| \geq |T|$),给出变换$c1\,c2$表示将两个字符串中所有$c1$字符变为$c2$,求$S$的每一个长度为$T ...
- React-使用combineReducers完成对数据对拆分管理
数据都放在reducer.js下不利于对数据进行管理,可以把一个大的reducer.js拆分成多个小的reducer.js. 小的reducer.js const defaultState={ foc ...
- JAVA核心:内存、比较和Final
1.java是如何管理内存的 java的内存管理就是对象的分配和释放问题.(其中包括两部分) 分配:内存的分配是由程序完成的,程序员需要通过关键字new为每个对象申请内存空间(基本类型除外),所有的对 ...
- 【下一代核心技术DevOps】:(七)持续集成Jenkins的应用(Aliyun Pipiline持续构建)
1. 前言 使用Jenkins比较好的就是可以在整个构建顺序中增加自定义的动作,比如构建成功给Leader发个邮件,给团队核心发个微信什么的. 当然最基本的核心还是它可以构建多种开发语言的项目,此类构 ...
- NB-IOT_BC95_B5常用AT指令集
.AT+<cmd>=? 测试命令,用于向模块询问支持的设置项目. .AT+<cmd>? 读取命令,用于让模块上报某个命令代表的设置项当前的值. .AT+<cmd>= ...
- kvm虚拟化管理平台WebVirtMgr部署-完整记录(2)
继上一篇kvm虚拟化管理平台WebVirtMgr部署-完整记录(1),接下来说说WebVirtMgr的日常配置:添加宿主机,创建虚机,磁盘扩容,快照等具体操作记录如下: 一.配置宿主机1.登录WebV ...
- Python下操作Memcache/Redis/RabbitMQ说明
一.MemcacheMemcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的.需要频繁访 ...
- B. Diagonal Walking v.2
链接 [https://i.cnblogs.com/EditPosts.aspx?opt=1] 题意 二维平面从原点出发k步,要到达的点(x,y),每个位置可以往8个方位移动,问到达目的地最多可以走多 ...