Logistic regression
Module: logistic_regression.py
This module implements Logistic Regression for binary and multi-class classification tasks.
It provides functionality to
- Train Logistic Regression classifiers on a dataset
- Evaluate model performance using classification metrics
- Visualize results with a confusion matrix
- Optimize hyperparameters using grid search
Classes:
Name | Description |
---|---|
LogisticRegressionClassifier |
Implements Logistic Regression using scikit-learn. |
Dependencies
- numpy
- sklearn.linear_model.LogisticRegression
- sklearn.metrics (classification metrics)
- sklearn.model_selection.GridSearchCV
- matplotlib, seaborn
- base.py (Classification)
Key Features
- Support for binary and multi-class classification
- Regularization options (L1, L2, ElasticNet)
- Grid search for hyperparameter tuning
- Automatic data preparation and evaluation
Version Info
- 28/Dec/2024: Initial version
LogisticRegressionClassifier
Bases: Classification
Implements Logistic Regression for classification tasks.
Source code in scirex/core/ml/supervised/classification/logistic_regression.py
73 74 75 76 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 |
|
__init__(random_state=42)
Initialize the Logistic Regression classifier.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
random_state
|
int
|
Seed for reproducibility. |
42
|
Source code in scirex/core/ml/supervised/classification/logistic_regression.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/logistic_regression.py
fit(X_train, y_train)
Train the Logistic Regression 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/logistic_regression.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/logistic_regression.py
grid_search(X_train, y_train, param_grid)
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 |
Source code in scirex/core/ml/supervised/classification/logistic_regression.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/logistic_regression.py
predict(X_test)
Predict the labels for the test data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X_test
|
ndarray
|
Test data features. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Array of predicted labels. |
Source code in scirex/core/ml/supervised/classification/logistic_regression.py
run(data, labels, split_ratio=0.2, param_grid=None)
Execute the full classification pipeline with optional grid search.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
ndarray
|
Input features. |
required |
labels
|
ndarray
|
Input labels. |
required |
split_ratio
|
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
|
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: Performance metrics. |