base
Module: base.py
This module provides the abstract base class for all regression algorithms in SciREX. It defines shared functionality for: - Data preparation (loading from CSV and standard scaling) - Regression performance metric computation (MSE, MAE, R2 score)
Classes:
Name | Description |
---|---|
Regression |
Abstract base class that outlines common behavior for regression 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 regression metrics
- Enforces subclasses to implement fit
, predict
, and get_model_params
Version Info
- 16/Jan/2025: Initial version
Regression
Bases: ABC
Abstract base class for regression algorithms in the SciREX library.
This class provides
- A consistent interface for loading and preparing data
- A standard approach to computing and returning regression metrics (MSE, MAE, R2)
- A method for plotting regression 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 regression model (e.g., "linear_regression", "random_forest"). |
random_state |
int
|
Random seed for reproducibility. |
model |
Optional
|
The trained regression model. |
plots_dir |
Path
|
Directory where regression plots will be saved. |
Source code in scirex/core/ml/supervised/regression/base.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 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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
|
__init__(model_type, random_state=42)
Initialize the base regression class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_type
|
str
|
A string identifier for the regression algorithm (e.g. "linear_regression", "random_forest", etc.). |
required |
random_state
|
int
|
Seed for reproducibility where applicable. Defaults to 42. |
42
|
Source code in scirex/core/ml/supervised/regression/base.py
evaluation_metrics(y_true, y_pred)
Compute and return regression evaluation metrics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_true
|
ndarray
|
The true target values. |
required |
y_pred
|
ndarray
|
The predicted target values. |
required |
Returns:
Type | Description |
---|---|
Dict[str, float]
|
Dict[str, float]: A dictionary of regression evaluation metrics. |
Source code in scirex/core/ml/supervised/regression/base.py
fit(X, y)
abstractmethod
Fit the regression model to the training data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
ndarray
|
The input features for training the model. |
required |
y
|
ndarray
|
The target values for training the model. |
required |
Subclasses must implement this method.
Source code in scirex/core/ml/supervised/regression/base.py
get_model_params()
abstractmethod
Get the model parameters as a dictionary.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: A dictionary of model parameters. |
plot_regression_results(y_true, y_pred)
Plot the regression results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_true
|
ndarray
|
The true target values. |
required |
y_pred
|
ndarray
|
The predicted target values. |
required |
Returns: Figure: The matplotlib figure object
Source code in scirex/core/ml/supervised/regression/base.py
predict(X)
abstractmethod
Generate predictions using the trained regression model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
ndarray
|
The input features for generating predictions. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The predicted target values. |
Source code in scirex/core/ml/supervised/regression/base.py
prepare_data(path)
Load and preprocess data from a CSV file.
1.This method reads the dataset from the specified path, drops any rows with missing values, 2.Scales the features using StandardScaler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The path to the dataset. |
required |
Returns:
Type | Description |
---|---|
Tuple[ndarray, ndarray]
|
Tuple[np.ndarray, np.ndarray]: A tuple of prepared features (X) and target values (y). |
Source code in scirex/core/ml/supervised/regression/base.py
run(data=None, path=None, test_size=0.2)
Run the complete regression pipeline: data loading/preprocessing, fitting the model, and computing regression metrics on the test set.
Args: data (optional,[np.array]): preprocessed adta array of shape (n_samples, n_features). path(Optional[str]): The path to the dataset. test_size(float): The proportion of the dataset for test set is to set as default 0.2.
Dict[str,Any]: A dictionary with the following keys: - "params" (Dict[str, Any]): Model parameters from 'self.get_model_params() - "MSE" (float): Mean Squared Error of the regression model. - "MAE" (float): Mean Absolute Error of the regression model. - "R2" (float): R =-Squared Score of the regression model.