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 vastorbit as vo

heart = vo.read_csv("heart.csv")
heart.head(100)
123
age
Integer
100%
123
sex
Integer
100%
123
cp
Integer
100%
123
trestbps
Integer
100%
123
chol
Integer
100%
123
fbs
Integer
100%
123
restecg
Integer
100%
123
thalach
Integer
100%
123
exang
Integer
100%
123
oldpeak
Double
100%
123
slope
Integer
100%
123
ca
Integer
100%
123
thal
Integer
100%
123
target
Integer
100%
167101202290012912.61230
263131452331015002.30011
363001241970113610.01020
452101122300116000.02120
558001301970113100.61021
653101422260011110.02031
757101502760011210.61110
844121302330117910.42021
95112942270115410.02131
1054021102140115801.61021
1140101101670011412.01030
1257111242610114100.32030
1362001402680016003.60220
1453101402031015513.10030
1562111282081014000.02021
1658121052400015410.61031
1770111562450014300.02021
1845101152600018500.02021
1942131482440017800.82221
2058001702251014612.81210
2161101402070013811.92130
2262001402680016003.60220
2360101302530114411.42130
2454101402390116001.22021
2561101381660012513.61120
2663021352520017200.02021
2742121301800115000.02021
2857121282290015000.41130
2944121302330117910.42021
3054101242660010912.21130
3151121002220114311.21021
3258111252200114400.41431
3368121182770115101.02131
3455101402170111115.60030
3542101363150112511.81010
3649121181490012600.82320
3753001382340016000.02021
3852121721991116200.52031
3951131252130012511.42121
4051101402610018610.02021
4170101451740112512.60030
4235001381830118201.42021
4358121122300016502.51130
4459131602730012500.02020
4560101402930017001.21230
4656101321840010512.11110
4735001381830118201.42021
4861101381660012513.61120
4958031502831016201.02021
5052101282550116110.02130
5158111202840016001.81020
5237121302500118703.50021
5352101282550116110.02130
5467101202290012912.61230
5565131382821017401.41120
5646111011971115600.02031
5768021202110011501.51021
5843101153030118101.21021
5968021202110011501.51021
6051101402990117311.62030
6152101122300116000.02120
6264121403350115800.02020
6359131702880015900.21030
6452101252120116801.02230
6559131602730012500.02020
6660031502400117100.92021
6741121122500117900.02021
6841111102350115300.02021
6956111202400116900.00021
7056111202360117800.82021
7148021302750113900.22021
7239121403210018200.02021
7364131702270015500.61031
7457101401920114800.41011
7559131602730012500.02020
7660101302060013212.41230
7761101402070013811.92130
7843021222130116500.21021
7954101201880111301.41130
8059101382710018200.02021
8157101322070116810.02031
8257111542320016400.02120
8357101301310111511.21130
8448101242740016600.51030
8570101451740112512.60030
8657101652891012401.01330
8761101202600114013.61130
8857101102010112611.51011
8960001502580015702.61230
9063001504070015404.01330
9155001282050213012.01130
9264001803250115410.02021
9354101102390112612.81130
9452101282041115611.01000
9551101402990117311.62030
966202130263019701.21130
9759131782700014504.20031
9852111342010115800.82121
9942001022650012200.61021
10059101352340116100.51031

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 vastorbit.machine_learning.vast import DBSCAN

model = DBSCAN(eps = 20, min_samples = 10)
model.fit(heart, ["thalach", "chol"])
model.plot()
heart_dbscan = model.predict()
heart_dbscan["outliers_dbscan"] = "CAST((dbscan_clusters = -1) AS integer)"
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 vastorbit.machine_learning.vast 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.