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 fromverticapy
are 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_acidityNumeric(8)123volatile_acidityNumeric(9)123citric_acidNumeric(8)123residual_sugarNumeric(9)123chloridesFloat(22)123free_sulfur_dioxideNumeric(9)123total_sulfur_dioxideNumeric(9)123densityFloat(22)123pHNumeric(8)123sulphatesNumeric(8)123alcoholFloat(22)123qualityInteger123goodIntegerAbccolorVarchar(20)1 3.8 0.31 0.02 11.1 0.036 20.0 114.0 0.99248 3.75 0.44 12.4 6 0 white 2 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 3 4.2 0.17 0.36 1.8 0.029 93.0 161.0 0.98999 3.65 0.89 12.0 7 1 white 4 4.2 0.215 0.23 5.1 0.041 64.0 157.0 0.99688 3.42 0.44 8.0 3 0 white 5 4.4 0.32 0.39 4.3 0.03 31.0 127.0 0.98904 3.46 0.36 12.8 8 1 white 6 4.4 0.46 0.1 2.8 0.024 31.0 111.0 0.98816 3.48 0.34 13.1 6 0 white 7 4.4 0.54 0.09 5.1 0.038 52.0 97.0 0.99022 3.41 0.4 12.2 7 1 white 8 4.5 0.19 0.21 0.95 0.033 89.0 159.0 0.99332 3.34 0.42 8.0 5 0 white 9 4.6 0.445 0.0 1.4 0.053 11.0 178.0 0.99426 3.79 0.55 10.2 5 0 white 10 4.6 0.52 0.15 2.1 0.054 8.0 65.0 0.9934 3.9 0.56 13.1 4 0 red 11 4.7 0.145 0.29 1.0 0.042 35.0 90.0 0.9908 3.76 0.49 11.3 6 0 white 12 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 13 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 14 4.7 0.6 0.17 2.3 0.058 17.0 106.0 0.9932 3.85 0.6 12.9 6 0 red 15 4.7 0.67 0.09 1.0 0.02 5.0 9.0 0.98722 3.3 0.34 13.6 5 0 white 16 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 17 4.8 0.13 0.32 1.2 0.042 40.0 98.0 0.9898 3.42 0.64 11.8 7 1 white 18 4.8 0.17 0.28 2.9 0.03 22.0 111.0 0.9902 3.38 0.34 11.3 7 1 white 19 4.8 0.21 0.21 10.2 0.037 17.0 112.0 0.99324 3.66 0.48 12.2 7 1 white 20 4.8 0.225 0.38 1.2 0.074 47.0 130.0 0.99132 3.31 0.4 10.3 6 0 white 21 4.8 0.26 0.23 10.6 0.034 23.0 111.0 0.99274 3.46 0.28 11.5 7 1 white 22 4.8 0.29 0.23 1.1 0.044 38.0 180.0 0.98924 3.28 0.34 11.9 6 0 white 23 4.8 0.33 0.0 6.5 0.028 34.0 163.0 0.9937 3.35 0.61 9.9 5 0 white 24 4.8 0.34 0.0 6.5 0.028 33.0 163.0 0.9939 3.36 0.61 9.9 6 0 white 25 4.8 0.65 0.12 1.1 0.013 4.0 10.0 0.99246 3.32 0.36 13.5 4 0 white 26 4.9 0.235 0.27 11.75 0.03 34.0 118.0 0.9954 3.07 0.5 9.4 6 0 white 27 4.9 0.33 0.31 1.2 0.016 39.0 150.0 0.98713 3.33 0.59 14.0 8 1 white 28 4.9 0.335 0.14 1.3 0.036 69.0 168.0 0.99212 3.47 0.46 10.4666666666667 5 0 white 29 4.9 0.335 0.14 1.3 0.036 69.0 168.0 0.99212 3.47 0.46 10.4666666666667 5 0 white 30 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 31 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 32 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 33 4.9 0.47 0.17 1.9 0.035 60.0 148.0 0.98964 3.27 0.35 11.5 6 0 white 34 5.0 0.17 0.56 1.5 0.026 24.0 115.0 0.9906 3.48 0.39 10.8 7 1 white 35 5.0 0.2 0.4 1.9 0.015 20.0 98.0 0.9897 3.37 0.55 12.05 6 0 white 36 5.0 0.235 0.27 11.75 0.03 34.0 118.0 0.9954 3.07 0.5 9.4 6 0 white 37 5.0 0.24 0.19 5.0 0.043 17.0 101.0 0.99438 3.67 0.57 10.0 5 0 white 38 5.0 0.24 0.21 2.2 0.039 31.0 100.0 0.99098 3.69 0.62 11.7 6 0 white 39 5.0 0.24 0.34 1.1 0.034 49.0 158.0 0.98774 3.32 0.32 13.1 7 1 white 40 5.0 0.255 0.22 2.7 0.043 46.0 153.0 0.99238 3.75 0.76 11.3 6 0 white 41 5.0 0.27 0.32 4.5 0.032 58.0 178.0 0.98956 3.45 0.31 12.6 7 1 white 42 5.0 0.27 0.32 4.5 0.032 58.0 178.0 0.98956 3.45 0.31 12.6 7 1 white 43 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 44 5.0 0.29 0.54 5.7 0.035 54.0 155.0 0.98976 3.27 0.34 12.9 8 1 white 45 5.0 0.3 0.33 3.7 0.03 54.0 173.0 0.9887 3.36 0.3 13.0 7 1 white 46 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 47 5.0 0.33 0.16 1.5 0.049 10.0 97.0 0.9917 3.48 0.44 10.7 6 0 white 48 5.0 0.33 0.16 1.5 0.049 10.0 97.0 0.9917 3.48 0.44 10.7 6 0 white 49 5.0 0.33 0.16 1.5 0.049 10.0 97.0 0.9917 3.48 0.44 10.7 6 0 white 50 5.0 0.33 0.18 4.6 0.032 40.0 124.0 0.99114 3.18 0.4 11.0 6 0 white 51 5.0 0.33 0.23 11.8 0.03 23.0 158.0 0.99322 3.41 0.64 11.8 6 0 white 52 5.0 0.35 0.25 7.8 0.031 24.0 116.0 0.99241 3.39 0.4 11.3 6 0 white 53 5.0 0.35 0.25 7.8 0.031 24.0 116.0 0.99241 3.39 0.4 11.3 6 0 white 54 5.0 0.38 0.01 1.6 0.048 26.0 60.0 0.99084 3.7 0.75 14.0 6 0 red 55 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 56 5.0 0.42 0.24 2.0 0.06 19.0 50.0 0.9917 3.72 0.74 14.0 8 1 red 57 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 58 5.0 0.455 0.18 1.9 0.036 33.0 106.0 0.98746 3.21 0.83 14.0 7 1 white 59 5.0 0.55 0.14 8.3 0.032 35.0 164.0 0.9918 3.53 0.51 12.5 8 1 white 60 5.0 0.61 0.12 1.3 0.009 65.0 100.0 0.9874 3.26 0.37 13.5 5 0 white 61 5.0 0.74 0.0 1.2 0.041 16.0 46.0 0.99258 4.01 0.59 12.5 6 0 red 62 5.0 1.02 0.04 1.4 0.045 41.0 85.0 0.9938 3.75 0.48 10.5 4 0 red 63 5.0 1.04 0.24 1.6 0.05 32.0 96.0 0.9934 3.74 0.62 11.5 5 0 red 64 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 65 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 66 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 67 5.1 0.21 0.28 1.4 0.047 48.0 148.0 0.99168 3.5 0.49 10.4 5 0 white 68 5.1 0.23 0.18 1.0 0.053 13.0 99.0 0.98956 3.22 0.39 11.5 5 0 white 69 5.1 0.25 0.36 1.3 0.035 40.0 78.0 0.9891 3.23 0.64 12.1 7 1 white 70 5.1 0.26 0.33 1.1 0.027 46.0 113.0 0.98946 3.35 0.43 11.4 7 1 white 71 5.1 0.26 0.34 6.4 0.034 26.0 99.0 0.99449 3.23 0.41 9.2 6 0 white 72 5.1 0.29 0.28 8.3 0.026 27.0 107.0 0.99308 3.36 0.37 11.0 6 0 white 73 5.1 0.29 0.28 8.3 0.026 27.0 107.0 0.99308 3.36 0.37 11.0 6 0 white 74 5.1 0.3 0.3 2.3 0.048 40.0 150.0 0.98944 3.29 0.46 12.2 6 0 white 75 5.1 0.305 0.13 1.75 0.036 17.0 73.0 0.99 3.4 0.51 12.3333333333333 5 0 white 76 5.1 0.31 0.3 0.9 0.037 28.0 152.0 0.992 3.54 0.56 10.1 6 0 white 77 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 78 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 79 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 80 5.1 0.33 0.27 6.7 0.022 44.0 129.0 0.99221 3.36 0.39 11.0 7 1 white 81 5.1 0.35 0.26 6.8 0.034 36.0 120.0 0.99188 3.38 0.4 11.5 6 0 white 82 5.1 0.35 0.26 6.8 0.034 36.0 120.0 0.99188 3.38 0.4 11.5 6 0 white 83 5.1 0.35 0.26 6.8 0.034 36.0 120.0 0.99188 3.38 0.4 11.5 6 0 white 84 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 85 5.1 0.42 0.0 1.8 0.044 18.0 88.0 0.99157 3.68 0.73 13.6 7 1 red 86 5.1 0.42 0.01 1.5 0.017 25.0 102.0 0.9894 3.38 0.36 12.3 7 1 white 87 5.1 0.47 0.02 1.3 0.034 18.0 44.0 0.9921 3.9 0.62 12.8 6 0 red 88 5.1 0.51 0.18 2.1 0.042 16.0 101.0 0.9924 3.46 0.87 12.9 7 1 red 89 5.1 0.52 0.06 2.7 0.052 30.0 79.0 0.9932 3.32 0.43 9.3 5 0 white 90 5.1 0.585 0.0 1.7 0.044 14.0 86.0 0.99264 3.56 0.94 12.9 7 1 red 91 5.2 0.155 0.33 1.6 0.028 13.0 59.0 0.98975 3.3 0.84 11.9 8 1 white 92 5.2 0.155 0.33 1.6 0.028 13.0 59.0 0.98975 3.3 0.84 11.9 8 1 white 93 5.2 0.16 0.34 0.8 0.029 26.0 77.0 0.99155 3.25 0.51 10.1 6 0 white 94 5.2 0.17 0.27 0.7 0.03 11.0 68.0 0.99218 3.3 0.41 9.8 5 0 white 95 5.2 0.185 0.22 1.0 0.03 47.0 123.0 0.99218 3.55 0.44 10.15 6 0 white 96 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 97 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 98 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 99 5.2 0.24 0.15 7.1 0.043 32.0 134.0 0.99378 3.24 0.48 9.9 6 0 white 100 5.2 0.24 0.45 3.8 0.027 21.0 128.0 0.992 3.55 0.49 11.2 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
PCA
model: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_component
parameter. If it is not provided, then all are considered.Hint
In
verticapy
1.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)
Important
To train a model, you can directly use the
vDataFrame
or 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.255704870531137 citric_acid 0.232055113862187 residual_sugar 0.784335096491506 chlorides 0.0412739282041459 free_sulfur_dioxide 0.743079726641283 total_sulfur_dioxide 1.03787465340886 density 0.0561515090418321 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 -3.87539586294116 -9.70263141971311 5.73365140692485 2 1.8007158864829 -1.98185721028904 -1.57290632355351 3 58.2585435866957 50.2581718382341 -7.157700338295 4 47.8458923519163 22.9883205947403 -2.56964241029969 5 11.0115318397046 -2.12012882814391 -1.8375746016803 6 -4.60521361896981 1.5341694499065 -2.75824074459435 7 -13.2771414595065 25.2504890643806 -0.476520460754179 8 55.3788310502415 46.7802985538193 -7.45917494667214 9 55.8773719495786 -33.4658268951551 -5.76129791729379 10 -54.6555678140085 -10.2240557382057 -1.16251198171631 11 -24.1648594959502 10.2439927861505 -3.75116700698459 12 59.5176275497709 25.258097509706 -7.09430396571515 13 -9.05011526637347 4.63228831511623 -3.60981634879058 14 -12.7149929321467 -10.9367841190281 -2.67613969311331 15 -109.83712113806 -0.19489867094966 -0.211036337506947 16 15.9284646994512 -11.5572095245047 -2.82109432623325 17 -15.2310907537324 13.2721178525629 -4.02619559516123 18 -6.66895840225276 -7.21300376171408 -2.27046351728112 19 -6.5550265533149 -12.1686938056515 5.01741673181463 20 17.5008031764373 12.6595659366785 -5.22319739599442 21 -6.12385714274914 -6.09641638348886 5.34842459664887 22 64.0218965257457 -7.6609298570904 -7.00387756953786 23 46.8056869539979 -7.53188964107484 -0.739011856339026 24 46.5746543609322 -8.49535015989575 -0.720852160376789 25 -109.09098458572 -1.40699916293401 -0.100865448889536 26 3.27549637771107 2.98554661691686 6.13681673815753 27 35.0776851714978 0.281798297982036 -6.05124271309635 28 59.5163276885018 25.2569270184036 -7.08677590412429 29 59.5163276885018 25.2569270184036 -7.08677590412429 30 26.6743114830131 -4.95190539016235 -5.45347556437632 31 26.6743114830131 -4.95190539016235 -5.45347556437632 32 -75.180559206192 2.90998249909939 -0.653853292464544 33 38.0171694522185 21.1542722104449 -5.62104279390702 34 -2.37547571780678 -6.22192340926697 -3.81553048439253 35 -19.8139354364376 -6.17937852955219 -2.78776089879949 36 3.2747555833307 2.98500940837631 6.13920308997688 37 -17.4503747760159 -9.74850498290234 0.446947775612021 38 -15.3204945867663 4.06143755060113 -2.83284045680938 39 45.1607450949272 8.14262728404729 -6.62246702457769 40 39.6841016220826 6.39508394279764 -4.60997393999157 41 66.8204822700176 12.3302615531612 -4.15952189478502 42 66.8204822700176 12.3302615531612 -4.15952189478502 43 10.5149045897773 9.18318394872144 -4.85132644225315 44 43.5859575590291 13.7940321018126 -2.06416136230967 45 61.0027620426438 9.58292669483431 -4.70032804310602 46 51.7904435247003 0.533685199469238 -1.19389604379981 47 -23.1002361108527 -15.6854930106014 -2.7681976678508 48 -23.1002361108527 -15.6854930106014 -2.7681976678508 49 -23.1002361108527 -15.6854930106014 -2.7681976678508 50 10.1884723538412 7.30376481892969 -1.48712788146904 51 39.6146400834973 -16.9614288466675 4.85177815550988 52 -1.14706454035176 -6.34637338860446 2.39197736130292 53 -1.14706454035176 -6.34637338860446 2.39197736130292 54 -55.3970836353244 8.44951985367211 -2.05075012096447 55 -35.1489550264151 6.78834277464059 -0.126587947134508 56 -66.7170483841066 3.98339148251936 -1.12767424697563 57 14.1943699338795 4.69155400256368 12.4206564896857 58 -9.05233764951458 4.63067668949458 -3.60265729333253 59 48.0643491900927 -6.72080812877328 0.746048150327689 60 -7.53034129056041 37.1090070862536 -4.78644698313109 61 -71.3209829620148 1.95095263269743 -1.55326105628057 62 -27.6243365464012 17.2212353028306 -3.24496967423472 63 -19.002636112788 5.93811212781568 -3.28776432567215 64 -29.4490299121844 -12.1146438784988 -2.59415770058483 65 -29.750093053573 -8.99107737676142 -3.2848216598803 66 32.0875872308678 4.17530649922571 -1.14257902778671 67 35.2360399214631 9.46054341305244 -5.69064790913363 68 -20.4892045223688 -13.2472443987493 -3.47184703179337 69 -34.6741775069697 17.9008639524076 -3.22563541364455 70 0.730202192986314 15.6320679596781 -4.78499660843747 71 -17.2594071938456 -0.500180186642626 1.73125273716163 72 -9.18358839473754 -1.33806872969595 3.15843640640076 73 -9.18358839473754 -1.33806872969595 3.15843640640076 74 35.3631549238248 1.24771708830033 -4.80588411457336 75 -44.8177038153327 -3.32408322957084 -1.97193555518784 76 34.4954873429935 -10.9184000917509 -5.77391014958169 77 -29.0409734384488 -6.03520319370948 -2.7598636252927 78 -29.0409734384488 -6.03520319370948 -2.7598636252927 79 -29.0409734384488 -6.03520319370948 -2.7598636252927 80 16.0557078794116 10.0876558355333 0.301566854171818 81 5.46454014126889 4.38084910265817 0.914005555368653 82 5.46454014126889 4.38084910265817 0.914005555368653 83 5.46454014126889 4.38084910265817 0.914005555368653 84 -46.2544093147873 -5.02925948152294 -1.95463187806248 85 -30.0115077058734 -5.79669845533482 -2.61808240679804 86 -14.7925554427983 -2.23755020006416 -3.49815587620536 87 -72.8022460813843 4.36130645643567 -1.45815612704746 88 -17.8181544771345 -10.7466772356141 -2.67193978657889 89 -35.9331209476592 7.94028679930402 -1.34254106589017 90 -32.8783174079216 -9.22839098854763 -2.47901890123736 91 -59.3563097995857 -3.94947065130502 -1.50484516743191 92 -59.3563097995857 -3.94947065130502 -1.50484516743191 93 -38.8823966562381 4.48833970280952 -3.12535345595975 94 -51.0910613999878 -8.03023822650388 -2.46212354452028 95 10.6850783476395 14.2732474183979 -5.15136456874776 96 -25.5351793082778 -8.88356110618463 -1.05583066878426 97 -56.4856688259442 -0.5290378076123 -1.58351166931636 98 71.7372841256507 -6.28542003701019 -2.06235268516737 99 18.1733465755406 -2.74862458726538 0.954111381558329 100 9.66114424275959 -12.0957510762698 -1.94944007569165 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_transformed
includes 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 Model Tracking and Versioning for more details on model tracking and versioning.
Model Exporting#
To Memmodel
model.to_memmodel()
Note
MemModel
objects 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-learn
model.The preceding methods for exporting the model use
MemModel
, and it is recommended to useMemModel
directly.SQL
To get the SQL query use below:
model.to_sql() Out[9]: ['("fixed_acidity" - 7.21530706479914) * -0.00740794380368571 + ("volatile_acidity" - 0.339665999692166) * -0.00118432129646007 + ("citric_acid" - 0.318633215330152) * 0.000486867004519229 + ("residual_sugar" - 5.44323533938741) * 0.0410197294279797 + ("chlorides" - 0.0560338617823611) * -0.000168197431343702 + ("free_sulfur_dioxide" - 30.5253193781745) * 0.230481489722234 + ("total_sulfur_dioxide" - 115.744574418963) * 0.972166693841427 + ("density" - 0.994696633830999) * 1.7724964231297e-06 + ("pH" - 3.21850084654456) * -0.000655520779893176 + ("sulphates" - 0.531268277666615) * -0.000704339207760154 + ("alcohol" - 10.4918008311528) * -0.0054518247482017 + ("quality" - 5.81837771279052) * -0.000532705277259338 + ("good" - 0.196552254886871) * -0.000326386219750347', '("fixed_acidity" - 7.21530706479914) * -0.00537208540551764 + ("volatile_acidity" - 0.339665999692166) * -0.000787179265926509 + ("citric_acid" - 0.318633215330152) * -0.000247100931315118 + ("residual_sugar" - 5.44323533938741) * 0.0186262696200211 + ("chlorides" - 0.0560338617823611) * 6.67995503853851e-05 + ("free_sulfur_dioxide" - 30.5253193781745) * 0.972615829133942 + ("total_sulfur_dioxide" - 115.744574418963) * -0.231392918488152 + ("density" - 0.994696633830999) * 1.27197739295473e-06 + ("pH" - 3.21850084654456) * 0.00064802744147177 + ("sulphates" - 0.531268277666615) * 0.000346564126401858 + ("alcohol" - 10.4918008311528) * 0.00288222663864476 + ("quality" - 5.81837771279052) * 0.00915670157688827 + ("good" - 0.196552254886871) * 0.00256735969031283', '("fixed_acidity" - 7.21530706479914) * 0.0238635181935159 + ("volatile_acidity" - 0.339665999692166) * 0.000908752011459457 + ("citric_acid" - 0.318633215330152) * 0.0019208440336196 + ("residual_sugar" - 5.44323533938741) * 0.995191758737064 + ("chlorides" - 0.0560338617823611) * 0.000177543760783315 + ("free_sulfur_dioxide" - 30.5253193781745) * -0.0271071934686967 + ("total_sulfur_dioxide" - 115.744574418963) * -0.0358591349601494 + ("density" - 0.994696633830999) * 0.000460901781338666 + ("pH" - 3.21850084654456) * -0.00691133676962082 + ("sulphates" - 0.531268277666615) * -0.00193655721535319 + ("alcohol" - 10.4918008311528) * -0.0826607385648245 + ("quality" - 5.81837771279052) * -0.00888756383923461 + ("good" - 0.196552254886871) * -0.00592921565549895']
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