A Python package for advanced Random Forest modeling, including classification and regression, hyperparameter tuning, and model visualization.
- Random Forest Modeling: Supports
RandomForestClassifierandRandomForestRegressor. - Model Tuning: Perform hyperparameter tuning using grid search and randomized search.
- Model Evaluation: Evaluate model performance with cross-validation.
- Visualization: Visualize model performance with confusion matrices, ROC curves, and precision-recall curves.
- Custom Exceptions: Handles errors with custom exception classes.
You can install the package using pip:
pip install random-forest-package- Basic Example:
fromrandom_forest_package.modelimportRandomForestModelfromrandom_forest_package.tunerimportModelTunerfromrandom_forest_package.visualizerimportModelVisualizer# Initialize and train the modelrf_model=RandomForestModel(n_estimators=100, random_state=42)
rf_model.train(X_train, y_train)
# Perform hyperparameter tuningtuner=ModelTuner(rf_model)
param_grid= {'n_estimators': [50, 100, 200], 'max_depth': [None, 10, 20]}
tuner.grid_search(X_train, y_train, param_grid)
# Visualize model performancevisualizer=ModelVisualizer(rf_model)
visualizer.plot_confusion_matrix(X_test, y_test)
visualizer.plot_roc_curve(X_test, y_test)
visualizer.plot_precision_recall_curve(X_test, y_test)- Advanced Tuning Example:
fromrandom_forest_package.tunerimportModelTunerfromsklearn.ensembleimportRandomForestClassifierfromsklearn.datasetsimportload_iris# Load dataX, y=load_iris(return_X_y=True)
# Initialize and tune the modelmodel=RandomForestClassifier()
tuner=ModelTuner(model)
param_distributions= {'n_estimators': [10, 50, 100], 'max_depth': [None, 10, 20]}
tuner.randomized_search(X, y, param_distributions, n_iter=10)
# Cross-validationresults=tuner.cross_validate(X, y)
print(f"Mean score: {results['mean_score']}, Std score: {results['std_score']}")Creating and Using a Random Forest Classifier
fromrandom_forest_package.classifierimportRandomForestClassifierModelfromrandom_forest_package.trainerimportModelTrainerfromrandom_forest_package.evaluatorimportModelEvaluatorfromrandom_forest_package.tunerimportModelTunerfromrandom_forest_package.visualizerimportModelVisualizer# Create a Random Forest Classifierclassifier=RandomForestClassifierModel(n_estimators=100, max_depth=10, random_state=42)
# Train the Classifiertrainer=ModelTrainer(classifier)
trainer.train(X_train, y_train)
# Evaluate the Classifierevaluator=ModelEvaluator(classifier)
accuracy, conf_matrix, class_report=evaluator.evaluate(X_test, y_test)
# Tune the Classifier's Hyperparametersparam_grid= {'n_estimators': [50, 100, 200], 'max_depth': [None, 10, 20, 30]}
tuner=ModelTuner(classifier, param_grid, search_type='grid')
best_params=tuner.tune(X_train, y_train)
# Visualize the Classifier's Performancevisualizer=ModelVisualizer(classifier)
visualizer.plot_confusion_matrix(X_test, y_test)
visualizer.plot_roc_curve(X_test, y_test)
visualizer.plot_precision_recall_curve(X_test, y_test)
print("Best Parameters:", best_params)
print("Accuracy:", accuracy)
print("Confusion Matrix:\n", conf_matrix)
print("Classification Report:\n", class_report)Creating and Using a Random Forest Regressor
fromrandom_forest_package.regressorimportRandomForestRegressorModelfromrandom_forest_package.trainerimportModelTrainerfromrandom_forest_package.evaluatorimportModelEvaluatorfromrandom_forest_package.tunerimportModelTunerfromrandom_forest_package.visualizerimportModelVisualizer# Create a Random Forest Regressorregressor=RandomForestRegressorModel(n_estimators=100, max_depth=10, random_state=42)
# Train the Regressortrainer=ModelTrainer(regressor)
trainer.train(X_train, y_train)
# Evaluate the Regressorevaluator=ModelEvaluator(regressor)
mse=evaluator.evaluate(X_test, y_test)
# Tune the Regressor's Hyperparametersparam_grid= {'n_estimators': [50, 100, 200], 'max_depth': [None, 10, 20, 30]}
tuner=ModelTuner(regressor, param_grid, search_type='random')
best_params=tuner.tune(X_train, y_train)
print("Best Parameters:", best_params)
print("Mean Squared Error:", mse)To preprocess data:
importpandasaspdfromrandom_forest_package.preprocessimportpreprocess_data# Example dataX=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 functions can be used to generate plots of model performance:
fromrandom_forest_package.visualizerimportModelVisualizer# Initialize the visualizervisualizer=ModelVisualizer(rf_model)
# Plot confusion matrixvisualizer.plot_confusion_matrix(X_test, y_test)
# Plot ROC curvevisualizer.plot_roc_curve(X_test, y_test)
# Plot precision-recall curvevisualizer.plot_precision_recall_curve(X_test, y_test)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:
classModelCreationError(Exception):
"""Raised when there is an error in creating the model."""passclassTrainingError(Exception):
"""Raised when there is an error during training."""passclassEvaluationError(Exception):
"""Raised when there is an error during evaluation."""passTests are written using pytest. To run the tests:
poetry run pytestrandom_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
This project is licensed under the MIT License - see the LICENSE file for details.
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.
Karim Mirzaguliyev - karimmirzaguliyev@gmail.com