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 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: 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
SVDmodel: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_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 -----+-------------------- 1 | fixed_acidity 2 | volatile_acidity 3 | citric_acid 4 | residual_sugar 5 | chlorides 6 |free_sulfur_dioxide 7 |total_sulfur_dioxide 8 | density 9 | ph 10 | sulphates 11 | alcohol 12 | quality 13 | good =============== singular_values =============== index| value |explained_variance|accumulated_explained_variance -----+-----------+------------------+------------------------------ 1 |10781.47113| 0.98836 | 0.98836 2 | 974.23648 | 0.00807 | 0.99643 3 | 541.13928 | 0.00249 | 0.99892 ====================== right_singular_vectors ====================== index|vector1 |vector2 |vector3 -----+--------+--------+-------- 1 | 0.04727| 0.03964| 0.52192 2 | 0.00207| 0.00189| 0.02995 3 | 0.00224| 0.00120| 0.01739 4 | 0.04440| 0.01975| 0.01309 5 | 0.00035| 0.00048| 0.00461 6 | 0.24921| 0.96168|-0.11007 7 | 0.96268|-0.25865|-0.07057 8 | 0.00671| 0.00545| 0.06231 9 | 0.02158| 0.01869| 0.20599 10 | 0.00345| 0.00374| 0.03886 11 | 0.06974| 0.06456| 0.71942 12 | 0.03914| 0.04152| 0.37864 13 | 0.00127| 0.00394| 0.01877 ======== counters ======== counter_name |counter_value ------------------+------------- accepted_row_count| 6497 rejected_row_count| 0 iteration_count | 1 =========== call_string =========== SELECT SVD('"public"."_verticapy_tmp_svd_v_mldb_9649885e979711efa8720242ac120002_"', '"public"."_verticapy_tmp_view_v_mldb_967a90e8979711efa8720242ac120002_"', '"fixed_acidity", "volatile_acidity", "citric_acid", "residual_sugar", "chlorides", "free_sulfur_dioxide", "total_sulfur_dioxide", "density", "pH", "sulphates", "alcohol", "quality", "good"' USING PARAMETERS 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.19669356683238 volatile_acidity 0.264348755504044 citric_acid 0.228932614877664 residual_sugar 5.38039944191312 chlorides 0.0427139489873566 free_sulfur_dioxide 2.10040482930185 total_sulfur_dioxide 4.73074234245674 density 0.309070644222815 pH 1.14052266015885 sulphates 0.304887828479003 alcohol 3.19983025734727 quality 2.33115583120493 good 0.352765059376178 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.0166627178211831 0.0504421228529202 -0.013344560552529 2 0.0156076978382921 0.0224989559819784 -0.0151162620266151 3 0.0163554466085546 0.0466574148608037 -0.0189083140020647 4 0.0162665231231895 -0.0352134384874341 -0.00229993058464557 5 0.00981065936878354 0.0148445654146031 0.00584151877390349 6 0.0105589692445995 -0.00637543518725687 0.00714797062666492 7 0.0105691701583817 -0.0113629436261072 0.00952468977202701 8 0.0128160451773933 0.013109827216752 -0.0025356496114282 9 0.0170823856441917 -0.00894566848931371 -0.00511840498754079 10 0.01148166275695 0.00361722019279731 0.000741660518064574 11 0.0144487952276349 0.00024278733309064 0.00295673168007066 12 0.0167162120544499 0.0247153778951737 -0.0122822601942572 13 0.0167162120544499 0.0247153778951737 -0.0122822601942572 14 0.0148611056419884 0.0061308952609764 -0.00358340879790028 15 0.0168497956817113 0.00888850199443394 -0.00497628224283744 16 0.0148159331848086 -0.0176893913881306 0.00119415364309952 17 0.0103808460535981 0.00597608952054056 0.00930357835576713 18 0.00461567673665519 0.0049791934244293 0.018138757313459 19 0.00704632805117089 -0.00125311656786212 0.0133444561329969 20 0.0143405642039511 -0.0114881981132763 -0.00142287511458726 21 0.012689743611027 0.0106316956818193 0.000312629430421607 22 0.00982671259866448 -0.000974362410638911 0.00930281068428659 23 0.00786669266643752 0.00980318166512077 0.00588189636259572 24 0.00759694685748644 0.00644547936960487 0.00873790743769186 25 0.00746567931633152 0.000580293432369368 0.0110374354934679 26 0.00913821037224146 -0.00619404051276112 0.0133172050281699 27 0.00713915669203781 -0.00147626901033191 0.0139435588532038 28 0.00766585574596053 -0.00733264255115155 0.013044853454326 29 0.00982186154519613 -0.00610044600219932 0.00953957549822287 30 0.0117630356588413 0.0123113194977316 0.00394473005468317 31 0.0117630356588413 0.0123113194977316 0.00394473005468317 32 0.0062625552222831 -0.0055006986357797 0.0122314271845807 33 0.0109806291910484 -0.00120336535627242 0.00501126433948926 34 0.0069851910897219 -0.00755805549704856 0.0124509445455597 35 0.00556912516109348 -0.0115356581477962 0.0196220352844239 36 0.0100644386992172 0.00131547361500665 0.00539601949020896 37 0.0107975829765916 0.00765119025354379 0.00194143463687917 38 0.00853354432493088 0.00412057394934463 0.0117034037043015 39 0.0126558160360363 0.0423486272590535 -0.00312577080242463 40 0.0103856303007436 0.00177412645780564 0.00945355385725723 41 0.00903149479937041 0.00150626041835165 0.0124657052635593 42 0.0140475821485117 0.00840551221277986 -0.004608497262778 43 0.0104195563585142 -0.000537882454022647 0.00830982305095836 44 0.00558814828594127 0.00956099194815167 0.016322500746599 45 0.00461743056708228 0.00499546872504762 0.0185245483547168 46 0.00875096711402058 -0.0103713468437911 0.0142634641711807 47 0.00724430200053943 -0.000819287310697858 0.0125466006016753 48 0.0114965524755585 0.000558345635808813 0.0079760342384512 49 0.00949751509796975 -0.0183035784661458 0.00752797466264932 50 0.00857471859687872 0.00615122546302568 0.0140852505457725 51 0.00920474133494145 -0.0067745211493323 0.00636506242060123 52 0.00715987191061613 0.00290318034738615 0.0158229788910861 53 0.00715987191061613 0.00290318034738615 0.0158229788910861 54 0.00992788293510493 -0.0046321495407913 0.00534278816556469 55 0.00650027761342404 0.0002518023306758 0.0125288979875086 56 0.0145389772109947 -0.0152637651083441 -0.00148627501289218 57 0.0116603488567791 -0.0161204938061663 0.00489916163863051 58 0.00698213326744077 -0.00415537009300246 0.0147639464775544 59 0.0159238868675167 -0.00132116295361841 -0.00530550641046172 60 0.0103879947895724 -0.0150512197248635 0.0106826508532783 61 0.0101595023710441 0.013687851330626 0.00377990863531279 62 0.00892564310757659 -0.000186425942651864 0.00870090957104276 63 0.00921648672303232 0.000896089735417004 0.00847604569533058 64 0.00921648672303232 0.000896089735417004 0.00847604569533058 65 0.00863245300180869 0.000504677407110386 0.00965340172716782 66 0.0162272743393317 -0.0219887449948917 6.75072234815565e-05 67 0.0095971753666285 0.00930625490267071 0.00924409662983734 68 0.00140815966249489 0.00279527766643168 0.0229879402130233 69 0.00140815966249489 0.00279527766643168 0.0229879402130233 70 0.00575938015638363 0.00133669205867381 0.012700407074592 71 0.00141831849431529 0.00364874034592818 0.0203361836071961 72 0.00164558017749426 0.00435048306567297 0.0242630196619369 73 0.00946569736637315 -0.00423153360402477 0.00715471670080586 74 0.0149420958951146 0.0175500738589989 -0.00523461898273193 75 0.0149420958951146 0.0175500738589989 -0.00523461898273193 76 0.0108808120470814 -0.00102285428969076 0.00391604302246656 77 0.00823529917153261 -0.0154500518288816 0.012027438850318 78 0.0183926133854933 0.0103315279006879 -0.012098374653085 79 0.0183926133854933 0.0103315279006879 -0.012098374653085 80 0.0117941234099257 0.0015656348045772 0.00437321643388602 81 0.0179988502107907 0.0506091538108886 -0.0165849155545074 82 0.0108194451524607 0.00873811921846562 0.00577501476653969 83 0.00947203277315955 0.000114681525119134 0.010531982769587 84 0.00541352591341548 -0.00679212978833105 0.0159364926788033 85 0.0102155978186162 0.00321107285522031 0.00879663376326423 86 0.0146132428240696 0.00704590454733349 -0.00251472784943461 87 0.0146132428240696 0.00704590454733349 -0.00251472784943461 88 0.0108095748535132 0.0035495045454257 0.0041954863279059 89 0.0108095748535132 0.0035495045454257 0.0041954863279059 90 0.00905701501282419 0.0150484470352413 0.00828405316257603 91 0.00833807787024743 0.000348390495919757 0.0118211843078814 92 0.00909086365511396 0.00435154691663435 0.00969683017045655 93 0.0106381154224292 -0.0117365728020144 0.00951598261532732 94 0.0106381154224292 -0.0117365728020144 0.00951598261532732 95 0.0162584836396806 -0.0167607105529164 -0.00032876694519059 96 0.0159197115977615 -0.0131421418650892 -0.00623628882664583 97 0.0117469104552609 0.00376899278297677 0.00349557208238807 98 0.0170775916463106 -0.0232983402539546 -0.00167013652288743 99 0.0170775916463106 -0.0232983402539546 -0.00167013652288743 100 0.00935079373998315 -0.00413825670835599 0.0056855667486921 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 - 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 /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" * 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.0396399788533407 / 974.23647729684 + "volatile_acidity" * 0.00188557954207477 / 974.23647729684 + "citric_acid" * 0.00119732121820963 / 974.23647729684 + "residual_sugar" * 0.0197505553836925 / 974.23647729684 + "chlorides" * 0.000483104744026939 / 974.23647729684 + "free_sulfur_dioxide" * 0.961682217712392 / 974.23647729684 + "total_sulfur_dioxide" * -0.258651563180512 / 974.23647729684 + "density" * 0.00544627404807704 / 974.23647729684 + "pH" * 0.018692335692342 / 974.23647729684 + "sulphates" * 0.003738649322261 / 974.23647729684 + "alcohol" * 0.0645579238733929 / 974.23647729684 + "quality" * 0.0415184345466082 / 974.23647729684 + "good" * 0.00393668701479983 / 974.23647729684', '"fixed_acidity" * 0.521916714261153 / 541.139278464763 + "volatile_acidity" * 0.0299488656760591 / 541.139278464763 + "citric_acid" * 0.0173905391926743 / 541.139278464763 + "residual_sugar" * 0.0130884723973556 / 541.139278464763 + "chlorides" * 0.00460654396890657 / 541.139278464763 + "free_sulfur_dioxide" * -0.110072060878328 / 541.139278464763 + "total_sulfur_dioxide" * -0.0705721810950697 / 541.139278464763 + "density" * 0.0623122781813199 / 541.139278464763 + "pH" * 0.205991789180222 / 541.139278464763 + "sulphates" * 0.0388553814537632 / 541.139278464763 + "alcohol" * 0.719424850273745 / 541.139278464763 + "quality" * 0.378635713837341 / 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