Example: Train sizing¶
This example shows how to asses a model's performance based on the size of the training set.
The data used is a variation on the Australian weather dataset from Kaggle. You can download it from here. The goal of this dataset is to predict whether or not it will rain tomorrow training a binary classifier on target RainTomorrow
.
Load the data¶
In [1]:
Copied!
# Import packages
import pandas as pd
from atom import ATOMClassifier
# Import packages
import pandas as pd
from atom import ATOMClassifier
In [2]:
Copied!
# Load the data
X = pd.read_csv("docs_source/examples/datasets/weatherAUS.csv")
# Let's have a look
X.head()
# Load the data
X = pd.read_csv("docs_source/examples/datasets/weatherAUS.csv")
# Let's have a look
X.head()
Out[2]:
Location | MinTemp | MaxTemp | Rainfall | Evaporation | Sunshine | WindGustDir | WindGustSpeed | WindDir9am | WindDir3pm | ... | Humidity9am | Humidity3pm | Pressure9am | Pressure3pm | Cloud9am | Cloud3pm | Temp9am | Temp3pm | RainToday | RainTomorrow | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | MelbourneAirport | 18.0 | 26.9 | 21.4 | 7.0 | 8.9 | SSE | 41.0 | W | SSE | ... | 95.0 | 54.0 | 1019.5 | 1017.0 | 8.0 | 5.0 | 18.5 | 26.0 | Yes | 0 |
1 | Adelaide | 17.2 | 23.4 | 0.0 | NaN | NaN | S | 41.0 | S | WSW | ... | 59.0 | 36.0 | 1015.7 | 1015.7 | NaN | NaN | 17.7 | 21.9 | No | 0 |
2 | Cairns | 18.6 | 24.6 | 7.4 | 3.0 | 6.1 | SSE | 54.0 | SSE | SE | ... | 78.0 | 57.0 | 1018.7 | 1016.6 | 3.0 | 3.0 | 20.8 | 24.1 | Yes | 0 |
3 | Portland | 13.6 | 16.8 | 4.2 | 1.2 | 0.0 | ESE | 39.0 | ESE | ESE | ... | 76.0 | 74.0 | 1021.4 | 1020.5 | 7.0 | 8.0 | 15.6 | 16.0 | Yes | 1 |
4 | Walpole | 16.4 | 19.9 | 0.0 | NaN | NaN | SE | 44.0 | SE | SE | ... | 78.0 | 70.0 | 1019.4 | 1018.9 | NaN | NaN | 17.4 | 18.1 | No | 0 |
5 rows × 22 columns
Run the pipeline¶
In [3]:
Copied!
# Initialize atom and prepare the data
atom = ATOMClassifier(X, verbose=2, random_state=1)
atom.clean()
atom.impute(strat_num="median", strat_cat="most_frequent", max_nan_rows=0.8)
atom.encode()
# Initialize atom and prepare the data
atom = ATOMClassifier(X, verbose=2, random_state=1)
atom.clean()
atom.impute(strat_num="median", strat_cat="most_frequent", max_nan_rows=0.8)
atom.encode()
<< ================== ATOM ================== >> Configuration ==================== >> Algorithm task: Binary classification. Dataset stats ==================== >> Shape: (142193, 22) Train set size: 113755 Test set size: 28438 ------------------------------------- Memory: 25.03 MB Scaled: False Missing values: 316559 (10.1%) Categorical features: 5 (23.8%) Duplicates: 45 (0.0%) Fitting Cleaner... Cleaning the data... Fitting Imputer... Imputing missing values... --> Dropping 161 samples for containing more than 16 missing values. --> Imputing 481 missing values with median (12.0) in column MinTemp. --> Imputing 265 missing values with median (22.6) in column MaxTemp. --> Imputing 1354 missing values with median (0.0) in column Rainfall. --> Imputing 60682 missing values with median (4.8) in column Evaporation. --> Imputing 67659 missing values with median (8.4) in column Sunshine. --> Imputing 9187 missing values with most_frequent (W) in column WindGustDir. --> Imputing 9127 missing values with median (39.0) in column WindGustSpeed. --> Imputing 9852 missing values with most_frequent (N) in column WindDir9am. --> Imputing 3617 missing values with most_frequent (SE) in column WindDir3pm. --> Imputing 1187 missing values with median (13.0) in column WindSpeed9am. --> Imputing 2469 missing values with median (19.0) in column WindSpeed3pm. --> Imputing 1613 missing values with median (70.0) in column Humidity9am. --> Imputing 3449 missing values with median (52.0) in column Humidity3pm. --> Imputing 13863 missing values with median (1017.6) in column Pressure9am. --> Imputing 13830 missing values with median (1015.2) in column Pressure3pm. --> Imputing 53496 missing values with median (5.0) in column Cloud9am. --> Imputing 56933 missing values with median (5.0) in column Cloud3pm. --> Imputing 743 missing values with median (16.7) in column Temp9am. --> Imputing 2565 missing values with median (21.1) in column Temp3pm. --> Imputing 1354 missing values with most_frequent (No) in column RainToday. Fitting Encoder... Encoding categorical columns... --> Target-encoding feature Location. Contains 49 classes. --> Target-encoding feature WindGustDir. Contains 16 classes. --> Target-encoding feature WindDir9am. Contains 16 classes. --> Target-encoding feature WindDir3pm. Contains 16 classes. --> Ordinal-encoding feature RainToday. Contains 2 classes.
In [4]:
Copied!
# Analyze the impact of the training set's size on a LR model
atom.train_sizing("LR", train_sizes=10, n_bootstrap=5)
# Analyze the impact of the training set's size on a LR model
atom.train_sizing("LR", train_sizes=10, n_bootstrap=5)
Training ========================= >> Metric: f1 Run: 0 =========================== >> Models: LR01 Size of training set: 11362 (10%) Size of test set: 28408 Results for LogisticRegression: Fit --------------------------------------------- Train evaluation --> f1: 0.563 Test evaluation --> f1: 0.5854 Time elapsed: 1.201s Bootstrap --------------------------------------- Evaluation --> f1: 0.5849 ± 0.002 Time elapsed: 0.909s ------------------------------------------------- Time: 2.110s Final results ==================== >> Total time: 2.133s ------------------------------------- LogisticRegression --> f1: 0.5849 ± 0.002 Run: 1 =========================== >> Models: LR02 Size of training set: 22724 (20%) Size of test set: 28408 Results for LogisticRegression: Fit --------------------------------------------- Train evaluation --> f1: 0.582 Test evaluation --> f1: 0.5873 Time elapsed: 1.239s Bootstrap --------------------------------------- Evaluation --> f1: 0.5852 ± 0.0021 Time elapsed: 1.076s ------------------------------------------------- Time: 2.315s Final results ==================== >> Total time: 2.342s ------------------------------------- LogisticRegression --> f1: 0.5852 ± 0.0021 Run: 2 =========================== >> Models: LR03 Size of training set: 34087 (30%) Size of test set: 28408 Results for LogisticRegression: Fit --------------------------------------------- Train evaluation --> f1: 0.581 Test evaluation --> f1: 0.5851 Time elapsed: 1.426s Bootstrap --------------------------------------- Evaluation --> f1: 0.5861 ± 0.0009 Time elapsed: 1.457s ------------------------------------------------- Time: 2.883s Final results ==================== >> Total time: 2.913s ------------------------------------- LogisticRegression --> f1: 0.5861 ± 0.0009 Run: 3 =========================== >> Models: LR04 Size of training set: 45449 (40%) Size of test set: 28408 Results for LogisticRegression: Fit --------------------------------------------- Train evaluation --> f1: 0.5827 Test evaluation --> f1: 0.5869 Time elapsed: 1.501s Bootstrap --------------------------------------- Evaluation --> f1: 0.5863 ± 0.0017 Time elapsed: 1.585s ------------------------------------------------- Time: 3.086s Final results ==================== >> Total time: 3.118s ------------------------------------- LogisticRegression --> f1: 0.5863 ± 0.0017 Run: 4 =========================== >> Models: LR05 Size of training set: 56812 (50%) Size of test set: 28408 Results for LogisticRegression: Fit --------------------------------------------- Train evaluation --> f1: 0.5819 Test evaluation --> f1: 0.585 Time elapsed: 1.673s Bootstrap --------------------------------------- Evaluation --> f1: 0.5854 ± 0.0017 Time elapsed: 1.635s ------------------------------------------------- Time: 3.308s Final results ==================== >> Total time: 3.347s ------------------------------------- LogisticRegression --> f1: 0.5854 ± 0.0017 Run: 5 =========================== >> Models: LR06 Size of training set: 68174 (60%) Size of test set: 28408 Results for LogisticRegression: Fit --------------------------------------------- Train evaluation --> f1: 0.5832 Test evaluation --> f1: 0.5848 Time elapsed: 1.865s Bootstrap --------------------------------------- Evaluation --> f1: 0.5849 ± 0.0018 Time elapsed: 2.218s ------------------------------------------------- Time: 4.083s Final results ==================== >> Total time: 4.141s ------------------------------------- LogisticRegression --> f1: 0.5849 ± 0.0018 Run: 6 =========================== >> Models: LR07 Size of training set: 79536 (70%) Size of test set: 28408 Results for LogisticRegression: Fit --------------------------------------------- Train evaluation --> f1: 0.5873 Test evaluation --> f1: 0.5849 Time elapsed: 2.302s Bootstrap --------------------------------------- Evaluation --> f1: 0.5852 ± 0.0012 Time elapsed: 2.969s ------------------------------------------------- Time: 5.271s Final results ==================== >> Total time: 5.364s ------------------------------------- LogisticRegression --> f1: 0.5852 ± 0.0012 Run: 7 =========================== >> Models: LR08 Size of training set: 90899 (80%) Size of test set: 28408 Results for LogisticRegression: Fit --------------------------------------------- Train evaluation --> f1: 0.589 Test evaluation --> f1: 0.5837 Time elapsed: 5.182s Bootstrap --------------------------------------- Evaluation --> f1: 0.5853 ± 0.0026 Time elapsed: 4.662s ------------------------------------------------- Time: 9.844s Final results ==================== >> Total time: 9.934s ------------------------------------- LogisticRegression --> f1: 0.5853 ± 0.0026 Run: 8 =========================== >> Models: LR09 Size of training set: 102261 (90%) Size of test set: 28408 Results for LogisticRegression: Fit --------------------------------------------- Train evaluation --> f1: 0.5871 Test evaluation --> f1: 0.5845 Time elapsed: 7.434s Bootstrap --------------------------------------- Evaluation --> f1: 0.5846 ± 0.002 Time elapsed: 7.263s ------------------------------------------------- Time: 14.697s Final results ==================== >> Total time: 14.891s ------------------------------------- LogisticRegression --> f1: 0.5846 ± 0.002 Run: 9 =========================== >> Models: LR10 Size of training set: 113624 (100%) Size of test set: 28408 Results for LogisticRegression: Fit --------------------------------------------- Train evaluation --> f1: 0.5858 Test evaluation --> f1: 0.5848 Time elapsed: 7.974s Bootstrap --------------------------------------- Evaluation --> f1: 0.5848 ± 0.0007 Time elapsed: 6.985s ------------------------------------------------- Time: 14.959s Final results ==================== >> Total time: 15.082s ------------------------------------- LogisticRegression --> f1: 0.5848 ± 0.0007
Analyze the results¶
In [5]:
Copied!
# The results are now multi-index, where frac is the fraction
# of the training set used to fit the model. The model names
# end with the fraction as well (without the dot)
atom.results
# The results are now multi-index, where frac is the fraction
# of the training set used to fit the model. The model names
# end with the fraction as well (without the dot)
atom.results
Out[5]:
f1_train | f1_test | time_fit | f1_bootstrap | time_bootstrap | time | ||
---|---|---|---|---|---|---|---|
frac | model | ||||||
0.1 | LR01 | 0.5621 | 0.5848 | 1.200989 | 0.584922 | 0.909424 | 2.110413 |
0.2 | LR02 | 0.5832 | 0.5846 | 1.238803 | 0.585234 | 1.076237 | 2.315040 |
0.3 | LR03 | 0.5800 | 0.5852 | 1.426356 | 0.586118 | 1.456787 | 2.883143 |
0.4 | LR04 | 0.5845 | 0.5857 | 1.501177 | 0.586348 | 1.585152 | 3.086329 |
0.5 | LR05 | 0.5833 | 0.5865 | 1.672944 | 0.585384 | 1.635141 | 3.308085 |
0.6 | LR06 | 0.5831 | 0.5832 | 1.865011 | 0.584891 | 2.217760 | 4.082771 |
0.7 | LR07 | 0.5878 | 0.5858 | 2.301655 | 0.585235 | 2.969447 | 5.271102 |
0.8 | LR08 | 0.5916 | 0.5886 | 5.181574 | 0.585269 | 4.662252 | 9.843826 |
0.9 | LR09 | 0.5856 | 0.5833 | 7.434335 | 0.584633 | 7.262542 | 14.696877 |
1.0 | LR10 | 0.5858 | 0.5848 | 7.974012 | 0.584836 | 6.985089 | 14.959101 |
In [6]:
Copied!
# Every model can be accessed through its name
atom.lr05.plot_shap_waterfall(show=6)
# Every model can be accessed through its name
atom.lr05.plot_shap_waterfall(show=6)
In [7]:
Copied!
# Plot the train sizing's results
atom.plot_learning_curve()
# Plot the train sizing's results
atom.plot_learning_curve()