Skip to main content

A package to facilitate random forest modeling

Project description

Random Forest Package

A Python package to facilitate random forest modeling, supporting both classification and regression tasks using object-oriented design principles.

Installation

To install the package, use:

pip install random-forest-package

Usage

Creating and Using a Random Forest Classifier

from random_forest_package.classifier import RandomForestClassifierModel
from random_forest_package.trainer import ModelTrainer
from random_forest_package.evaluator import ModelEvaluator

# Create a Random Forest Classifier
classifier = RandomForestClassifierModel(n_estimators=100, max_depth=10, random_state=42)

# Train the Classifier
trainer = ModelTrainer(classifier)
trainer.train(X_train, y_train)

# Evaluate the Classifier
evaluator = ModelEvaluator(classifier)
accuracy, conf_matrix, class_report = evaluator.evaluate(X_test, y_test)

print("Accuracy:", accuracy)
print("Confusion Matrix:\n", conf_matrix)
print("Classification Report:\n", class_report)

Creating and Using a Random Forest Regressor

from random_forest_package.regressor import RandomForestRegressorModel
from random_forest_package.trainer import ModelTrainer
from random_forest_package.evaluator import ModelEvaluator

# Create a Random Forest Regressor
regressor = RandomForestRegressorModel(n_estimators=100, max_depth=10, random_state=42)

# Train the Regressor
trainer = ModelTrainer(regressor)
trainer.train(X_train, y_train)

# Evaluate the Regressor
evaluator = ModelEvaluator(regressor)
mse = evaluator.evaluate(X_test, y_test)

print("Mean Squared Error:", mse)

Preprocessing Data

To preprocess data:

import pandas as pd
from random_forest_package.preprocess import preprocess_data

# Example data
X = pd.DataFrame({'feature1': [1, 2, 3], 'feature2': [4, 5, 6]})
y = pd.Series([0, 1, 0])

X_train, X_test, y_train, y_test = preprocess_data(X, y, test_size=0.2, random_state=42)

Visualization

Visualization functions can be used to generate plots of model performance:

from random_forest_package.visualizer import ModelVisualizer

# Initialize the visualizer
visualizer = ModelVisualizer(rf_model)

# Plot confusion matrix
visualizer.plot_confusion_matrix(X_test, y_test)

# Plot ROC curve
visualizer.plot_roc_curve(X_test, y_test)

# Plot precision-recall curve
visualizer.plot_precision_recall_curve(X_test, y_test)

Custom Exceptions

This package provides custom exceptions for better error handling:

  • ModelCreationError: Raised when there is an error creating the random forest model.
  • PreprocessingError: Raised when there is an error during data preprocessing.
  • TrainingError: Raised when there is an error during model training.
  • EvaluationError: Raised when there is an error during model evaluation.
  • VisualizationError: Raised when there is an error during visualization.

Example of handling a custom exception:

class ModelCreationError(Exception):
    """Raised when there is an error in creating the model."""
    pass

class TrainingError(Exception):
    """Raised when there is an error during training."""
    pass

class EvaluationError(Exception):
    """Raised when there is an error during evaluation."""
    pass

Testing

Tests are written using pytest. To run the tests:

poetry run pytest

Project Structure

random_forest_package/
│
├── random_forest_package/
│   ├── __init__.py
│   ├── base_model.py          # Contains the abstract base class for the models
│   ├── classifier.py          # Contains the RandomForestClassifier class
│   ├── regressor.py           # Contains the RandomForestRegressor class
│   ├── preprocess.py          # Contains data preprocessing classes or functions
│   ├── trainer.py             # Contains classes for training models
│   ├── evaluator.py           # Contains classes for evaluating models
│   ├── utils.py               # Utility functions or classes
│   ├── visualizer.py          # Utility visualize cases
│   └── exceptions.py          # Custom exceptions
│
├── tests/
│   ├── __init__.py
│   ├── test_classifier.py     # Tests for the classifier
│   ├── test_regressor.py      # Tests for the regressor
│   ├── test_preprocess.py     # Tests for preprocessing
│   ├── test_trainer.py        # Tests for training
│   ├── test_evaluator.py      # Tests for evaluation
│   └── test_utils.py          # Tests for utility functions
│
├── .gitignore
├── LICENSE
├── README.md
└── pyproject.toml

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Authors

Karim Mirzaguliyev - karimmirzaguliyev@gmail.com

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

random_forest_package-0.1.5.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

random_forest_package-0.1.5-py3-none-any.whl (2.8 kB view details)

Uploaded Python 3

File details

Details for the file random_forest_package-0.1.5.tar.gz.

File metadata

  • Download URL: random_forest_package-0.1.5.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for random_forest_package-0.1.5.tar.gz
Algorithm Hash digest
SHA256 03a40b98e191693da9f06801b0fbe4f35f91b20ff91639cd721e4737fa09e239
MD5 13458846dcf7e612196ead2c1832f606
BLAKE2b-256 cf7a7fe22a74a4a54c4f263d9b8627623ef0f7f1d8a2d8b893dc84864ec32bb8

See more details on using hashes here.

File details

Details for the file random_forest_package-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for random_forest_package-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 c562628cabaf120899bf76bc645b8e96f63a2343e10eb07a0b479b6621c0d733
MD5 58d1832a7da88264e6c56a806e4cbdbc
BLAKE2b-256 f0e02fea5c1ea63d933908418a411f20fc24a6441b43c3b19ea861c1c808e4d3

See more details on using hashes here.

Supported by

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