base
Module: base.py
This module provides the abstract base class for all classification algorithms in SciREX. It defines shared functionality for: - Data preparation (loading from CSV and standard scaling) - Classification performance metric computation (accuracy, precision, recall, f1-score)
Classes:
Name | Description |
---|---|
Classification |
Abstract base class that outlines common behavior for classification algorithms. |
Dependencies
- numpy, pandas, sklearn
- abc, pathlib, time, typing (for structural and type support)
Key Features
- Consistent interface for loading and preparing data
- Standard approach to computing and returning classification metrics
- Enforces subclasses to implement
fit
,predict
, andget_model_params
Version Info
- 28/Dec/2024: Initial version
Classification
Bases: ABC
Abstract base class for classification algorithms in the SciREX library.
This class provides
- A consistent interface for loading and preparing data
- A standard approach to computing and returning classification metrics (accuracy, precision, recall, F1-score)
- A method for plotting confusion matrix for classification results
Subclasses must
- Implement the
fit(X: np.ndarray, y: np.ndarray) -> None
method, which should populateself.model
. - Implement the
get_model_params() -> Dict[str, Any]
method, which returns a dict of model parameters for logging/debugging.
Attributes:
Name | Type | Description |
---|---|---|
model_type |
str
|
The name or identifier of the classification model (e.g., "logistic_regression", "decision_tree"). |
random_state |
int
|
Random seed for reproducibility. |
model |
Optional
|
The trained classification model. |
plots_dir |
Path
|
Directory where confusion matrix plots will be saved. |
Source code in scirex/core/ml/supervised/classification/base.py
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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
|
__init__(model_type, random_state=42)
Initialize the base classification class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_type
|
str
|
A string identifier for the classification algorithm (e.g. "logistic_regression", "decision_tree", etc.). |
required |
random_state
|
int
|
Seed for reproducibility where applicable. Defaults to 42. |
42
|
Source code in scirex/core/ml/supervised/classification/base.py
fit(X, y)
abstractmethod
Fit the classification model on the training dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
ndarray
|
A 2D array of shape (n_samples, n_features) containing the features. |
required |
y
|
ndarray
|
A 1D array of shape (n_samples,) containing the labels. |
required |
Subclasses must implement this method. After fitting the model,
self.model
should be populated with the trained model.
Source code in scirex/core/ml/supervised/classification/base.py
get_model_params()
abstractmethod
Return model parameters for logging or debugging.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: A dictionary containing key model parameters and potentially any learned attributes (e.g., coefficients, intercept). |
Source code in scirex/core/ml/supervised/classification/base.py
plot_confusion_matrix(y_true, y_pred)
Plot the confusion matrix using the true and predicted labels.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_true
|
ndarray
|
True labels for the test data. |
required |
y_pred
|
ndarray
|
Predicted labels for the test data. |
required |
Returns:
Name | Type | Description |
---|---|---|
Figure |
Figure
|
A matplotlib Figure object containing the confusion matrix plot. |
Source code in scirex/core/ml/supervised/classification/base.py
prepare_data(path)
Load and preprocess data from a CSV file, returning features and labels.
This method
- Reads the CSV file into a pandas DataFrame.
- Drops rows containing NaN values.
- Selects only numeric columns from the DataFrame.
- Scales the features using scikit-learn's StandardScaler.
- Assumes the last column is the target label.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
Filepath to the CSV data file. |
required |
Returns:
Type | Description |
---|---|
Tuple[ndarray, ndarray]
|
Tuple[np.ndarray, np.ndarray]: - Features dataset (X) of shape (n_samples, n_features). - Labels (y) of shape (n_samples,). |
Source code in scirex/core/ml/supervised/classification/base.py
run(data=None, path=None, test_size=0.2)
Run the complete classification pipeline: data loading/preprocessing, fitting the model, and computing classification metrics on the test set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Optional[ndarray]
|
Preprocessed data array of shape (n_samples, n_features). |
None
|
path
|
Optional[str]
|
Path to a CSV file from which to read data.
If |
None
|
test_size
|
float
|
The proportion of the dataset to include in the test split (default 0.2). |
0.2
|
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: A dictionary with the following keys:
- "params" (Dict[str, Any]): Model parameters from |