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
1018pip 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
1464Creating and Using a Random Forest Classifier
1565
1666``` py
1767from random_forest_package.classifier import RandomForestClassifierModel
1868from random_forest_package.trainer import ModelTrainer
1969from 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
2274classifier = RandomForestClassifierModel(n_estimators = 100 , max_depth = 10 , random_state = 42 )
@@ -29,6 +81,18 @@ trainer.train(X_train, y_train)
2981evaluator = ModelEvaluator(classifier)
3082accuracy, 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)
3296print (" Accuracy:" , accuracy)
3397print (" Confusion Matrix:\n " , conf_matrix)
3498print (" Classification Report:\n " , class_report)
@@ -40,6 +104,8 @@ Creating and Using a Random Forest Regressor
40104from random_forest_package.regressor import RandomForestRegressorModel
41105from random_forest_package.trainer import ModelTrainer
42106from 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
45111regressor = RandomForestRegressorModel(n_estimators = 100 , max_depth = 10 , random_state = 42 )
@@ -52,7 +118,14 @@ trainer.train(X_train, y_train)
52118evaluator = ModelEvaluator(regressor)
53119mse = 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)
55127print (" Mean Squared Error:" , mse)
128+
56129```
57130
58131### Preprocessing Data
0 commit comments