Skip to content

Commit 167eeaf

Browse files
authored
Merge pull request #15 from k4rimDev/development
✔️ Update documentation
2 parents 5bc0c4f + 22c5bff commit 167eeaf

3 files changed

Lines changed: 79 additions & 6 deletions

File tree

README.md

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,74 @@
11
# Random Forest Package
22

3-
A Python package to facilitate random forest modeling, supporting both classification and regression tasks using object-oriented design principles.
3+
A Python package for advanced Random Forest modeling, including classification and regression, hyperparameter tuning, and model visualization.
4+
5+
## Features
6+
7+
- **Random Forest Modeling**: Supports `RandomForestClassifier` and `RandomForestRegressor`.
8+
- **Model Tuning**: Perform hyperparameter tuning using grid search and randomized search.
9+
- **Model Evaluation**: Evaluate model performance with cross-validation.
10+
- **Visualization**: Visualize model performance with confusion matrices, ROC curves, and precision-recall curves.
11+
- **Custom Exceptions**: Handles errors with custom exception classes.
412

513
## Installation
614

7-
To install the package, use:
15+
You can install the package using pip:
816

9-
```sh
17+
```bash
1018
pip install random-forest-package
1119
```
12-
### Usage
20+
## Usage
21+
22+
- Basic Example:
23+
```py
24+
from random_forest_package.model import RandomForestModel
25+
from random_forest_package.tuner import ModelTuner
26+
from random_forest_package.visualizer import ModelVisualizer
27+
28+
# Initialize and train the model
29+
rf_model = RandomForestModel(n_estimators=100, random_state=42)
30+
rf_model.train(X_train, y_train)
31+
32+
# Perform hyperparameter tuning
33+
tuner = ModelTuner(rf_model)
34+
param_grid = {'n_estimators': [50, 100, 200], 'max_depth': [None, 10, 20]}
35+
tuner.grid_search(X_train, y_train, param_grid)
36+
37+
# Visualize model performance
38+
visualizer = ModelVisualizer(rf_model)
39+
visualizer.plot_confusion_matrix(X_test, y_test)
40+
visualizer.plot_roc_curve(X_test, y_test)
41+
visualizer.plot_precision_recall_curve(X_test, y_test)
42+
```
43+
44+
- Advanced Tuning Example:
45+
```py
46+
from random_forest_package.tuner import ModelTuner
47+
from sklearn.ensemble import RandomForestClassifier
48+
from sklearn.datasets import load_iris
49+
50+
# Load data
51+
X, y = load_iris(return_X_y=True)
52+
53+
# Initialize and tune the model
54+
model = RandomForestClassifier()
55+
tuner = ModelTuner(model)
56+
param_distributions = {'n_estimators': [10, 50, 100], 'max_depth': [None, 10, 20]}
57+
tuner.randomized_search(X, y, param_distributions, n_iter=10)
58+
59+
# Cross-validation
60+
results = tuner.cross_validate(X, y)
61+
print(f"Mean score: {results['mean_score']}, Std score: {results['std_score']}")
62+
```
1363

1464
Creating and Using a Random Forest Classifier
1565

1666
```py
1767
from random_forest_package.classifier import RandomForestClassifierModel
1868
from random_forest_package.trainer import ModelTrainer
1969
from random_forest_package.evaluator import ModelEvaluator
70+
from random_forest_package.tuner import ModelTuner
71+
from random_forest_package.visualizer import ModelVisualizer
2072

2173
# Create a Random Forest Classifier
2274
classifier = RandomForestClassifierModel(n_estimators=100, max_depth=10, random_state=42)
@@ -29,6 +81,18 @@ trainer.train(X_train, y_train)
2981
evaluator = ModelEvaluator(classifier)
3082
accuracy, conf_matrix, class_report = evaluator.evaluate(X_test, y_test)
3183

84+
# Tune the Classifier's Hyperparameters
85+
param_grid = {'n_estimators': [50, 100, 200], 'max_depth': [None, 10, 20, 30]}
86+
tuner = ModelTuner(classifier, param_grid, search_type='grid')
87+
best_params = tuner.tune(X_train, y_train)
88+
89+
# Visualize the Classifier's Performance
90+
visualizer = ModelVisualizer(classifier)
91+
visualizer.plot_confusion_matrix(X_test, y_test)
92+
visualizer.plot_roc_curve(X_test, y_test)
93+
visualizer.plot_precision_recall_curve(X_test, y_test)
94+
95+
print("Best Parameters:", best_params)
3296
print("Accuracy:", accuracy)
3397
print("Confusion Matrix:\n", conf_matrix)
3498
print("Classification Report:\n", class_report)
@@ -40,6 +104,8 @@ Creating and Using a Random Forest Regressor
40104
from random_forest_package.regressor import RandomForestRegressorModel
41105
from random_forest_package.trainer import ModelTrainer
42106
from random_forest_package.evaluator import ModelEvaluator
107+
from random_forest_package.tuner import ModelTuner
108+
from random_forest_package.visualizer import ModelVisualizer
43109

44110
# Create a Random Forest Regressor
45111
regressor = RandomForestRegressorModel(n_estimators=100, max_depth=10, random_state=42)
@@ -52,7 +118,14 @@ trainer.train(X_train, y_train)
52118
evaluator = ModelEvaluator(regressor)
53119
mse = evaluator.evaluate(X_test, y_test)
54120

121+
# Tune the Regressor's Hyperparameters
122+
param_grid = {'n_estimators': [50, 100, 200], 'max_depth': [None, 10, 20, 30]}
123+
tuner = ModelTuner(regressor, param_grid, search_type='random')
124+
best_params = tuner.tune(X_train, y_train)
125+
126+
print("Best Parameters:", best_params)
55127
print("Mean Squared Error:", mse)
128+
56129
```
57130

58131
### Preprocessing Data

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "random-forest-package"
3-
version = "0.1.5"
3+
version = "0.1.6"
44
description = "A Python package to facilitate random forest modeling."
55
authors = ["Karim Mirzaguliyev <karimmirzaguliyev@gmail.com>"]
66
readme = "README.md"

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
setup(
55
name='random_forest_package',
6-
version='0.1.5',
6+
version='0.1.6',
77
packages=find_packages(),
88
install_requires=[
99
'scikit-learn',

0 commit comments

Comments
 (0)