Loading...

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 from verticapy are 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()
123
pclass
Integer
123
survived
Integer
Abc
Varchar(164)
Abc
sex
Varchar(20)
123
age
Numeric(8)
123
sibsp
Integer
123
parch
Integer
Abc
ticket
Varchar(36)
123
fare
Numeric(12)
Abc
cabin
Varchar(30)
Abc
embarked
Varchar(20)
Abc
boat
Varchar(100)
123
body
Integer
Abc
Varchar(100)
110male71.000PC 1760949.5042[null]C[null]22
210male45.00011378435.5TS[null][null]
310male[null]0011379831.0[null]S[null][null]
410male17.00011305947.1[null]S[null][null]
510male27.01013508136.7792C89C[null][null]
610male37.011PC 1775683.1583E52C[null][null]
710male31.010F.C. 1275052.0B71S[null][null]
810male50.010PC 17761106.425C86C[null]62
910female36.000PC 1753131.6792A29C[null][null]
1010male37.01011380353.1C123S[null][null]
1110male24.000PC 1759379.2B86C[null][null]
1210male45.0103697383.475C83S[null][null]
1310male40.0001120590.0B94S[null]110
1410male42.00011303842.5B11S[null][null]
1510male[null]001746351.8625E46S[null][null]
1610male42.01011378952.0[null]S[null]38
1710male[null]00PC 1760030.6958[null]C14[null]
1810male29.00011350130.0D6S[null]126
1910male46.0001305075.2417C6C[null]292
2010male54.0001746351.8625E46S[null]175
2110male47.00011379642.4[null]S[null][null]
2210male58.00235273113.275D48C[null]122
2310male45.50011304328.5C124S[null]166
2410male29.01011377666.6C2S[null][null]
2510male47.00011046552.0C110S[null]207
2610male38.000199720.0[null]S[null][null]
2710male22.000PC 17760135.6333[null]C[null]232
2810male31.000PC 1759050.4958A24S[null][null]
2910male50.0101350755.9E44S[null][null]
3010male56.0001776430.6958A7C[null][null]
3110male57.010PC 17569146.5208B78C[null][null]
3210female63.010PC 17483221.7792C55 C57S[null][null]
3310male61.0003696332.3208D50S[null]46
3410male21.0013528177.2875D26S[null]169
3510male51.001PC 1759761.3792[null]C[null][null]
3611female63.0101350277.9583D7S10[null]
3711female32.0001181376.2917D15C8[null]
3811female58.00011378326.55C103S8[null]
3911female44.000PC 1761027.7208B4C6[null]
4011female41.00016966134.5E40C3[null]
4111female53.000PC 1760627.4458[null]C6[null]
4211male36.001PC 17755512.3292B51 B53 B55C3[null]
4311female58.001PC 17755512.3292B51 B53 B55C3[null]
4411male11.012113760120.0B96 B98S4[null]
4511female76.0101987778.85C46S6[null]
4611female[null]0111350555.0E33S6[null]
4711female39.011PC 1775683.1583E49C14[null]
4811female27.012F.C. 1275052.0B71S3[null]
4911female[null]0017421110.8833[null]C4[null]
5011female35.000113503211.5C130C4[null]
5111female22.00111237859.4[null]C7[null]
5211female25.0101176555.4417E50C5[null]
5311male48.010PC 1757276.7292D33C3[null]
5411female35.0103697383.475C83SD[null]
5511male27.000PC 1757276.7292D49C3[null]
5611female24.0001176783.1583C54C7[null]
5711female52.0111274993.5B69S3[null]
5811female44.00111136157.9792B18C4[null]
5911female15.00124160211.3375B5S2[null]
6011male30.0101323657.75C78C11[null]
6111female31.01035273113.275D36C6[null]
6211female39.000PC 17758108.9C105C8[null]
6311female22.00111350961.9792B36C5[null]
6411male52.00011378630.5C104S6[null]
6511female43.00124160211.3375B3S2[null]
6611female33.00011015286.5B77S8[null]
6711male45.01116966134.5E34C3[null]
6811female40.01116966134.5E34C3[null]
6911male48.0101999652.0C126S5 7[null]
7011female[null]00PC 1758579.2[null]CD[null]
7111female35.000PC 17755512.3292[null]C3[null]
7211female60.01011081375.25D37C5[null]
7311male21.001PC 1759761.3792[null]CA[null]
7420male23.000C.A. 3103010.5[null]S[null][null]
7520male28.00024435826.0[null]S[null][null]
7620male60.0112975039.0[null]S[null][null]
7720female44.01024425226.0[null]S[null][null]
7820male29.010200326.0[null]S[null][null]
7920male18.000S.O.C. 1487973.5[null]S[null][null]
8020male18.000S.O.C. 1487973.5[null]S[null][null]
8120male54.0002840326.0[null]S[null][null]
8220male18.00023617113.0[null]S[null][null]
8320male36.00022923613.0[null]S[null]236
8420male34.0102866421.0[null]S[null][null]
8520male21.0102813311.5[null]S[null][null]
8620male21.0102813411.5[null]S[null][null]
8720male24.00023386613.0[null]S[null]155
8820male34.0001223313.0[null]S[null][null]
8920male30.00025065313.0[null]S[null]75
9020male44.00024874613.0[null]S[null]35
9120male49.01222084565.0[null]S[null][null]
9220male21.020S.O.C. 1487973.5[null]S[null][null]
9320male21.000S.O.C. 1487973.5[null]S[null][null]
9420female60.0102406526.0[null]S[null][null]
9520male24.020C.A. 3102931.5[null]S[null][null]
9620male22.020C.A. 3102931.5[null]S[null][null]
9720male35.00023373412.35[null]Q[null][null]
9820male31.000C.A. 1872310.5[null]S[null]165
9920male36.000SC/Paris 216312.875DC[null][null]
10020male[null]00SC/A.3 286115.5792[null]C[null][null]
Rows: 1-100 | Columns: 14

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 OneHotEncoder model:

