import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pylab import rcParams
%matplotlib inline
rcParams['figure.figsize'] = 5,4
fname = 'c:/py_data/iris.data.csv'
df = pd.read_csv(filepath_or_buffer=fname, header=None, sep=',')
df.columns=['Sepal Length', 'Sepal Width', 'Petal Length', 'Petal Width', 'Species']
x = df.iloc[:,0:4].values
y = df.iloc[:,4].values
df[:5]
| Sepal Length | Sepal Width | Petal Length | Petal Width | Species | |
|---|---|---|---|---|---|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
# Sepal Width > 4 or < 2.05 potential outlier
df.boxplot(return_type='dict')
plt.plot()
[]
sepal_width = x[:,1]
iris_outliers = (sepal_width > 4)
df[iris_outliers]
| Sepal Length | Sepal Width | Petal Length | Petal Width | Species | |
|---|---|---|---|---|---|
| 15 | 5.7 | 4.4 | 1.5 | 0.4 | setosa |
| 32 | 5.2 | 4.1 | 1.5 | 0.1 | setosa |
| 33 | 5.5 | 4.2 | 1.4 | 0.2 | setosa |
sepal_width = x[:,1]
iris_outliers = (sepal_width < 2.05)
df[iris_outliers]
| Sepal Length | Sepal Width | Petal Length | Petal Width | Species | |
|---|---|---|---|---|---|
| 60 | 5.0 | 2.0 | 3.5 | 1.0 | versicolor |
pd.options.display.float_format = '{:1f}'.format
x_df = pd.DataFrame(x)
print(x_df.describe())
0 1 2 3 count 150.000000 150.000000 150.000000 150.000000 mean 5.843333 3.057333 3.758000 1.199333 std 0.828066 0.435866 1.765298 0.762238 min 4.300000 2.000000 1.000000 0.100000 25% 5.100000 2.800000 1.600000 0.300000 50% 5.800000 3.000000 4.350000 1.300000 75% 6.400000 3.300000 5.100000 1.800000 max 7.900000 4.400000 6.900000 2.500000
# For Sepal Width: IQR = 3.3 - 2.8 = 0.5 | 0.5 * 1.5 = 0.75
# 2.8 - 0.75 = 2.05 (lower limit) | 3.3 + 0.75 = 4.05 (Upper limit)