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) Scaled: False Categorical features: 1 (12.5%) Outlier values: 192 (0.6%) ------------------------------------- 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... >>> Total explained variance: 0.976
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 | absolut.. | random | 7 | 8 | 19 | None | 0.016 | -7.725 | -7.725 | 0.106s | 0.112s | | Initial point 2 | absolut.. | best | 6 | 3 | 12 | 0.9 | 0.0 | -8.3285 | -7.725 | 0.223s | 0.527s | | Iteration 3 | absolut.. | random | 7 | 14 | 16 | None | 0.028 | -6.8909 | -6.8909 | 0.104s | 0.808s | | Iteration 4 | friedma.. | best | 6 | 19 | 16 | 0.9 | 0.031 | -6.6381 | -6.6381 | 0.068s | 1.055s | | Iteration 5 | friedma.. | random | None | 19 | 13 | 0.7 | 0.029 | -6.8263 | -6.6381 | 0.064s | 1.294s | Results for Decision Tree: Bayesian Optimization --------------------------- Best call --> Iteration 4 Best parameters --> {'criterion': 'friedman_mse', 'splitter': 'best', 'max_depth': 6, 'min_samples_split': 19, 'min_samples_leaf': 16, 'max_features': 0.9, 'ccp_alpha': 0.031} Best evaluation --> neg_mean_squared_error: -6.6381 Time elapsed: 1.471s Fit --------------------------------------------- Train evaluation --> neg_mean_squared_error: -6.1632 Test evaluation --> neg_mean_squared_error: -5.4816 Time elapsed: 0.015s Bootstrap --------------------------------------- Evaluation --> neg_mean_squared_error: -5.7348 ± 0.2036 Time elapsed: 0.062s ------------------------------------------------- Total time: 1.549s Running BO for Bagging Regressor... | 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.007 | -6.007 | 0.897s | 0.901s | | Initial point 2 | 131 | 0.5 | 0.5 | False | False | -7.047 | -6.007 | 0.616s | 1.578s | | Iteration 3 | 50 | 0.9 | 0.6 | False | True | -5.3357 | -5.3357 | 0.449s | 2.189s | | Iteration 4 | 74 | 0.5 | 0.5 | False | True | -6.1811 | -5.3357 | 0.370s | 2.728s | | Iteration 5 | 18 | 0.8 | 0.6 | True | False | -6.3648 | -5.3357 | 0.145s | 3.035s | Results for Bagging Regressor: Bayesian Optimization --------------------------- Best call --> Iteration 3 Best parameters --> {'n_estimators': 50, 'max_samples': 0.9, 'max_features': 0.6, 'bootstrap': False, 'bootstrap_features': True} Best evaluation --> neg_mean_squared_error: -5.3357 Time elapsed: 3.207s Fit --------------------------------------------- Train evaluation --> neg_mean_squared_error: -0.0867 Test evaluation --> neg_mean_squared_error: -4.9533 Time elapsed: 0.510s Bootstrap --------------------------------------- Evaluation --> neg_mean_squared_error: -5.1436 ± 0.0702 Time elapsed: 2.113s ------------------------------------------------- Total time: 5.833s 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 | absolut.. | 1 | 9 | 7 | 0.6 | True | 0.016 | 0.6 | -10.2144 | -10.2144 | 0.389s | 0.395s | | Initial point 2 | 369 | absolut.. | None | 3 | 12 | 0.9 | True | 0.035 | 0.8 | -9.4213 | -9.4213 | 4.798s | 5.259s | | Iteration 3 | 385 | squared.. | None | 6 | 18 | 0.9 | False | 0.02 | --- | -5.5509 | -5.5509 | 0.533s | 5.979s | | Iteration 4 | 425 | squared.. | 1 | 20 | 19 | 0.7 | False | 0.016 | --- | -9.1701 | -5.5509 | 0.329s | 6.497s | | Iteration 5 | 445 | squared.. | None | 7 | 20 | 0.6 | False | 0.004 | --- | -6.9472 | -5.5509 | 0.440s | 7.228s | Results for Extra-Trees: Bayesian Optimization --------------------------- Best call --> Iteration 3 Best parameters --> {'n_estimators': 385, 'criterion': 'squared_error', 'max_depth': None, 'min_samples_split': 6, 'min_samples_leaf': 18, 'max_features': 0.9, 'bootstrap': False, 'ccp_alpha': 0.02} Best evaluation --> neg_mean_squared_error: -5.5509 Time elapsed: 7.428s Fit --------------------------------------------- Train evaluation --> neg_mean_squared_error: -6.1021 Test evaluation --> neg_mean_squared_error: -5.0002 Time elapsed: 0.620s Bootstrap --------------------------------------- Evaluation --> neg_mean_squared_error: -4.9113 ± 0.0278 Time elapsed: 3.026s ------------------------------------------------- Total time: 11.074s Final results ==================== >> Duration: 18.457s ------------------------------------- Decision Tree --> neg_mean_squared_error: -5.7348 ± 0.2036 ~ Bagging Regressor --> neg_mean_squared_error: -5.1436 ± 0.0702 ~ Extra-Trees --> neg_mean_squared_error: -4.9113 ± 0.0278 ~ !
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 [14]:
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))