verticapy.machine_learning.vertica.decomposition.MCA¶
- class verticapy.machine_learning.vertica.decomposition.MCA(name: str = None, overwrite_model: bool = False)¶
Creates a MCA (multiple correspondence analysis) object using the Vertica PCA algorithm. MCA is a PCA applied to a complete disjunctive table. The input relation is transformed to a TCDT (transformed complete disjunctive table) before applying the PCA.
Important
This algorithm is not Vertica Native and relies solely on SQL for attribute computation. While this model does not take advantage of the benefits provided by a model management system, including versioning and tracking, the SQL code it generates can still be used to create a pipeline.
Parameters¶
- name: str, optional
Name of the model. The model is stored in the database.
- overwrite_model: bool, optional
If set to
True, training a model with the same name as an existing model overwrites the existing model.
Attributes¶
Many attributes are created during the fitting phase.
- principal_components_: numpy.array
Matrix of the principal components.
- mean_: numpy.array
List of the averages of each input feature.
- cos2_: numpy.array
Quality of representation of each observation in the principal component space. A high cos2 value indicates that the observation is well-represented in the reduced-dimensional space defined by the principal components, while a low value suggests poor representation.
- explained_variance_: numpy.array
Represents the proportion of the total variance in the original dataset that is captured by a specific principal component or a combination of principal components.
Note
All attributes can be accessed using the
get_attributes()method.Note
Several other attributes can be accessed by using the
get_vertica_attributes()method.Examples¶
The following examples provide a basic understanding of usage. For more detailed examples, please refer to the Machine Learning or the Examples section on the website.
Load data for machine learning¶
We import
verticapy:import verticapy as vp
Hint
By assigning an alias to
verticapy, we mitigate the risk of code collisions with other libraries. This precaution is necessary because verticapy uses commonly known function names like “average” and “median”, which can potentially lead to naming conflicts. The use of an alias ensures that the functions fromverticapyare used as intended without interfering with functions from other libraries.For this example, we will use the Titanic dataset.
import verticapy.datasets as vpd data = vpd.load_titanic()
123pclass123survivedAbcAbcsex123age123sibsp123parchAbcticket123fareAbccabinAbcembarkedAbcboat123bodyAbc1 1 0 male 71.0 0 0 PC 17609 49.5042 [null] C [null] 22 2 1 0 male 45.0 0 0 113784 35.5 T S [null] [null] 3 1 0 male [null] 0 0 113798 31.0 [null] S [null] [null] 4 1 0 male 17.0 0 0 113059 47.1 [null] S [null] [null] 5 1 0 male 27.0 1 0 13508 136.7792 C89 C [null] [null] 6 1 0 male 37.0 1 1 PC 17756 83.1583 E52 C [null] [null] 7 1 0 male 31.0 1 0 F.C. 12750 52.0 B71 S [null] [null] 8 1 0 male 50.0 1 0 PC 17761 106.425 C86 C [null] 62 9 1 0 female 36.0 0 0 PC 17531 31.6792 A29 C [null] [null] 10 1 0 male 37.0 1 0 113803 53.1 C123 S [null] [null] 11 1 0 male 24.0 0 0 PC 17593 79.2 B86 C [null] [null] 12 1 0 male 45.0 1 0 36973 83.475 C83 S [null] [null] 13 1 0 male 40.0 0 0 112059 0.0 B94 S [null] 110 14 1 0 male 42.0 0 0 113038 42.5 B11 S [null] [null] 15 1 0 male [null] 0 0 17463 51.8625 E46 S [null] [null] 16 1 0 male 42.0 1 0 113789 52.0 [null] S [null] 38 17 1 0 male [null] 0 0 PC 17600 30.6958 [null] C 14 [null] 18 1 0 male 29.0 0 0 113501 30.0 D6 S [null] 126 19 1 0 male 46.0 0 0 13050 75.2417 C6 C [null] 292 20 1 0 male 54.0 0 0 17463 51.8625 E46 S [null] 175 21 1 0 male 47.0 0 0 113796 42.4 [null] S [null] [null] 22 1 0 male 58.0 0 2 35273 113.275 D48 C [null] 122 23 1 0 male 45.5 0 0 113043 28.5 C124 S [null] 166 24 1 0 male 29.0 1 0 113776 66.6 C2 S [null] [null] 25 1 0 male 47.0 0 0 110465 52.0 C110 S [null] 207 26 1 0 male 38.0 0 0 19972 0.0 [null] S [null] [null] 27 1 0 male 22.0 0 0 PC 17760 135.6333 [null] C [null] 232 28 1 0 male 31.0 0 0 PC 17590 50.4958 A24 S [null] [null] 29 1 0 male 50.0 1 0 13507 55.9 E44 S [null] [null] 30 1 0 male 56.0 0 0 17764 30.6958 A7 C [null] [null] 31 1 0 male 57.0 1 0 PC 17569 146.5208 B78 C [null] [null] 32 1 0 female 63.0 1 0 PC 17483 221.7792 C55 C57 S [null] [null] 33 1 0 male 61.0 0 0 36963 32.3208 D50 S [null] 46 34 1 0 male 21.0 0 1 35281 77.2875 D26 S [null] 169 35 1 0 male 51.0 0 1 PC 17597 61.3792 [null] C [null] [null] 36 1 1 female 63.0 1 0 13502 77.9583 D7 S 10 [null] 37 1 1 female 32.0 0 0 11813 76.2917 D15 C 8 [null] 38 1 1 female 58.0 0 0 113783 26.55 C103 S 8 [null] 39 1 1 female 44.0 0 0 PC 17610 27.7208 B4 C 6 [null] 40 1 1 female 41.0 0 0 16966 134.5 E40 C 3 [null] 41 1 1 female 53.0 0 0 PC 17606 27.4458 [null] C 6 [null] 42 1 1 male 36.0 0 1 PC 17755 512.3292 B51 B53 B55 C 3 [null] 43 1 1 female 58.0 0 1 PC 17755 512.3292 B51 B53 B55 C 3 [null] 44 1 1 male 11.0 1 2 113760 120.0 B96 B98 S 4 [null] 45 1 1 female 76.0 1 0 19877 78.85 C46 S 6 [null] 46 1 1 female [null] 0 1 113505 55.0 E33 S 6 [null] 47 1 1 female 39.0 1 1 PC 17756 83.1583 E49 C 14 [null] 48 1 1 female 27.0 1 2 F.C. 12750 52.0 B71 S 3 [null] 49 1 1 female [null] 0 0 17421 110.8833 [null] C 4 [null] 50 1 1 female 35.0 0 0 113503 211.5 C130 C 4 [null] 51 1 1 female 22.0 0 1 112378 59.4 [null] C 7 [null] 52 1 1 female 25.0 1 0 11765 55.4417 E50 C 5 [null] 53 1 1 male 48.0 1 0 PC 17572 76.7292 D33 C 3 [null] 54 1 1 female 35.0 1 0 36973 83.475 C83 S D [null] 55 1 1 male 27.0 0 0 PC 17572 76.7292 D49 C 3 [null] 56 1 1 female 24.0 0 0 11767 83.1583 C54 C 7 [null] 57 1 1 female 52.0 1 1 12749 93.5 B69 S 3 [null] 58 1 1 female 44.0 0 1 111361 57.9792 B18 C 4 [null] 59 1 1 female 15.0 0 1 24160 211.3375 B5 S 2 [null] 60 1 1 male 30.0 1 0 13236 57.75 C78 C 11 [null] 61 1 1 female 31.0 1 0 35273 113.275 D36 C 6 [null] 62 1 1 female 39.0 0 0 PC 17758 108.9 C105 C 8 [null] 63 1 1 female 22.0 0 1 113509 61.9792 B36 C 5 [null] 64 1 1 male 52.0 0 0 113786 30.5 C104 S 6 [null] 65 1 1 female 43.0 0 1 24160 211.3375 B3 S 2 [null] 66 1 1 female 33.0 0 0 110152 86.5 B77 S 8 [null] 67 1 1 male 45.0 1 1 16966 134.5 E34 C 3 [null] 68 1 1 female 40.0 1 1 16966 134.5 E34 C 3 [null] 69 1 1 male 48.0 1 0 19996 52.0 C126 S 5 7 [null] 70 1 1 female [null] 0 0 PC 17585 79.2 [null] C D [null] 71 1 1 female 35.0 0 0 PC 17755 512.3292 [null] C 3 [null] 72 1 1 female 60.0 1 0 110813 75.25 D37 C 5 [null] 73 1 1 male 21.0 0 1 PC 17597 61.3792 [null] C A [null] 74 2 0 male 23.0 0 0 C.A. 31030 10.5 [null] S [null] [null] 75 2 0 male 28.0 0 0 244358 26.0 [null] S [null] [null] 76 2 0 male 60.0 1 1 29750 39.0 [null] S [null] [null] 77 2 0 female 44.0 1 0 244252 26.0 [null] S [null] [null] 78 2 0 male 29.0 1 0 2003 26.0 [null] S [null] [null] 79 2 0 male 18.0 0 0 S.O.C. 14879 73.5 [null] S [null] [null] 80 2 0 male 18.0 0 0 S.O.C. 14879 73.5 [null] S [null] [null] 81 2 0 male 54.0 0 0 28403 26.0 [null] S [null] [null] 82 2 0 male 18.0 0 0 236171 13.0 [null] S [null] [null] 83 2 0 male 36.0 0 0 229236 13.0 [null] S [null] 236 84 2 0 male 34.0 1 0 28664 21.0 [null] S [null] [null] 85 2 0 male 21.0 1 0 28133 11.5 [null] S [null] [null] 86 2 0 male 21.0 1 0 28134 11.5 [null] S [null] [null] 87 2 0 male 24.0 0 0 233866 13.0 [null] S [null] 155 88 2 0 male 34.0 0 0 12233 13.0 [null] S [null] [null] 89 2 0 male 30.0 0 0 250653 13.0 [null] S [null] 75 90 2 0 male 44.0 0 0 248746 13.0 [null] S [null] 35 91 2 0 male 49.0 1 2 220845 65.0 [null] S [null] [null] 92 2 0 male 21.0 2 0 S.O.C. 14879 73.5 [null] S [null] [null] 93 2 0 male 21.0 0 0 S.O.C. 14879 73.5 [null] S [null] [null] 94 2 0 female 60.0 1 0 24065 26.0 [null] S [null] [null] 95 2 0 male 24.0 2 0 C.A. 31029 31.5 [null] S [null] [null] 96 2 0 male 22.0 2 0 C.A. 31029 31.5 [null] S [null] [null] 97 2 0 male 35.0 0 0 233734 12.35 [null] Q [null] [null] 98 2 0 male 31.0 0 0 C.A. 18723 10.5 [null] S [null] 165 99 2 0 male 36.0 0 0 SC/Paris 2163 12.875 D C [null] [null] 100 2 0 male [null] 0 0 SC/A.3 2861 15.5792 [null] C [null] [null] Rows: 1-100 | Columns: 14Note
VerticaPy offers a wide range of sample datasets that are ideal for training and testing purposes. You can explore the full list of available datasets in the Datasets, which provides detailed information on each dataset and how to use them effectively. These datasets are invaluable resources for honing your data analysis and machine learning skills within the VerticaPy environment.
Model Initialization¶
First we import the
MCAmodel:from verticapy.machine_learning.vertica import MCA
Then we can create the model:
model = MCA()
You can select the number of components by the
n_componentparameter. If it is not provided, then all are considered.Important
As this model is not native, it solely relies on SQL statements to compute various attributes, storing them within the object. No data is saved in the database.
Model Training¶
Before fitting the model, we need to calculate the Transformed Completely Disjontive Table before fitting the model:
tcdt = data[["survived", "pclass", "sex"]].cdt()
We can now fit the model:
model.fit(tcdt)
Important
To train a model, you can directly use the
vDataFrameor the name of the relation stored in the database.Scores¶
The decomposition score on the dataset for each transformed column can be calculated by:
model.score() Out[6]: None Score survived_0 0.0 survived_1 0.0 pclass_1 0.0 pclass_2 0.0 pclass_3 0.0 sex_female 0.0 sex_male 0.0 Rows: 1-7 | Columns: 2
For more details on the function, check out
score()You can also fetch the explained variance by:
model.explained_variance_ Out[7]: array([3.71602747e-01, 3.42234698e-01, 2.01530443e-01, 8.46321118e-02, 4.58523775e-17, 3.71471553e-18, 4.48009740e-16])
Principal Components¶
To get the transformed dataset in the form of principal components:
model.transform(tcdt) Out[8]: None col1 col2 col3 col4 col5 col6 \\ 1 0.00229204335572604 0.00032174051601337 0.000797337308334705 -0.00224766909385811 -1.78934016041576e-16 2.14129929065887e-16 \\ 2 0.00229204335572604 0.00032174051601337 0.000797337308334705 -0.00224766909385811 -1.78934016041576e-16 2.14129929065887e-16 \\ 3 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 4 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 5 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 6 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 7 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 8 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 9 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 10 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 11 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 12 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 13 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 14 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 15 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 16 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 17 0.00229204335572604 0.00032174051601337 0.000797337308334705 -0.00224766909385811 -1.78934016041576e-16 2.14129929065887e-16 \\ 18 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 19 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 20 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 21 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 22 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 23 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 24 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 25 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 26 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 27 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 28 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 29 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 30 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 31 0.00146911605526444 -0.000826178800615592 0.00235861847839742 -0.000590677084987079 -1.78642636707721e-16 2.13642038088269e-16 \\ 32 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 33 0.0024040087227709 0.000370460404419619 0.00166740181096483 0.00135391740166905 -1.7609476160238e-16 2.1391308863139e-16 \\ 34 0.0024040087227709 0.000370460404419619 0.00166740181096483 0.00135391740166905 -1.7609476160238e-16 2.1391308863139e-16 \\ 35 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 36 0.0024040087227709 0.000370460404419619 0.00166740181096483 0.00135391740166905 -1.7609476160238e-16 2.1391308863139e-16 \\ 37 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 38 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 39 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 40 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 41 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 42 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 43 0.0024040087227709 0.000370460404419619 0.00166740181096483 0.00135391740166905 -1.7609476160238e-16 2.1391308863139e-16 \\ 44 0.0024040087227709 0.000370460404419619 0.00166740181096483 0.00135391740166905 -1.7609476160238e-16 2.1391308863139e-16 \\ 45 0.0024040087227709 0.000370460404419619 0.00166740181096483 0.00135391740166905 -1.7609476160238e-16 2.1391308863139e-16 \\ 46 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 47 0.0024040087227709 0.000370460404419619 0.00166740181096483 0.00135391740166905 -1.7609476160238e-16 2.1391308863139e-16 \\ 48 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 49 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 50 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 51 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 52 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 53 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 54 0.0024040087227709 0.000370460404419619 0.00166740181096483 0.00135391740166905 -1.7609476160238e-16 2.1391308863139e-16 \\ 55 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 56 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 57 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 58 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 59 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 60 0.0024040087227709 0.000370460404419619 0.00166740181096483 0.00135391740166905 -1.7609476160238e-16 2.1391308863139e-16 \\ 61 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 62 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 63 0.0024040087227709 0.000370460404419619 0.00166740181096483 0.00135391740166905 -1.7609476160238e-16 2.1391308863139e-16 \\ 64 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 65 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 66 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 67 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 68 0.0024040087227709 0.000370460404419619 0.00166740181096483 0.00135391740166905 -1.7609476160238e-16 2.1391308863139e-16 \\ 69 0.0024040087227709 0.000370460404419619 0.00166740181096483 0.00135391740166905 -1.7609476160238e-16 2.1391308863139e-16 \\ 70 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 71 0.0024040087227709 0.000370460404419619 0.00166740181096483 0.00135391740166905 -1.7609476160238e-16 2.1391308863139e-16 \\ 72 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 73 0.0024040087227709 0.000370460404419619 0.00166740181096483 0.00135391740166905 -1.7609476160238e-16 2.1391308863139e-16 \\ 74 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 75 0.0024040087227709 0.000370460404419619 0.00166740181096483 0.00135391740166905 -1.7609476160238e-16 2.1391308863139e-16 \\ 76 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 77 0.0024040087227709 0.000370460404419619 0.00166740181096483 0.00135391740166905 -1.7609476160238e-16 2.1391308863139e-16 \\ 78 0.0024040087227709 0.000370460404419619 0.00166740181096483 0.00135391740166905 -1.7609476160238e-16 2.1391308863139e-16 \\ 79 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 80 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 81 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 82 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 83 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 84 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 85 0.0032269360232325 0.00151837972104858 0.00010612064090212 -0.000303074607201982 -1.76386140936236e-16 2.14455189717633e-16 \\ 86 -0.00288205990078061 0.00144320031180486 0.00136902297293012 -0.000247042678679404 -1.78588426599097e-16 2.1391308863139e-16 \\ 87 -0.00288205990078061 0.00144320031180486 0.00136902297293012 -0.000247042678679404 -1.78588426599097e-16 2.1391308863139e-16 \\ 88 -0.00288205990078061 0.00144320031180486 0.00136902297293012 -0.000247042678679404 -1.78588426599097e-16 2.1391308863139e-16 \\ 89 -0.00288205990078061 0.00144320031180486 0.00136902297293012 -0.000247042678679404 -1.78588426599097e-16 2.1391308863139e-16 \\ 90 -0.00288205990078061 0.00144320031180486 0.00136902297293012 -0.000247042678679404 -1.78588426599097e-16 2.1391308863139e-16 \\ 91 -0.00288205990078061 0.00144320031180486 0.00136902297293012 -0.000247042678679404 -1.78588426599097e-16 2.1391308863139e-16 \\ 92 -0.00288205990078061 0.00144320031180486 0.00136902297293012 -0.000247042678679404 -1.78588426599097e-16 2.1391308863139e-16 \\ 93 -0.00288205990078061 0.00144320031180486 0.00136902297293012 -0.000247042678679404 -1.78588426599097e-16 2.1391308863139e-16 \\ 94 -0.00288205990078061 0.00144320031180486 0.00136902297293012 -0.000247042678679404 -1.78588426599097e-16 2.1391308863139e-16 \\ 95 -0.00288205990078061 0.00144320031180486 0.00136902297293012 -0.000247042678679404 -1.78588426599097e-16 2.1391308863139e-16 \\ 96 -0.00288205990078061 0.00144320031180486 0.00136902297293012 -0.000247042678679404 -1.78588426599097e-16 2.1391308863139e-16 \\ 97 -0.00288205990078061 0.00144320031180486 0.00136902297293012 -0.000247042678679404 -1.78588426599097e-16 2.1391308863139e-16 \\ 98 -0.00288205990078061 0.00144320031180486 0.00136902297293012 -0.000247042678679404 -1.78588426599097e-16 2.1391308863139e-16 \\ 99 -0.00288205990078061 0.00144320031180486 0.00136902297293012 -0.000247042678679404 -1.78588426599097e-16 2.1391308863139e-16 \\ 100 -0.00288205990078061 0.00144320031180486 0.00136902297293012 -0.000247042678679404 -1.78588426599097e-16 2.1391308863139e-16 \\ None col7 1 -1.31025832544873e-16 2 -1.31025832544873e-16 3 -1.31649248794052e-16 4 -1.31649248794052e-16 5 -1.31649248794052e-16 6 -1.31649248794052e-16 7 -1.31649248794052e-16 8 -1.31649248794052e-16 9 -1.31649248794052e-16 10 -1.31649248794052e-16 11 -1.31649248794052e-16 12 -1.31649248794052e-16 13 -1.31649248794052e-16 14 -1.31649248794052e-16 15 -1.31649248794052e-16 16 -1.31649248794052e-16 17 -1.31025832544873e-16 18 -1.31649248794052e-16 19 -1.31649248794052e-16 20 -1.31649248794052e-16 21 -1.31649248794052e-16 22 -1.31649248794052e-16 23 -1.31649248794052e-16 24 -1.31649248794052e-16 25 -1.31649248794052e-16 26 -1.31649248794052e-16 27 -1.31649248794052e-16 28 -1.31649248794052e-16 29 -1.31649248794052e-16 30 -1.31649248794052e-16 31 -1.31649248794052e-16 32 -1.33844758193336e-16 33 -1.34468174442515e-16 34 -1.34468174442515e-16 35 -1.33844758193336e-16 36 -1.34468174442515e-16 37 -1.33844758193336e-16 38 -1.33844758193336e-16 39 -1.33844758193336e-16 40 -1.33844758193336e-16 41 -1.33844758193336e-16 42 -1.33844758193336e-16 43 -1.34468174442515e-16 44 -1.34468174442515e-16 45 -1.34468174442515e-16 46 -1.33844758193336e-16 47 -1.34468174442515e-16 48 -1.33844758193336e-16 49 -1.33844758193336e-16 50 -1.33844758193336e-16 51 -1.33844758193336e-16 52 -1.33844758193336e-16 53 -1.33844758193336e-16 54 -1.34468174442515e-16 55 -1.33844758193336e-16 56 -1.33844758193336e-16 57 -1.33844758193336e-16 58 -1.33844758193336e-16 59 -1.33844758193336e-16 60 -1.34468174442515e-16 61 -1.33844758193336e-16 62 -1.33844758193336e-16 63 -1.34468174442515e-16 64 -1.33844758193336e-16 65 -1.33844758193336e-16 66 -1.33844758193336e-16 67 -1.33844758193336e-16 68 -1.34468174442515e-16 69 -1.34468174442515e-16 70 -1.33844758193336e-16 71 -1.34468174442515e-16 72 -1.33844758193336e-16 73 -1.34468174442515e-16 74 -1.33844758193336e-16 75 -1.34468174442515e-16 76 -1.33844758193336e-16 77 -1.34468174442515e-16 78 -1.34468174442515e-16 79 -1.33844758193336e-16 80 -1.33844758193336e-16 81 -1.33844758193336e-16 82 -1.33844758193336e-16 83 -1.33844758193336e-16 84 -1.33844758193336e-16 85 -1.33844758193336e-16 86 -1.3403449357352e-16 87 -1.3403449357352e-16 88 -1.3403449357352e-16 89 -1.3403449357352e-16 90 -1.3403449357352e-16 91 -1.3403449357352e-16 92 -1.3403449357352e-16 93 -1.3403449357352e-16 94 -1.3403449357352e-16 95 -1.3403449357352e-16 96 -1.3403449357352e-16 97 -1.3403449357352e-16 98 -1.3403449357352e-16 99 -1.3403449357352e-16 100 -1.3403449357352e-16 Rows: 1-100 | Columns: 7
Please refer to
transform()for more details on transforming avDataFrame.Similarly, you can perform the inverse tranform to get the original features using:
model.inverse_transform(data_transformed)
The variable
data_transformedincludes the MCA components.Plots - MCA¶
You can plot the first two dimensions conveniently using:
model.plot()
Plots - Scree¶
You can also plot the Scree plot:
model.plot_scree()
Loading....Plots - Decomposition Circle¶
You can also plot the Decomposition Circles:
model.plot_circle()
Model Register¶
As this model is not native, it does not support model management and versioning. However, it is possible to use the SQL code it generates for deployment.
Model Exporting¶
To Memmodel
model.to_memmodel()
Note
MemModelobjects serve as in-memory representations of machine learning models. They can be used for both in-database and in-memory prediction tasks. These objects can be pickled in the same way that you would pickle ascikit-learnmodel.The preceding methods for exporting the model use
MemModel, and it is recommended to useMemModeldirectly.SQL
To get the SQL query use below:
model.to_sql() Out[9]: ['("survived_0" - -0.999189627228525) * -0.181634302254857 + ("survived_1" - -0.999189627228525) * 0.316447317706116 + ("pclass_1" - -0.999189627228525) * 0.648146903282895 + ("pclass_2" - -0.999189627228525) * -0.588909547134058 + ("pclass_3" - -0.999189627228525) * -0.0749536366765797 + ("sex_female" - -0.999189627228525) * 0.272960421482614 + ("sex_male" - -0.999189627228525) * -0.140839529511913', '("survived_0" - -0.999189627228525) * -0.232487358829214 + ("survived_1" - -0.999189627228525) * 0.405044642937851 + ("pclass_1" - -0.999189627228525) * 0.12304188125764 + ("pclass_2" - -0.999189627228525) * 0.689909726160875 + ("pclass_3" - -0.999189627228525) * -0.327414307734686 + ("sex_female" - -0.999189627228525) * 0.38075847078997 + ("sex_male" - -0.999189627228525) * -0.196460144633645', '("survived_0" - -0.999189627228525) * 0.134292054542375 + ("survived_1" - -0.999189627228525) * -0.233966601691513 + ("pclass_1" - -0.999189627228525) * 0.612207252170771 + ("pclass_2" - -0.999189627228525) * 0.25190527149492 + ("pclass_3" - -0.999189627228525) * -0.386503963792662 + ("sex_female" - -0.999189627228525) * -0.517868305005971 + ("sex_male" - -0.999189627228525) * 0.267204776538706', '("survived_0" - -0.999189627228525) * -0.377802794939543 + ("survived_1" - -0.999189627228525) * 0.658216424961079 + ("pclass_1" - -0.999189627228525) * -0.169709763121012 + ("pclass_2" - -0.999189627228525) * -0.0518795498186792 + ("pclass_3" - -0.999189627228525) * 0.100130089738782 + ("sex_female" - -0.999189627228525) * -0.549615059411761 + ("sex_male" - -0.999189627228525) * 0.283585165789851', '("survived_0" - -0.999189627228525) * 0.831334231648148 + ("survived_1" - -0.999189627228525) * 0.477168882961496 + ("pclass_1" - -0.999189627228525) * 0.105081084294435 + ("pclass_2" - -0.999189627228525) * 0.0872307718982565 + ("pclass_3" - -0.999189627228525) * 0.223297304125609 + ("sex_female" - -0.999189627228525) * 0.0516338525183842 + ("sex_male" - -0.999189627228525) * 0.100071323690393', '("survived_0" - -0.999189627228525) * 0.0 + ("survived_1" - -0.999189627228525) * 1.24263724796271e-16 + ("pclass_1" - -0.999189627228525) * -0.158651249622961 + ("pclass_2" - -0.999189627228525) * -0.131700877090842 + ("pclass_3" - -0.999189627228525) * -0.337133905448692 + ("sex_female" - -0.999189627228525) * 0.421207057725456 + ("sex_male" - -0.999189627228525) * 0.816339392829816', '("survived_0" - -0.999189627228525) * -0.247128905111581 + ("survived_1" - -0.999189627228525) * -0.141846948087061 + ("pclass_1" - -0.999189627228525) * 0.353489618841737 + ("pclass_2" - -0.999189627228525) * 0.293441702820515 + ("pclass_3" - -0.999189627228525) * 0.751165440038467 + ("sex_female" - -0.999189627228525) * 0.173694732678169 + ("sex_male" - -0.999189627228525) * 0.336636934285787']
To Python
To obtain the prediction function in Python syntax, use the following code:
X = [[0, 1, 0, 1, 1, 0, 1]] model.to_python()(X) Out[11]: array([[-0.23724135, 1.41279017, 0.02580683, 0.88306341, 2.76206562, 0.95707065, 2.75761838]])
Hint
The
to_python()method is used to retrieve the Principal Component values. For specific details on how to use this method for different model types, refer to the relevant documentation for each model.- __init__(name: str = None, overwrite_model: bool = False) None¶
Must be overridden in the child class
Methods
__init__([name, overwrite_model])Must be overridden in the child class
contour([nbins, chart])Draws the model's contour plot.
deployInverseSQL([key_columns, ...])Returns the SQL code needed to deploy the inverse model.
deploySQL([X, n_components, cutoff, ...])Returns the SQL code needed to deploy the model.
does_model_exists(name[, raise_error, ...])Checks whether the model is stored in the Vertica database.
drop()Drops the model from the Vertica database.
export_models(name, path[, kind])Exports machine learning models.
fit(input_relation[, X, return_report])Trains the model.
get_attributes([attr_name])Returns the model attributes.
get_match_index(x, col_list[, str_check])Returns the matching index.
Returns the parameters of the model.
get_plotting_lib([class_name, chart, ...])Returns the first available library (Plotly, Matplotlib, or Highcharts) to draw a specific graphic.
get_vertica_attributes([attr_name])Returns the model Vertica attributes.
import_models(path[, schema, kind])Imports machine learning models.
inverse_transform(vdf[, X])Applies the Inverse Model on a
vDataFrame.plot([dimensions, chart])Draws a decomposition scatter plot.
plot_circle([dimensions, chart])Draws a decomposition circle.
plot_contrib([dimension, chart])Draws a decomposition contribution plot of the input dimension.
plot_cos2([dimensions, chart])Draws a MCA (multiple correspondence analysis) cos2 plot of the two input dimensions.
plot_scree([chart])Draws a decomposition scree plot.
plot_var([dimensions, method, chart])Draws the MCA (multiple correspondence analysis) graph.
register(registered_name[, raise_error])Registers the model and adds it to in-DB Model versioning environment with a status of 'under_review'.
score([X, input_relation, metric, p])Returns the decomposition score on a dataset for each transformed column.
set_params([parameters])Sets the parameters of the model.
Summarizes the model.
to_binary(path)Exports the model to the Vertica Binary format.
Converts the model to an InMemory object that can be used for different types of predictions.
to_pmml(path)Exports the model to PMML.
to_python([return_proba, ...])Returns the Python function needed for in-memory scoring without using built-in Vertica functions.
to_sql([X, return_proba, ...])Returns the SQL code needed to deploy the model without using built-in Vertica functions.
to_tf(path)Exports the model to the Frozen Graph format (TensorFlow).
transform([vdf, X, n_components, cutoff])Applies the model on a
vDataFrame.Attributes