Multiclass classification¶
This example shows how to compare the performance of three models on a multiclass classification task.
Import the wine dataset from sklearn.datasets. This is a small and easy to train dataset whose goal is to predict wines into three groups (which cultivator it's from) using features based on the results of chemical analysis.
Load the data¶
In [1]:
                Copied!
                
                
            # Import packages
from sklearn.datasets import load_wine
from atom import ATOMClassifier
# Import packages
from sklearn.datasets import load_wine
from atom import ATOMClassifier
    
        In [2]:
                Copied!
                
                
            # Load data
X, y = load_wine(return_X_y=True, as_frame=True)
# Let's have a look
X.head()
# Load data
X, y = load_wine(return_X_y=True, as_frame=True)
# Let's have a look
X.head()
    
        Out[2]:
| alcohol | malic_acid | ash | alcalinity_of_ash | magnesium | total_phenols | flavanoids | nonflavanoid_phenols | proanthocyanins | color_intensity | hue | od280/od315_of_diluted_wines | proline | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 14.23 | 1.71 | 2.43 | 15.6 | 127.0 | 2.80 | 3.06 | 0.28 | 2.29 | 5.64 | 1.04 | 3.92 | 1065.0 | 
| 1 | 13.20 | 1.78 | 2.14 | 11.2 | 100.0 | 2.65 | 2.76 | 0.26 | 1.28 | 4.38 | 1.05 | 3.40 | 1050.0 | 
| 2 | 13.16 | 2.36 | 2.67 | 18.6 | 101.0 | 2.80 | 3.24 | 0.30 | 2.81 | 5.68 | 1.03 | 3.17 | 1185.0 | 
| 3 | 14.37 | 1.95 | 2.50 | 16.8 | 113.0 | 3.85 | 3.49 | 0.24 | 2.18 | 7.80 | 0.86 | 3.45 | 1480.0 | 
| 4 | 13.24 | 2.59 | 2.87 | 21.0 | 118.0 | 2.80 | 2.69 | 0.39 | 1.82 | 4.32 | 1.04 | 2.93 | 735.0 | 
Run the pipeline¶
In [3]:
                Copied!
                
                
            atom = ATOMClassifier(X, y, n_jobs=-1, warnings=False, verbose=2, random_state=1)
# Fit the pipeline with the selected models
atom.run(
    models=["LR","LDA", "RF"],
    metric="roc_auc_ovr",
    n_calls=4,
    n_initial_points=3,
    bo_params={"base_estimator": "rf", "max_time": 100},
    n_bootstrap=5,
)
atom = ATOMClassifier(X, y, n_jobs=-1, warnings=False, verbose=2, random_state=1)
# Fit the pipeline with the selected models
atom.run(
    models=["LR","LDA", "RF"],
    metric="roc_auc_ovr",
    n_calls=4,
    n_initial_points=3,
    bo_params={"base_estimator": "rf", "max_time": 100},
    n_bootstrap=5,
)
    
        << ================== ATOM ================== >>
Algorithm task: multiclass classification.
Parallel processing with 16 cores.
Dataset stats ==================== >>
Shape: (178, 14)
Memory: 19.35 kB
Scaled: False
Outlier values: 9 (0.4%)
-------------------------------------
Train set size: 143
Test set size: 35
-------------------------------------
|   |    dataset |      train |       test |
| - | ---------- | ---------- | ---------- |
| 0 |   59 (1.2) |   47 (1.2) |   12 (1.3) |
| 1 |   71 (1.5) |   57 (1.5) |   14 (1.6) |
| 2 |   48 (1.0) |   39 (1.0) |    9 (1.0) |
Training ========================= >>
Models: LR, LDA, RF
Metric: roc_auc_ovr
Running BO for Logistic Regression...
| call             | penalty |       C |  solver | max_iter | l1_ratio | roc_auc_ovr | best_roc_auc_ovr |    time | total_time |
| ---------------- | ------- | ------- | ------- | -------- | -------- | ----------- | ---------------- | ------- | ---------- |
| Initial point 1  |    none |     --- |   lbfgs |      745 |      --- |      0.9745 |           0.9745 |  2.036s |     2.042s |
| Initial point 2  |      l2 |  0.0085 | newto.. |      490 |      --- |      0.9963 |           0.9963 |  0.474s |     2.797s |
| Initial point 3  |      l2 |  0.0368 | libli.. |      352 |      --- |      0.9814 |           0.9963 |  0.008s |     2.905s |
| Iteration 4      |      l2 |  5.6426 | newto.. |      378 |      --- |         1.0 |              1.0 |  0.495s |     3.716s |
Bayesian Optimization ---------------------------
Best call --> Iteration 4
Best parameters --> {'penalty': 'l2', 'C': 5.6426, 'solver': 'newton-cg', 'max_iter': 378}
Best evaluation --> roc_auc_ovr: 1.0
Time elapsed: 4.056s
Fit ---------------------------------------------
Train evaluation --> roc_auc_ovr: 1.0
Test evaluation --> roc_auc_ovr: 1.0
Time elapsed: 0.429s
Bootstrap ---------------------------------------
Evaluation --> roc_auc_ovr: 0.9998 ± 0.0005
Time elapsed: 1.983s
-------------------------------------------------
Total time: 6.469s
Running BO for Linear Discriminant Analysis...
| call             |  solver | shrinkage | roc_auc_ovr | best_roc_auc_ovr |    time | total_time |
| ---------------- | ------- | --------- | ----------- | ---------------- | ------- | ---------- |
| Initial point 1  |   eigen |       1.0 |      0.9112 |           0.9112 |  0.010s |     0.013s |
| Initial point 2  |     svd |       --- |         1.0 |              1.0 |  0.007s |     0.115s |
| Initial point 3  |     svd |       --- |         1.0 |              1.0 |  0.000s |     0.212s |
| Iteration 4      |    lsqr |       0.8 |      0.9442 |              1.0 |  0.009s |     0.513s |
Bayesian Optimization ---------------------------
Best call --> Initial point 2
Best parameters --> {'solver': 'svd'}
Best evaluation --> roc_auc_ovr: 1.0
Time elapsed: 0.805s
Fit ---------------------------------------------
Train evaluation --> roc_auc_ovr: 1.0
Test evaluation --> roc_auc_ovr: 1.0
Time elapsed: 0.012s
Bootstrap ---------------------------------------
Evaluation --> roc_auc_ovr: 1.0 ± 0.0
Time elapsed: 0.036s
-------------------------------------------------
Total time: 0.854s
Running BO for Random Forest...
| call             | n_estimators | criterion | max_depth | min_samples_split | min_samples_leaf | max_features | bootstrap | max_samples | ccp_alpha | roc_auc_ovr | best_roc_auc_ovr |    time | total_time |
| ---------------- | ------------ | --------- | --------- | ----------------- | ---------------- | ------------ | --------- | ----------- | --------- | ----------- | ---------------- | ------- | ---------- |
| Initial point 1  |          245 |   entropy |      None |                13 |                6 |         sqrt |      True |         0.5 |    0.0121 |         1.0 |              1.0 |  0.299s |     0.306s |
| Initial point 2  |          400 |   entropy |        14 |                 7 |               19 |          0.6 |      True |         0.5 |    0.0187 |      0.9898 |              1.0 |  0.502s |     0.916s |
| Initial point 3  |           78 |      gini |         9 |                 2 |               14 |          0.8 |     False |         --- |    0.0181 |      0.9623 |              1.0 |  0.095s |     1.113s |
| Iteration 4      |          394 |   entropy |         6 |                19 |               14 |          0.8 |     False |         --- |    0.0252 |      0.9981 |              1.0 |  0.551s |     2.513s |
Bayesian Optimization ---------------------------
Best call --> Initial point 1
Best parameters --> {'n_estimators': 245, 'criterion': 'entropy', 'max_depth': None, 'min_samples_split': 13, 'min_samples_leaf': 6, 'max_features': 'sqrt', 'bootstrap': True, 'max_samples': 0.5, 'ccp_alpha': 0.0121}
Best evaluation --> roc_auc_ovr: 1.0
Time elapsed: 3.131s
Fit ---------------------------------------------
Train evaluation --> roc_auc_ovr: 1.0
Test evaluation --> roc_auc_ovr: 0.9988
Time elapsed: 0.420s
Bootstrap ---------------------------------------
Evaluation --> roc_auc_ovr: 0.9988 ± 0.0007
Time elapsed: 1.698s
-------------------------------------------------
Total time: 5.251s
Final results ==================== >>
Duration: 12.574s
-------------------------------------
Logistic Regression          --> roc_auc_ovr: 0.9998 ± 0.0005
Linear Discriminant Analysis --> roc_auc_ovr: 1.0 ± 0.0 !
Random Forest                --> roc_auc_ovr: 0.9988 ± 0.0007
Analyze the results¶
In [4]:
                Copied!
                
                
            atom.results
atom.results
    
        Out[4]:
| metric_bo | time_bo | metric_train | metric_test | time_fit | mean_bootstrap | std_bootstrap | time_bootstrap | time | |
|---|---|---|---|---|---|---|---|---|---|
| LR | 1.0 | 4.056s | 1.0 | 1.000000 | 0.429s | 0.999773 | 0.000454 | 1.983s | 6.469s | 
| LDA | 1.0 | 0.805s | 1.0 | 1.000000 | 0.012s | 1.000000 | 0.000000 | 0.036s | 0.854s | 
| RF | 1.0 | 3.131s | 1.0 | 0.998792 | 0.420s | 0.998822 | 0.000741 | 1.698s | 5.251s | 
In [5]:
                Copied!
                
                
            # Show the score for some different metrics
atom.evaluate(["precision_macro", "recall_macro", "jaccard_weighted"])
# Show the score for some different metrics
atom.evaluate(["precision_macro", "recall_macro", "jaccard_weighted"])
    
        Out[5]:
| precision_macro | recall_macro | jaccard_weighted | |
|---|---|---|---|
| LR | 0.966667 | 0.97619 | 0.945714 | 
| LDA | 1.000000 | 1.00000 | 1.000000 | 
| RF | 0.974359 | 0.97619 | 0.945055 | 
In [6]:
                Copied!
                
                
            # Some plots allow you to choose the target class to look at
atom.rf.plot_probabilities(dataset="train", target=2)
# Some plots allow you to choose the target class to look at
atom.rf.plot_probabilities(dataset="train", target=2)
    
        In [7]:
                Copied!
                
                
            atom.lda.heatmap_plot(target=2, show=10, figsize=(8, 5))
atom.lda.heatmap_plot(target=2, show=10, figsize=(8, 5))