吴裕雄 python 机器学习——核化PCAKernelPCA模型
# -*- coding: utf-8 -*- import numpy as np
import matplotlib.pyplot as plt from sklearn import datasets,decomposition def load_data():
'''
加载用于降维的数据
'''
# 使用 scikit-learn 自带的 iris 数据集
iris=datasets.load_iris()
return iris.data,iris.target #核化PCAKernelPCA模型
def test_KPCA(*data):
X,y=data
kernels=['linear','poly','rbf','sigmoid']
# 依次测试四种核函数
for kernel in kernels:
kpca=decomposition.KernelPCA(n_components=None,kernel=kernel)
kpca.fit(X)
print('kernel=%s --> lambdas: %s'% (kernel,kpca.lambdas_)) # 产生用于降维的数据集
X,y=load_data()
# 调用 test_KPCA
test_KPCA(X,y)

...................

....................

def plot_KPCA(*data):
'''
绘制经过 KernelPCA 降维到二维之后的样本点
'''
X,y=data
kernels=['linear','poly','rbf','sigmoid']
fig=plt.figure()
# 颜色集合,不同标记的样本染不同的颜色
colors=((1,0,0),(0,1,0),(0,0,1),(0.5,0.5,0),(0,0.5,0.5),(0.5,0,0.5),(0.4,0.6,0),(0.6,0.4,0),(0,0.6,0.4),(0.5,0.3,0.2)) for i,kernel in enumerate(kernels):
kpca=decomposition.KernelPCA(n_components=2,kernel=kernel)
kpca.fit(X)
# 原始数据集转换到二维
X_r=kpca.transform(X)
## 两行两列,每个单元显示一种核函数的 KernelPCA 的效果图
ax=fig.add_subplot(2,2,i+1)
for label ,color in zip( np.unique(y),colors):
position=y==label
ax.scatter(X_r[position,0],X_r[position,1],label="target= %d"%label,
color=color)
ax.set_xlabel("X[0]")
ax.set_ylabel("X[1]")
ax.legend(loc="best")
ax.set_title("kernel=%s"%kernel)
plt.suptitle("KPCA")
plt.show() # 调用 plot_KPCA
plot_KPCA(X,y)

def plot_KPCA_poly(*data):
'''
绘制经过 使用 poly 核的KernelPCA 降维到二维之后的样本点
'''
X,y=data
fig=plt.figure()
# 颜色集合,不同标记的样本染不同的颜色
colors=((1,0,0),(0,1,0),(0,0,1),(0.5,0.5,0),(0,0.5,0.5),(0.5,0,0.5),(0.4,0.6,0),(0.6,0.4,0),(0,0.6,0.4),(0.5,0.3,0.2))
# poly 核的参数组成的列表。
# 每个元素是个元组,代表一组参数(依次为:p 值, gamma 值, r 值)
# p 取值为:3,10
# gamma 取值为 :1,10
# r 取值为:1,10
# 排列组合一共 8 种组合
Params=[(3,1,1),(3,10,1),(3,1,10),(3,10,10),(10,1,1),(10,10,1),(10,1,10),(10,10,10)]
for i,(p,gamma,r) in enumerate(Params):
# poly 核,目标为2维
kpca=decomposition.KernelPCA(n_components=2,kernel='poly',gamma=gamma,degree=p,coef0=r)
kpca.fit(X)
# 原始数据集转换到二维
X_r=kpca.transform(X)
## 两行四列,每个单元显示核函数为 poly 的 KernelPCA 一组参数的效果图
ax=fig.add_subplot(2,4,i+1)
for label ,color in zip( np.unique(y),colors):
position=y==label
ax.scatter(X_r[position,0],X_r[position,1],label="target= %d"%label,
color=color)
ax.set_xlabel("X[0]")
# 隐藏 x 轴刻度
ax.set_xticks([])
# 隐藏 y 轴刻度
ax.set_yticks([])
ax.set_ylabel("X[1]")
ax.legend(loc="best")
ax.set_title(r"$ (%s (x \cdot z+1)+%s)^{%s}$"%(gamma,r,p))
plt.suptitle("KPCA-Poly")
plt.show() # 调用 plot_KPCA_poly
plot_KPCA_poly(X,y)

def plot_KPCA_rbf(*data):
'''
绘制经过 使用 rbf 核的KernelPCA 降维到二维之后的样本点
'''
X,y=data
fig=plt.figure()
# 颜色集合,不同标记的样本染不同的颜色
colors=((1,0,0),(0,1,0),(0,0,1),(0.5,0.5,0),(0,0.5,0.5),(0.5,0,0.5),(0.4,0.6,0),(0.6,0.4,0),(0,0.6,0.4),(0.5,0.3,0.2))
# rbf 核的参数组成的列表。每个参数就是 gamma值
Gammas=[0.5,1,4,10]
for i,gamma in enumerate(Gammas):
kpca=decomposition.KernelPCA(n_components=2,kernel='rbf',gamma=gamma)
kpca.fit(X)
# 原始数据集转换到二维
X_r=kpca.transform(X)
## 两行两列,每个单元显示核函数为 rbf 的 KernelPCA 一组参数的效果图
ax=fig.add_subplot(2,2,i+1)
for label ,color in zip( np.unique(y),colors):
position=y==label
ax.scatter(X_r[position,0],X_r[position,1],label="target= %d"%label,
color=color)
ax.set_xlabel("X[0]")
# 隐藏 x 轴刻度
ax.set_xticks([])
# 隐藏 y 轴刻度
ax.set_yticks([])
ax.set_ylabel("X[1]")
ax.legend(loc="best")
ax.set_title(r"$\exp(-%s||x-z||^2)$"%gamma)
plt.suptitle("KPCA-rbf")
plt.show() # 调用 plot_KPCA_rbf
plot_KPCA_rbf(X,y)

