Weighted Subspace Random Forest Classifier
Project description
wsrf
A Python implementation of the Weighted Subspace Random Forest (WSRF) model for high-dimensional classification.
Overview
The Random Forest (Breiman 2001) is one of the most popular and successful ensemble models. However, higher dimensional data can benefit from better subsampling methods for creation of the forests from the underlying decision trees. The weighted subspace random forest (wsrf) model (Xu, Huang, Williams, Wang & Ye, 2012) is one effective way of doing this.
The wsrf has an excellent R implementation (Zhao, Williams & Huang, 2017). But despite its success in classifying high-dimensional data, wsrf did not have a Python implementation. This library provides a Python implementation of wsrf.
Where to get it
# PyPI
pip install wsrf
Features
- Weighted Subspace Sampling: Uses feature importance to guide subspace selection
- Out-of-Bag (OOB) Error Estimation: Per-tree and ensemble-level OOB error tracking
- Feature Importance: Calculates and tracks feature importance scores
- Tree Correlation Analysis: Measures diversity in ensemble predictions
- Ensemble Strength: Breiman-style strength calculations
- Modern
src/Layout: Clean, pip-installable package structure - Comprehensive Tests: 37 tests with 87% code coverage
Usage
Basic Classification
from wsrf import WSRFClassifier
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
# Create dataset
X, y = make_classification(n_samples=1000, n_features=20, n_classes=2, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train WSRF classifier
model = WSRFClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
# Make predictions
predictions = model.predict(X_test)
probabilities = model.predict_proba(X_test)
print(f"Accuracy: {(predictions == y_test).mean():.3f}")
Feature Importance
from wsrf import WSRFClassifier, importance
model = WSRFClassifier(n_estimators=100)
model.fit(X_train, y_train)
# Get feature importances
importances = importance(model)
print("Feature importances:", importances)
OOB Error Rate
from wsrf import WSRFClassifier, oob_error_rate
model = WSRFClassifier(n_estimators=100)
model.fit(X_train, y_train)
# Calculate out-of-bag error
oob_err = oob_error_rate(model, X_train, y_train)
print(f"OOB Error Rate: {oob_err:.3f}")
Weighted vs Unweighted Subspace Sampling
# Weighted sampling (default - uses feature importance)
model_weighted = WSRFClassifier(n_estimators=100, use_weights=True)
# Unweighted sampling (standard random subspace method)
model_unweighted = WSRFClassifier(n_estimators=100, use_weights=False)
Advanced Options
model = WSRFClassifier(
n_estimators=500, # Number of trees in the forest
nodesize=1, # Minimum samples per leaf (min_samples_leaf)
use_weights=True, # Use feature importance for subspace sampling
random_state=42 # Random seed for reproducibility
)
API Reference
Main Classes
WSRFClassifier: Weighted Subspace Random Forest classifierfit(X, y): Train the modelpredict(X): Make class predictionspredict_proba(X): Get class probabilities
Utility Functions
importance(model): Get feature importance scoresoob_error_rate(model, X, y): Calculate OOB error ratetree_correlation(model, X): Compute tree correlation matrixstrength(model, X, y): Calculate Breiman-style ensemble strengthcombine_forests(*forests): Combine multiple trained forestssubset_forest(model, indices): Create a subset of trees from a forest
Running Coverage Analysis
# Terminal report with missing lines
pytest tests/ --cov=wsrf --cov-report=term-missing
# HTML report for detailed analysis
pytest tests/ --cov=wsrf --cov-report=html
open htmlcov/index.html
References
-
Breiman, L. (2001). "Random Forests." Machine Learning, 45(1), 5-32.
-
Xu, B., Huang, J. Z., Williams, G., Wang, Q., & Ye, Y. (2012). "Classifying Very High-Dimensional Data with Random Forests Built from Small Subspaces." International Journal of Data Warehousing and Mining, 8(2), 44-63.
-
Zhao, H., Williams, G. J., & Huang, J. Z. (2017). "wsrf: An R Package for Classification with Scalable Weighted Subspace Random Forests." Journal of Statistical Software, 77(3), 1-30.
License
MIT License - refer to the LICENSE file in this repo for more details
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate and maintain the existing code style.
Citation
If you use this package in your research, please cite:
@software{wsrf_python,
title = {wsrf: Weighted Subspace Random Forest for Python},
author = {Dipyaman Sanyal and Satyavrat Bondre},
year = {2025},
url = {https://github.com/dono-bdec/wsrf}
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file wsrf-0.1.2.tar.gz.
File metadata
- Download URL: wsrf-0.1.2.tar.gz
- Upload date:
- Size: 14.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
46e3e647fd3be74479a8ec9c1ec36a6dfbbb1e598f59b5420889e51897fcd9c8
|
|
| MD5 |
7fa3a7eeb97d02f4e80bcfb1506b3709
|
|
| BLAKE2b-256 |
ec3b5be5faf73aae8e93e64eaf2f44285f75ba09153a92c9275fe3673a04e3ba
|
File details
Details for the file wsrf-0.1.2-py3-none-any.whl.
File metadata
- Download URL: wsrf-0.1.2-py3-none-any.whl
- Upload date:
- Size: 10.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1d13f89a830d6132d08454f6496791904589db38dfbd9e7048edaf594b79a7da
|
|
| MD5 |
0490bb71e9b594351fc05b2eb87e15a1
|
|
| BLAKE2b-256 |
f9bf818f426e49f7d979f66d2c28f9e690426fdbaa2f406a16930e645ca47b14
|