from verticapy.machine_learning.vertica import OneHotEncoder

Then we can create the model:

model = OneHotEncoder(
    drop_first = False,
    column_naming = "values",
)

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, ["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 vDataFrame or 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 transform function. Let us transform the data and display the first datapoints.

model.transform(data)
123
pclass
Integer
123
survived
Integer
Abc
Varchar(164)
Abc
sex
Varchar(20)
123
sex_female
Integer
123
sex_male
Integer
123
age
Numeric(8)
123
sibsp
Integer
123
parch
Integer
123
parch_0
Integer
123
parch_1
Integer
123
parch_2
Integer
123
parch_3
Integer
123
parch_4
Integer
123
parch_5
Integer
123
parch_6
Integer
123
parch_9
Integer
Abc
ticket
Varchar(36)
123
fare
Numeric(12)
Abc
cabin
Varchar(30)
Abc
embarked
Varchar(20)
Abc
boat
Varchar(100)
123
body
Integer
Abc
Varchar(100)
110male0171.00010000000PC 1760949.5042[null]C[null]22
210male0145.0001000000011378435.5TS[null][null]
310male01[null]001000000011379831.0[null]S[null][null]
410male0117.0001000000011305947.1[null]S[null][null]
510male0127.0101000000013508136.7792C89C[null][null]
610male0137.01101000000PC 1775683.1583E52C[null][null]
710male0131.01010000000F.C. 1275052.0B71S[null][null]
810male0150.01010000000PC 17761106.425C86C[null]62
910female1036.00010000000PC 1753131.6792A29C[null][null]
1010male0137.0101000000011380353.1C123S[null][null]
1110male0124.00010000000PC 1759379.2B86C[null][null]
1210male0145.010100000003697383.475C83S[null][null]
1310male0140.000100000001120590.0B94S[null]110
1410male0142.0001000000011303842.5B11S[null][null]
1510male01[null]00100000001746351.8625E46S[null][null]
1610male0142.0101000000011378952.0[null]S[null]38
1710male01[null]0010000000PC 1760030.6958[null]C14[null]
1810male0129.0001000000011350130.0D6S[null]126
1910male0146.000100000001305075.2417C6C[null]292
2010male0154.000100000001746351.8625E46S[null]175
2110male0147.0001000000011379642.4[null]S[null][null]
2210male0158.0020010000035273113.275D48C[null]122
2310male0145.5001000000011304328.5C124S[null]166
2410male0129.0101000000011377666.6C2S[null][null]
2510male0147.0001000000011046552.0C110S[null]207
2610male0138.00010000000199720.0[null]S[null][null]
2710male0122.00010000000PC 17760135.6333[null]C[null]232
2810male0131.00010000000PC 1759050.4958A24S[null][null]
2910male0150.010100000001350755.9E44S[null][null]
3010male0156.000100000001776430.6958A7C[null][null]
3110male0157.01010000000PC 17569146.5208B78C[null][null]
3210female1063.01010000000PC 17483221.7792C55 C57S[null][null]
3310male0161.000100000003696332.3208D50S[null]46
3410male0121.001010000003528177.2875D26S[null]169
3510male0151.00101000000PC 1759761.3792[null]C[null][null]
3611female1063.010100000001350277.9583D7S10[null]
3711female1032.000100000001181376.2917D15C8[null]
3811female1058.0001000000011378326.55C103S8[null]
3911female1044.00010000000PC 1761027.7208B4C6[null]
4011female1041.0001000000016966134.5E40C3[null]
4111female1053.00010000000PC 1760627.4458[null]C6[null]
4211male0136.00101000000PC 17755512.3292B51 B53 B55C3[null]
4311female1058.00101000000PC 17755512.3292B51 B53 B55C3[null]
4411male0111.01200100000113760120.0B96 B98S4[null]
4511female1076.010100000001987778.85C46S6[null]
4611female10[null]010100000011350555.0E33S6[null]
4711female1039.01101000000PC 1775683.1583E49C14[null]
4811female1027.01200100000F.C. 1275052.0B71S3[null]
4911female10[null]001000000017421110.8833[null]C4[null]
5011female1035.00010000000113503211.5C130C4[null]
5111female1022.0010100000011237859.4[null]C7[null]
5211female1025.010100000001176555.4417E50C5[null]
5311male0148.01010000000PC 1757276.7292D33C3[null]
5411female1035.010100000003697383.475C83SD[null]
5511male0127.00010000000PC 1757276.7292D49C3[null]
5611female1024.000100000001176783.1583C54C7[null]
5711female1052.011010000001274993.5B69S3[null]
5811female1044.0010100000011136157.9792B18C4[null]
5911female1015.0010100000024160211.3375B5S2[null]
6011male0130.010100000001323657.75C78C11[null]
6111female1031.0101000000035273113.275D36C6[null]
6211female1039.00010000000PC 17758108.9C105C8[null]
6311female1022.0010100000011350961.9792B36C5[null]
6411male0152.0001000000011378630.5C104S6[null]
6511female1043.0010100000024160211.3375B3S2[null]
6611female1033.0001000000011015286.5B77S8[null]
6711male0145.0110100000016966134.5E34C3[null]
6811female1040.0110100000016966134.5E34C3[null]
6911male0148.010100000001999652.0C126S5 7[null]
7011female10[null]0010000000PC 1758579.2[null]CD[null]
7111female1035.00010000000PC 17755512.3292[null]C3[null]
7211female1060.0101000000011081375.25D37C5[null]
7311male0121.00101000000PC 1759761.3792[null]CA[null]
7420male0123.00010000000C.A. 3103010.5[null]S[null][null]
7520male0128.0001000000024435826.0[null]S[null][null]
7620male0160.011010000002975039.0[null]S[null][null]
7720female1044.0101000000024425226.0[null]S[null][null]
7820male0129.01010000000200326.0[null]S[null][null]
7920male0118.00010000000S.O.C. 1487973.5[null]S[null][null]
8020male0118.00010000000S.O.C. 1487973.5[null]S[null][null]
8120male0154.000100000002840326.0[null]S[null][null]
8220male0118.0001000000023617113.0[null]S[null][null]
8320male0136.0001000000022923613.0[null]S[null]236
8420male0134.010100000002866421.0[null]S[null][null]
8520male0121.010100000002813311.5[null]S[null][null]
8620male0121.010100000002813411.5[null]S[null][null]
8720male0124.0001000000023386613.0[null]S[null]155
8820male0134.000100000001223313.0[null]S[null][null]
8920male0130.0001000000025065313.0[null]S[null]75
9020male0144.0001000000024874613.0[null]S[null]35
9120male0149.0120010000022084565.0[null]S[null][null]
9220male0121.02010000000S.O.C. 1487973.5[null]S[null][null]
9320male0121.00010000000S.O.C. 1487973.5[null]S[null][null]
9420female1060.010100000002406526.0[null]S[null][null]
9520male0124.02010000000C.A. 3102931.5[null]S[null][null]
9620male0122.02010000000C.A. 3102931.5[null]S[null][null]
9720male0135.0001000000023373412.35[null]Q[null][null]
9820male0131.00010000000C.A. 1872310.5[null]S[null]165
9920male0136.00010000000SC/Paris 216312.875DC[null][null]
10020male01[null]0010000000SC/A.3 286115.5792[null]C[null][null]
Rows: 1-100 | Columns: 24

Please refer to transform() for more details on transforming a vDataFrame.

Similarly, you can perform the inverse transform to get the original features using:

model.inverse_transform(data_transformed)

The variable data_transformed includes the OneHotEncoder components.

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

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 a scikit-learn model.

The preceding methods for exporting the model use MemModel, and it is recommended to use MemModel directly.

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.

get_params()

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.

summarize()

Summarizes the model.

to_binary(path)

Exports the model to the Vertica Binary format.

to_memmodel()

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