verticapy.machine_learning.vertica.preprocessing.OneHotEncoder¶
- class verticapy.machine_learning.vertica.preprocessing.OneHotEncoder(name: str = None, overwrite_model: bool = False, extra_levels: dict | None = None, drop_first: bool = True, ignore_null: bool = True, separator: str = '_', column_naming: Literal['indices', 'values', 'values_relaxed'] = 'indices', null_column_name: str = 'null')¶
Creates a Vertica OneHotEncoder object.
Parameters¶
- name: str, optional
Name of the model.
- overwrite_model: bool, optional
If set to
True, training a model with the same name as an existing model overwrites the existing model.- extra_levels: dict, optional
Additional levels in each category that are not in the input relation.
- drop_first: bool, optional
If set to True, treats the first level of the categorical variable as the reference level. Otherwise, every level of the categorical variable has a corresponding column in the output view.
- ignore_null: bool, optional
If set to True, Null values set all corresponding one-hot binary columns to null. Otherwise, null values in the input columns are treated as a categorical level.
- separator: str, optional
The character that separates the input variable name and the indicator variable level in the output table. To avoid using any separator, set this parameter to null value.
- column_naming: str, optional
Appends categorical levels to column names according to the specified method:
- indices:
Uses integer indices to represent categorical levels.
- values :
Uses categorical level names. If duplicate column names occur, the function attempts to disambiguate them by appending _n, where n is a zero-based integer index (_0, _1, …, _n).
- null_column_name: str, optional
The string used in naming the indicator column for null values, used only if ignore_null is set to false and column_naming is set to ‘values’.
Attributes¶
Many attributes are created during the fitting phase.
- categories_: numpy.array
ArrayLike of the categories of the different features.
- column_naming_: str
Method used to name the model’s outputs.
- drop_first_: bool
If False, the first dummy of each category was dropped.
Note
All attributes can be accessed using the
get_attributes()method.Note
Several other attributes can be accessed by using the
get_vertica_attributes()method.Examples¶
The following examples provide a basic understanding of usage. For more detailed examples, please refer to the Machine Learning or the Examples section on the website.
Load data for machine learning¶
We import
verticapy:import verticapy as vp
Hint
By assigning an alias to
verticapy, we mitigate the risk of code collisions with other libraries. This precaution is necessary because verticapy uses commonly known function names like “average” and “median”, which can potentially lead to naming conflicts. The use of an alias ensures that the functions fromverticapyare used as intended without interfering with functions from other libraries.For this example, we will use the Titanic dataset.
import verticapy.datasets as vpd data = vpd.load_titanic()
123pclass123survivedAbcAbcsex123age123sibsp123parchAbcticket123fareAbccabinAbcembarkedAbcboat123bodyAbc1 1 0 male 71.0 0 0 PC 17609 49.5042 [null] C [null] 22 2 1 0 male 45.0 0 0 113784 35.5 T S [null] [null] 3 1 0 male [null] 0 0 113798 31.0 [null] S [null] [null] 4 1 0 male 17.0 0 0 113059 47.1 [null] S [null] [null] 5 1 0 male 27.0 1 0 13508 136.7792 C89 C [null] [null] 6 1 0 male 37.0 1 1 PC 17756 83.1583 E52 C [null] [null] 7 1 0 male 31.0 1 0 F.C. 12750 52.0 B71 S [null] [null] 8 1 0 male 50.0 1 0 PC 17761 106.425 C86 C [null] 62 9 1 0 female 36.0 0 0 PC 17531 31.6792 A29 C [null] [null] 10 1 0 male 37.0 1 0 113803 53.1 C123 S [null] [null] 11 1 0 male 24.0 0 0 PC 17593 79.2 B86 C [null] [null] 12 1 0 male 45.0 1 0 36973 83.475 C83 S [null] [null] 13 1 0 male 40.0 0 0 112059 0.0 B94 S [null] 110 14 1 0 male 42.0 0 0 113038 42.5 B11 S [null] [null] 15 1 0 male [null] 0 0 17463 51.8625 E46 S [null] [null] 16 1 0 male 42.0 1 0 113789 52.0 [null] S [null] 38 17 1 0 male [null] 0 0 PC 17600 30.6958 [null] C 14 [null] 18 1 0 male 29.0 0 0 113501 30.0 D6 S [null] 126 19 1 0 male 46.0 0 0 13050 75.2417 C6 C [null] 292 20 1 0 male 54.0 0 0 17463 51.8625 E46 S [null] 175 21 1 0 male 47.0 0 0 113796 42.4 [null] S [null] [null] 22 1 0 male 58.0 0 2 35273 113.275 D48 C [null] 122 23 1 0 male 45.5 0 0 113043 28.5 C124 S [null] 166 24 1 0 male 29.0 1 0 113776 66.6 C2 S [null] [null] 25 1 0 male 47.0 0 0 110465 52.0 C110 S [null] 207 26 1 0 male 38.0 0 0 19972 0.0 [null] S [null] [null] 27 1 0 male 22.0 0 0 PC 17760 135.6333 [null] C [null] 232 28 1 0 male 31.0 0 0 PC 17590 50.4958 A24 S [null] [null] 29 1 0 male 50.0 1 0 13507 55.9 E44 S [null] [null] 30 1 0 male 56.0 0 0 17764 30.6958 A7 C [null] [null] 31 1 0 male 57.0 1 0 PC 17569 146.5208 B78 C [null] [null] 32 1 0 female 63.0 1 0 PC 17483 221.7792 C55 C57 S [null] [null] 33 1 0 male 61.0 0 0 36963 32.3208 D50 S [null] 46 34 1 0 male 21.0 0 1 35281 77.2875 D26 S [null] 169 35 1 0 male 51.0 0 1 PC 17597 61.3792 [null] C [null] [null] 36 1 1 female 63.0 1 0 13502 77.9583 D7 S 10 [null] 37 1 1 female 32.0 0 0 11813 76.2917 D15 C 8 [null] 38 1 1 female 58.0 0 0 113783 26.55 C103 S 8 [null] 39 1 1 female 44.0 0 0 PC 17610 27.7208 B4 C 6 [null] 40 1 1 female 41.0 0 0 16966 134.5 E40 C 3 [null] 41 1 1 female 53.0 0 0 PC 17606 27.4458 [null] C 6 [null] 42 1 1 male 36.0 0 1 PC 17755 512.3292 B51 B53 B55 C 3 [null] 43 1 1 female 58.0 0 1 PC 17755 512.3292 B51 B53 B55 C 3 [null] 44 1 1 male 11.0 1 2 113760 120.0 B96 B98 S 4 [null] 45 1 1 female 76.0 1 0 19877 78.85 C46 S 6 [null] 46 1 1 female [null] 0 1 113505 55.0 E33 S 6 [null] 47 1 1 female 39.0 1 1 PC 17756 83.1583 E49 C 14 [null] 48 1 1 female 27.0 1 2 F.C. 12750 52.0 B71 S 3 [null] 49 1 1 female [null] 0 0 17421 110.8833 [null] C 4 [null] 50 1 1 female 35.0 0 0 113503 211.5 C130 C 4 [null] 51 1 1 female 22.0 0 1 112378 59.4 [null] C 7 [null] 52 1 1 female 25.0 1 0 11765 55.4417 E50 C 5 [null] 53 1 1 male 48.0 1 0 PC 17572 76.7292 D33 C 3 [null] 54 1 1 female 35.0 1 0 36973 83.475 C83 S D [null] 55 1 1 male 27.0 0 0 PC 17572 76.7292 D49 C 3 [null] 56 1 1 female 24.0 0 0 11767 83.1583 C54 C 7 [null] 57 1 1 female 52.0 1 1 12749 93.5 B69 S 3 [null] 58 1 1 female 44.0 0 1 111361 57.9792 B18 C 4 [null] 59 1 1 female 15.0 0 1 24160 211.3375 B5 S 2 [null] 60 1 1 male 30.0 1 0 13236 57.75 C78 C 11 [null] 61 1 1 female 31.0 1 0 35273 113.275 D36 C 6 [null] 62 1 1 female 39.0 0 0 PC 17758 108.9 C105 C 8 [null] 63 1 1 female 22.0 0 1 113509 61.9792 B36 C 5 [null] 64 1 1 male 52.0 0 0 113786 30.5 C104 S 6 [null] 65 1 1 female 43.0 0 1 24160 211.3375 B3 S 2 [null] 66 1 1 female 33.0 0 0 110152 86.5 B77 S 8 [null] 67 1 1 male 45.0 1 1 16966 134.5 E34 C 3 [null] 68 1 1 female 40.0 1 1 16966 134.5 E34 C 3 [null] 69 1 1 male 48.0 1 0 19996 52.0 C126 S 5 7 [null] 70 1 1 female [null] 0 0 PC 17585 79.2 [null] C D [null] 71 1 1 female 35.0 0 0 PC 17755 512.3292 [null] C 3 [null] 72 1 1 female 60.0 1 0 110813 75.25 D37 C 5 [null] 73 1 1 male 21.0 0 1 PC 17597 61.3792 [null] C A [null] 74 2 0 male 23.0 0 0 C.A. 31030 10.5 [null] S [null] [null] 75 2 0 male 28.0 0 0 244358 26.0 [null] S [null] [null] 76 2 0 male 60.0 1 1 29750 39.0 [null] S [null] [null] 77 2 0 female 44.0 1 0 244252 26.0 [null] S [null] [null] 78 2 0 male 29.0 1 0 2003 26.0 [null] S [null] [null] 79 2 0 male 18.0 0 0 S.O.C. 14879 73.5 [null] S [null] [null] 80 2 0 male 18.0 0 0 S.O.C. 14879 73.5 [null] S [null] [null] 81 2 0 male 54.0 0 0 28403 26.0 [null] S [null] [null] 82 2 0 male 18.0 0 0 236171 13.0 [null] S [null] [null] 83 2 0 male 36.0 0 0 229236 13.0 [null] S [null] 236 84 2 0 male 34.0 1 0 28664 21.0 [null] S [null] [null] 85 2 0 male 21.0 1 0 28133 11.5 [null] S [null] [null] 86 2 0 male 21.0 1 0 28134 11.5 [null] S [null] [null] 87 2 0 male 24.0 0 0 233866 13.0 [null] S [null] 155 88 2 0 male 34.0 0 0 12233 13.0 [null] S [null] [null] 89 2 0 male 30.0 0 0 250653 13.0 [null] S [null] 75 90 2 0 male 44.0 0 0 248746 13.0 [null] S [null] 35 91 2 0 male 49.0 1 2 220845 65.0 [null] S [null] [null] 92 2 0 male 21.0 2 0 S.O.C. 14879 73.5 [null] S [null] [null] 93 2 0 male 21.0 0 0 S.O.C. 14879 73.5 [null] S [null] [null] 94 2 0 female 60.0 1 0 24065 26.0 [null] S [null] [null] 95 2 0 male 24.0 2 0 C.A. 31029 31.5 [null] S [null] [null] 96 2 0 male 22.0 2 0 C.A. 31029 31.5 [null] S [null] [null] 97 2 0 male 35.0 0 0 233734 12.35 [null] Q [null] [null] 98 2 0 male 31.0 0 0 C.A. 18723 10.5 [null] S [null] 165 99 2 0 male 36.0 0 0 SC/Paris 2163 12.875 D C [null] [null] 100 2 0 male [null] 0 0 SC/A.3 2861 15.5792 [null] C [null] [null] Rows: 1-100 | Columns: 14Note
VerticaPy offers a wide range of sample datasets that are ideal for training and testing purposes. You can explore the full list of available datasets in the Datasets, which provides detailed information on each dataset and how to use them effectively. These datasets are invaluable resources for honing your data analysis and machine learning skills within the VerticaPy environment.
Model Initialization¶
First we import the
OneHotEncodermodel:from verticapy.machine_learning.vertica import OneHotEncoder
Then we can create the model:
model = OneHotEncoder( drop_first = False, column_naming = "values", )
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, ["sex", "parch"]) =========== call_string =========== SELECT one_hot_encoder_fit('"public"."_verticapy_tmp_onehotencoder_v_mldb_720603c4979b11efa8720242ac120002_"','"public"."_verticapy_tmp_view_v_mldb_7245a2d6979b11efa8720242ac120002_"','"sex", "parch"' USING PARAMETERS exclude_columns='', output_view='', extra_levels='{}'); ================== integer_categories ================== category_name|category_level|category_level_index -------------+--------------+-------------------- parch | 0 | 0 parch | 1 | 1 parch | 2 | 2 parch | 3 | 3 parch | 4 | 4 parch | 5 | 5 parch | 6 | 6 parch | 9 | 7 ================== varchar_categories ================== category_name|category_level|category_level_index -------------+--------------+-------------------- sex | female | 0 sex | male | 1
Important
To train a model, you can directly use the
vDataFrameor the name of the relation stored in the database.Classes¶
To have a look at the identified classes/categories you can use:
model.categories_ Out[5]: [['female', 'male'], ['0', '1', '2', '3', '4', '5', '6', '9']]
Conversion/Transformation¶
To get the transformed dataset in the form that is encoded, we can use the
transformfunction. Let us transform the data and display the first datapoints.model.transform(data)
123pclass123survivedAbcAbcsex123sex_female123sex_male123age123sibsp123parch123parch_0123parch_1123parch_2123parch_3123parch_4123parch_5123parch_6123parch_9Abcticket123fareAbccabinAbcembarkedAbcboat123bodyAbc1 1 0 male 0 1 71.0 0 0 1 0 0 0 0 0 0 0 PC 17609 49.5042 [null] C [null] 22 2 1 0 male 0 1 45.0 0 0 1 0 0 0 0 0 0 0 113784 35.5 T S [null] [null] 3 1 0 male 0 1 [null] 0 0 1 0 0 0 0 0 0 0 113798 31.0 [null] S [null] [null] 4 1 0 male 0 1 17.0 0 0 1 0 0 0 0 0 0 0 113059 47.1 [null] S [null] [null] 5 1 0 male 0 1 27.0 1 0 1 0 0 0 0 0 0 0 13508 136.7792 C89 C [null] [null] 6 1 0 male 0 1 37.0 1 1 0 1 0 0 0 0 0 0 PC 17756 83.1583 E52 C [null] [null] 7 1 0 male 0 1 31.0 1 0 1 0 0 0 0 0 0 0 F.C. 12750 52.0 B71 S [null] [null] 8 1 0 male 0 1 50.0 1 0 1 0 0 0 0 0 0 0 PC 17761 106.425 C86 C [null] 62 9 1 0 female 1 0 36.0 0 0 1 0 0 0 0 0 0 0 PC 17531 31.6792 A29 C [null] [null] 10 1 0 male 0 1 37.0 1 0 1 0 0 0 0 0 0 0 113803 53.1 C123 S [null] [null] 11 1 0 male 0 1 24.0 0 0 1 0 0 0 0 0 0 0 PC 17593 79.2 B86 C [null] [null] 12 1 0 male 0 1 45.0 1 0 1 0 0 0 0 0 0 0 36973 83.475 C83 S [null] [null] 13 1 0 male 0 1 40.0 0 0 1 0 0 0 0 0 0 0 112059 0.0 B94 S [null] 110 14 1 0 male 0 1 42.0 0 0 1 0 0 0 0 0 0 0 113038 42.5 B11 S [null] [null] 15 1 0 male 0 1 [null] 0 0 1 0 0 0 0 0 0 0 17463 51.8625 E46 S [null] [null] 16 1 0 male 0 1 42.0 1 0 1 0 0 0 0 0 0 0 113789 52.0 [null] S [null] 38 17 1 0 male 0 1 [null] 0 0 1 0 0 0 0 0 0 0 PC 17600 30.6958 [null] C 14 [null] 18 1 0 male 0 1 29.0 0 0 1 0 0 0 0 0 0 0 113501 30.0 D6 S [null] 126 19 1 0 male 0 1 46.0 0 0 1 0 0 0 0 0 0 0 13050 75.2417 C6 C [null] 292 20 1 0 male 0 1 54.0 0 0 1 0 0 0 0 0 0 0 17463 51.8625 E46 S [null] 175 21 1 0 male 0 1 47.0 0 0 1 0 0 0 0 0 0 0 113796 42.4 [null] S [null] [null] 22 1 0 male 0 1 58.0 0 2 0 0 1 0 0 0 0 0 35273 113.275 D48 C [null] 122 23 1 0 male 0 1 45.5 0 0 1 0 0 0 0 0 0 0 113043 28.5 C124 S [null] 166 24 1 0 male 0 1 29.0 1 0 1 0 0 0 0 0 0 0 113776 66.6 C2 S [null] [null] 25 1 0 male 0 1 47.0 0 0 1 0 0 0 0 0 0 0 110465 52.0 C110 S [null] 207 26 1 0 male 0 1 38.0 0 0 1 0 0 0 0 0 0 0 19972 0.0 [null] S [null] [null] 27 1 0 male 0 1 22.0 0 0 1 0 0 0 0 0 0 0 PC 17760 135.6333 [null] C [null] 232 28 1 0 male 0 1 31.0 0 0 1 0 0 0 0 0 0 0 PC 17590 50.4958 A24 S [null] [null] 29 1 0 male 0 1 50.0 1 0 1 0 0 0 0 0 0 0 13507 55.9 E44 S [null] [null] 30 1 0 male 0 1 56.0 0 0 1 0 0 0 0 0 0 0 17764 30.6958 A7 C [null] [null] 31 1 0 male 0 1 57.0 1 0 1 0 0 0 0 0 0 0 PC 17569 146.5208 B78 C [null] [null] 32 1 0 female 1 0 63.0 1 0 1 0 0 0 0 0 0 0 PC 17483 221.7792 C55 C57 S [null] [null] 33 1 0 male 0 1 61.0 0 0 1 0 0 0 0 0 0 0 36963 32.3208 D50 S [null] 46 34 1 0 male 0 1 21.0 0 1 0 1 0 0 0 0 0 0 35281 77.2875 D26 S [null] 169 35 1 0 male 0 1 51.0 0 1 0 1 0 0 0 0 0 0 PC 17597 61.3792 [null] C [null] [null] 36 1 1 female 1 0 63.0 1 0 1 0 0 0 0 0 0 0 13502 77.9583 D7 S 10 [null] 37 1 1 female 1 0 32.0 0 0 1 0 0 0 0 0 0 0 11813 76.2917 D15 C 8 [null] 38 1 1 female 1 0 58.0 0 0 1 0 0 0 0 0 0 0 113783 26.55 C103 S 8 [null] 39 1 1 female 1 0 44.0 0 0 1 0 0 0 0 0 0 0 PC 17610 27.7208 B4 C 6 [null] 40 1 1 female 1 0 41.0 0 0 1 0 0 0 0 0 0 0 16966 134.5 E40 C 3 [null] 41 1 1 female 1 0 53.0 0 0 1 0 0 0 0 0 0 0 PC 17606 27.4458 [null] C 6 [null] 42 1 1 male 0 1 36.0 0 1 0 1 0 0 0 0 0 0 PC 17755 512.3292 B51 B53 B55 C 3 [null] 43 1 1 female 1 0 58.0 0 1 0 1 0 0 0 0 0 0 PC 17755 512.3292 B51 B53 B55 C 3 [null] 44 1 1 male 0 1 11.0 1 2 0 0 1 0 0 0 0 0 113760 120.0 B96 B98 S 4 [null] 45 1 1 female 1 0 76.0 1 0 1 0 0 0 0 0 0 0 19877 78.85 C46 S 6 [null] 46 1 1 female 1 0 [null] 0 1 0 1 0 0 0 0 0 0 113505 55.0 E33 S 6 [null] 47 1 1 female 1 0 39.0 1 1 0 1 0 0 0 0 0 0 PC 17756 83.1583 E49 C 14 [null] 48 1 1 female 1 0 27.0 1 2 0 0 1 0 0 0 0 0 F.C. 12750 52.0 B71 S 3 [null] 49 1 1 female 1 0 [null] 0 0 1 0 0 0 0 0 0 0 17421 110.8833 [null] C 4 [null] 50 1 1 female 1 0 35.0 0 0 1 0 0 0 0 0 0 0 113503 211.5 C130 C 4 [null] 51 1 1 female 1 0 22.0 0 1 0 1 0 0 0 0 0 0 112378 59.4 [null] C 7 [null] 52 1 1 female 1 0 25.0 1 0 1 0 0 0 0 0 0 0 11765 55.4417 E50 C 5 [null] 53 1 1 male 0 1 48.0 1 0 1 0 0 0 0 0 0 0 PC 17572 76.7292 D33 C 3 [null] 54 1 1 female 1 0 35.0 1 0 1 0 0 0 0 0 0 0 36973 83.475 C83 S D [null] 55 1 1 male 0 1 27.0 0 0 1 0 0 0 0 0 0 0 PC 17572 76.7292 D49 C 3 [null] 56 1 1 female 1 0 24.0 0 0 1 0 0 0 0 0 0 0 11767 83.1583 C54 C 7 [null] 57 1 1 female 1 0 52.0 1 1 0 1 0 0 0 0 0 0 12749 93.5 B69 S 3 [null] 58 1 1 female 1 0 44.0 0 1 0 1 0 0 0 0 0 0 111361 57.9792 B18 C 4 [null] 59 1 1 female 1 0 15.0 0 1 0 1 0 0 0 0 0 0 24160 211.3375 B5 S 2 [null] 60 1 1 male 0 1 30.0 1 0 1 0 0 0 0 0 0 0 13236 57.75 C78 C 11 [null] 61 1 1 female 1 0 31.0 1 0 1 0 0 0 0 0 0 0 35273 113.275 D36 C 6 [null] 62 1 1 female 1 0 39.0 0 0 1 0 0 0 0 0 0 0 PC 17758 108.9 C105 C 8 [null] 63 1 1 female 1 0 22.0 0 1 0 1 0 0 0 0 0 0 113509 61.9792 B36 C 5 [null] 64 1 1 male 0 1 52.0 0 0 1 0 0 0 0 0 0 0 113786 30.5 C104 S 6 [null] 65 1 1 female 1 0 43.0 0 1 0 1 0 0 0 0 0 0 24160 211.3375 B3 S 2 [null] 66 1 1 female 1 0 33.0 0 0 1 0 0 0 0 0 0 0 110152 86.5 B77 S 8 [null] 67 1 1 male 0 1 45.0 1 1 0 1 0 0 0 0 0 0 16966 134.5 E34 C 3 [null] 68 1 1 female 1 0 40.0 1 1 0 1 0 0 0 0 0 0 16966 134.5 E34 C 3 [null] 69 1 1 male 0 1 48.0 1 0 1 0 0 0 0 0 0 0 19996 52.0 C126 S 5 7 [null] 70 1 1 female 1 0 [null] 0 0 1 0 0 0 0 0 0 0 PC 17585 79.2 [null] C D [null] 71 1 1 female 1 0 35.0 0 0 1 0 0 0 0 0 0 0 PC 17755 512.3292 [null] C 3 [null] 72 1 1 female 1 0 60.0 1 0 1 0 0 0 0 0 0 0 110813 75.25 D37 C 5 [null] 73 1 1 male 0 1 21.0 0 1 0 1 0 0 0 0 0 0 PC 17597 61.3792 [null] C A [null] 74 2 0 male 0 1 23.0 0 0 1 0 0 0 0 0 0 0 C.A. 31030 10.5 [null] S [null] [null] 75 2 0 male 0 1 28.0 0 0 1 0 0 0 0 0 0 0 244358 26.0 [null] S [null] [null] 76 2 0 male 0 1 60.0 1 1 0 1 0 0 0 0 0 0 29750 39.0 [null] S [null] [null] 77 2 0 female 1 0 44.0 1 0 1 0 0 0 0 0 0 0 244252 26.0 [null] S [null] [null] 78 2 0 male 0 1 29.0 1 0 1 0 0 0 0 0 0 0 2003 26.0 [null] S [null] [null] 79 2 0 male 0 1 18.0 0 0 1 0 0 0 0 0 0 0 S.O.C. 14879 73.5 [null] S [null] [null] 80 2 0 male 0 1 18.0 0 0 1 0 0 0 0 0 0 0 S.O.C. 14879 73.5 [null] S [null] [null] 81 2 0 male 0 1 54.0 0 0 1 0 0 0 0 0 0 0 28403 26.0 [null] S [null] [null] 82 2 0 male 0 1 18.0 0 0 1 0 0 0 0 0 0 0 236171 13.0 [null] S [null] [null] 83 2 0 male 0 1 36.0 0 0 1 0 0 0 0 0 0 0 229236 13.0 [null] S [null] 236 84 2 0 male 0 1 34.0 1 0 1 0 0 0 0 0 0 0 28664 21.0 [null] S [null] [null] 85 2 0 male 0 1 21.0 1 0 1 0 0 0 0 0 0 0 28133 11.5 [null] S [null] [null] 86 2 0 male 0 1 21.0 1 0 1 0 0 0 0 0 0 0 28134 11.5 [null] S [null] [null] 87 2 0 male 0 1 24.0 0 0 1 0 0 0 0 0 0 0 233866 13.0 [null] S [null] 155 88 2 0 male 0 1 34.0 0 0 1 0 0 0 0 0 0 0 12233 13.0 [null] S [null] [null] 89 2 0 male 0 1 30.0 0 0 1 0 0 0 0 0 0 0 250653 13.0 [null] S [null] 75 90 2 0 male 0 1 44.0 0 0 1 0 0 0 0 0 0 0 248746 13.0 [null] S [null] 35 91 2 0 male 0 1 49.0 1 2 0 0 1 0 0 0 0 0 220845 65.0 [null] S [null] [null] 92 2 0 male 0 1 21.0 2 0 1 0 0 0 0 0 0 0 S.O.C. 14879 73.5 [null] S [null] [null] 93 2 0 male 0 1 21.0 0 0 1 0 0 0 0 0 0 0 S.O.C. 14879 73.5 [null] S [null] [null] 94 2 0 female 1 0 60.0 1 0 1 0 0 0 0 0 0 0 24065 26.0 [null] S [null] [null] 95 2 0 male 0 1 24.0 2 0 1 0 0 0 0 0 0 0 C.A. 31029 31.5 [null] S [null] [null] 96 2 0 male 0 1 22.0 2 0 1 0 0 0 0 0 0 0 C.A. 31029 31.5 [null] S [null] [null] 97 2 0 male 0 1 35.0 0 0 1 0 0 0 0 0 0 0 233734 12.35 [null] Q [null] [null] 98 2 0 male 0 1 31.0 0 0 1 0 0 0 0 0 0 0 C.A. 18723 10.5 [null] S [null] 165 99 2 0 male 0 1 36.0 0 0 1 0 0 0 0 0 0 0 SC/Paris 2163 12.875 D C [null] [null] 100 2 0 male 0 1 [null] 0 0 1 0 0 0 0 0 0 0 SC/A.3 2861 15.5792 [null] C [null] [null] Rows: 1-100 | Columns: 24Please refer to
transform()for more details on transforming avDataFrame.Similarly, you can perform the inverse transform to get the original features using:
model.inverse_transform(data_transformed)
The variable
data_transformedincludes theOneHotEncodercomponents.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[6]: [['(CASE WHEN "sex" = \'female\' THEN 1 ELSE 0 END) AS "sex_female"', '(CASE WHEN "sex" = \'male\' THEN 1 ELSE 0 END) AS "sex_male"'], ['(CASE WHEN "parch" = \'0\' THEN 1 ELSE 0 END) AS "parch_0"', '(CASE WHEN "parch" = \'1\' THEN 1 ELSE 0 END) AS "parch_1"', '(CASE WHEN "parch" = \'2\' THEN 1 ELSE 0 END) AS "parch_2"', '(CASE WHEN "parch" = \'3\' THEN 1 ELSE 0 END) AS "parch_3"', '(CASE WHEN "parch" = \'4\' THEN 1 ELSE 0 END) AS "parch_4"', '(CASE WHEN "parch" = \'5\' THEN 1 ELSE 0 END) AS "parch_5"', '(CASE WHEN "parch" = \'6\' THEN 1 ELSE 0 END) AS "parch_6"', '(CASE WHEN "parch" = \'9\' THEN 1 ELSE 0 END) AS "parch_9"']]
To Python
To obtain the prediction function in Python syntax, use the following code:
X = [['1', '3']] model.to_python()(X) Out[8]: array([[0, 0, 0, 0, 0, 1, 0, 0, 0, 0]])
Hint
The
to_python()method is used to transform the data and compute the different categories. 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, extra_levels: dict | None = None, drop_first: bool = True, ignore_null: bool = True, separator: str = '_', column_naming: Literal['indices', 'values', 'values_relaxed'] = 'indices', null_column_name: str = 'null') 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, key_columns, exclude_columns])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.register(registered_name[, raise_error])Registers the model and adds it to in-DB Model versioning environment with a status of 'under_review'.
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])Applies the model on a
vDataFrame.Attributes