Loading...

verticapy.machine_learning.vertica.neighbors.LocalOutlierFactor

class verticapy.machine_learning.vertica.neighbors.LocalOutlierFactor(name: str = None, overwrite_model: bool = False, n_neighbors: int = 20, p: int = 2)

[Beta Version] Creates a LocalOutlierFactor object by using the Local Outlier Factor algorithm as defined by Markus M. Breunig, Hans-Peter Kriegel, Raymond T. Ng and Jörg Sander. This object is using pure SQL to compute all the distances and final score.

Warning

This algorithm uses a CROSS JOIN during computation and is therefore computationally expensive at O(n * n), where n is the total number of elements. Since LocalOutlierFactor uses the p- distance, it is highly sensitive to unnormalized data. A table is created at the end of the learning phase.

Important

This algorithm is not Vertica Native and relies solely on SQL for attribute computation. While this model does not take advantage of the benefits provided by a model management system, including versioning and tracking, the SQL code it generates can still be used to create a pipeline.

Parameters

name: str, optional

Name of the model. This is not a built-in model, so this name is used to build the final table.

overwrite_model: bool, optional

If set to True, training a model with the same name as an existing model overwrites the existing model.

n_neighbors: int, optional

Number of neighbors to consider when computing the score.

p: int, optional

The p of the p-distances (distance metric used during the model computation).

Attributes

Many attributes are created during the fitting phase.

n_neighbors_: int

Number of neighbors.

p_: int

The p of the p-distances.

n_errors_: int

Number of errors during the model fitting phase.

cnt_: int

Number of elements accepted during the model fitting phase.

Note

All attributes can be accessed using the get_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 winequality dataset.

import verticapy.datasets as vpd

