pca python sklearn_Python sklearn.decomposition 模块,KernelPCA() 实例源码 - 编程字典

def __init__(self, data, input_dimensionality, output_dimensionality, kernel_type='rbf',

degree=2, sigma=0, kernel_scaling=1, c=1, scaler=None):

"""

Creates a Kernel SEF object

:param data: the data to be used by the kernel

:param input_dimensionality: dimensionality of the input space

:param output_dimensionality: dimensionality of the target space

:param learning_rate: learning rate to be used for the optimization

:param kernel_type: supported kernel: 'rbf', 'poly', and 'linear'

:param degree: degree of the polynomial kernel

:param sigma: the sigma value for the RBF kernel

:param kernel_scaling: scaling parameter for the kernel

:param c: constant kernel param for linear and poly kernels

:param regularizer_weight: weight of the regularizer

:param scaler: the sklearn-compatible scaler (or None)

"""

# Call base constructor

SEF_Base.__init__(self, input_dimensionality, output_dimensionality, scaler=scaler)

# Adjustable parameters

self.kernel_type = kernel_type

self.degree = degree

self.sigma_kernel = np.float32(sigma)

self.alpha = kernel_scaling

self.c = c

# If scaler is used, fit it!

if self.scaler is None:

data = np.float32(data)

else:

pass

data = np.float32(self.scaler.fit_transform(data))

# If the rbf kernel is used and no sigma is supplied, estimate it!

if sigma == 0 and self.kernel_type == 'rbf':

sigma_kernel = np.float32(mean_data_distance(data))

self.sigma_kernel = sigma_kernel

else:

self.sigma_kernel = 1

# Use kPCA for initialization

kpca = KernelPCA(kernel=self.kernel_type, n_components=self.output_dimensionality,

gamma=(1.0 / (self.sigma_kernel ** 2)), degree=self.degree, eigen_solver='dense')

kpca.fit(data)

A = kpca.alphas_

# Scale the coefficients to have unit norm (avoid rescaling)

A = A / np.sqrt(np.diag(np.dot(A.T, np.dot(np.dot(data, data.T), A))))

# Model parameters

self.X_kernel = Variable(torch.from_numpy(np.float32(data)), requires_grad=False)

self.A = Variable(torch.from_numpy(np.float32(A)), requires_grad=True)

self.trainable_params = [self.A]

self.non_trainable_params = [self.X_kernel]

你可能感兴趣的:(pca,python,sklearn)