How to find outliers in Python?

How to find outliers in Python?

This recipe helps you find outliers in Python
In [2]:
## How to find outliers in Python 
def Kickstarter_Example_30():
    print()
    print(format('How to find outliers in Python', '*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # Load libraries
    from sklearn.covariance import EllipticEnvelope
    from sklearn.datasets import make_blobs
    import matplotlib.pyplot as plt

    # Create simulated data
    X, _ = make_blobs(n_samples = 100,
                      n_features = 20,
                      centers = 7,
                      cluster_std = 1.1,
                      shuffle = True,
                      random_state = 42)

    # Detect Outliers
    # Create detector
    outlier_detector = EllipticEnvelope(contamination=.1)

    # Fit detector
    outlier_detector.fit(X)

    # Predict outliers
    print(); print(X)
    print(); print(outlier_detector.predict(X))
    plt.scatter(X[:,0], X[:,1])

    # Show the scatterplot
    plt.show()

Kickstarter_Example_30()
**************************How to find outliers in Python**************************

[[ 4.93252797  7.68541287 -3.97876821 ...  4.52684633 -3.24863123
   9.41974416]
 [-9.3234536   4.59276437 -4.39779468 ... -7.09597087  8.20227193
   2.26134033]
 [-8.7338198   3.08658417 -3.49905765 ... -6.82385124  8.775862
   1.38825176]
 ...
 [-2.83969517 -6.07980264  6.47763993 ... -9.36607752 -2.57352093
  -9.39410402]
 [-2.1671993  10.63717797  5.58330442 ...  0.50898027 -1.25365592
  -5.02572796]
 [ 7.21074034  9.28156979 -3.54240715 ...  3.89782083 -3.2259812
  11.03335594]]

[-1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1 -1  1  1  1 -1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1 -1 -1 -1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1 -1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1 -1  1  1 -1  1  1  1  1  1
  1  1  1 -1]


Stuck at work?
Can't find the recipe you are looking for. Let us know and we will find an expert to create the recipe for you. Click here
Companies using this Recipe
1 developer from HvH
1 developer from MSI Services
1 developer from Vodafone
1 developer from Altimetrik
1 developer from Infosys
1 developer from MudraCircle
1 developer from YASH Technologies
1 developer from ANAC
1 developer from KPMG
1 developer from Scotiabank