Skip to main content

Implementation of Cross-cluster Weighted Forests

Project description

Cross-Cluster Weighted Forests

A Python implementation of the Cross-Cluster Weighted Forests method for ensemble learning across multiple clusters or datasets. Full details can be found here: https://arxiv.org/abs/2105.07610

Installation

pip install PyCCWF

To install from source:

git clone https://github.com/m-ramchandran/PyCCWF.git
cd PyCCWF
pip install -e .

Quick Start

Running the Example

Currently, only regression is implemented. Classification will be included in a later release.

from PyCCWF import CrossClusterForest, sim_data, evaluate_model, plot_results, interpret_results

# Generate example data
data = sim_data(nclusters=15, ncoef=20, ntest=5,
                multimodal=True, n_samples=200, clustszind=2)
clusters_list = data['clusters_list']

# Split into train and test
train_clusters = clusters_list[:-5]
test_clusters = clusters_list[-5:]

# Initialize and fit model
model = CrossClusterForest(
  ntree=100,  # trees for cluster models
  merged_ntree=500,  # trees for merged model
  outcome_col='y',  # name of target variable
  k=10,  # number of clusters
  cluster_ind=1  # use k-means clustering
)

# Fit the model
model.fit(train_clusters, ncoef=20)

# Evaluate
eval_mod = evaluate_model(model, test_clusters)
predictions, improvements, performance = eval_mod['predictions'], eval_mod['improvements'], eval_mod['performance']
# Dataframe of predictions across all test clusters (concatenated):
print(predictions)
#Average percent change in RMSE from baseline Random Forest across all test clusters, for each weighting method
print(improvements)
#Dataframe of average RMSEs per method (column) per test cluster (rows)
print(performance)

#plot improvements:
plot_results(improvements)

#interpret results
interpret_results(improvements)

Using Your Own Data - Pre-clustered Version

If you have multiple pre-existing clusters (e.g., multiple studies or datasets): In this case, there is the option to use k-means to create new clusters from the concatenated inputted training clusters (cluster_ind = 1), or just use the pre-existing clusters (cluster_ind = 0)

# Prepare your clusters
import pandas as pd
clusters = [
    pd.DataFrame(...),  # Cluster 1
    pd.DataFrame(...),  # Cluster 2
    # ...
]

# Split into train and test
train_clusters = clusters[:-n_test]
test_clusters = clusters[-n_test:]

# Initialize and fit
model = CrossClusterForest(
    ntree=100,
    merged_ntree=500,
    outcome_col='target_column_name',
    k=10,
    cluster_ind=1
)

# Fit
model.fit(train_clusters, ncoef=n_features)

# Predict using different methods
predictions = model.predict(new_data, method='stack_ridge')

Using Your Own Data - Single Dataset Version

If you have a single dataset that you want to fit CCWF to:

from PyCCWF import SingleDatasetForest

# Initialize
model = SingleDatasetForest(
  ntree=100,
  merged_ntree=500,
  outcome_col='target_column_name',
  k=10  # number of clusters
)

# Fit (two options):
# Option 1: X includes the outcome column
model.fit(X)

# Option 2: Separate X and y
model.fit(X, y)

# Predict
predictions = model.predict(X_test, method='stack_ridge')

Methods Available

The package implements four key methods:

  • merged: Single random forest on merged data
  • unweighted: Simple average of cluster-specific models
  • stack_ridge: Ridge regression stacking
  • stack_lasso: Lasso regression stacking

Parameters

CrossClusterForest

  • ntree: Number of trees in cluster-specific models (default: 100)
  • merged_ntree: Number of trees in merged model (default: 500)
  • outcome_col: Name of target variable column
  • k: Number of clusters for k-means
  • cluster_ind: Whether to use k-means clustering (1) or not (0)

SingleDatasetForest

  • ntree: Number of trees in cluster-specific models (default: 100)
  • merged_ntree: Number of trees in merged model (default: 500)
  • outcome_col: Name of target variable column
  • k: Number of clusters for k-means

Additional functionality

See more comprehensive examples at https://github.com/m-ramchandran/PyCCWF/tree/main/examples

Citation

If you use this package, please cite: @article{ramchandran2021cross, title={Cross-cluster weighted forests}, author={Ramchandran, Maya and Mukherjee, Rajarshi and Parmigiani, Giovanni}, journal={arXiv preprint arXiv:2105.07610}, year={2024} }

License

MIT License

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

pyccwf-0.1.2.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

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

PyCCWF-0.1.2-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file pyccwf-0.1.2.tar.gz.

File metadata

  • Download URL: pyccwf-0.1.2.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.18

File hashes

Hashes for pyccwf-0.1.2.tar.gz
Algorithm Hash digest
SHA256 7287ba570757dcb1bb8302c4a91ce94d7c5f1d2193b1d53b3cea8d555f5c4527
MD5 0f2fd4fcf6b570374595adb889858b1d
BLAKE2b-256 5f7eeb327dd5a4b6e695cc447846b4f65614c128079ac226d864ef2a284d4f8b

See more details on using hashes here.

File details

Details for the file PyCCWF-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: PyCCWF-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 18.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.18

File hashes

Hashes for PyCCWF-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 dcbb94014b06a1e367578a1ae801623152532061aa1f9baa308f273f32ba12bc
MD5 4f6ce28788d576f9b73f3fe11c2a20d8
BLAKE2b-256 9893671e69b6495872432d86cef932a1a27aa5f3ed187f2a05fb88ea38b244b6

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