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, random_state=42)
# Initialize atom for regression tasks
atom = ATOMRegressor(X, "Rings", verbose=2, 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... >>> Keeping 6 components. >>> 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]:
Index(['pca0', 'pca1', 'pca2', 'pca3', 'pca4', 'pca5', 'Rings'], dtype='object')
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 | 12 | 8 | 19 | auto | 0.0161 | -8.0987 | -8.0987 | 0.094s | 0.109s | | Initial point 2 | absolute_.. | best | 11 | 3 | 12 | None | 0.0 | -6.7018 | -6.7018 | 0.250s | 0.359s | | Iteration 3 | absolute_.. | best | 11 | 4 | 1 | None | 0.0086 | -6.8759 | -6.7018 | 0.313s | 0.781s | | Iteration 4 | absolute_.. | random | 12 | 3 | 18 | 0.9 | 0.0036 | -6.9111 | -6.7018 | 0.109s | 1.141s | | Iteration 5 | friedman_.. | random | 3 | 12 | 19 | auto | 0.001 | -6.4336 | -6.4336 | 0.047s | 1.287s | Bayesian Optimization --------------------------- Best call --> Iteration 5 Best parameters --> {'criterion': 'friedman_mse', 'splitter': 'random', 'max_depth': 3, 'min_samples_split': 12, 'min_samples_leaf': 19, 'max_features': 'auto', 'ccp_alpha': 0.001} Best evaluation --> neg_mean_squared_error: -6.4336 Time elapsed: 1.428s Fit --------------------------------------------- Train evaluation --> neg_mean_squared_error: -7.4877 Test evaluation --> neg_mean_squared_error: -7.5847 Time elapsed: 0.000s Bootstrap --------------------------------------- Evaluation --> neg_mean_squared_error: -7.4566 ± 0.1197 Time elapsed: 0.047s ------------------------------------------------- Total time: 1.475s 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.938s | 0.938s | | Initial point 2 | 131 | 0.5 | 0.5 | False | False | -5.4837 | -5.4837 | 0.609s | 1.547s | | Iteration 3 | 302 | 0.5 | 0.5 | True | True | -6.0919 | -5.4837 | 1.094s | 2.735s | | Iteration 4 | 191 | 0.5 | 0.5 | False | False | -5.3972 | -5.3972 | 0.844s | 3.688s | | Iteration 5 | 217 | 0.5 | 0.5 | False | False | -4.9339 | -4.9339 | 0.969s | 4.766s | 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: 4.875s Fit --------------------------------------------- Train evaluation --> neg_mean_squared_error: -1.3974 Test evaluation --> neg_mean_squared_error: -5.7349 Time elapsed: 1.278s Bootstrap --------------------------------------- Evaluation --> neg_mean_squared_error: -5.9024 ± 0.058 Time elapsed: 5.357s ------------------------------------------------- Total time: 11.513s Running BO for Extra-Trees... | call | n_estimators | criterion | max_depth | min_samples_split | min_samples_leaf | max_features | bootstrap | max_samples | ccp_alpha | neg_mean_squared_error | best_neg_mean_squared_error | time | total_time | | ---------------- | ------------ | ------------- | --------- | ----------------- | ---------------- | ------------ | --------- | ----------- | --------- | ---------------------- | --------------------------- | ------- | ---------- | | Initial point 1 | 112 | absolute_er.. | 3 | 9 | 7 | 0.6 | True | 0.6 | 0.0117 | -8.95 | -8.95 | 0.698s | 0.704s | | Initial point 2 | 369 | absolute_er.. | None | 3 | 12 | None | True | 0.9 | 0.0216 | -6.6286 | -6.6286 | 6.882s | 7.586s | | Iteration 3 | 285 | squared_error | None | 5 | 3 | None | True | 0.9 | 0.0095 | -6.0219 | -6.0219 | 0.664s | 8.376s | | Iteration 4 | 133 | squared_error | 13 | 11 | 1 | 0.6 | False | --- | 0.0004 | -5.1558 | -5.1558 | 0.250s | 8.735s | | Iteration 5 | 75 | squared_error | 3 | 11 | 2 | 0.8 | False | --- | 0.0029 | -5.9369 | -5.1558 | 0.094s | 8.938s | Bayesian Optimization --------------------------- Best call --> Iteration 4 Best parameters --> {'n_estimators': 133, 'criterion': 'squared_error', 'max_depth': 13, 'min_samples_split': 11, 'min_samples_leaf': 1, 'max_features': 0.6, 'bootstrap': False, 'ccp_alpha': 0.0004} Best evaluation --> neg_mean_squared_error: -5.1558 Time elapsed: 9.079s Fit --------------------------------------------- Train evaluation --> neg_mean_squared_error: -3.638 Test evaluation --> neg_mean_squared_error: -5.5804 Time elapsed: 0.285s Bootstrap --------------------------------------- Evaluation --> neg_mean_squared_error: -5.6727 ± 0.1028 Time elapsed: 1.172s ------------------------------------------------- Total time: 10.535s Final results ==================== >> Duration: 23.523s ------------------------------------- Decision Tree --> neg_mean_squared_error: -7.4566 ± 0.1197 ~ Bagging --> neg_mean_squared_error: -5.9024 ± 0.058 ~ Extra-Trees --> neg_mean_squared_error: -5.6727 ± 0.1028 ~ !
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))