def plot_KPCA_sigmoid(*data):
'''
绘制经过 使用 sigmoid 核的KernelPCA 降维到二维之后的样本点
'''
X,y=data
fig=plt.figure()
# 颜色集合,不同标记的样本染不同的颜色
colors=((1,0,0),(0,1,0),(0,0,1),(0.5,0.5,0),(0,0.5,0.5),(0.5,0,0.5),(0.4,0.6,0),(0.6,0.4,0),(0,0.6,0.4),(0.5,0.3,0.2))
# sigmoid 核的参数组成的列表。
Params=[(0.01,0.1),(0.01,0.2),(0.1,0.1),(0.1,0.2),(0.2,0.1),(0.2,0.2)]
# 每个元素就是一种参数组合(依次为 gamma,coef0)
# gamma 取值为: 0.01,0.1,0.2
# coef0 取值为: 0.1,0.2
# 排列组合一共有 6 种组合
for i,(gamma,r) in enumerate(Params):
kpca=decomposition.KernelPCA(n_components=2,kernel='sigmoid',gamma=gamma,coef0=r)
kpca.fit(X)
# 原始数据集转换到二维
X_r=kpca.transform(X)
## 三行两列,每个单元显示核函数为 sigmoid 的 KernelPCA 一组参数的效果图
ax=fig.add_subplot(3,2,i+1)
for label ,color in zip( np.unique(y),colors):
position=y==label
ax.scatter(X_r[position,0],X_r[position,1],label="target= %d"%label,
color=color)
ax.set_xlabel("X[0]")
# 隐藏 x 轴刻度
ax.set_xticks([])
# 隐藏 y 轴刻度
ax.set_yticks([])
ax.set_ylabel("X[1]")
ax.legend(loc="best")
ax.set_title(r"$\tanh(%s(x\cdot z)+%s)$"%(gamma,r))
plt.suptitle("KPCA-sigmoid")
plt.show() # 调用 plot_KPCA_sigmoid
plot_KPCA_sigmoid(X,y)

吴裕雄 python 机器学习——核化PCAKernelPCA模型的更多相关文章
- 吴裕雄 python 机器学习——分类决策树模型
import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.model_s ...
- 吴裕雄 python 机器学习——回归决策树模型
import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.model_s ...
- 吴裕雄 python 机器学习——线性回归模型
import numpy as np from sklearn import datasets,linear_model from sklearn.model_selection import tra ...
- 吴裕雄 python 机器学习——K均值聚类KMeans模型
import numpy as np import matplotlib.pyplot as plt from sklearn import cluster from sklearn.metrics ...
- 吴裕雄 python 机器学习——混合高斯聚类GMM模型
import numpy as np import matplotlib.pyplot as plt from sklearn import mixture from sklearn.metrics ...
- 吴裕雄 python 机器学习——层次聚类AgglomerativeClustering模型
import numpy as np import matplotlib.pyplot as plt from sklearn import cluster from sklearn.metrics ...
- 吴裕雄 python 机器学习——密度聚类DBSCAN模型
import numpy as np import matplotlib.pyplot as plt from sklearn import cluster from sklearn.metrics ...
- 吴裕雄 python 机器学习——等度量映射Isomap降维模型
# -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt from sklearn import datas ...
- 吴裕雄 python 机器学习——支持向量机非线性回归SVR模型
import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm fr ...
随机推荐
- FatMouse and Cheese HDU - 1078 dp
#include<cstdio> #include<iostream> #include<cstring> using namespace std; int n,k ...
- Java8之Stream详解
Java8中提供了Stream对集合操作作出了极大的简化,学习了Stream之后,我们以后不用使用for循环就能对集合作出很好的操作. 一.流的初始化与转换 Java中的Stream的所有操作 ...
- 菜得一P!
什么时候才能改变粗心大意的坏毛病!太吃亏了...
- 【已解决】使用 yarn 安装时,报错node_modules\node sass:Command failed.
npm install -g mirror-config-china --registry=http://registry.npm.taobao.org npm install node-sass y ...
- @HystrixCommand 不能被导包
添加pom文件 <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hy ...
- (转) 统计在从1到n的正整数中1出现的次数
1. 题目描述 输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如输入12,从1到12这些整数中包含1的数字有1,10,11和12,1一共出现了5次. 2. 题目来源 第一次看到是在 ...
- .gitignore文件说明-git提交时可忽略的文件
在我们平时做项目时,总有一些文件不想提交到服务器的版本管理上 这时就要设置相应的忽略规则,来忽略这些文件的提交 规则 作用/mtk 过滤整个文件夹*.zip 过滤所有.zip文件/mtk/do.c 过 ...
- python之路set
一.set和其他集合的区别: list :允许重复的集合,修改 tuple:允许重复的集合,不修改 dict:字典 set:不允许重复的集合,set不允许重复的,列表是无序的 1.创建一个set s= ...
- vue+muse-ui
1.可以很好的配合vue2.0开发 2.安装: npm install muse-ui --save 3.引入: 在main.js中加入 import Vue from 'vue' import M ...
- ORA-01935: missing user or role name
问题描述 ORA-01935: missing user or role name ORA-01935:缺少用户或角色名