data = vpd.load_winequality()
123
fixed_acidity
Numeric(8)
123
volatile_acidity
Numeric(9)
123
citric_acid
Numeric(8)
123
residual_sugar
Numeric(9)
123
chlorides
Float(22)
123
free_sulfur_dioxide
Numeric(9)
123
total_sulfur_dioxide
Numeric(9)
123
density
Float(22)
123
pH
Numeric(8)
123
sulphates
Numeric(8)
123
alcohol
Float(22)
123
quality
Integer
123
good
Integer
Abc
color
Varchar(20)
13.90.2250.44.20.0329.0118.00.9893.570.3612.881white
24.70.3350.141.30.03669.0168.00.992123.470.4610.550white
34.70.4550.181.90.03633.0106.00.987463.210.8314.071white
44.70.7850.03.40.03623.0134.00.989813.530.9213.860white
54.90.3450.341.00.06832.0143.00.991383.240.410.150white
64.90.3450.341.00.06832.0143.00.991383.240.410.150white
74.90.420.02.10.04816.042.00.991543.710.7414.071red
85.00.270.41.20.07642.0124.00.992043.320.4710.160white
95.00.310.06.40.04643.0166.00.9943.30.639.960white
105.00.40.54.30.04629.080.00.99023.490.6613.660red
115.00.440.0418.60.03938.0128.00.99853.370.5710.260white
125.10.110.321.60.02812.090.00.990083.570.5212.260white
135.10.140.250.70.03915.089.00.99193.220.439.260white
145.10.1650.225.70.04742.0146.00.99343.180.559.960white
155.10.330.221.60.02718.089.00.98933.510.3812.571white
165.10.330.221.60.02718.089.00.98933.510.3812.571white
175.10.330.221.60.02718.089.00.98933.510.3812.571white
185.10.390.211.70.02715.072.00.98943.50.4512.560white
195.20.20.273.20.04716.093.00.992353.440.5310.171white
205.20.210.311.70.04817.061.00.989533.240.3712.071white
215.20.220.466.20.06641.0187.00.993623.190.429.7333333333333350white
225.20.310.22.40.02727.0117.00.988863.560.4513.071white
235.20.320.251.80.10313.050.00.99573.380.559.250red
245.20.340.376.20.03142.0133.00.990763.250.4112.560white
255.20.360.021.60.03124.0104.00.98963.440.3512.260white
265.20.3650.0813.50.04137.0142.00.9973.460.399.960white
275.20.480.041.60.05419.0106.00.99273.540.6212.271red
285.20.50.182.00.03623.0129.00.989493.360.7713.471white
295.30.160.391.00.02840.0101.00.991563.570.5910.660white
305.30.160.391.00.02840.0101.00.991563.570.5910.660white
315.30.1650.241.10.05125.0105.00.99253.320.479.150white
325.30.230.560.90.04146.0141.00.991193.160.629.750white
335.30.30.31.20.02925.093.00.987423.310.413.671white
345.30.330.31.20.04825.0119.00.990453.320.6211.360white
355.30.360.276.30.02840.0132.00.991863.370.411.660white
365.30.360.276.30.02840.0132.00.991863.370.411.660white
375.30.40.253.90.03145.0130.00.990723.310.5811.7571white
385.30.470.112.20.04816.089.00.991823.540.8813.671red
395.30.470.112.20.04816.089.00.991823.540.8813.566666666666771red
405.30.7150.191.50.1617.062.00.993953.620.6111.050red
415.40.220.291.20.04569.0152.00.991783.760.6311.071white
425.40.5950.12.80.04226.080.00.99323.360.389.350white
435.40.740.091.70.08916.026.00.994023.670.5611.660red
445.50.120.331.00.03823.0131.00.991643.250.459.850white
455.50.120.331.00.03823.0131.00.991643.250.459.850white
465.50.140.274.60.02922.0104.00.99493.340.449.050white
475.50.140.274.60.02922.0104.00.99493.340.449.050white
485.50.160.311.20.02631.068.00.98983.330.4411.6560white
495.50.160.311.20.02631.068.00.98983.330.4411.633333333333360white
505.50.180.225.50.03710.086.00.991563.460.4412.250white
515.50.240.451.70.04622.0113.00.992243.220.4810.050white
525.50.290.31.10.02220.0110.00.988693.340.3812.871white
535.50.310.293.00.02716.0102.00.990673.230.5611.260white
545.50.320.454.90.02825.0191.00.99223.510.4911.571white
555.50.350.351.10.04514.0167.00.9923.340.689.960white
565.50.3750.381.70.03617.098.00.991423.290.3910.560white
575.60.150.265.550.05151.0139.00.993363.470.511.060white
585.60.150.315.30.0388.079.00.99233.30.3910.560white
595.60.160.271.40.04453.0168.00.99183.280.3710.160white
605.60.1750.290.80.04320.067.00.991123.280.489.960white
615.60.1850.197.10.04836.0110.00.994383.260.419.560white
625.60.1850.197.10.04836.0110.00.994383.260.419.560white
635.60.220.321.20.02429.097.00.988233.20.4613.0571white
645.60.260.181.40.03418.0135.00.991743.320.3510.260white
655.60.260.265.70.03112.080.00.99233.250.3810.850white
665.60.260.511.40.02925.093.00.994283.230.4910.560white
675.60.280.284.20.04452.0158.00.9923.350.4410.771white
685.60.30.16.40.04334.0142.00.993823.140.489.850white
695.60.350.145.00.04648.0198.00.99373.30.7110.350white
705.60.490.134.50.03917.0116.00.99073.420.913.771white
715.60.490.134.50.03917.0116.00.99073.420.913.771white
725.60.660.02.20.0873.011.00.993783.710.6312.871red
735.60.660.02.20.0873.011.00.993783.710.6312.871red
745.70.150.4711.40.03549.0128.00.994563.030.3410.581white
755.70.180.262.20.02321.095.00.98933.070.5412.360white
765.70.180.361.20.0469.071.00.991993.70.6810.971white
775.70.20.32.50.04638.0125.00.992763.340.59.960white
785.70.210.320.90.03838.0121.00.990743.240.4610.660white
795.70.210.374.50.0458.0140.00.993323.290.6210.660white
805.70.220.216.00.04441.0113.00.998623.220.468.960white
815.70.220.216.00.04441.0113.00.998623.220.468.960white
825.70.220.216.00.04441.0113.00.998623.220.468.960white
835.70.220.216.00.04441.0113.00.998623.220.468.960white
845.70.220.216.00.04441.0113.00.998623.220.468.960white
855.70.220.293.50.0427.0146.00.989993.170.3612.160white
865.70.230.289.650.02526.0121.00.99253.280.3811.360white
875.70.250.2612.50.04952.5106.00.996913.080.459.460white
885.70.250.2612.50.04952.5120.00.996913.080.459.460white
895.70.250.2711.50.0424.0120.00.994113.330.3110.860white
905.70.260.2417.80.05923.0124.00.997733.30.510.150white
915.70.260.2417.80.05923.0124.00.997733.30.510.150white
925.70.260.2417.80.05923.0124.00.997733.30.510.150white
935.70.270.321.20.04620.0155.00.99343.80.4110.260white
945.70.280.2417.50.04460.0167.00.99893.310.449.450white
955.70.320.181.40.02926.0104.00.99063.440.3711.060white
965.70.320.384.750.03323.094.00.9913.420.4211.871white
975.70.360.344.20.02621.077.00.99073.410.4511.960white
985.80.140.156.10.04227.0123.00.993623.060.69.960white
995.80.150.321.20.03714.0119.00.991373.190.510.260white
1005.80.170.341.80.04596.0170.00.990353.380.911.881white
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 LocalOutlierFactor model:

from verticapy.machine_learning.vertica import LocalOutlierFactor

Then we can create the model:

model = LocalOutlierFactor(
    n_neighbors = 10,
    p = 2,
)

Important

As this model is not native, it solely relies on SQL statements to compute various attributes, storing them within the object. No data is saved in the database.

Model Training

We can now fit the model:

model.fit(data, X = ["density", "sulphates"])

Important

