<jupyter_start><jupyter_text>Detect and Delete outliers with Optimus An outlier is an observation that lies an abnormal distance from other values in a random sample from a population. In a sense, this definition leaves it up to the analyst (or a consensus process) to decide what will be considered abnormal. Before abnormal observations can be singled out, it is necessary to characterize normal observations.You have to be careful when studying outliers because how do you know if an outlier is the result of a data glitch, or a real data point -- indeed maybe not an outlier.<jupyter_code>%load_ext autoreload
%autoreload 2
import sys
sys.path.append("..")
from optimus import Optimus
# Create optimus
op = Optimus()
from pyspark.sql.types import *
df = op.create.df(
    [
                ("words", "str", True),
                ("num", "int", True),
                ("animals", "str", True),
                ("thing", StringType(), True),
                ("two strings", StringType(), True),
                ("filter", StringType(), True),
                ("num 2", "int", True),
                ("date", "string", True),
                ("num 3", "str", True)
                
            ],[
                ("  I like     fish  ", 1, "dog", "&^%$#housé", "cat-car", "a",1, "20150510", '3'),
                ("    zombies", 2, "cat", "tv", "dog-tv", "b",2, "20160510", '3'),
                ("simpsons   cat lady", 2, "frog", "table","eagle-tv-plus","1",3, "20170510", '4'),
                (None, 3, "eagle", "glass", "lion-pc", "c",4, "20180510", '5'),
                (None, 5, "eagle", "glass", "lion-pc", "c",4, "20180510", '5'),
               (None, 6, "eagle", "glass", "lion-pc", "c",4, "20180510", '5'),
             (None, 7, "eagle", "glass", "lion-pc", "c",4, "20180510", '5'),
             (None, 20, "eagle", "glass", "lion-pc", "c",4, "20180510", '5')
    
            ]
            )

df.table()<jupyter_output><empty_output><jupyter_text>From a quick inspection of the dataframe we can guess that the 1000 in the column `num` can be an outlier. You can perform a very intense search to see if it is actually and outlier, if you need something like that please check out [these articles and tutorials](http://www.datasciencecentral.com/profiles/blogs/11-articles-and-tutorials-about-outliers) With optimus you can perform several analysis too to check if a value is an outlier. First lets run some visual analysis. Remember to check the [Main Example](https://github.com/ironmussa/Optimus/blob/master/examples/Optimus_Example.ipynb) for more.  Outlier detection One of the commonest ways of finding outliers in one-dimensional data is to mark as a potential outlier any point that is more than two standard deviations, say, from the mean (I am referring to sample means and standard deviations here and in what follows). But the presence of outliers is likely to have a strong effect on the mean and the standard deviation, making this technique unreliable. That's why we have programmed in Optimus the median absolute deviation from median, commonly shortened to the median absolute deviation (MAD). It is the median of the set comprising the absolute values of the differences between the median and each data point. If you want more information on the subject please read the amazing article by Leys et al. about dtecting outliers [here](http://www.sciencedirect.com/science/article/pii/S0022103113000668)<jupyter_code>from optimus.outliers.outliers import OutlierDetector
od = OutlierDetector()<jupyter_output><empty_output><jupyter_text>Zscore<jupyter_code>od.z_score(df, "num", threshold=1).table()
od.z_score(df, ["num", "num 2"],1).table()<jupyter_output><empty_output><jupyter_text>IQR<jupyter_code>od.iqr(df, "num").table()<jupyter_output><empty_output><jupyter_text>MAD<jupyter_code>od.mad(df, "num", 1).table()<jupyter_output><empty_output><jupyter_text>Modified Zscore<jupyter_code>od.modified_z_score(df, "num", 1).table()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12553244800801805, 'fraction_numerical': 0.03006765221748935, 'mean_word_length': 3.604382929642445, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '493436', 'license': 'Apache-2.0', 'n_tokens_mistral': 1324, 'n_tokens_neox': 1177, 'n_words': 438, 'path': 'examples/new-api-outliers.ipynb', 'repo_name': 'pallav1991/Optimus', 'size': '3991'}