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
home.dest
Varchar(100)
110female2.012113781151.55C22 C26S[null][null]Montreal, PQ / Chesterville, ON
210male30.012113781151.55C22 C26S[null]135Montreal, PQ / Chesterville, ON
310female25.012113781151.55C22 C26S[null][null]Montreal, PQ / Chesterville, ON
410male39.0001120500.0A36S[null][null]Belfast, NI
510male71.000PC 1760949.5042[null]C[null]22Montevideo, Uruguay
610male47.010PC 17757227.525C62 C64C[null]124New York, NY
710male[null]00PC 1731825.925[null]S[null][null]New York, NY
810male24.001PC 17558247.5208B58 B60C[null][null]Montreal, PQ
910male36.0001305075.2417C6CA[null]Winnipeg, MN
1010male25.0001390526.0[null]C[null]148San Francisco, CA
1110male45.00011378435.5TS[null][null]Trenton, NJ
1210male42.00011048926.55D22S[null][null]London / Winnipeg, MB
1310male41.00011305430.5A21S[null][null]Pomeroy, WA
1410male48.000PC 1759150.4958B10C[null]208Omaha, NE
1510male[null]0011237939.6[null]C[null][null]Philadelphia, PA
1610male45.00011305026.55B38S[null][null]Washington, DC
1710male[null]0011379831.0[null]S[null][null][null]
1810male33.0006955.0B51 B53 B55S[null][null]New York, NY
1910male28.00011305947.1[null]S[null][null]Montevideo, Uruguay
2010male17.00011305947.1[null]S[null][null]Montevideo, Uruguay
2110male49.0001992426.0[null]S[null][null]Ascot, Berkshire / Rochester, NY
2210male36.0101987778.85C46S[null]172Little Onn Hall, Staffs
2310male46.010W.E.P. 573461.175E31S[null][null]Amenia, ND
2410male[null]001120510.0[null]S[null][null]Liverpool, England / Belfast
2510male27.01013508136.7792C89C[null][null]Los Angeles, CA
2610male[null]0011046552.0A14S[null][null]Stoughton, MA
2710male47.000572725.5875E58S[null][null]Victoria, BC
2810male37.011PC 1775683.1583E52C[null][null]Lakewood, NJ
2910male[null]0011379126.55[null]S[null][null]Roachdale, IN
3010male70.011WE/P 573571.0B22S[null]269Milwaukee, WI
3110male39.010PC 1759971.2833C85C[null][null]New York, NY
3210male31.010F.C. 1275052.0B71S[null][null]Montreal, PQ
3310male50.010PC 17761106.425C86C[null]62Deephaven, MN / Cedar Rapids, IA
3410male39.000PC 1758029.7A18C[null]133Philadelphia, PA
3510female36.000PC 1753131.6792A29C[null][null]New York, NY
3610male[null]00PC 17483221.7792C95S[null][null][null]
3710male30.00011305127.75C111C[null][null]New York, NY
3810male19.03219950263.0C23 C25 C27S[null][null]Winnipeg, MB
3910male64.01419950263.0C23 C25 C27S[null][null]Winnipeg, MB
4010male[null]0011377826.55D34S[null][null]Westcliff-on-Sea, Essex
4110male[null]001120580.0B102S[null][null][null]
4210male37.01011380353.1C123S[null][null]Scituate, MA
4310male47.00011132038.5E63S[null]275St Anne's-on-Sea, Lancashire
4410male24.000PC 1759379.2B86C[null][null][null]
4510male71.000PC 1775434.6542A5C[null][null]New York, NY
4610male38.001PC 17582153.4625C91S[null]147Winnipeg, MB
4710male46.000PC 1759379.2B82 B84C[null][null]New York, NY
4810male[null]0011379642.4[null]S[null][null][null]
4910male45.0103697383.475C83S[null][null]New York, NY
5010male40.0001120590.0B94S[null]110[null]
5110male55.0111274993.5B69S[null]307Montreal, PQ
5210male42.00011303842.5B11S[null][null]London / Middlesex
5310male[null]001746351.8625E46S[null][null]Brighton, MA
5410male55.00068050.0C39S[null][null]London / Birmingham
5510male42.01011378952.0[null]S[null]38New York, NY
5610male[null]00PC 1760030.6958[null]C14[null]New York, NY
5710female50.000PC 1759528.7125C49C[null][null]Paris, France New York, NY
5810male46.00069426.0[null]S[null]80Bennington, VT
5910male50.00011304426.0E60S[null][null]London
6010male32.500113503211.5C132C[null]45[null]
6110male58.0001177129.7B37C[null]258Buffalo, NY
6210male41.0101746451.8625D21S[null][null]Southington / Noank, CT
6310male[null]0011302826.55C124S[null][null]Portland, OR
6410male[null]00PC 1761227.7208[null]C[null][null]Chicago, IL
6510male29.00011350130.0D6S[null]126Springfield, MA
6610male30.00011380145.5[null]S[null][null]London / New York, NY
6710male30.00011046926.0C106S[null][null]Brockton, MA
6810male19.01011377353.1D30S[null][null]New York, NY
6910male46.0001305075.2417C6C[null]292Vancouver, BC
7010male54.0001746351.8625E46S[null]175Dorchester, MA
7110male28.010PC 1760482.1708[null]C[null][null]New York, NY
7210male65.0001350926.55E38S[null]249East Bridgewater, MA
7310male44.0201992890.0C78Q[null]230Fond du Lac, WI
7410male55.00011378730.5C30S[null][null]Montreal, PQ
7510male47.00011379642.4[null]S[null][null]Washington, DC
7610male37.001PC 1759629.7C118C[null][null]Brooklyn, NY
7710male58.00235273113.275D48C[null]122Lexington, MA
7810male64.00069326.0[null]S[null]263Isle of Wight, England
7910male65.00111350961.9792B30C[null]234Providence, RI
8010male28.500PC 1756227.7208D43C[null]189?Havana, Cuba
8110male[null]001120520.0[null]S[null][null]Belfast
8210male45.50011304328.5C124S[null]166Surbiton Hill, Surrey
8310male23.0001274993.5B24S[null][null]Montreal, PQ
8410male29.01011377666.6C2S[null][null]Isleworth, England
8510male18.010PC 17758108.9C65C[null][null]Madrid, Spain
8610male47.00011046552.0C110S[null]207Worcester, MA
8710male38.000199720.0[null]S[null][null]Rotterdam, Netherlands
8810male22.000PC 17760135.6333[null]C[null]232[null]
8910male[null]00PC 17757227.525[null]C[null][null][null]
9010male31.000PC 1759050.4958A24S[null][null]Trenton, NJ
9110male[null]0011376750.0A32S[null][null]Seattle, WA
9210male36.0001304940.125A10C[null][null]Winnipeg, MB
9310male55.010PC 1760359.4[null]C[null][null]New York, NY
9410male33.00011379026.55[null]S[null]109London
9510male61.013PC 17608262.375B57 B59 B63 B66C[null][null]Haverford, PA / Cooperstown, NY
9610male50.0101350755.9E44S[null][null]Duluth, MN
9710male56.00011379226.55[null]S[null][null]New York, NY
9810male56.0001776430.6958A7C[null][null]St James, Long Island, NY
9910male24.0101369560.0C31S[null][null]Huntington, WV
10010male[null]0011305626.0A19S[null][null]Streatham, Surrey
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"])

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
home.dest
Varchar(100)
110female102.01200100000113781151.55C22 C26S[null][null]Montreal, PQ / Chesterville, ON
210male0130.01200100000113781151.55C22 C26S[null]135Montreal, PQ / Chesterville, ON
310female1025.01200100000113781151.55C22 C26S[null][null]Montreal, PQ / Chesterville, ON
410male0139.000100000001120500.0A36S[null][null]Belfast, NI
510male0171.00010000000PC 1760949.5042[null]C[null]22Montevideo, Uruguay
610male0147.01010000000PC 17757227.525C62 C64C[null]124New York, NY
710male01[null]0010000000PC 1731825.925[null]S[null][null]New York, NY
810male0124.00101000000PC 17558247.5208B58 B60C[null][null]Montreal, PQ
910male0136.000100000001305075.2417C6CA[null]Winnipeg, MN
1010male0125.000100000001390526.0[null]C[null]148San Francisco, CA
1110male0145.0001000000011378435.5TS[null][null]Trenton, NJ
1210male0142.0001000000011048926.55D22S[null][null]London / Winnipeg, MB
1310male0141.0001000000011305430.5A21S[null][null]Pomeroy, WA
1410male0148.00010000000PC 1759150.4958B10C[null]208Omaha, NE
1510male01[null]001000000011237939.6[null]C[null][null]Philadelphia, PA
1610male0145.0001000000011305026.55B38S[null][null]Washington, DC
1710male01[null]001000000011379831.0[null]S[null][null][null]
1810male0133.000100000006955.0B51 B53 B55S[null][null]New York, NY
1910male0128.0001000000011305947.1[null]S[null][null]Montevideo, Uruguay
2010male0117.0001000000011305947.1[null]S[null][null]Montevideo, Uruguay
2110male0149.000100000001992426.0[null]S[null][null]Ascot, Berkshire / Rochester, NY
2210male0136.010100000001987778.85C46S[null]172Little Onn Hall, Staffs
2310male0146.01010000000W.E.P. 573461.175E31S[null][null]Amenia, ND
2410male01[null]00100000001120510.0[null]S[null][null]Liverpool, England / Belfast
2510male0127.0101000000013508136.7792C89C[null][null]Los Angeles, CA
2610male01[null]001000000011046552.0A14S[null][null]Stoughton, MA
2710male0147.00010000000572725.5875E58S[null][null]Victoria, BC
2810male0137.01101000000PC 1775683.1583E52C[null][null]Lakewood, NJ
2910male01[null]001000000011379126.55[null]S[null][null]Roachdale, IN
3010male0170.01101000000WE/P 573571.0B22S[null]269Milwaukee, WI
3110male0139.01010000000PC 1759971.2833C85C[null][null]New York, NY
3210male0131.01010000000F.C. 1275052.0B71S[null][null]Montreal, PQ
3310male0150.01010000000PC 17761106.425C86C[null]62Deephaven, MN / Cedar Rapids, IA
3410male0139.00010000000PC 1758029.7A18C[null]133Philadelphia, PA
3510female1036.00010000000PC 1753131.6792A29C[null][null]New York, NY
3610male01[null]0010000000PC 17483221.7792C95S[null][null][null]
3710male0130.0001000000011305127.75C111C[null][null]New York, NY
3810male0119.0320010000019950263.0C23 C25 C27S[null][null]Winnipeg, MB
3910male0164.0140000100019950263.0C23 C25 C27S[null][null]Winnipeg, MB
4010male01[null]001000000011377826.55D34S[null][null]Westcliff-on-Sea, Essex
4110male01[null]00100000001120580.0B102S[null][null][null]
4210male0137.0101000000011380353.1C123S[null][null]Scituate, MA
4310male0147.0001000000011132038.5E63S[null]275St Anne's-on-Sea, Lancashire
4410male0124.00010000000PC 1759379.2B86C[null][null][null]
4510male0171.00010000000PC 1775434.6542A5C[null][null]New York, NY
4610male0138.00101000000PC 17582153.4625C91S[null]147Winnipeg, MB
4710male0146.00010000000PC 1759379.2B82 B84C[null][null]New York, NY
4810male01[null]001000000011379642.4[null]S[null][null][null]
4910male0145.010100000003697383.475C83S[null][null]New York, NY
5010male0140.000100000001120590.0B94S[null]110[null]
5110male0155.011010000001274993.5B69S[null]307Montreal, PQ
5210male0142.0001000000011303842.5B11S[null][null]London / Middlesex
5310male01[null]00100000001746351.8625E46S[null][null]Brighton, MA
5410male0155.0001000000068050.0C39S[null][null]London / Birmingham
5510male0142.0101000000011378952.0[null]S[null]38New York, NY
5610male01[null]0010000000PC 1760030.6958[null]C14[null]New York, NY
5710female1050.00010000000PC 1759528.7125C49C[null][null]Paris, France New York, NY
5810male0146.0001000000069426.0[null]S[null]80Bennington, VT
5910male0150.0001000000011304426.0E60S[null][null]London
6010male0132.50010000000113503211.5C132C[null]45[null]
6110male0158.000100000001177129.7B37C[null]258Buffalo, NY
6210male0141.010100000001746451.8625D21S[null][null]Southington / Noank, CT
6310male01[null]001000000011302826.55C124S[null][null]Portland, OR
6410male01[null]0010000000PC 1761227.7208[null]C[null][null]Chicago, IL
6510male0129.0001000000011350130.0D6S[null]126Springfield, MA
6610male0130.0001000000011380145.5[null]S[null][null]London / New York, NY
6710male0130.0001000000011046926.0C106S[null][null]Brockton, MA
6810male0119.0101000000011377353.1D30S[null][null]New York, NY
6910male0146.000100000001305075.2417C6C[null]292Vancouver, BC
7010male0154.000100000001746351.8625E46S[null]175Dorchester, MA
7110male0128.01010000000PC 1760482.1708[null]C[null][null]New York, NY
7210male0165.000100000001350926.55E38S[null]249East Bridgewater, MA
7310male0144.020100000001992890.0C78Q[null]230Fond du Lac, WI
7410male0155.0001000000011378730.5C30S[null][null]Montreal, PQ
7510male0147.0001000000011379642.4[null]S[null][null]Washington, DC
7610male0137.00101000000PC 1759629.7C118C[null][null]Brooklyn, NY
7710male0158.0020010000035273113.275D48C[null]122Lexington, MA
7810male0164.0001000000069326.0[null]S[null]263Isle of Wight, England
7910male0165.0010100000011350961.9792B30C[null]234Providence, RI
8010male0128.50010000000PC 1756227.7208D43C[null]189?Havana, Cuba
8110male01[null]00100000001120520.0[null]S[null][null]Belfast
8210male0145.5001000000011304328.5C124S[null]166Surbiton Hill, Surrey
8310male0123.000100000001274993.5B24S[null][null]Montreal, PQ
8410male0129.0101000000011377666.6C2S[null][null]Isleworth, England
8510male0118.01010000000PC 17758108.9C65C[null][null]Madrid, Spain
8610male0147.0001000000011046552.0C110S[null]207Worcester, MA
8710male0138.00010000000199720.0[null]S[null][null]Rotterdam, Netherlands
8810male0122.00010000000PC 17760135.6333[null]C[null]232[null]
8910male01[null]0010000000PC 17757227.525[null]C[null][null][null]
9010male0131.00010000000PC 1759050.4958A24S[null][null]Trenton, NJ
9110male01[null]001000000011376750.0A32S[null][null]Seattle, WA
9210male0136.000100000001304940.125A10C[null][null]Winnipeg, MB
9310male0155.01010000000PC 1760359.4[null]C[null][null]New York, NY
9410male0133.0001000000011379026.55[null]S[null]109London
9510male0161.01300010000PC 17608262.375B57 B59 B63 B66C[null][null]Haverford, PA / Cooperstown, NY
9610male0150.010100000001350755.9E44S[null][null]Duluth, MN
9710male0156.0001000000011379226.55[null]S[null][null]New York, NY
9810male0156.000100000001776430.6958A7C[null][null]St James, Long Island, NY
9910male0124.010100000001369560.0C31S[null][null]Huntington, WV
10010male01[null]001000000011305626.0A19S[null][null]Streatham, Surrey
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 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 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