Python:Kernel based PCA手工代码实现

'''
Auther: DeniuHe
Date:2021-03-16
'''
import numpy as np
from sklearn import datasets
from sklearn.metrics.pairwise import rbf_kernel
from sklearn.decomposition import KernelPCA
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

class Kernel_PCA():
    def __init__(self,X):
        self.X = X
        self.N = self.X.shape[0]
        self.K = rbf_kernel(X=self.X,gamma=15)
        self.In = np.ones((self.N,self.N)) * (1/self.N)
        self.K_hat = self.K - self.K @ self.In - self.In @ self.K + self.In @ self.K @ self.In
        self.L, self.U = np.linalg.eig(self.K_hat)


    # dimensionality reduction
    def DR(self,n_component):
        a = [i for i in range(n_component)]
        ord_ids = np.flipud(np.argsort(self.L))
        tar_ids = ord_ids[a]
        trans_X = self.K_hat @ self.U[:,tar_ids] / np.sqrt(self.L[tar_ids])
        return trans_X



if __name__ == '__main__':
    X, y = datasets.make_moons(noise=0.1,n_samples=300,random_state=100)
    # plt.scatter(X[:,0],X[:,1],c=y)
    # plt.show()
    # X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.1,random_state=100)
    kpca = Kernel_PCA(X=X)
    trans_X = kpca.DR(n_component=2)
    plt.scatter(trans_X[:,0],trans_X[:,1],c=y)
    plt.show()
    X, y = datasets.make_moons(noise=0.1, n_samples=300, random_state=100)
    diaobao = KernelPCA(kernel='rbf',n_components=2,gamma=15)
    X_diao = diaobao.fit_transform(X)
    plt.scatter(X_diao[:,0],X_diao[:,1],c=y)
    plt.show()

两个月亮

Python:Kernel based PCA手工代码实现

 

本文代码效果 (gamma=15)

Python:Kernel based PCA手工代码实现

sklearn调包效果(参数保持相同,即gamma=15) 

Python:Kernel based PCA手工代码实现

 

结果方向貌似反了,不过不要紧。 

上一篇:oracle笔记


下一篇:2021-03-15