naive bayes
Module: naive_bayes.py
This module implements Naive Bayes classification algorithms, including: - Gaussian Naive Bayes - Multinomial Naive Bayes - Bernoulli Naive Bayes
It provides functionality to
- Train Naive Bayes classifiers on a dataset
- Evaluate model performance using classification metrics
- Visualize results with a confusion matrix
- Optimize hyperparameters using grid search
Classes:
Name | Description |
---|---|
NaiveBayes |
Implements Naive Bayes classification using scikit-learn. |
Dependencies
- numpy
- sklearn.naive_bayes.GaussianNB
- sklearn.naive_bayes.MultinomialNB
- sklearn.naive_bayes.BernoulliNB
- sklearn.metrics (classification metrics)
- base.py (Classification)
Key Features
- Support for Gaussian, Multinomial, and Bernoulli Naive Bayes
- Grid search for hyperparameter tuning
- Automatic data preparation and evaluation
Version Info
- 28/Dec/2024: Initial version
NaiveBayes
Bases: Classification
Implements Naive Bayes classification for Gaussian, Multinomial, and Bernoulli distributions.
Source code in scirex/core/ml/supervised/classification/naive_bayes.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
|
__init__(model_type='gaussian', random_state=42)
Initialize the NaiveBayes classifier.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_type
|
str
|
Type of Naive Bayes classifier. Options are: "gaussian", "multinomial", "bernoulli". |
'gaussian'
|
random_state
|
int
|
Seed for reproducibility where applicable. |
42
|
Source code in scirex/core/ml/supervised/classification/naive_bayes.py
evaluate(X_test, y_test)
Evaluate the model on test data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X_test
|
ndarray
|
Test data features. |
required |
y_test
|
ndarray
|
Test data labels. |
required |
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: Dictionary containing evaluation metrics (accuracy, precision, recall, F1-score). |
Source code in scirex/core/ml/supervised/classification/naive_bayes.py
fit(X_train, y_train)
Train the Naive Bayes model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X_train
|
ndarray
|
Training data features. |
required |
y_train
|
ndarray
|
Training data labels. |
required |
Source code in scirex/core/ml/supervised/classification/naive_bayes.py
get_model_params()
Return the parameters of the model.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: Dictionary containing model parameters. |
Source code in scirex/core/ml/supervised/classification/naive_bayes.py
grid_search(X_train, y_train, param_grid, cv=5)
Perform hyperparameter tuning using grid search.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X_train
|
ndarray
|
Training data features. |
required |
y_train
|
ndarray
|
Training data labels. |
required |
param_grid
|
Dict[str, Any]
|
Dictionary of hyperparameters to search. |
required |
cv
|
int
|
Number of cross-validation folds. Default is 5. |
5
|
Source code in scirex/core/ml/supervised/classification/naive_bayes.py
plot(X_test, y_test)
Plot the confusion matrix for the test data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X_test
|
ndarray
|
Test data features. |
required |
y_test
|
ndarray
|
Test data labels. |
required |
Source code in scirex/core/ml/supervised/classification/naive_bayes.py
run(data, labels, test_size=0.2, param_grid=None, cv=5)
Execute the full classification pipeline with optional grid search.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
ndarray
|
Input features. |
required |
labels
|
ndarray
|
Input labels. |
required |
test_size
|
float
|
Proportion of data to use for testing. Defaults to 0.2. |
0.2
|
param_grid
|
Dict[str, Any]
|
Dictionary of hyperparameters to search for grid search. If None, grid search will not be performed. |
None
|
cv
|
int
|
Number of cross-validation folds for grid search. Default is 5. |
5
|
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: Performance metrics. |
Source code in scirex/core/ml/supervised/classification/naive_bayes.py
save_model(file_path)
Save the trained model to a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
Path where the model will be saved. |
required |