Regression¶
This example shows how to use ATOM to apply PCA on the data and run a regression pipeline.
Download the abalone dataset from https://archive.ics.uci.edu/ml/datasets/Abalone. The goal of this dataset is to predict the rings (age) of abalone shells from physical measurements.
Load the data¶
In [1]:
Copied!
# Import packages
import pandas as pd
from atom import ATOMRegressor
# Import packages
import pandas as pd
from atom import ATOMRegressor
In [2]:
Copied!
# Load the data
X = pd.read_csv("./datasets/abalone.csv")
# Let's have a look
X.head()
# Load the data
X = pd.read_csv("./datasets/abalone.csv")
# Let's have a look
X.head()
Out[2]:
Sex | Length | Diameter | Height | Whole weight | Shucked weight | Viscera weight | Shell weight | Rings | |
---|---|---|---|---|---|---|---|---|---|
0 | M | 0.455 | 0.365 | 0.095 | 0.5140 | 0.2245 | 0.1010 | 0.150 | 15 |
1 | M | 0.350 | 0.265 | 0.090 | 0.2255 | 0.0995 | 0.0485 | 0.070 | 7 |
2 | F | 0.530 | 0.420 | 0.135 | 0.6770 | 0.2565 | 0.1415 | 0.210 | 9 |
3 | M | 0.440 | 0.365 | 0.125 | 0.5160 | 0.2155 | 0.1140 | 0.155 | 10 |
4 | I | 0.330 | 0.255 | 0.080 | 0.2050 | 0.0895 | 0.0395 | 0.055 | 7 |
In [3]:
Copied!
# Initialize atom for regression tasks
atom = ATOMRegressor(X, "Rings", verbose=2, warnings=False, random_state=42)
# Initialize atom for regression tasks
atom = ATOMRegressor(X, "Rings", verbose=2, warnings=False, random_state=42)
<< ================== ATOM ================== >> Algorithm task: regression. Dataset stats ==================== >> Shape: (4177, 9) Memory: 509.72 kB Scaled: False Categorical features: 1 (12.5%) Outlier values: 196 (0.7%) ------------------------------------- Train set size: 3342 Test set size: 835
In [4]:
Copied!
# Encode the categorical features
atom.encode()
# Encode the categorical features
atom.encode()
Fitting Encoder... Encoding categorical columns... --> OneHot-encoding feature Sex. Contains 3 classes.
In [5]:
Copied!
# Plot the dataset's correlation matrix
atom.plot_correlation()
# Plot the dataset's correlation matrix
atom.plot_correlation()
In [6]:
Copied!
# Apply PCA for dimensionality reduction
atom.feature_selection(strategy="pca", n_features=6)
# Apply PCA for dimensionality reduction
atom.feature_selection(strategy="pca", n_features=6)
Fitting FeatureSelector... Performing feature selection ... --> Applying Principal Component Analysis... >>> Scaling features... >>> Explained variance ratio: 0.977
In [7]:
Copied!
# Note that the fetaures are automatically renamed to Component 1, 2, etc...
atom.columns
# Note that the fetaures are automatically renamed to Component 1, 2, etc...
atom.columns
Out[7]:
['component 1', 'component 2', 'component 3', 'component 4', 'component 5', 'component 6', 'Rings']
In [8]:
Copied!
# Use the plotting methods to see the retained variance ratio
atom.plot_pca()
# Use the plotting methods to see the retained variance ratio
atom.plot_pca()
In [9]:
Copied!
atom.plot_components(figsize=(8, 6))
atom.plot_components(figsize=(8, 6))
Run the pipeline¶
In [10]:
Copied!
atom.run(
models=["Tree", "Bag", "ET"],
metric="MSE",
n_calls=5,
n_initial_points=2,
bo_params={"base_estimator": "GBRT"},
n_bootstrap=5,
)
atom.run(
models=["Tree", "Bag", "ET"],
metric="MSE",
n_calls=5,
n_initial_points=2,
bo_params={"base_estimator": "GBRT"},
n_bootstrap=5,
)
Training ========================= >> Models: Tree, Bag, ET Metric: neg_mean_squared_error Running BO for Decision Tree... | call | criterion | splitter | max_depth | min_samples_split | min_samples_leaf | max_features | ccp_alpha | neg_mean_squared_error | best_neg_mean_squared_error | time | total_time | | ---------------- | ----------- | -------- | --------- | ----------------- | ---------------- | ------------ | --------- | ---------------------- | --------------------------- | ------- | ---------- | | Initial point 1 | absolute_.. | random | 7 | 8 | 19 | auto | 0.0161 | -8.1076 | -8.1076 | 0.108s | 0.114s | | Initial point 2 | absolute_.. | best | 6 | 3 | 12 | None | 0.0 | -6.4884 | -6.4884 | 0.244s | 0.628s | | Iteration 3 | absolute_.. | best | 6 | 4 | 1 | None | 0.0086 | -6.8759 | -6.4884 | 0.261s | 1.089s | | Iteration 4 | poisson | best | 7 | 3 | 17 | auto | 0.0039 | -9.2175 | -6.4884 | 0.076s | 1.359s | | Iteration 5 | poisson | best | 6 | 7 | 14 | None | 0.0004 | -8.9588 | -6.4884 | 0.074s | 1.627s | Bayesian Optimization --------------------------- Best call --> Initial point 2 Best parameters --> {'criterion': 'absolute_error', 'splitter': 'best', 'max_depth': 6, 'min_samples_split': 3, 'min_samples_leaf': 12, 'max_features': None, 'ccp_alpha': 0.0} Best evaluation --> neg_mean_squared_error: -6.4884 Time elapsed: 1.830s Fit --------------------------------------------- Train evaluation --> neg_mean_squared_error: -5.8483 Test evaluation --> neg_mean_squared_error: -7.021 Time elapsed: 0.277s Bootstrap --------------------------------------- Evaluation --> neg_mean_squared_error: -7.1589 ± 0.2396 Time elapsed: 1.181s ------------------------------------------------- Total time: 3.289s Running BO for Bagging... | call | n_estimators | max_samples | max_features | bootstrap | bootstrap_features | neg_mean_squared_error | best_neg_mean_squared_error | time | total_time | | ---------------- | ------------ | ----------- | ------------ | --------- | ------------------ | ---------------------- | --------------------------- | ------- | ---------- | | Initial point 1 | 112 | 0.9 | 0.6 | False | False | -6.5592 | -6.5592 | 0.978s | 0.984s | | Initial point 2 | 131 | 0.5 | 0.5 | False | False | -5.4837 | -5.4837 | 0.687s | 1.757s | | Iteration 3 | 302 | 0.5 | 0.5 | True | True | -6.0919 | -5.4837 | 1.236s | 3.176s | | Iteration 4 | 191 | 0.5 | 0.5 | False | False | -5.3972 | -5.3972 | 0.989s | 4.541s | | Iteration 5 | 217 | 0.5 | 0.5 | False | False | -4.9339 | -4.9339 | 1.136s | 5.882s | Bayesian Optimization --------------------------- Best call --> Iteration 5 Best parameters --> {'n_estimators': 217, 'max_samples': 0.5, 'max_features': 0.5, 'bootstrap': False, 'bootstrap_features': False} Best evaluation --> neg_mean_squared_error: -4.9339 Time elapsed: 6.093s Fit --------------------------------------------- Train evaluation --> neg_mean_squared_error: -1.3974 Test evaluation --> neg_mean_squared_error: -5.7349 Time elapsed: 1.417s Bootstrap --------------------------------------- Evaluation --> neg_mean_squared_error: -5.9024 ± 0.058 Time elapsed: 6.126s ------------------------------------------------- Total time: 13.638s Running BO for Extra-Trees... | call | n_estimators | criterion | max_depth | min_samples_split | min_samples_leaf | max_features | bootstrap | ccp_alpha | max_samples | neg_mean_squared_error | best_neg_mean_squared_error | time | total_time | | ---------------- | ------------ | ------------- | --------- | ----------------- | ---------------- | ------------ | --------- | --------- | ----------- | ---------------------- | --------------------------- | ------- | ---------- | | Initial point 1 | 112 | absolute_er.. | 1 | 9 | 7 | 0.6 | True | 0.0161 | 0.6 | -10.9838 | -10.9838 | 0.492s | 0.499s | | Initial point 2 | 369 | absolute_er.. | None | 3 | 12 | None | True | 0.0347 | 0.7 | -6.8941 | -6.8941 | 5.217s | 5.810s | | Iteration 3 | 383 | absolute_er.. | None | 6 | 20 | None | True | 0.0271 | 0.8 | -7.5893 | -6.8941 | 5.447s | 11.471s | | Iteration 4 | 221 | absolute_er.. | 1 | 4 | 1 | log2 | True | 0.0339 | 0.7 | -9.5 | -6.8941 | 0.929s | 12.611s | | Iteration 5 | 470 | squared_error | None | 3 | 13 | 0.6 | True | 0.0162 | 0.6 | -5.8203 | -5.8203 | 0.525s | 13.345s | Bayesian Optimization --------------------------- Best call --> Iteration 5 Best parameters --> {'n_estimators': 470, 'criterion': 'squared_error', 'max_depth': None, 'min_samples_split': 3, 'min_samples_leaf': 13, 'max_features': 0.6, 'bootstrap': True, 'ccp_alpha': 0.0162, 'max_samples': 0.6} Best evaluation --> neg_mean_squared_error: -5.8203 Time elapsed: 13.568s Fit --------------------------------------------- Train evaluation --> neg_mean_squared_error: -6.3561 Test evaluation --> neg_mean_squared_error: -6.613 Time elapsed: 0.565s Bootstrap --------------------------------------- Evaluation --> neg_mean_squared_error: -6.56 ± 0.0946 Time elapsed: 2.500s ------------------------------------------------- Total time: 16.634s Final results ==================== >> Duration: 33.563s ------------------------------------- Decision Tree --> neg_mean_squared_error: -7.1589 ± 0.2396 ~ Bagging --> neg_mean_squared_error: -5.9024 ± 0.058 ~ ! Extra-Trees --> neg_mean_squared_error: -6.56 ± 0.0946 ~
Analyze the results¶
In [11]:
Copied!
# Use the errors or residuals plots to check the model performances
atom.plot_residuals()
# Use the errors or residuals plots to check the model performances
atom.plot_residuals()
In [12]:
Copied!
atom.plot_errors()
atom.plot_errors()
In [13]:
Copied!
# Analyze the relation between the target response and the features
atom.n_jobs = 8 # The method can be slow...
atom.ET.plot_partial_dependence(columns=(0, (2, 3)), figsize=(12, 5))
# Analyze the relation between the target response and the features
atom.n_jobs = 8 # The method can be slow...
atom.ET.plot_partial_dependence(columns=(0, (2, 3)), figsize=(12, 5))