Skip to main content

XAI-DESReg: Dynamic Ensemble Selection for Regression tasks

Project description

XAI-DESReg

Installation

pip install xaidesreg

Example

Loading necessary libraries and dataset:

import pandas as pd 

# models 
from xgboost import XGBRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.linear_model import LinearRegression 

# metrics 
from sklearn.model_selection import train_test_split  
from sklearn.preprocessing import LabelEncoder
from ucimlrepo import fetch_ucirepo

# dataset
from ucimlrepo import fetch_ucirepo   

# InfoDESReg 
from xaidesreg import DES

Loading data:

abalone = fetch_ucirepo(id=1) 
X = abalone.data.features 
y = abalone.data.targets  

le = LabelEncoder() 
X['Sex'] = le.fit_transform(X['Sex'])  

Split the dataset into training, validation for DES (DSEL) and testing.

TEST_SIZE = 0.2 

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=TEST_SIZE, random_state=42) 
X_train, X_dsel, y_train, y_dsel = train_test_split(X_train, y_train, test_size=0.2, random_state=42)   
  1. Models pool generation
pool_models = [
    XGBRegressor(random_state=42), 
    RandomForestRegressor(random_state=42), 
    LinearRegression(), 
    KNeighborsRegressor(), 
]
  1. Train the models (pool):
for model in pool_models: 
    model.fit(X_train, y_train)  
  1. Usage of our library:
des = DES(pool_regressors=pool_models, 
          k=6, 
          knn_metric='minkowski', 
          metrics='mape', 
          threshold=0.1)

des.fit(X_dsel, y_dsel) 
  1. Testing
des.score(X_test, y_test) # based on MSE 
  1. Explainability
index = 47
X_test.iloc[index]

prediction, contribution_df, neighbors_df = des.predict_xai(X_test.iloc[[index]])

print(prediction) 

The content of contribution data frame:

Model Predictions Competence Weights
XGBRegressor 9.133 0.064 15.609
RandomForestRegressor 8.890 0.078 12.853
LinearRegression 8.913 0.090 11.141
KNeighborsRegressor 9.000 0.072 13.846

The samples in the region of competence (neighbors_df):

Sex Length Diameter Height Whole_weight Shucked_weight Viscera_weight Shell_weight Target
2 0.565 0.44 0.125 0.802 0.3595 0.1825 0.215 9
2 0.55 0.425 0.15 0.8315 0.411 0.1765 0.2165 10
2 0.56 0.415 0.13 0.7615 0.3695 0.17 0.1955 8
2 0.545 0.41 0.12 0.793 0.434 0.1405 0.19 9
2 0.56 0.415 0.145 0.852 0.43 0.1885 0.205 8
2 0.54 0.42 0.135 0.8075 0.3485 0.1795 0.235 11

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xaidesreg-0.1.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xaidesreg-0.1.1-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file xaidesreg-0.1.1.tar.gz.

File metadata

  • Download URL: xaidesreg-0.1.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.5

File hashes

Hashes for xaidesreg-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4a3378f7c9422056d8bc646eb829a8e24f5461d8891729085f57616e48547d1a
MD5 8973cff4ec4adede919a60472c016bf8
BLAKE2b-256 40beafa07611cb70802475856daa3d6b817261d5b36855da7fb43b80523996e8

See more details on using hashes here.

File details

Details for the file xaidesreg-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: xaidesreg-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.5

File hashes

Hashes for xaidesreg-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9445a341cfd673dbf51b5042fb3e52d4b9174d5d54970e8b9a0b1069d887a247
MD5 4643e9aa5a60f039aa3e77a2c8653e1d
BLAKE2b-256 06bb1c8931f67a4dee0eac8d4174964c5d4267a0840ee68fb385e1d50c2029f8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page