verticapy.machine_learning.vertica.decomposition.PCA¶
- class verticapy.machine_learning.vertica.decomposition.PCA(name: str = None, overwrite_model: bool = False, n_components: int = 0, scale: bool = False, method: Literal['lapack'] = 'lapack')¶
Creates a PCA (Principal Component Analysis) object using the Vertica PCA algorithm.
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.- n_components: int, optional
The number of components to keep in the model. If this value is not provided, all components are kept. The maximum number of components is the number of non-zero singular values returned by the internal call to SVD. This number is less than or equal to SVD (number of columns, number of rows).
- scale: bool, optional
A Boolean value that specifies whether to standardize the columns during the preparation step.
- method: str, optional
The method used to calculate PCA.
- lapack:
Lapack definition.
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 winequality dataset.
import verticapy.datasets as vpd data = vpd.load_winequality()
123fixed_acidity123volatile_acidity123citric_acid123residual_sugar123chlorides123free_sulfur_dioxide123total_sulfur_dioxide123density123pH123sulphates123alcohol123quality123goodAbccolor1 3.9 0.225 0.4 4.2 0.03 29.0 118.0 0.989 3.57 0.36 12.8 8 1 white 2 4.7 0.335 0.14 1.3 0.036 69.0 168.0 0.99212 3.47 0.46 10.5 5 0 white 3 4.7 0.455 0.18 1.9 0.036 33.0 106.0 0.98746 3.21 0.83 14.0 7 1 white 4 4.7 0.785 0.0 3.4 0.036 23.0 134.0 0.98981 3.53 0.92 13.8 6 0 white 5 4.9 0.345 0.34 1.0 0.068 32.0 143.0 0.99138 3.24 0.4 10.1 5 0 white 6 4.9 0.345 0.34 1.0 0.068 32.0 143.0 0.99138 3.24 0.4 10.1 5 0 white 7 4.9 0.42 0.0 2.1 0.048 16.0 42.0 0.99154 3.71 0.74 14.0 7 1 red 8 5.0 0.27 0.4 1.2 0.076 42.0 124.0 0.99204 3.32 0.47 10.1 6 0 white 9 5.0 0.31 0.0 6.4 0.046 43.0 166.0 0.994 3.3 0.63 9.9 6 0 white 10 5.0 0.4 0.5 4.3 0.046 29.0 80.0 0.9902 3.49 0.66 13.6 6 0 red 11 5.0 0.44 0.04 18.6 0.039 38.0 128.0 0.9985 3.37 0.57 10.2 6 0 white 12 5.1 0.11 0.32 1.6 0.028 12.0 90.0 0.99008 3.57 0.52 12.2 6 0 white 13 5.1 0.14 0.25 0.7 0.039 15.0 89.0 0.9919 3.22 0.43 9.2 6 0 white 14 5.1 0.165 0.22 5.7 0.047 42.0 146.0 0.9934 3.18 0.55 9.9 6 0 white 15 5.1 0.33 0.22 1.6 0.027 18.0 89.0 0.9893 3.51 0.38 12.5 7 1 white 16 5.1 0.33 0.22 1.6 0.027 18.0 89.0 0.9893 3.51 0.38 12.5 7 1 white 17 5.1 0.33 0.22 1.6 0.027 18.0 89.0 0.9893 3.51 0.38 12.5 7 1 white 18 5.1 0.39 0.21 1.7 0.027 15.0 72.0 0.9894 3.5 0.45 12.5 6 0 white 19 5.2 0.2 0.27 3.2 0.047 16.0 93.0 0.99235 3.44 0.53 10.1 7 1 white 20 5.2 0.21 0.31 1.7 0.048 17.0 61.0 0.98953 3.24 0.37 12.0 7 1 white 21 5.2 0.22 0.46 6.2 0.066 41.0 187.0 0.99362 3.19 0.42 9.73333333333333 5 0 white 22 5.2 0.31 0.2 2.4 0.027 27.0 117.0 0.98886 3.56 0.45 13.0 7 1 white 23 5.2 0.32 0.25 1.8 0.103 13.0 50.0 0.9957 3.38 0.55 9.2 5 0 red 24 5.2 0.34 0.37 6.2 0.031 42.0 133.0 0.99076 3.25 0.41 12.5 6 0 white 25 5.2 0.36 0.02 1.6 0.031 24.0 104.0 0.9896 3.44 0.35 12.2 6 0 white 26 5.2 0.365 0.08 13.5 0.041 37.0 142.0 0.997 3.46 0.39 9.9 6 0 white 27 5.2 0.48 0.04 1.6 0.054 19.0 106.0 0.9927 3.54 0.62 12.2 7 1 red 28 5.2 0.5 0.18 2.0 0.036 23.0 129.0 0.98949 3.36 0.77 13.4 7 1 white 29 5.3 0.16 0.39 1.0 0.028 40.0 101.0 0.99156 3.57 0.59 10.6 6 0 white 30 5.3 0.16 0.39 1.0 0.028 40.0 101.0 0.99156 3.57 0.59 10.6 6 0 white 31 5.3 0.165 0.24 1.1 0.051 25.0 105.0 0.9925 3.32 0.47 9.1 5 0 white 32 5.3 0.23 0.56 0.9 0.041 46.0 141.0 0.99119 3.16 0.62 9.7 5 0 white 33 5.3 0.3 0.3 1.2 0.029 25.0 93.0 0.98742 3.31 0.4 13.6 7 1 white 34 5.3 0.33 0.3 1.2 0.048 25.0 119.0 0.99045 3.32 0.62 11.3 6 0 white 35 5.3 0.36 0.27 6.3 0.028 40.0 132.0 0.99186 3.37 0.4 11.6 6 0 white 36 5.3 0.36 0.27 6.3 0.028 40.0 132.0 0.99186 3.37 0.4 11.6 6 0 white 37 5.3 0.4 0.25 3.9 0.031 45.0 130.0 0.99072 3.31 0.58 11.75 7 1 white 38 5.3 0.47 0.11 2.2 0.048 16.0 89.0 0.99182 3.54 0.88 13.6 7 1 red 39 5.3 0.47 0.11 2.2 0.048 16.0 89.0 0.99182 3.54 0.88 13.5666666666667 7 1 red 40 5.3 0.715 0.19 1.5 0.161 7.0 62.0 0.99395 3.62 0.61 11.0 5 0 red 41 5.4 0.22 0.29 1.2 0.045 69.0 152.0 0.99178 3.76 0.63 11.0 7 1 white 42 5.4 0.595 0.1 2.8 0.042 26.0 80.0 0.9932 3.36 0.38 9.3 5 0 white 43 5.4 0.74 0.09 1.7 0.089 16.0 26.0 0.99402 3.67 0.56 11.6 6 0 red 44 5.5 0.12 0.33 1.0 0.038 23.0 131.0 0.99164 3.25 0.45 9.8 5 0 white 45 5.5 0.12 0.33 1.0 0.038 23.0 131.0 0.99164 3.25 0.45 9.8 5 0 white 46 5.5 0.14 0.27 4.6 0.029 22.0 104.0 0.9949 3.34 0.44 9.0 5 0 white 47 5.5 0.14 0.27 4.6 0.029 22.0 104.0 0.9949 3.34 0.44 9.0 5 0 white 48 5.5 0.16 0.31 1.2 0.026 31.0 68.0 0.9898 3.33 0.44 11.65 6 0 white 49 5.5 0.16 0.31 1.2 0.026 31.0 68.0 0.9898 3.33 0.44 11.6333333333333 6 0 white 50 5.5 0.18 0.22 5.5 0.037 10.0 86.0 0.99156 3.46 0.44 12.2 5 0 white 51 5.5 0.24 0.45 1.7 0.046 22.0 113.0 0.99224 3.22 0.48 10.0 5 0 white 52 5.5 0.29 0.3 1.1 0.022 20.0 110.0 0.98869 3.34 0.38 12.8 7 1 white 53 5.5 0.31 0.29 3.0 0.027 16.0 102.0 0.99067 3.23 0.56 11.2 6 0 white 54 5.5 0.32 0.45 4.9 0.028 25.0 191.0 0.9922 3.51 0.49 11.5 7 1 white 55 5.5 0.35 0.35 1.1 0.045 14.0 167.0 0.992 3.34 0.68 9.9 6 0 white 56 5.5 0.375 0.38 1.7 0.036 17.0 98.0 0.99142 3.29 0.39 10.5 6 0 white 57 5.6 0.15 0.26 5.55 0.051 51.0 139.0 0.99336 3.47 0.5 11.0 6 0 white 58 5.6 0.15 0.31 5.3 0.038 8.0 79.0 0.9923 3.3 0.39 10.5 6 0 white 59 5.6 0.16 0.27 1.4 0.044 53.0 168.0 0.9918 3.28 0.37 10.1 6 0 white 60 5.6 0.175 0.29 0.8 0.043 20.0 67.0 0.99112 3.28 0.48 9.9 6 0 white 61 5.6 0.185 0.19 7.1 0.048 36.0 110.0 0.99438 3.26 0.41 9.5 6 0 white 62 5.6 0.185 0.19 7.1 0.048 36.0 110.0 0.99438 3.26 0.41 9.5 6 0 white 63 5.6 0.22 0.32 1.2 0.024 29.0 97.0 0.98823 3.2 0.46 13.05 7 1 white 64 5.6 0.26 0.18 1.4 0.034 18.0 135.0 0.99174 3.32 0.35 10.2 6 0 white 65 5.6 0.26 0.26 5.7 0.031 12.0 80.0 0.9923 3.25 0.38 10.8 5 0 white 66 5.6 0.26 0.5 11.4 0.029 25.0 93.0 0.99428 3.23 0.49 10.5 6 0 white 67 5.6 0.28 0.28 4.2 0.044 52.0 158.0 0.992 3.35 0.44 10.7 7 1 white 68 5.6 0.3 0.1 6.4 0.043 34.0 142.0 0.99382 3.14 0.48 9.8 5 0 white 69 5.6 0.35 0.14 5.0 0.046 48.0 198.0 0.9937 3.3 0.71 10.3 5 0 white 70 5.6 0.49 0.13 4.5 0.039 17.0 116.0 0.9907 3.42 0.9 13.7 7 1 white 71 5.6 0.49 0.13 4.5 0.039 17.0 116.0 0.9907 3.42 0.9 13.7 7 1 white 72 5.6 0.66 0.0 2.2 0.087 3.0 11.0 0.99378 3.71 0.63 12.8 7 1 red 73 5.6 0.66 0.0 2.2 0.087 3.0 11.0 0.99378 3.71 0.63 12.8 7 1 red 74 5.7 0.15 0.47 11.4 0.035 49.0 128.0 0.99456 3.03 0.34 10.5 8 1 white 75 5.7 0.18 0.26 2.2 0.023 21.0 95.0 0.9893 3.07 0.54 12.3 6 0 white 76 5.7 0.18 0.36 1.2 0.046 9.0 71.0 0.99199 3.7 0.68 10.9 7 1 white 77 5.7 0.2 0.3 2.5 0.046 38.0 125.0 0.99276 3.34 0.5 9.9 6 0 white 78 5.7 0.21 0.32 0.9 0.038 38.0 121.0 0.99074 3.24 0.46 10.6 6 0 white 79 5.7 0.21 0.37 4.5 0.04 58.0 140.0 0.99332 3.29 0.62 10.6 6 0 white 80 5.7 0.22 0.2 16.0 0.044 41.0 113.0 0.99862 3.22 0.46 8.9 6 0 white 81 5.7 0.22 0.2 16.0 0.044 41.0 113.0 0.99862 3.22 0.46 8.9 6 0 white 82 5.7 0.22 0.2 16.0 0.044 41.0 113.0 0.99862 3.22 0.46 8.9 6 0 white 83 5.7 0.22 0.2 16.0 0.044 41.0 113.0 0.99862 3.22 0.46 8.9 6 0 white 84 5.7 0.22 0.2 16.0 0.044 41.0 113.0 0.99862 3.22 0.46 8.9 6 0 white 85 5.7 0.22 0.29 3.5 0.04 27.0 146.0 0.98999 3.17 0.36 12.1 6 0 white 86 5.7 0.23 0.28 9.65 0.025 26.0 121.0 0.9925 3.28 0.38 11.3 6 0 white 87 5.7 0.25 0.26 12.5 0.049 52.5 106.0 0.99691 3.08 0.45 9.4 6 0 white 88 5.7 0.25 0.26 12.5 0.049 52.5 120.0 0.99691 3.08 0.45 9.4 6 0 white 89 5.7 0.25 0.27 11.5 0.04 24.0 120.0 0.99411 3.33 0.31 10.8 6 0 white 90 5.7 0.26 0.24 17.8 0.059 23.0 124.0 0.99773 3.3 0.5 10.1 5 0 white 91 5.7 0.26 0.24 17.8 0.059 23.0 124.0 0.99773 3.3 0.5 10.1 5 0 white 92 5.7 0.26 0.24 17.8 0.059 23.0 124.0 0.99773 3.3 0.5 10.1 5 0 white 93 5.7 0.27 0.32 1.2 0.046 20.0 155.0 0.9934 3.8 0.41 10.2 6 0 white 94 5.7 0.28 0.24 17.5 0.044 60.0 167.0 0.9989 3.31 0.44 9.4 5 0 white 95 5.7 0.32 0.18 1.4 0.029 26.0 104.0 0.9906 3.44 0.37 11.0 6 0 white 96 5.7 0.32 0.38 4.75 0.033 23.0 94.0 0.991 3.42 0.42 11.8 7 1 white 97 5.7 0.36 0.34 4.2 0.026 21.0 77.0 0.9907 3.41 0.45 11.9 6 0 white 98 5.8 0.14 0.15 6.1 0.042 27.0 123.0 0.99362 3.06 0.6 9.9 6 0 white 99 5.8 0.15 0.32 1.2 0.037 14.0 119.0 0.99137 3.19 0.5 10.2 6 0 white 100 5.8 0.17 0.34 1.8 0.045 96.0 170.0 0.99035 3.38 0.9 11.8 8 1 white 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.
We can drop the “color” column as it is varchar type.
data.drop("color")
Model Initialization¶
First we import the
PCAmodel:from verticapy.machine_learning.vertica import PCA
Then we can create the model:
model = PCA( n_components = 3, )
You can select the number of components by the
n_componentparameter. If it is not provided, then all are considered.Hint
In
verticapy1.0.x and higher, you do not need to specify the model name, as the name is automatically assigned. If you need to re-use the model, you can fetch the model name from the model’s attributes.Important
The model name is crucial for the model management system and versioning. It’s highly recommended to provide a name if you plan to reuse the model later.
Model Training¶
We can now fit the model:
model.fit(data) ======= columns ======= index| name | mean | sd -----+--------------------+---------+-------- 1 | fixed_acidity | 7.21531 | 1.29643 2 | volatile_acidity | 0.33967 | 0.16464 3 | citric_acid | 0.31863 | 0.14532 4 | residual_sugar | 5.44324 | 4.75780 5 | chlorides | 0.05603 | 0.03503 6 |free_sulfur_dioxide |30.52532 |17.74940 7 |total_sulfur_dioxide|115.74457|56.52185 8 | density | 0.99470 | 0.00300 9 | ph | 3.21850 | 0.16079 10 | sulphates | 0.53127 | 0.14881 11 | alcohol |10.49180 | 1.19271 12 | quality | 5.81838 | 0.87326 13 | good | 0.19655 | 0.39742 =============== singular_values =============== index| value |explained_variance|accumulated_explained_variance -----+--------+------------------+------------------------------ 1 |58.06985| 0.95351 | 0.95351 2 |11.98567| 0.04062 | 0.99413 3 | 4.13105| 0.00483 | 0.99896 ==================== principal_components ==================== index| PC1 | PC2 | PC3 -----+--------+--------+-------- 1 |-0.00741|-0.00537| 0.02386 2 |-0.00118|-0.00079| 0.00091 3 | 0.00049|-0.00025| 0.00192 4 | 0.04102| 0.01863| 0.99519 5 |-0.00017| 0.00007| 0.00018 6 | 0.23048| 0.97262|-0.02711 7 | 0.97217|-0.23139|-0.03586 8 | 0.00000| 0.00000| 0.00046 9 |-0.00066| 0.00065|-0.00691 10 |-0.00070| 0.00035|-0.00194 11 |-0.00545| 0.00288|-0.08266 12 |-0.00053| 0.00916|-0.00889 13 |-0.00033| 0.00257|-0.00593 ======== counters ======== counter_name |counter_value ------------------+------------- accepted_row_count| 6497 rejected_row_count| 0 iteration_count | 1 =========== call_string =========== SELECT PCA('"public"."_verticapy_tmp_pca_v_mldb_6a3e7c2e979711efa8720242ac120002_"', '"public"."_verticapy_tmp_view_v_mldb_6a7b3c04979711efa8720242ac120002_"', '"fixed_acidity", "volatile_acidity", "citric_acid", "residual_sugar", "chlorides", "free_sulfur_dioxide", "total_sulfur_dioxide", "density", "pH", "sulphates", "alcohol", "quality", "good"' USING PARAMETERS scale=false, num_components=3);
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[4]: None Score fixed_acidity 3.26018188659356 volatile_acidity 0.255704870531141 citric_acid 0.232055113862187 residual_sugar 0.784335096491506 chlorides 0.0412739282041458 free_sulfur_dioxide 0.743079726641214 total_sulfur_dioxide 1.03787465340887 density 0.0561515090418319 pH 0.801959298244123 sulphates 0.303876275255451 alcohol 4.02272456571807 quality 2.54477536599627 good 0.348243267430713 Rows: 1-13 | Columns: 2
For more details on the function, check out
score()You can also fetch the explained variance by:
model.explained_variance_ Out[5]: array([0.95351037, 0.04062083, 0.00482552])
Principal Components¶
To get the transformed dataset in the form of principal components:
model.transform(data) Out[6]: None col1 col2 col3 1 58.2585435866956 50.2581718382341 -7.15770033829499 2 47.8458923519163 22.9883205947403 -2.56964241029969 3 55.3788310502415 46.7802985538193 -7.45917494667214 4 55.8773719495785 -33.4658268951551 -5.7612979172938 5 -15.2310907537325 13.2721178525629 -4.02619559516123 6 -6.66895840225282 -7.21300376171408 -2.27046351728112 7 -6.55502655331496 -12.1686938056515 5.01741673181464 8 17.5008031764373 12.6595659366785 -5.22319739599441 9 64.0218965257457 -7.6609298570904 -7.00387756953786 10 3.27549637771102 2.98554661691686 6.13681673815753 11 35.0776851714977 0.281798297982037 -6.05124271309634 12 59.5163276885018 25.2569270184036 -7.08677590412429 13 59.5163276885018 25.2569270184036 -7.08677590412429 14 39.6841016220826 6.39508394279764 -4.60997393999157 15 61.0027620426437 9.58292669483431 -4.70032804310602 16 39.6146400834972 -16.9614288466675 4.85177815550989 17 -9.05233764951464 4.63067668949458 -3.60265729333252 18 -71.3209829620149 1.95095263269743 -1.55326105628056 19 -44.8177038153328 -3.32408322957085 -1.97193555518783 20 34.4954873429935 -10.9184000917509 -5.77391014958169 21 16.0557078794116 10.0876558355333 0.301566854171824 22 -14.7925554427983 -2.23755020006416 -3.49815587620536 23 -35.9331209476593 7.94028679930402 -1.34254106589017 24 -38.8823966562382 4.48833970280952 -3.12535345595975 25 -40.2475149621483 -1.34154829779239 -2.46499047023164 26 -22.2439190727728 -7.64669388773425 -3.15856230273649 27 -43.8486203630266 -3.54797443411161 -2.06066615700047 28 -37.9825447838218 -9.06930587461121 -2.65098373716963 29 -14.7176386387758 -7.22915685663195 2.79821670371247 30 5.87868202428021 11.3419902859004 -4.68490339600052 31 5.87868202428021 11.3419902859004 -4.68490339600052 32 -53.0361658378291 -7.56802962653592 -2.38792515916192 33 -2.18645483941726 -2.00295741680722 1.98891773276953 34 -45.2419386762454 -9.41311081776226 -2.18616368987377 35 -60.7026869530663 -13.9295260643832 -1.53198510227278 36 -12.0977825304334 0.221015212890943 -2.27030993497004 37 -4.23590901086943 6.69306391189276 1.74416419472203 38 -28.9423854285489 2.29809920192316 1.98015091955561 39 14.9711139961645 41.0221634644192 -5.48379105461812 40 -8.89199083888603 0.544774112136061 -0.802536442046489 41 -23.5633723605189 -0.158474443586148 -3.57342555010811 42 30.951920634207 8.46775316590347 -3.85908075190824 43 -8.39746657541938 -1.63604022426093 -1.26320600579139 44 -60.9274109944813 6.70068383920975 -1.25659879942325 45 -71.3239461395363 1.94880379853522 -1.54371564900316 46 -26.3271101035469 -11.8316349249606 -3.20114227347833 47 -42.6631180778819 -2.82046371621985 -2.53137244580647 48 3.13858172797976 -0.313314340722323 -3.63199332923701 49 -17.7257199740003 -19.042961915661 -2.81789228127042 50 -28.6845498098243 4.16886554183883 -2.88451102497602 51 -21.1368994490638 -7.86904708071976 -0.0505301707979124 52 -43.8567232892656 0.616666828063992 -1.00122049575341 53 -43.8567232892656 0.616666828063992 -1.00122049575341 54 -13.3898146125559 -5.57989179416356 0.0789195527929948 55 -50.6701591427734 -1.94729914664207 -2.23100117676321 56 36.7343745322282 -14.5349150809514 -5.37438544593779 57 5.54392122869397 -16.3153376375122 -2.86742379241561 58 -45.4995344865387 -6.22144753680841 -1.59002136120339 59 51.3890491383617 -0.515319080832429 -5.1735252192907 60 -8.4552801596779 -15.8433463863653 5.18028150863589 61 -11.3452508986281 12.3250923024257 -4.1362072202068 62 -24.4591677987751 -1.65753683516123 9.43165664047288 63 -21.3517440617296 -0.525811760264606 2.75160644879837 64 -21.3517440617296 -0.525811760264606 2.75160644879837 65 -27.6662555296056 -1.09200730619686 2.52334048417307 66 54.9395589948006 -20.7582582225397 -1.17977372548587 67 -17.5483170110743 7.68829760512098 -2.69484923618189 68 -105.914733294145 -1.11813575030598 0.541918795710582 69 -105.914733294145 -1.11813575030598 0.541918795710582 70 -58.6556742195508 -1.06977984719543 -1.38022635757763 71 -105.684929342291 -0.162232221938359 0.432516347466094 72 -103.48483223194 0.383321377182943 1.08956518419427 73 -18.4568875105065 -5.38113396956644 0.927947984624256 74 40.3122897522569 17.5707761764461 -3.81164089238009 75 40.3122897522569 17.5707761764461 -3.81164089238009 76 -3.2004038488878 -1.7974698694657 1.03254923632082 77 -31.5872109925239 -16.7698728126436 -2.69617343137619 78 77.8939482830483 11.6668429317711 -6.56181482485684 79 77.8939482830483 11.6668429317711 -6.56181482485684 80 6.49764491315117 0.901393112002678 -4.27003463262866 81 72.7569547959708 50.8734792819764 -8.33797192598707 82 -4.23685380698288 7.57556593326435 -3.69053012357174 83 -18.694106678149 -1.31855012765248 -3.54737619016085 84 -62.3171405117399 -9.19510699563876 7.63941681809391 85 -10.7167283879474 1.93446727781806 -3.44551093652981 86 36.9370652532915 7.17362872293809 -0.275934599737356 87 36.9370652532915 7.17362872293809 -0.275934599737356 88 -4.11501330759734 2.60757174492509 3.63976956684187 89 -4.11501330759734 2.60757174492509 3.63976956684187 90 -23.4522869302903 13.1845512500111 -3.0741906393028 91 -30.9328980436483 -1.40965296609462 -0.190808411699063 92 -22.8580984418803 2.74602275333044 -3.32639842349154 93 -5.79893309122244 -12.5114932944398 -0.0707707043574328 94 -5.79893309122244 -12.5114932944398 -0.0707707043574328 95 55.1436384119657 -15.6605619821546 -0.638954470669958 96 51.7376826854555 -11.9415002244243 -5.83312383826673 97 5.97630943862789 3.070478923037 -4.54308193034896 98 64.1812667449478 -21.7614310143679 4.85207523153766 99 64.1812667449478 -21.7614310143679 4.85207523153766 100 -19.6058608927542 -5.24345400802905 -3.34594564413817 Rows: 1-100 | Columns: 3
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 PCA components.Plots - PCA¶
You can plot the first two components conveniently using:
model.plot()
Plots - Scree¶
You can also plot the Scree plot:
model.plot_scree()
Loading....Parameter Modification¶
In order to see the parameters:
model.get_params() Out[7]: {'n_components': 3, 'scale': False, 'method': 'lapack'}
And to manually change some of the parameters:
model.set_params({'n_components': 3})
Model Register¶
In order to register the model for tracking and versioning:
model.register("model_v1")
Please refer to /notebooks/ml/model_tracking_versioning/index.ipynb for more details on model tracking and versioning.
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]: ['("fixed_acidity" - 7.21530706479914) * -0.0074079438036857 + ("volatile_acidity" - 0.339665999692166) * -0.00118432129646062 + ("citric_acid" - 0.318633215330152) * 0.000486867004519226 + ("residual_sugar" - 5.44323533938741) * 0.0410197294279797 + ("chlorides" - 0.0560338617823611) * -0.000168197431343703 + ("free_sulfur_dioxide" - 30.5253193781746) * 0.230481489722234 + ("total_sulfur_dioxide" - 115.744574418963) * 0.972166693841427 + ("density" - 0.994696633830999) * 1.77249642312968e-06 + ("pH" - 3.21850084654456) * -0.000655520779893174 + ("sulphates" - 0.531268277666616) * -0.000704339207760155 + ("alcohol" - 10.4918008311528) * -0.0054518247482017 + ("quality" - 5.81837771279052) * -0.000532705277259336 + ("good" - 0.196552254886871) * -0.000326386219750347', '("fixed_acidity" - 7.21530706479914) * -0.00537208540551765 + ("volatile_acidity" - 0.339665999692166) * -0.000787179265927931 + ("citric_acid" - 0.318633215330152) * -0.000247100931315455 + ("residual_sugar" - 5.44323533938741) * 0.0186262696200209 + ("chlorides" - 0.0560338617823611) * 6.67995503853843e-05 + ("free_sulfur_dioxide" - 30.5253193781746) * 0.972615829133943 + ("total_sulfur_dioxide" - 115.744574418963) * -0.231392918488152 + ("density" - 0.994696633830999) * 1.27197739295354e-06 + ("pH" - 3.21850084654456) * 0.000648027441471814 + ("sulphates" - 0.531268277666616) * 0.00034656412640182 + ("alcohol" - 10.4918008311528) * 0.00288222663864494 + ("quality" - 5.81837771279052) * 0.00915670157688833 + ("good" - 0.196552254886871) * 0.00256735969031285', '("fixed_acidity" - 7.21530706479914) * 0.023863518193516 + ("volatile_acidity" - 0.339665999692166) * 0.000908752011458777 + ("citric_acid" - 0.318633215330152) * 0.00192084403361971 + ("residual_sugar" - 5.44323533938741) * 0.995191758737064 + ("chlorides" - 0.0560338617823611) * 0.000177543760783034 + ("free_sulfur_dioxide" - 30.5253193781746) * -0.0271071934686966 + ("total_sulfur_dioxide" - 115.744574418963) * -0.0358591349601494 + ("density" - 0.994696633830999) * 0.000460901781338664 + ("pH" - 3.21850084654456) * -0.00691133676962069 + ("sulphates" - 0.531268277666616) * -0.00193655721535334 + ("alcohol" - 10.4918008311528) * -0.0826607385648236 + ("quality" - 5.81837771279052) * -0.0088875638392344 + ("good" - 0.196552254886871) * -0.00592921565549885']
To Python
To obtain the prediction function in Python syntax, use the following code:
X = [[3.8, 0.3, 0.02, 11, 0.03, 20, 113, 0.99, 3, 0.4, 12, 6, 0]] model.to_python()(X) Out[11]: array([[-4.84895114, -9.47474643, 5.70830533]])
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, n_components: int = 0, scale: bool = False, method: Literal['lapack'] = 'lapack') 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_scree([chart])Draws a decomposition scree plot.
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