Loading...

verticapy.machine_learning.memmodel.decomposition.PCA#

class verticapy.machine_learning.memmodel.decomposition.PCA(principal_components: list | ndarray, mean: list | ndarray)#

InMemoryModel implementation of the PCA algorithm.

Parameters#

principal_components: ArrayLike

Matrix of the principal components.

mean: ArrayLike

List of the averages of each input feature.

Note

PCA are defined entirely by their attributes. For example, principal_components and mean define a PCA model.

Attributes#

Attributes are identical to the input parameters, followed by an underscore (‘_’).

Examples#

Initalization

Import the required module.

from verticapy.machine_learning.memmodel.decomposition import PCA

A PCA model is defined by its principal_components and mean value. In this example, we will use the following:

principal_components = [
    [0.4, 0.5],
    [0.3, 0.2],
]


mean = [0.1, 0.3]

Let’s create a PCA model.

model_pca = PCA(principal_components, mean)

Create a dataset.

data = [[4, 5]]

Making In-Memory Transformation

Use transform() method to do transformation.

model_pca.transform(data)
Out[6]: array([[2.97, 2.89]])

Deploy SQL Code

Let’s use the following column names:

cnames = ['col1', 'col2']

Use transform_sql() method to get the SQL code needed to deploy the model using its attributes.

model_pca.transform_sql(cnames)
Out[8]: 
['(col1 - 0.1) * 0.4 + (col2 - 0.3) * 0.3',
 '(col1 - 0.1) * 0.5 + (col2 - 0.3) * 0.2']

Perform an Oblimin Rotation

Use rotate() method to perform Oblimin (Varimax, Quartimax) rotation on PCA matrix.

model_pca.rotate()

Note

You can determine the type of rotation by adjusting value of gamma in rotate() method. It must be between 0.0 and 1.0.

Use gamma = 0.0, for Quartimax rotation:

gamma = 0.0

model_pca.rotate(gamma)

Use gamma = 1.0, for Varimax rotation:

gamma = 1.0

model_pca.rotate(gamma)

Use get_attributes() method to check the attributes of the rotated model.

model_pca.get_attributes()
Out[14]: 
{'principal_components': array([[0.07739603, 0.63561769],
        [0.15004967, 0.3278492 ]]),
 'mean': array([0.1, 0.3])}

Hint

This object can be pickled and used in any in-memory environment, just like SKLEARN models.

__init__(principal_components: list | ndarray, mean: list | ndarray) None#

Methods

__init__(principal_components, mean)

get_attributes()

Returns the model attributes.

matrix_rotation(Phi[, gamma, q, tol])

Performs an Oblimin (Varimax, Quartimax) rotation on the input matrix.

rotate([gamma, q, tol])

Performs an Oblimin (Varimax, Quartimax) rotation on the PCA matrix.

set_attributes(**kwargs)

Sets the model attributes.

transform(X)

Transforms and applies the PCA model to the input matrix.

transform_sql(X)

Transforms and returns the SQL needed to deploy the PCA model.

Attributes

object_type

Must be overridden in child class