Loading...

Outliers

Outliers are data points that differ significantly from the rest of the data. While some outliers can reveal some important information (machine failure, systems fraud…), they can also be simple errors.

Some machine learning algorithms are sensitive to outliers. In fact, they can destroy the final predictions because of how much bias they add to the data, and handling outliers in our data is one of the most important parts of the data preparation.

Outliers consist of three main types:

  • Global Outliers: Values far outside the entirety of their source dataset.

  • Contextual Outliers: Values deviate significantly from the rest of the data points in the same context.

  • Collective Outliers: Values that aren’t global or contextual outliers, but as a collection deviate significantly from the entire dataset.

Global outliers are often the most critical type and can add a significant amount of bias into the data. Fortunately, we can easily identify these outliers by computing the Z-Score.

Let’s look at some examples using the Heart Disease dataset. This dataset contains information on patients who are likely to have heart-related complications.

import verticapy as vp

heart = vp.read_csv("heart.csv")
heart.head(100)
123
age
Int
100%
...
123
thal
Int
100%
123
target
Int
100%
129...21
229...21
329...21
429...21
535...21
635...21
735...21
835...21
935...30
1035...30
1135...30
1239...21
1339...21
1439...21
1542...31
1642...31
1742...31
1842...21
1942...21
2042...21
2143...21
2243...21
2343...21
2443...21
2543...21
2643...21
2744...20
2844...20
2944...20
3044...21
3144...21
3244...21
3344...21
3444...21
3544...21
3645...21
3745...21
3845...21
3945...21
4045...21
4145...21
4248...21
4348...21
4448...21
4551...30
4651...30
4751...30
4851...30
4951...21
5051...21
5151...21
5251...21
5351...21
5451...21
5551...21
5652...21
5752...21
5852...21
5952...31
6052...31
6152...31
6252...00
6352...00
6452...00
6552...00
6652...21
6752...21
6852...21
6952...21
7052...21
7152...21
7252...21
7352...21
7452...21
7553...21
7653...21
7753...21
7854...21
7954...21
8054...21
8154...20
8254...20
8354...20
8454...20
8554...31
8654...31
8754...31
8855...30
8955...30
9055...30
9155...30
9255...20
9355...20
9455...20
9555...20
9655...21
9755...21
9855...21
9956...21
10056...21

Let’s focus on a patient’s maximum heart rate (thalach) and the cholesterol (chol) to identify some outliers.

heart.scatter(["thalach", "chol"])

We can see some outliers of the distribution: people with high cholesterol and others with a very low heart rate. Let’s compute the global outliers using the outliers() method.

heart.outliers(["thalach", "chol"], "global_outliers")
heart.scatter(["thalach", "chol"], by = "global_outliers")

It is also possible to draw an outlier plot using the outliers_plot() method.

heart.outliers_plot(["thalach", "chol"],)

We’ve detected some global outliers in the distribution and we can impute these with the fill_outliers() method.

Generally, you can identify global outliers with the Z-Score. We’ll consider a Z-Score greater than 3 indicates that the datapoint is an outlier. Some less precise techniques consider the data points belonging in the first and last alpha-quantile as outliers. You’re free to choose either of these strategies when filling outliers.

heart["thalach"].fill_outliers(
    use_threshold = True,
    threshold = 3.0,
    method = "winsorize",
)
heart["chol"].fill_outliers(
    use_threshold = True,
    threshold = 3.0,
    method = "winsorize",
)
heart.scatter(
    ["thalach", "chol"],
    by = "global_outliers",
)

Other techniques like DBSCAN or local outlier factor (LOF) can be to used to check other data points for outliers.

from verticapy.machine_learning.vertica import DBSCAN

model = DBSCAN(eps = 20, min_samples = 10)
model.fit(heart, ["thalach", "chol"])
model.plot()
heart_dbscan = model.predict()
heart_dbscan["outliers_dbscan"] = "(dbscan_cluster = -1)::int"
heart_dbscan.scatter(
    ["thalach", "chol"],
    by = "outliers_dbscan",
)

While DBSCAN identifies outliers when computing the clusters, LOF computes an outlier score. Generally, a LOF Score greater than 1.5 indicates an outlier.

from verticapy.machine_learning.vertica import LocalOutlierFactor

model = LocalOutlierFactor()
model.fit(heart, ["thalach", "chol",])
model.plot()
heart_lof = model.predict()
heart_lof["outliers"] = "(CASE WHEN lof_score > 1.5 THEN 1 ELSE 0 END)"
heart_lof.scatter(
    ["thalach", "chol"],
    by = "outliers",
)

We have many other techniques like the KMeans clustering for finding outliers, but the most important method is using the Z-Score. After identifying outliers, we just have to decide how to impute the missing values. We’ll focus on missing values in the next lesson.