Note
Go to the end to download the full example code.
Outlier Detection
Installation
# To install the required package, use the following command:
# !pip install modeva
Authentication
# To get authentication, use the following command: (To get full access please replace the token to your own token)
# from modeva.utils.authenticate import authenticate
# authenticate(auth_code='eaaa4301-b140-484c-8e93-f9f633c8bacb')
Import modeva modules
from modeva import DataSet
Load a simulated Friedman data
from sklearn.datasets import make_friedman1
ds = DataSet()
ds.load("BikeSharing")
Outlier detection by CBLOF
results = ds.detect_outlier_cblof(dataset="main", method="kmeans", threshold=0.9)
results.plot()
Outlier detection by Isolation forest
results = ds.detect_outlier_isolation_forest()
results.plot()
Outlier detection by PCA
results = ds.detect_outlier_pca(dataset="main", method="reconst_error")
outliers_sample_index = results.table['outliers'].index
results.plot()
View and use outlier detection results
Outliers table
results.table['outliers']
non-outliers table
results.table['non-outliers']
Evaluate outlier scores of samples
results.func(results.table['outliers'])
array([ 168.80888244, 5715.43198656, 175.61735138, 211.85382308,
156.26453099, 209.51999835, 211.79767387, 211.73697749,
235.88587421, 621.2061903 , 608.74776566, 609.80034006,
320.24817709, 368.87536445, 240.46290845, 195.73641902,
165.52155268, 162.90809961, 209.06127797, 204.12610231,
155.52862194, 154.05854653, 180.41918877, 209.46772427,
218.84143273, 372.36448723, 357.14542513, 270.89950898,
171.66474376, 153.33929231, 231.67840948, 152.59640815,
184.70190371, 259.31578766, 240.19263566, 227.47420513,
218.93479591, 222.61051285, 450.79188068, 388.49001337,
188.26615062, 407.02266701, 360.30015909, 367.02626084,
357.97693524, 351.92668003, 426.42893266, 367.62816698,
363.48135929, 393.35065168, 399.18987489, 418.65165913,
349.18556114, 346.98308978, 365.45158774, 364.6044837 ,
175.69384703, 235.91068841, 306.31283248, 151.3853301 ,
146.98167196, 207.14116501, 472.33487535, 322.76369899,
287.25083864, 240.63637407, 149.74390769, 149.70188146,
150.21251159, 147.87911921, 162.5578808 , 225.61547987,
231.96951261, 459.56982065, 636.78271948, 347.09995579,
222.87002197, 145.88442917, 313.81501786, 5903.76208571,
281.00800494, 318.38916324, 186.18936461, 148.28803616,
5899.69492471, 231.21724945, 524.31748876, 169.95762593,
498.9256966 , 909.84887775, 154.51392887, 1645.78816548,
192.84206194, 292.05277588, 895.82005634, 167.53140674,
192.90730136, 414.81026454, 267.46437948, 329.56894394,
149.29608429, 149.40332419, 187.1357814 , 184.02787417,
179.77817902, 224.94183861, 221.49503037, 176.67085877,
158.54404596, 203.51183993, 191.93596193, 346.1561841 ,
264.32951642, 358.71191863, 364.69757477, 362.50267776,
465.14829747, 433.94123217, 348.04516607, 433.62366964,
440.64281872, 265.71355176, 395.04690421, 614.28428485,
280.36961626, 283.94522266, 1743.08502739, 243.56428495,
219.13851997, 259.62801585, 235.94663616, 248.15643793,
270.54353516, 206.45241788, 306.72357187, 505.90846229,
178.64105114, 302.16388558, 292.2781786 , 383.01317716,
176.58104582, 173.53708623, 355.92844676, 288.34199682,
614.91345761, 1099.07222623, 181.2676489 , 271.78342061,
272.39213012, 574.1437221 , 592.23432213, 223.78376908,
239.61621132, 214.03700983, 479.71629034, 239.77621682,
372.66353155, 241.18869216, 194.21861797, 1225.42852454,
223.68058051, 476.40788594, 238.52352739, 156.17874497,
629.95049483, 366.83766388, 393.14050269, 539.00925887,
939.99180371, 255.16244766, 404.30968127, 1245.37112163,
194.09364341, 698.52302015])
Evaluate outlier scores of samples
results.func(results.table['non-outliers'])
array([ 8.10695093, 7.31850721, 15.14193855, ..., 10.38888373,
6.99092807, 9.9109071 ])
Apply outlier removal
ds.set_inactive_samples(dataset="main", sample_idx=outliers_sample_index)
ds.x.shape
(17205, 12)
Total running time of the script: (0 minutes 7.248 seconds)