The dataset is about consists of Placement data of MBA students of a B-school. It includes the following features:
You can download the dataset from kaggle https://www.kaggle.com/benroshan/factors-affecting-campus-placement?select=Placement_Data_Full_Class.csv
import pandas as pd
import numpy as np
df = pd.read_csv("Placement.csv")
df.head()
df.isna().sum()
datadrop = df.dropna()

outliers =[]
def outliers_zscore(data):
max_dev = 3
mean = np.mean(data)
std = np.std(data)
for i in data:
Z_score = (i-mean)/std
if np.abs(Z_score)>max_dev:
outliers.append(i)
return outliers
z_score_outlier = outliers_zscore(datadrop['salary'])
z_score_outlier
Let's say we have a set of data and we want to determine what are the possible outliers. Before understanding the method of detecting outliers, we need to understand what Outliers are and how are they important or not important in different cases.
An outlier is basically an extremely high or extremely low value in our dataset.
Ex.,
Suppose the normal salary of a person is \$5k per month. But one month he received a salary of $10k. This can be treated as an outlier which might be because of the annual bonus he would have got.
Another example we could think of is in case of fraudelent credit card transaction. Suppose a person credic card transactions have always been within the range of \$150-\\$200, however there were couple of transactions which were more than \$2000. This is clearly an outlier, which could be regarded as a fraudelent transaction.
Quantiles: These are basically measures/segments in which the data can be shown. These include percentiles, quartiles, deciles and so on. We calculate these data after arranging them in ascending order.
i. Percentile: Percentile is defined as the percentage of data that is present below the value of the original value. For example if someone scores a percentile of 90 then the number of students below that are 90 percent of the students.
ii. Decile: Decile is defined as the 10th percentile, which means the data points that are present below decile is 10 percent of the whole data set.
iii. Quartile: Quartile is defined as 1/4th of the data or the 25th percentile and can be categorized into 3 quartiles: 1st Quartile consists of 25 percent of the data, 2nd Quartile is 50 percent of the data and 3rd quartile is 75 percent of the data. We also call the 2nd quartile as the median or 50th percentile or 5th decile.
iv. Interquartile Range(IQR): Interquartile range is defined as the difference between the third quartile and the first quartile and can be effective in figuring out the outliers in the dataset.
Our data contains outliers if it is outside Q1(first quartile) or Q3(3rd Quartile). The 3rd quartile is the Upper Bound and the First Quartile is the Lower Bound
Upper Bound = Q3(third quartile) + 1.5(IQR) lower than Q1(first quartile) - 1.5(IQR)
Now that we have understood what all these means, let’s pull it back together and calculate it using an example:
Ex: 71, 70, 90, 70, 70, 60, 70, 72, 72, 320, 71, 69
Rearrange the numbers in order:
60, 69, 70, 70, 70, 70, 71, 71, 72, 72, 90, 320
Calculate the median(Q2) : (70+71)/2 = 70.5
Split the range into 2 halves with the first half less than 70.5 and the 2nd half greater than 70.5
1st half = 60, 69, 70, 70, 70, 70
2nd half = 71, 71, 72, 72, 90, 320
Q1 = calculate the median of the 1st half = (70 + 70)/2 = 70
Q3 = calculate the median of the 2nd half = (72 + 72)/2 =72
The interquartile range is : Q3-Q1 = 72 - 70 = 2
Upper Bound = Q3(third quartile) + 1.5(IQR) = 72 + 1.5(2) = 75
lower Bound = Q1(first quartile) - 1.5(IQR) = 70 - 1.5(2) = 67
Anything outside the Upper Bound and Lower Bound is considered an Outlier. So in our example the outliers would be 90, 320
quartile1,quartile3=np.percentile(datadrop['salary'],[25,75])
print(quartile1,quartile3)
interquartile_range = quartile3-quartile1
print(interquartile_range)
lower_bound = quartile1 -(1.5*interquartile_range)
upper_bound = quartile3 +(1.5*interquartile_range)
print(lower_bound,upper_bound)
import matplotlib.pyplot as plt
plt.boxplot(datadrop['salary'])
plt.show()