Outlier detection and removal using IQR | Feature engineering tutorial python # 4

codebasics · Beginner ·🛠️ AI Tools & Apps ·6y ago

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 Python Tutorial - 1. Install python on windows
Python Tutorial - 1. Install python on windows
codebasics
2 Python Tutorial - 2. Variables
Python Tutorial - 2. Variables
codebasics
3 Python Tutorial - 3. Numbers
Python Tutorial - 3. Numbers
codebasics
4 Python Tutorial - 4. Strings
Python Tutorial - 4. Strings
codebasics
5 Python Tutorial - 5. Lists
Python Tutorial - 5. Lists
codebasics
6 Python Tutorial - 6. Install PyCharm on Windows
Python Tutorial - 6. Install PyCharm on Windows
codebasics
7 PyCharm Tutorial - 7. Debug python code using PyCharm
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
8 Python Tutorial -  8. If Statement
Python Tutorial - 8. If Statement
codebasics
9 Python Tutorial - 9. For loop
Python Tutorial - 9. For loop
codebasics
10 Python Tutorial -  10. Functions
Python Tutorial - 10. Functions
codebasics
11 Python Tutorial - 11. Dictionaries and Tuples
Python Tutorial - 11. Dictionaries and Tuples
codebasics
12 Python Tutorial - 12. Modules
Python Tutorial - 12. Modules
codebasics
13 Python Tutorial - 13. Reading/Writing Files
Python Tutorial - 13. Reading/Writing Files
codebasics
14 How to install Julia on Windows
How to install Julia on Windows
codebasics
15 Python Tutorial - 14. Working With JSON
Python Tutorial - 14. Working With JSON
codebasics
16 Julia Tutorial - 1. Variables
Julia Tutorial - 1. Variables
codebasics
17 Julia Tutorial - 2. Numbers
Julia Tutorial - 2. Numbers
codebasics
18 Python Tutorial - 15. if __name__ == "__main__"
Python Tutorial - 15. if __name__ == "__main__"
codebasics
19 Julia Tutorial - Why Should I Learn Julia Programming Language
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
20 Python Tutorial  - 16. Exception Handling
Python Tutorial - 16. Exception Handling
codebasics
21 Julia Tutorial - 3. Complex and Rational Numbers
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
22 Julia Tutorial - 4. Strings
Julia Tutorial - 4. Strings
codebasics
23 Python Tutorial -  17. Class and Objects
Python Tutorial - 17. Class and Objects
codebasics
24 Julia Tutorial - 5. Functions
Julia Tutorial - 5. Functions
codebasics
25 Julia Tutorial - 6. If Statement and Ternary Operator
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
26 Julia Tutorial - 7. For While Loop
Julia Tutorial - 7. For While Loop
codebasics
27 Python Tutorial  - 18. Inheritance
Python Tutorial - 18. Inheritance
codebasics
28 Julia Tutorial - 8. begin and (;) Compound Expressions
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
29 Python Tutorial - 12.1 - Install Python Module (using pip)
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
30 Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
31 Julia Tutorial - 10. Exception Handling
Julia Tutorial - 10. Exception Handling
codebasics
32 Python Tutorial  - 19. Multiple Inheritance
Python Tutorial - 19. Multiple Inheritance
codebasics
33 Python Tutorial - 20. Raise Exception And Finally
Python Tutorial - 20. Raise Exception And Finally
codebasics
34 Python Tutorial - 21. Iterators
Python Tutorial - 21. Iterators
codebasics
35 Python Tutorial - 22. Generators
Python Tutorial - 22. Generators
codebasics
36 Python Tutorial - 23. List Set Dict Comprehensions
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
37 Python Tutorial - 24. Sets and Frozen Sets
Python Tutorial - 24. Sets and Frozen Sets
codebasics
38 Python Tutorial - 25. Command line argument processing using argparse
Python Tutorial - 25. Command line argument processing using argparse
codebasics
39 Debugging Tips - What is bug and debugging?
Debugging Tips - What is bug and debugging?
codebasics
40 Debugging Tips - Conditional Breakpoint
Debugging Tips - Conditional Breakpoint
codebasics
41 Debugging Tips - Watches and Call Stack
Debugging Tips - Watches and Call Stack
codebasics
42 Python Tutorial - 26. Multithreading - Introduction
Python Tutorial - 26. Multithreading - Introduction
codebasics
43 Git Tutorial 3:  How To Install Git
Git Tutorial 3: How To Install Git
codebasics
44 Git Tutorial 1: What is git / What is version control system?
Git Tutorial 1: What is git / What is version control system?
codebasics
45 Git Tutorial 2 : What is Github? | github tutorial
Git Tutorial 2 : What is Github? | github tutorial
codebasics
46 Git Tutorial 4: Basic Commands: add, commit, push
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
47 Git Tutorial 5: Undoing/Reverting/Resetting code changes
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
48 Git Tutorial 6: Branches (Create, Merge, Delete a branch)
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
49 Git Github Tutorial 10: What is Pull Request?
Git Github Tutorial 10: What is Pull Request?
codebasics
50 Git Tutorial 7: What is HEAD?
Git Tutorial 7: What is HEAD?
codebasics
51 Git Tutorial 9: Diff and Merge using meld
Git Tutorial 9: Diff and Merge using meld
codebasics
52 Difference between Multiprocessing and Multithreading
Difference between Multiprocessing and Multithreading
codebasics
53 Python Tutorial - 27. Multiprocessing Introduction
Python Tutorial - 27. Multiprocessing Introduction
codebasics
54 Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
55 Git Tutorial 8 - .gitignore file
Git Tutorial 8 - .gitignore file
codebasics
56 Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
57 Python Tutorial - 30. Multiprocessing Lock
Python Tutorial - 30. Multiprocessing Lock
codebasics
58 Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
59 What is code?
What is code?
codebasics
60 Python unit testing - pytest introduction
Python unit testing - pytest introduction
codebasics

Related Reads

📰
Maximize Google Workspace AI Power: Safeguard Data and Boost Performance in 2026
Maximize Google Workspace AI power by safeguarding data and boosting performance to stay ahead in 2026
Dev.to AI
📰
What is Gemini Spark, and what can it actually do for you?
Gemini Spark integrates AI into daily Google apps to enhance productivity, learn how to leverage it
TechCabal
📰
How I use python to save hours every week
Learn how to use Python to automate tasks and save hours every week, with practical steps for beginners and experienced users alike.
Dev.to AI
📰
What Are AI Software Solutions and How Can They Transform Your Business?
Discover how AI software solutions can transform your business by automating tasks, enhancing decision-making, and driving innovation
Dev.to · upwork floating infotech

Chapters (3)

What is percentile and IQR
4:15 Remove outliers using IQR
6:55 Exercise
Up next
Implement Microsoft Entra ID Auth with Delegated Graph API Calls in ASP.NET Core Web App Razor Pages
Dewiride Technologies
Watch →