verticapy.machine_learning.vertica.decomposition.SVD#
- class verticapy.machine_learning.vertica.decomposition.SVD(name: str = None, overwrite_model: bool = False, n_components: int = 0, method: Literal['lapack'] = 'lapack')#
Creates an SVD (Singular Value Decomposition) object using the Vertica SVD 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).
- method: str, optional
The method used to calculate SVD.
- lapack:
Lapack definition.
Attributes#
Many attributes are created during the fitting phase.
- values_: numpy.array
Matrix of the right singular vectors.
- values_: numpy.array
Array of the singular values for each input feature.
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: 14We can drop the “color” column as it is varchar type.
data.drop("color")
Note
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
SVD
model:from verticapy.machine_learning.vertica import SVD
Then we can create the model:
model = SVD( 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.19669356683238 volatile_acidity 0.264348755504044 citric_acid 0.228932614877665 residual_sugar 5.38039944191312 chlorides 0.0427139489873566 free_sulfur_dioxide 2.10040482930188 total_sulfur_dioxide 4.73074234245691 density 0.309070644222815 pH 1.14052266015885 sulphates 0.304887828479003 alcohol 3.19983025734727 quality 2.33115583120493 good 0.352765059376177 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.9883586 , 0.00807026, 0.00248987])
Principal Components#
To get the transformed dataset in the form of principal components:
model.transform(data) Out[6]: None col1 col2 col3 1 0.0108141080605074 -0.00898690374891701 0.00727314637892158 2 0.0113609654056891 -0.001188361075854 0.00674904946769466 3 0.0166627178211831 0.0504421228529201 -0.013344560552529 4 0.0156076978382921 0.0224989559819784 -0.0151162620266151 5 0.012213209822063 -0.00158312495446874 0.00561623220768627 6 0.0107729444391737 0.00256515037026606 0.00663577398135461 7 0.0100155674790493 0.0270441151710081 0.00376563452202662 8 0.0163554466085546 0.0466574148608037 -0.0189083140020647 9 0.0162665231231895 -0.0352134384874341 -0.00229993058464557 10 0.00612561656911707 -0.00800801568019847 0.0162712298233318 11 0.00897314841200707 0.011950961619011 0.00652009642959555 12 0.0167155507504623 0.0247094490831147 -0.012430840269128 13 0.0103795306807778 0.00596388304507683 0.00901423507482371 14 0.0100017231953311 -0.00992966839614197 0.010317295285701 15 0.00105751138767118 0.00394349706810463 0.0253800950241802 16 0.0126503572208966 -0.0113636033145426 0.00657373809766758 17 0.00981065936878354 0.0148445654146031 0.00584151877390348 18 0.0105589692445995 -0.00637543518725687 0.00714797062666492 19 0.0105691701583817 -0.0113629436261072 0.00952468977202705 20 0.0128160451773933 0.013109827216752 -0.00253564961142822 21 0.0106152336426165 -0.00521753382020842 0.00742653982907914 22 0.0170823856441917 -0.0089456684893137 -0.00511840498754081 23 0.0154778450255575 -0.00844424924439236 -0.00527451937022658 24 0.0154583833096818 -0.00938853426778954 -0.00436702722677493 25 0.00112031244917541 0.00264822944127992 0.0247287583690205 26 0.01148166275695 0.0036172201927973 0.000741660518064614 27 0.0144487952276349 0.000242787333090651 0.00295673168007065 28 0.0167162120544499 0.0247153778951736 -0.0122822601942573 29 0.0167162120544499 0.0247153778951736 -0.0122822601942573 30 0.0136246739310925 -0.00520528702297353 -0.00207523067964397 31 0.0136246739310925 -0.00520528702297353 -0.00207523067964397 32 0.00427461786124396 0.00619558925697606 0.021193124762103 33 0.0147347506278432 0.0212604638292909 -0.00583120404868525 34 0.0109545127582932 -0.00551409595328058 0.00576446729975465 35 0.00934990184656775 -0.00490655658311333 0.0096981848845159 36 0.0114821012145568 0.00362128901795188 0.000838108278379084 37 0.00954488009164201 -0.0087742944602826 0.00667892232556901 38 0.0097823446972645 0.00540987186800628 0.00686511042374751 39 0.0153847215828542 0.00788842583923433 -0.00194983681799175 40 0.0148611056419884 0.0061308952609764 -0.00358340879790029 41 0.0173895891344854 0.0115009120151289 -0.0069213084272989 42 0.0173895891344854 0.0115009120151289 -0.0069213084272989 43 0.0121643687586682 0.00976266966246829 -0.000797056270560854 44 0.0152536586305299 0.0137425595116523 -0.00203872797701861 45 0.0168497956817113 0.00888850199443394 -0.00497628224283744 46 0.0159577446628235 -0.000308884162375855 -0.00662477199184711 47 0.00901925111535918 -0.014608096467652 0.0100922049391821 48 0.00901925111535918 -0.014608096467652 0.0100922049391821 49 0.00901925111535918 -0.014608096467652 0.0100922049391821 50 0.0121376232603184 0.00791387265659954 0.000825965463865728 51 0.0148159331848086 -0.0176893913881306 0.00119415364309956 52 0.0110690260632225 -0.00566710155619228 0.00568347667217994 53 0.0110690260632225 -0.00566710155619228 0.00568347667217994 54 0.00610760123535686 0.0112349312993762 0.0161564895686208 55 0.00797094362520398 0.00891093373305621 0.0124018649063269 56 0.00506201349163946 0.00707814959395135 0.0203449582810378 57 0.0125014629881725 0.00511287008052087 7.31990933828669e-05 58 0.0103808460535981 0.00597608952054056 0.00930357835576712 59 0.0156267641483374 -0.00737015988358075 0.000296320931996781 60 0.0105716460915439 0.0390215154904031 0.00145608459238926 61 0.00461567673665519 0.0049791934244293 0.018138757313459 62 0.00865600250002652 0.0190843469128548 0.00382400225369011 63 0.00944116324386852 0.00739348227175051 0.00626789774520582 64 0.00845119313041551 -0.0106681490480454 0.012723953808117 65 0.00840739769300279 -0.00766543030535406 0.00809400514385817 66 0.0141461253309525 0.00400108861898725 -0.00378600461398395 67 0.0144459310487507 0.00930219157574227 -0.00528452907136737 68 0.00926654633906355 -0.0121787695713679 0.00956171013538002 69 0.00802816459064073 0.0201851385051224 0.00907586192594589 70 0.011286840064499 0.0167666532148082 0.00238562301666936 71 0.00957808963795109 0.000653930534385808 0.00470240138816252 72 0.0103352354224899 -0.000322779278897781 0.00594078563297295 73 0.0103352354224899 -0.000322779278897781 0.00594078563297295 74 0.0144581674641318 0.00105048013198394 -0.000880308960696798 75 0.00704632805117089 -0.00125311656786212 0.0133444561329968 76 0.0143405642039511 -0.0114881981132763 -0.00142287511458727 77 0.00850613281699714 -0.00441482870613591 0.0127431247799744 78 0.00850613281699714 -0.00441482870613591 0.0127431247799744 79 0.00850613281699714 -0.00441482870613591 0.0127431247799744 80 0.0126897436110269 0.0106316956818193 0.00031262943042162 81 0.0117011565329233 0.00511316508798839 0.00305555709434668 82 0.0117011565329233 0.00511316508798839 0.00305555709434668 83 0.0117011565329233 0.00511316508798839 0.00305555709434668 84 0.0069155247298427 -0.00290726213742451 0.0148426427801898 85 0.00842520674025287 -0.00406786111660374 0.01442894666402 86 0.00982671259866448 -0.000974362410638909 0.00930281068428658 87 0.00448585606275903 0.00750788014098886 0.0184363410346446 88 0.00953612346062723 -0.00953706740255909 0.0121541620122911 89 0.00786669266643752 0.00980318166512076 0.00588189636259572 90 0.00814908792851708 -0.00753491705347241 0.0145490178143425 91 0.00571174655543743 -0.00138118892270593 0.0176180582249371 92 0.00571174655543743 -0.00138118892270593 0.0176180582249371 93 0.00759694685748644 0.00644547936960487 0.00873790743769185 94 0.00644068949568042 -0.00603581304579231 0.0118720332715975 95 0.012191422156916 0.0149750163321693 -0.00135461681419584 96 0.00880846552072471 -0.00757448402887002 0.00955177673886906 97 0.0059799495172463 0.00199945607844215 0.0159252209807065 98 0.0177817555288171 -0.00791052940334145 -0.00973689484084486 99 0.0128497955493046 -0.00265075743350479 -3.69216962120902e-05 100 0.012062677406264 -0.0118012043899065 0.0061936554761859 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 - SVD#
You can plot the first two dimensions 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, '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" * 0.0472721802865271 / 10781.471128853 + "volatile_acidity" * 0.00206840925765328 / 10781.471128853 + "citric_acid" * 0.00224024758641517 / 10781.471128853 + "residual_sugar" * 0.044395714039541 / 10781.471128853 + "chlorides" * 0.000346326837712132 / 10781.471128853 + "free_sulfur_dioxide" * 0.249206286239206 / 10781.471128853 + "total_sulfur_dioxide" * 0.962684441178257 / 10781.471128853 + "density" * 0.00670829104944216 / 10781.471128853 + "pH" * 0.0215818136774093 / 10781.471128853 + "sulphates" * 0.00345087841775058 / 10781.471128853 + "alcohol" * 0.0697381862438041 / 10781.471128853 + "quality" * 0.0391425145196623 / 10781.471128853 + "good" * 0.00126598337328779 / 10781.471128853', '"fixed_acidity" * 0.0396399788533397 / 974.236477296841 + "volatile_acidity" * 0.00188557954207304 / 974.236477296841 + "citric_acid" * 0.00119732121820979 / 974.236477296841 + "residual_sugar" * 0.0197505553836915 / 974.236477296841 + "chlorides" * 0.000483104744026943 / 974.236477296841 + "free_sulfur_dioxide" * 0.961682217712392 / 974.236477296841 + "total_sulfur_dioxide" * -0.258651563180513 / 974.236477296841 + "density" * 0.00544627404807701 / 974.236477296841 + "pH" * 0.0186923356923426 / 974.236477296841 + "sulphates" * 0.00373864932226108 / 974.236477296841 + "alcohol" * 0.0645579238733936 / 974.236477296841 + "quality" * 0.0415184345466081 / 974.236477296841 + "good" * 0.00393668701479983 / 974.236477296841', '"fixed_acidity" * 0.521916714261151 / 541.139278464763 + "volatile_acidity" * 0.02994886567606 / 541.139278464763 + "citric_acid" * 0.0173905391926748 / 541.139278464763 + "residual_sugar" * 0.0130884723973582 / 541.139278464763 + "chlorides" * 0.00460654396890658 / 541.139278464763 + "free_sulfur_dioxide" * -0.110072060878328 / 541.139278464763 + "total_sulfur_dioxide" * -0.0705721810950697 / 541.139278464763 + "density" * 0.0623122781813198 / 541.139278464763 + "pH" * 0.205991789180222 / 541.139278464763 + "sulphates" * 0.0388553814537634 / 541.139278464763 + "alcohol" * 0.719424850273745 / 541.139278464763 + "quality" * 0.378635713837342 / 541.139278464763 + "good" * 0.0187739554796354 / 541.139278464763']
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([[ 0.0107203 , -0.00876453, 0.0065801 ]])
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, 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