verticapy.machine_learning.memmodel.decomposition.PCA¶
- class verticapy.machine_learning.memmodel.decomposition.PCA(principal_components: Annotated[list | ndarray, 'Array Like Structure'], mean: Annotated[list | ndarray, 'Array Like Structure'])¶
InMemoryModelimplementation of thePCAalgorithm.Parameters¶
- principal_components: ArrayLike
Matrix of the principal components.
- mean: ArrayLike
List of the averages of each input feature.
Note
PCAare defined entirely by their attributes. For example,principal_componentsandmeandefine 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
PCAmodel is defined by itsprincipal_componentsandmeanvalue. 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
PCAmodel.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 between0.0and1.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: Annotated[list | ndarray, 'Array Like Structure'], mean: Annotated[list | ndarray, 'Array Like Structure']) None¶
Methods
__init__(principal_components, mean)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
PCAmatrix.set_attributes(**kwargs)Sets the model attributes.
transform(X)Transforms and applies the
PCAmodel to the input matrix.Transforms and returns the SQL needed to deploy the
PCAmodel.Attributes
Must be overridden in child class