Moodle PHP Documentation 4.1
Moodle 4.1.19+ (Build: 20250613) (4f61b2974b9)
Phpml\DimensionReduction\KernelPCA Class Reference
Inheritance diagram for Phpml\DimensionReduction\KernelPCA:
Phpml\DimensionReduction\PCA Phpml\DimensionReduction\EigenTransformerBase

Public Member Functions

 __construct (int $kernel=self::KERNEL_RBF, ?float $totalVariance=null, ?int $numFeatures=null, ?float $gamma=null)
 Kernel principal component analysis (KernelPCA) is an extension of PCA using techniques of kernel methods.
 
 fit (array $data)
 Takes a data and returns a lower dimensional version of this data while preserving $totalVariance or $numFeatures.
 
 transform (array $sample)
 Transforms the given sample to a lower dimensional vector by using the variables obtained during the last run of fit.
 

Public Attributes

int $numFeatures = null
 Number of features to be preserved after the reduction.
 
float $totalVariance = 0.9
 Total variance to be conserved after the reduction.
 
const KERNEL_LAPLACIAN = 3
 
const KERNEL_LINEAR = 4
 
const KERNEL_RBF = 1
 
const KERNEL_SIGMOID = 2
 

Protected Member Functions

 calculateKernelMatrix (array $data, int $numRows)
 Calculates similarity matrix by use of selected kernel function
An n-by-m matrix is given and an n-by-n matrix is returned.
 
 calculateMeans (array $data, int $n)
 
 centerMatrix (array $matrix, int $n)
 Kernel matrix is centered in its original space by using the following conversion:
 
 eigenDecomposition (array $matrix)
 Calculates eigenValues and eigenVectors of the given matrix.
 
 getDistancePairs (array $sample)
 
 getKernel ()
 Returns the callable kernel function.
 
 normalize (array $data, int $n)
 Normalization of the data includes subtracting mean from each dimension therefore dimensions will be centered to zero.
 
 projectSample (array $pairs)
 
 reduce (array $data)
 Returns the reduced data.
 

Protected Attributes

array $data = []
 Original dataset used to fit KernelPCA.
 
array $eigValues = []
 Top eigenValues of the matrix.
 
array $eigVectors = []
 Top eigenvectors of the matrix.
 
bool $fit = false
 
float null $gamma
 Gamma value used by the kernel.
 
int $kernel
 
array $means = []
 

Constructor & Destructor Documentation

◆ __construct()

Phpml\DimensionReduction\KernelPCA::__construct ( int $kernel = self::KERNEL_RBF,
?float $totalVariance = null,
?int $numFeatures = null,
?float $gamma = null )

Kernel principal component analysis (KernelPCA) is an extension of PCA using techniques of kernel methods.

It is more suitable for data that involves vectors that are not linearly separable

Example: $kpca = new KernelPCA(KernelPCA::KERNEL_RBF, null, 2, 15.0); will initialize the algorithm with an RBF kernel having the gamma parameter as 15,0.
This transformation will return the same number of rows with only 2 columns.

Parameters
float$totalVarianceTotal variance to be preserved if numFeatures is not given
int$numFeaturesNumber of columns to be returned
float$gammaGamma parameter is used with RBF and Sigmoid kernels
Exceptions
InvalidArgumentException

Member Function Documentation

◆ centerMatrix()

Phpml\DimensionReduction\KernelPCA::centerMatrix ( array $matrix,
int $n )
protected

Kernel matrix is centered in its original space by using the following conversion:

K′ = K − N.K − K.N + N.K.N where N is n-by-n matrix filled with 1/n

◆ eigenDecomposition()

Phpml\DimensionReduction\EigenTransformerBase::eigenDecomposition ( array $matrix)
protectedinherited

Calculates eigenValues and eigenVectors of the given matrix.

Returns top eigenVectors along with the largest eigenValues. The total explained variance of these eigenVectors will be no less than desired $totalVariance value

◆ fit()

Phpml\DimensionReduction\KernelPCA::fit ( array $data)

Takes a data and returns a lower dimensional version of this data while preserving $totalVariance or $numFeatures.


$data is an n-by-m matrix and returned array is n-by-k matrix where k <= m

Reimplemented from Phpml\DimensionReduction\PCA.

◆ getKernel()

Phpml\DimensionReduction\KernelPCA::getKernel ( )
protected

Returns the callable kernel function.

Exceptions
Exception

◆ transform()

Phpml\DimensionReduction\KernelPCA::transform ( array $sample)

Transforms the given sample to a lower dimensional vector by using the variables obtained during the last run of fit.

Exceptions
InvalidArgumentException
InvalidOperationException

Reimplemented from Phpml\DimensionReduction\PCA.


The documentation for this class was generated from the following file: