【Pytorch】PCA的Pytorch实现

from sklearn import datasets
import torch
import numpy as np

def PCA(data, k=2):
	X = torch.from_numpy(data)
	X_mean = torch.mean(X, 0)
	X = X - X_mean.expand_as(X)

	U, S, V = torch.svd(torch.t(X))  # U*Diag(S)*V_T
	return torch.mm(X, U[:,:,k])

iris = datasets.load_iris()
X = iris.data
y = iris.target
X_PCA = PCA(X)
pca = X_PCA.numpy()

你可能感兴趣的:(Pytorch,numpy,深度学习,PCA)