+1 vote

I want to set the outlier values as 'NaN' values. Here is the code I am using right now. Can someone explain me ?

```import numpy as np, matplotlib.pyplot as plt
data = np.random.rand(1000)+5.0
plt.plot(data)
plt.xlabel('observation number')
plt.ylabel('recorded value')
plt.show()```
Aug 24, 2018 in Python
edited Aug 24, 2018 769 views

## 3 answers to this question.

Here's an implementation for the N-dimensional case (from some code for a paper here: https://github.com/joferkington/oost_paper_code/blob/master/utilities.py):

• 58,100 points

There are a huge number of ways to test for outliers, and you should give some thought to how you classify them. Ideally, you should use a-priori information (e.g. "anything above/below this value is unrealistic because...")

• 4,170 points

code from http://eurekastatistics.com/using-the-median-absolute-deviation-to-find-outliers  This uses the L1 distance instead of L2 distance, and has support for asymmetric distributions.

```def doubleMADsfromMedian(y,thresh=3.5):
# warning: this function does not check for NAs
# nor does it address issues when
# more than 50% of your data have identical values
m = np.median(y)
abs_dev = np.abs(y - m)
modified_z_score = 0.6745 * abs_dev / y_mad
modified_z_score[y == m] = 0
return modified_z_score > thresh```
• 4,710 points

reshown Aug 24, 2018 by Priyaj

## What are the different types of data types one can use in Python?

Python provides an array of built-in constants, ...READ MORE

## What are the arguments of sorted() function in Python?

Sorted() sorts any sequence (list, tuple) and ...READ MORE

## What are the various ways to manipulate a string in Python?

I have a string in Python. I ...READ MORE

## what is the practical use of polymorphism in Python?

Polymorphism is the ability to present the ...READ MORE

## How do I obtain the index list in a NumPy Array of all the NaN values present using Python?

Hi, it is pretty simple, to be ...READ MORE

## How to create Pandas series from numpy array?

Hi. Refer to the below command: import pandas ...READ MORE

## Pandas series with custom index

You can do it by specifying index. ...READ MORE