# -*- 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)