吴裕雄 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 ...
随机推荐
- element-ui的upload组件的clearFiles方法
<template> <div> <el-button @click="clearFiles">重新上传</el-button> & ...
- Yii2 JWT
Yii2 JWT 这个扩展为Yii framework 2.0提供了JWT集成(需要PHP 5.6+).它包括基本的HTTP身份验证支持. 目录 安装 依赖关系 基本用法 创建 从字符串分析 验证 令 ...
- Eclipse设置代码模板
个人博客 地址:http://www.wenhaofan.com/article/20180904173808 根据下列路径打开配置窗口 Window->Preferences->Java ...
- 手机内存卡RAW无法格式化的解决办法
突然出现这个问题,这是麻烦: 网上找了各种办法:什么软件修复,disk等修复,创建新磁盘,新扇到,win自动修复啊:开始----运行框中输入 :convert X: /fs:FAT(X为电脑 ...
- python数据赋值后,修改新数据,原数据如何保证不被修改?
python中对象,赋值后是同一地址,如果是可变对象,对其中一个修改会影响到另一个,如果要生成完全新的对象,应使用deepcopyimport copydata1=copy.deepcopy(data ...
- 【Python】数值运算操作符
- altair package and altair_viewer
pip install altair pip install altair_viewer Altair is a declarative statistical visualization libra ...
- 2019杭电多校 permutation2
Problem:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1007&cid=852 #include<bits ...
- C#中ESRI.ArcGIS.esriSystem的引用问题
ESRI.ArcGIS.esriSystem,在引用里没有它的同名引用,其实它对应的引用为ESRI.ArcGIS.System,所以添加“ESRI.ArcGIS.System”这个引用即可
- 使用LxRunOffline工具备份/还原Linux子系统(WSL)
安装WSL 开启WSL组件 首先需要打开WSL组件,可以使用以下命令: Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Win ...