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
LocalOutlierFactorobject 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
pof thep-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
pof thep-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 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: 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
LocalOutlierFactormodel: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
vDataFrameor the name of the relation stored in the database. The test set is optional and is only used to compute the test metrics. Inverticapy, we don’t work usingXmatrices andyvectors. 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()
123density123sulphates123lof_score1 0.98713 0.59 4.36760240314512 2 0.9874 0.37 4.32021671814121 3 0.98758 0.39 3.7364256606699 4 0.98802 0.64 1.8182825155705 5 0.988245 0.41 1.58134352906031 6 0.98836 0.4 1.70902727034283 7 0.98862 0.38 1.82669851967438 8 0.98862 0.38 1.82669851967438 9 0.98862 0.38 1.82669851967438 10 0.98868 0.47 1.48770345546672 11 0.98871 0.34 1.79206564222151 12 0.98876 0.4 1.47345186260012 13 0.98878 0.58 1.52653817884389 14 0.9888 0.37 1.53944272970454 15 0.98883 0.39 1.39532261169049 16 0.98884 0.34 1.69190487369848 17 0.98886 0.4 1.4001457997842 18 0.98892 0.33 1.2082734941048 19 0.98898 0.7 1.9449572852662 20 0.9891 0.95 1.11415525539495 21 0.98912 0.42 1.25477862666665 22 0.98912 0.42 1.25477862666665 23 0.98914 0.39 1.15343682988862 24 0.98915 0.38 1.09269298839458 25 0.98916 0.55 4.41599373920526 26 0.98918 0.35 1.21799298602868 27 0.9892 0.37 1.18418671515733 28 0.9892 0.39 1.1009756705585 29 0.9892 0.39 1.1009756705585 30 0.9892 0.4 1.23464479079862 31 0.98924 0.34 1.39521785099859 32 0.98924 0.47 1.21215375044708 33 0.98924 0.48 1.39880211923048 34 0.98926 0.96 1.11480169203391 35 0.98928 0.79 1.72938152535089 36 0.98934 0.5 1.18820938907835 37 0.98935 0.4 1.14231527325198 38 0.98936 0.44 1.4196440322354 39 0.98936 0.78 2.06253552191554 40 0.98939 0.37 1.12412017274419 41 0.98942 0.69 1.65088406101565 42 0.98944 0.46 1.19889850390061 43 0.98946 0.38 1.08161105501558 44 0.98946 0.38 1.08161105501558 45 0.9895 0.34 1.2032934968528 46 0.98952 0.48 1.15054714582466 47 0.98956 0.33 1.05773765261828 48 0.98958 0.4 1.12126795288485 49 0.98958 0.7 1.73273772536304 50 0.98958 0.7 1.73273772536304 51 0.98961 0.74 1.92230040977353 52 0.98962 0.49 1.29072485977742 53 0.98963 0.59 1.59987755169058 54 0.98964 0.35 1.10700228821976 55 0.98964 0.57 1.33041317303047 56 0.9897 0.5 1.11660997108509 57 0.98975 0.47 1.50550766073749 58 0.98976 0.34 1.09974193722117 59 0.9898 0.37 1.10524272152466 60 0.9898 0.37 1.10524272152466 61 0.9898 0.38 1.32663668980944 62 0.9898 0.65 1.95252119221961 63 0.98981 0.45 1.25677856097758 64 0.98982 0.39 1.25515455120034 65 0.98984 0.5 1.0959033606773 66 0.98984 0.6 1.18005458042221 67 0.98984 0.6 1.18005458042221 68 0.98986 0.56 1.25250194223904 69 0.98986 0.56 1.25250194223904 70 0.98988 0.37 1.0950506531524 71 0.98988 0.53 1.23765926785239 72 0.9899 0.35 1.09458429499182 73 0.9899 0.38 1.29636607204168 74 0.9899 0.46 1.14151777210026 75 0.9899 0.68 2.57724668033739 76 0.98993 0.46 1.13642171061767 77 0.98994 0.38 1.24451142915995 78 0.98994 0.4 1.08557158090384 79 0.98998 0.37 1.08363499704423 80 0.98998 0.37 1.08363499704423 81 0.98998 0.6 1.13812190663703 82 0.98999 0.54 1.25294087324641 83 0.99 0.33 1.15492962478117 84 0.99 0.4 1.1502004310307 85 0.99 0.48 1.09132395914269 86 0.99 0.48 1.09132395914269 87 0.99 0.5 1.04987138276508 88 0.99 0.61 1.87257037193063 89 0.99 0.62 1.33148671271711 90 0.99002 0.98 1.11713588917019 91 0.99004 0.36 1.32364752982055 92 0.99004 0.37 1.11916401334076 93 0.99005 0.59 1.29475458774561 94 0.99008 0.65 1.65342345769209 95 0.99008 0.66 1.20653188396538 96 0.9901 0.34 1.06832098824384 97 0.9901 0.38 1.07298425028634 98 0.99011 0.34 1.06832098824384 99 0.99012 0.37 1.14789205837579 100 0.99014 0.31 1.06796345626739 Rows: 1-100 | Columns: 3As 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.
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
vDataFrameof 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.
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