Outlier detection and removal using IQR | Feature engineering tutorial python # 4
Skills:
Data Literacy80%
Key Takeaways
This video teaches outlier detection and removal using Interquartile Range (IQR) technique in Python
Full Transcript
in this tutorial we will be removing outliers using IQR technique as usual we'll go over some theory first then we'll write code in Python pandas to remove the outliers and in the end we'll have an interesting exercise for you to solve here I have a heights database now visual examination you can easily tell that there are some outliers here these heights are in feet of course you cannot have a person with 40 feet height hence these all of these are outliers you know anything more than 77 point 1 similarly this is an Adel data set hence one point to height is clearly these two heights are outlier now how can you detect the same thing using an IQR technique in order to understand IQR you first need to understand percentile now what is percentile well percentile is basically for example here I have this five point three file which is 25th percentile it is also called q1 25th percentile value means in your data set 25% of samples are below this value total samples here are twenty twenty-five percent of twenty is five and five samples here are having value less than five point three five you see 5.0 5.1 all these values are less than five point three five hence this is 25th percentile similarly 75th percentile will be a value which is six point twenty seven so 75% of samples are having value less than six point two seven I hope you get it so that's why six point two seven is called 75th percentile your maximum value will be called hundredth percentile because of course all the values are below that value similarly minimum value will be called zero percentile because that doesn't have any value less than that particular value right so this is a quick summary of percentile now once you know percentile you can easy calculate IQR by dipping q3 and q1 here q3 which is 75th percentile is 6.27 - 5.35 is q1 point nine to five is your IQR value now to remove outliers you need to come up with lower and upper limit your lower limit will be q1 which is 25th percentile - one point five IQR now IQR if you think about it is just if it is this distance you know it is this much distance so you take that distance multiplied by 1 point 5 so you add 1/2 into it and then you extend that from here so from 5 point 35 minus 1 point 5 and you get this value 3.96 similarly upper limit will be q3 plus 1.5 IQR you know they just came up with this number you can just remember this that you have to do 1.5 IQR plus q3 to get your upper limit once you know the upper and lower limit you can exclude audit all data points which has value less than a lower limit so 3.96 see these two are excluded there they are termed as an outlier and any value a bow upper limit which is 7 point 66 can also be excluded so you can clearly see that these two three values are now excluded so just by looking at this data set visually you can kind of say that IQR is kind of working okay in this case because most of the people will have heights in this range adult having height two point three feet is very very it's probably not not possible and adult having a height of 14 feet is also not possible the person doesn't need stairs if he has this much height and just go to your first or second floor all right let's get into coding now i have that height CSV here and I'm gonna load that into my notebook and it will look something like this so here is my data frame and now from this data frame I will quickly do DF no subscribe describe to see the statistics so you can see that when this is 25th percentile value 75th percentile value maximum is 4 T minimum is 1 point T I always use describe function because it can tell you those quick stats ok now let's calculate Q 1 and Q 2 now by the way you can get Q 1 and Q 2 from here as well but the other way of getting Q 1 and Q 2 is you take your height column and you say quantile quantile is a function so one time and if you want 25th percentile this is what you need to do and if you need 75th percentile this is how you do it and I will quickly print q1 and q3 so these are my percentage value IQR will be very simple formula guys this IQR is really a very very simple technique IQR value is this and now I can easily find my lower and upper limit using this formula so I'm doing q1 minus 1 point 5 QR q3 plus box 1.5 QR these are my upper and lower limit and once you have upper and lower limit in a data frame you can easily detect these outliers by doing this you can set height less than lower limit or height greater than upper limit those are my outliers and you can clearly see see all these are coming as outliers these are the same ones that we saw in our presentation the cells marked in the yellow color all right now if you want to remove these outliers from your original data frame what you can do is something like this so I'm using and condition I want to make sure the remaining samples are within this limit of lower limit and upper limit okay and the resultant data frame that you get you can maybe store that into a new data frame call it DFM outlier and this is how it looks alright so that's all I had now the most interesting part of this tutorial which is an exercise believe me I'm not giving you any hard assignment these are very simple assignments but it will consolidate your understanding once you practice it on your own what you're going to do is use the heights and weights data set it is very similar to what I use but it has height and weight to parameters I got this file from this giggling you can download it or if you don't want to download it you can just go to my github page and you will find it in the exercise folder and then you need to plot histograms first for weight and then for height and then use IQR to detect outliers based on weight and then do the same thing based on height now I have a solution link but I know you you all are very good students since the students so you're not going to click on this link until you have practiced this on your own all right thank you very much for watching I will see you in next feature engineering tutorial
Original Description
IQR is another technique that one can use to detect and remove outliers. The formula for IQR is very simple. IQR = Q3-Q1. Where Q3 is 75th percentile and Q1 is 25th percentile. Once you have IQR you can find upper and lower limit by removing this formula,
lower_limit = Q1-1.5*IQR
upper_limit = Q3 +1.5*IQR
Anything less than a lower limit or above the upper limit is considered outlier. We will use python pandas to remove outliers on a sample dataset and in the end, as usual, I have an interesting exercise for you to practice
Code & Exercise: https://github.com/codebasics/py/blob/master/ML/FeatureEngineering/3_outlier_IQR/3_outliers_iqr.ipynb
Link for kaggle dataset: https://www.kaggle.com/mustafaali96/weight-height
Topics
00:00 What is percentile and IQR
04:15 Remove outliers using IQR
06:55 Exercise
Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses.
Website: https://codebasics.io/
Facebook: https://www.facebook.com/codebasicshub
Twitter: https://twitter.com/codebasicshub
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from codebasics · codebasics · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Python Tutorial - 1. Install python on windows
codebasics
Python Tutorial - 2. Variables
codebasics
Python Tutorial - 3. Numbers
codebasics
Python Tutorial - 4. Strings
codebasics
Python Tutorial - 5. Lists
codebasics
Python Tutorial - 6. Install PyCharm on Windows
codebasics
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
Python Tutorial - 8. If Statement
codebasics
Python Tutorial - 9. For loop
codebasics
Python Tutorial - 10. Functions
codebasics
Python Tutorial - 11. Dictionaries and Tuples
codebasics
Python Tutorial - 12. Modules
codebasics
Python Tutorial - 13. Reading/Writing Files
codebasics
How to install Julia on Windows
codebasics
Python Tutorial - 14. Working With JSON
codebasics
Julia Tutorial - 1. Variables
codebasics
Julia Tutorial - 2. Numbers
codebasics
Python Tutorial - 15. if __name__ == "__main__"
codebasics
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
Python Tutorial - 16. Exception Handling
codebasics
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
Julia Tutorial - 4. Strings
codebasics
Python Tutorial - 17. Class and Objects
codebasics
Julia Tutorial - 5. Functions
codebasics
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
Julia Tutorial - 7. For While Loop
codebasics
Python Tutorial - 18. Inheritance
codebasics
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
Julia Tutorial - 10. Exception Handling
codebasics
Python Tutorial - 19. Multiple Inheritance
codebasics
Python Tutorial - 20. Raise Exception And Finally
codebasics
Python Tutorial - 21. Iterators
codebasics
Python Tutorial - 22. Generators
codebasics
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
Python Tutorial - 24. Sets and Frozen Sets
codebasics
Python Tutorial - 25. Command line argument processing using argparse
codebasics
Debugging Tips - What is bug and debugging?
codebasics
Debugging Tips - Conditional Breakpoint
codebasics
Debugging Tips - Watches and Call Stack
codebasics
Python Tutorial - 26. Multithreading - Introduction
codebasics
Git Tutorial 3: How To Install Git
codebasics
Git Tutorial 1: What is git / What is version control system?
codebasics
Git Tutorial 2 : What is Github? | github tutorial
codebasics
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
Git Github Tutorial 10: What is Pull Request?
codebasics
Git Tutorial 7: What is HEAD?
codebasics
Git Tutorial 9: Diff and Merge using meld
codebasics
Difference between Multiprocessing and Multithreading
codebasics
Python Tutorial - 27. Multiprocessing Introduction
codebasics
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
Git Tutorial 8 - .gitignore file
codebasics
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
Python Tutorial - 30. Multiprocessing Lock
codebasics
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
What is code?
codebasics
Python unit testing - pytest introduction
codebasics
More on: Data Literacy
View skill →Related Reads
📰
📰
📰
📰
Maximize Google Workspace AI Power: Safeguard Data and Boost Performance in 2026
Dev.to AI
What is Gemini Spark, and what can it actually do for you?
TechCabal
How I use python to save hours every week
Dev.to AI
What Are AI Software Solutions and How Can They Transform Your Business?
Dev.to · upwork floating infotech
Chapters (3)
What is percentile and IQR
4:15
Remove outliers using IQR
6:55
Exercise
🎓
Tutor Explanation
DeepCamp AI