Python Tutorial : Model complexity and overfitting
Key Takeaways
This video tutorial covers model complexity and overfitting in Python using scikit-learn, focusing on tuning complexity parameters and using cross-validation for model selection.
Full Transcript
congratulations are deciding to continue with this course deciding how complex a model should be is one of the most critical skills a data scientist must have and is the subject of this lesson often classifiers have extra parameters that control their flexibility or complexity for example inspecting the documentation of the random forest classifier you will notice an entry for max underscore depth this stands for maximum depth a random forest classifier combines the predictions from a large number of decision trees using deeper trees makes the classifier more complex let's start by fitting a classifier with depth two and one with depth four to the credit scoring data set from the previous lesson how would a typical tree from each classifier look like we can access individual trees using the private estimators underscore attribute trees of depth to contain at most two nested decision rules whereas depth 4 produces much deeper rules although these trees come from the same classifier family and data they look very different tuning a complexity parameter is treated in the same way as model selection you need to split your data into training and test fit several classifiers of different depths to the training data and pick the one with the best test performance you can also keep a separate holdout dataset in order to get a fresh final estimate of the accuracy of the winning classifier an alternative approach is cross-validation which splits the data into several chunks and repeats the training test step by picking a different chunk per round to use us test data shown here in yellow while using the remaining data for training shown in blue accuracy is averaged over all rounds making this technique more stable cross-validation is implemented as Crossville score in the psychic learn model selection module the function takes as input a classifier instance and the full data x and y which it then proceeds to split several times three times by default the result are three estimates of accuracy one for each run that can be averaged using mean from numpy to easily optimize a hyper parameter like tree depth using cross-validation you can use the function grid search CV which takes as input a dictionary of parameters and values to try out and a classifier instance the resulting object is fitted to the entire dataset and stores the best-performing values in an attribute called underscore best underscore params let's now review the accuracy of our random forest as the depth ranges from 1 to 10 accuracy on the same data used for training known as in sample accuracy is shown here in blue as the trees become deeper the classifier becomes so complex that it can now almost memorize the training data this way it can reach a hundred percent in sample accuracy performance using cross-validation also known as out-of-sample accuracy is much lower than in sample performance and a much more realistic estimate of future performance the most important observation is that out-of-sample performance actually drops for depths greater than 10 due to overfitting trying too hard to memorize the training data leads to worse performance on the test data this also happens in real life if you memorize the answers to past exam questions you will only do well on the exam if the same questions appear in exactly the same wording you're already wiser than the average data scientist because you know that complex models are not always better than simple ones the exercises that follow lets you develop more intuition about this inside
Original Description
Want to learn more? Take the full course at https://learn.datacamp.com/courses/designing-machine-learning-workflows-in-python at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
Congratulations on deciding to continue with this course! Deciding how complex a model should be is one of the most critical skills a data scientist must-have, and is the subject of this lesson.
Often classifiers have extra parameters that control their flexibility, or complexity. For example, inspecting the documentation of the random forest classifier, you will notice an entry for max-underscore-depth. This stands for maximum depth. A random forest classifier combines the predictions from a large number of decision trees. Using deeper trees makes the classifier more complex.
Let's start by fitting a classifier with depth 2
and one with depth 4 to the credit scoring dataset from the previous lesson.
How would a typical tree from each classifier look like? We can access individual trees using the private estimators-underscore attribute.
Trees of depth 2 contain at most two nested decision rules,
whereas depth 4 produces much deeper rules. Although these trees come from the same classifier family and data, they look very different.
Tuning a complexity parameter is treated in the same way as model selection. You need to split your data into training and test, fit several classifiers of different depths to the training data, and pick the one with the best test performance. You can also keep a separate, hold-out dataset in order to get a fresh, final estimate of the accuracy of the winning classifier.
An alternative approach is cross-validation, which splits the data into several chunks, and repeats the training-test step by picking a different chunk per round to use as test data, shown here in yellow, while using the remaining data for training, shown in blue. Accuracy is averaged over all rounds, making this technique m
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from DataCamp · DataCamp · 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
SQL Server Tutorial: Date manipulation
DataCamp
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
R Tutorial: Adding aesthetics to represent a variable
DataCamp
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
Python Tutorial: Preparation for modeling
DataCamp
Python Tutorial: Machine Learning modeling steps
DataCamp
R Tutorial: The prior model
DataCamp
R Tutorial: Data & the likelihood
DataCamp
R Tutorial: The posterior model
DataCamp
R Tutorial: An Introduction to plotly
DataCamp
R Tutorial: Plotting a single variable
DataCamp
R Tutorial: Bivariate graphics
DataCamp
Python Tutorial: Customer Segmentation in Python
DataCamp
Python Tutorial: Time cohorts
DataCamp
Python Tutorial: Calculate cohort metrics
DataCamp
Python Tutorial: Cohort analysis visualization
DataCamp
R Tutorial: Building Dashboards with flexdashboard
DataCamp
R Tutorial: Anatomy of a flexdashboard
DataCamp
R Tutorial: Layout basics
DataCamp
R Tutorial: Advanced layouts
DataCamp
Python Tutorial: Time Series Analysis in Python
DataCamp
Python Tutorial: Correlation of Two Time Series
DataCamp
Python Tutorial: Simple Linear Regressions
DataCamp
Python Tutorial: Autocorrelation
DataCamp
R Tutorial: The gapminder dataset
DataCamp
R Tutorial: The filter verb
DataCamp
R Tutorial: The arrange verb
DataCamp
R Tutorial: The mutate verb
DataCamp
R Tutorial: What is cluster analysis?
DataCamp
R Tutorial: Distance between two observations
DataCamp
R Tutorial: The importance of scale
DataCamp
R Tutorial: Measuring distance for categorical data
DataCamp
Python Tutorial: Plotting multiple graphs
DataCamp
Python Tutorial: Customizing axes
DataCamp
Python Tutorial: Legends, annotations, & styles
DataCamp
Python Tutorial: Introduction to iterators
DataCamp
Python Tutorial: Playing with iterators
DataCamp
Python Tutorial: Using iterators to load large files into memory
DataCamp
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
SQL Tutorial: Tables: At the core of every database
DataCamp
SQL Tutorial: Update your database as the structure changes
DataCamp
Python Tutorial: Classification-Tree Learning
DataCamp
Python Tutorial: Decision-Tree for Classification
DataCamp
Python Tutorial: Decision-Tree for Regression
DataCamp
Python Tutorial: Census Subject Tables
DataCamp
Python Tutorial: Census Geography
DataCamp
Python Tutorial: Using the Census API
DataCamp
R Tutorial: A/B Testing in R
DataCamp
R Tutorial: Baseline Conversion Rates
DataCamp
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
R Tutorial: Introduction to qualitative data
DataCamp
R Tutorial: Understanding your qualitative variables
DataCamp
R Tutorial: Making Better Plots
DataCamp
SQL Tutorial: OLTP and OLAP
DataCamp
SQL Tutorial: Storing data
DataCamp
SQL Tutorial: Database design
DataCamp
Python Tutorial: Introduction to spaCy
DataCamp
Python Tutorial: Statistical Models
DataCamp
Python Tutorial: Rule-based Matching
DataCamp
More on: ML Pipelines
View skill →
🎓
Tutor Explanation
DeepCamp AI