To train a model, you can directly use the vDataFrame or the name of the relation stored in the database. The test set is optional and is only used to compute the test metrics. In verticapy, we don’t work using X matrices and y vectors. Instead, we work directly with lists of predictors and the response name.

Hint

For clustering and anomaly detection, the use of predictors is optional. In such cases, all available predictors are considered, which can include solely numerical variables or a combination of numerical and categorical variables, depending on the model’s capabilities.

Important

As this model is not native, it solely relies on SQL statements to compute various attributes, storing them within the object. No data is saved in the database.

Prediction

To find out the LOF score for each datapoint:

model.predict()
123
density
Float(22)
123
sulphates
Numeric(8)
123
lof_score
Float(22)
10.987130.594.36760240314512
20.98740.374.32021671814121
30.987580.393.7364256606699
40.988020.641.8182825155705
50.9882450.411.58134352906031
60.988360.41.70902727034283
70.988620.381.82669851967438
80.988620.381.82669851967438
90.988620.381.82669851967438
100.988680.471.48770345546672
110.988710.341.79206564222151
120.988760.41.47345186260012
130.988780.581.52653817884389
140.98880.371.53944272970454
150.988830.391.39532261169049
160.988840.341.69190487369848
170.988860.41.4001457997842
180.988920.331.2082734941048
190.988980.71.9449572852662
200.98910.951.11415525539495
210.989120.421.25477862666665
220.989120.421.25477862666665
230.989140.391.15343682988862
240.989150.381.09269298839458
250.989160.554.41599373920526
260.989180.351.21799298602868
270.98920.371.18418671515733
280.98920.391.1009756705585
290.98920.391.1009756705585
300.98920.41.23464479079862
310.989240.341.39521785099859
320.989240.471.21215375044708
330.989240.481.39880211923048
340.989260.961.11480169203391
350.989280.791.72938152535089
360.989340.51.18820938907835
370.989350.41.14231527325198
380.989360.441.4196440322354
390.989360.782.06253552191554
400.989390.371.12412017274419
410.989420.691.65088406101565
420.989440.461.19889850390061
430.989460.381.08161105501558
440.989460.381.08161105501558
450.98950.341.2032934968528
460.989520.481.15054714582466
470.989560.331.05773765261828
480.989580.41.12126795288485
490.989580.71.73273772536304
500.989580.71.73273772536304
510.989610.741.92230040977353
520.989620.491.29072485977742
530.989630.591.59987755169058
540.989640.351.10700228821976
550.989640.571.33041317303047
560.98970.51.11660997108509
570.989750.471.50550766073749
580.989760.341.09974193722117
590.98980.371.10524272152466
600.98980.371.10524272152466
610.98980.381.32663668980944
620.98980.651.95252119221961
630.989810.451.25677856097758
640.989820.391.25515455120034
650.989840.51.0959033606773
660.989840.61.18005458042221
670.989840.61.18005458042221
680.989860.561.25250194223904
690.989860.561.25250194223904
700.989880.371.0950506531524
710.989880.531.23765926785239
720.98990.351.09458429499182
730.98990.381.29636607204168
740.98990.461.14151777210026
750.98990.682.57724668033739
760.989930.461.13642171061767
770.989940.381.24451142915995
780.989940.41.08557158090384
790.989980.371.08363499704423
800.989980.371.08363499704423
810.989980.61.13812190663703
820.989990.541.25294087324641
830.990.331.15492962478117
840.990.41.1502004310307
850.990.481.09132395914269
860.990.481.09132395914269
870.990.51.04987138276508
880.990.611.87257037193063
890.990.621.33148671271711
900.990020.981.11713588917019
910.990040.361.32364752982055
920.990040.371.11916401334076
930.990050.591.29475458774561
940.990080.651.65342345769209
950.990080.661.20653188396538
960.99010.341.06832098824384
970.99010.381.07298425028634
980.990110.341.06832098824384
990.990120.371.14789205837579
1000.990140.311.06796345626739
Rows: 1-100 | Columns: 3

As shown above, a new column has been created, containing the lof score.

Plots - Outliers

Plots highlighting the outliers can be easily drawn using:

model.plot()

Important

Please refer to Machine Learning - LOF Plot for more examples.

Parameter Modification

In order to see the parameters:

model.get_params()
Out[5]: {'n_neighbors': 10, 'p': 2}

And to manually change some of the parameters:

model.set_params({'p': 3})

Model Register

As this model is not native, it does not support model management and versioning. However, it is possible to use the SQL code it generates for deployment.

__init__(name: str = None, overwrite_model: bool = False, n_neighbors: int = 20, p: int = 2) None

Must be overridden in the child class

Methods

__init__([name, overwrite_model, n_neighbors, p])

Must be overridden in the child class

contour([nbins, chart])

Draws the model's contour plot.

deploySQL([X])

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, key_columns, index, ...])

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.

plot([max_nb_points, chart])

Draws the model.

predict()

Creates a vDataFrame of the model.

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_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).

Attributes