Models Reference
This page documents the models sub-package.
medpipe.models.Calibrator
Calibrator class.
This class creates a Calibrator to calibrate predictions.
Calibrator
Class that creates a Calibrator.
Attributes:
| Name | Type | Description |
|---|---|---|
model |
LogisticRegression or IsotonicRegression
|
Calibrator model. |
model_type |
{logistic, isotonic}
|
Model type. |
logger |
logging.Logger or None, default: None
|
Logger object to log prints. If None print to terminal. |
Methods:
| Name | Description |
|---|---|
__init__ |
Initialise a Calibrator class instance. |
_set_model |
Set the model to default parameters. |
fit |
Fits the predictor based on input data. |
predict_proba |
Predicts probabilities from input data. |
predict |
Predicts labels from input data. |
Source code in src/medpipe/models/Calibrator.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 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 | |
__init__(model_type, hyperparameters={}, logger=None)
Initialise a Calibrator class instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_type
|
(logistic, isotonic)
|
Model type. |
"logistic"
|
hyperparameters
|
dict[str, value]
|
Model hyperparameter dictionary. |
{}
|
logger
|
Logger or None
|
Logger object to log prints. If None print to terminal. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Source code in src/medpipe/models/Calibrator.py
fit(X, y)
Fits the predictor to the training data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like of shape (n_samples,)
|
Training data. |
required |
y
|
array-like of shape (n_samples,)
|
Prediction data. |
required |
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Source code in src/medpipe/models/Calibrator.py
predict(X)
Predicts labels from input data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like of shape (n_samples,)
|
Training data. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
predictions |
array-like of shape (n_samples,)
|
Predicted labels. |
Source code in src/medpipe/models/Calibrator.py
predict_proba(X)
Predicts probabilities from input data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like of shape (n_samples,)
|
Training data. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
probabilities |
np.array of shape (n_samples, 2)
|
Predicted probabilities. |
Source code in src/medpipe/models/Calibrator.py
medpipe.models.Predictor
Predictor class.
This class creates a Predictor to train and make predictions.
Predictor
Class that creates a Predictor.
Attributes:
| Name | Type | Description |
|---|---|---|
model |
dict[str, model]
|
Predictor model. The key is the predicted label, the model is a HistGradientBoostingClassifier. |
model_type |
{hgb - c}
|
Model type. |
hyperparameters |
dict[str, value]
|
Model hyperparameter dictionary. |
logger |
logging.Logger or None, default: None
|
Logger object to log prints. If None print to terminal. |
Methods:
| Name | Description |
|---|---|
__init__ |
Initialise a Predictor class instance. |
_set_model |
Set the model to default parameters. |
fit |
Fits the predictor based on input data. |
predict_proba |
Predicts probabilities from input data. |
predict |
Predicts labels from input data. |
Source code in src/medpipe/models/Predictor.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 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 | |
__init__(model_type, hyperparameters, logger=None)
Initialise a Predictor class instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_type
|
hgb - c
|
Model type. |
"hgb-c"
|
hyperparameters
|
dict[str, value]
|
Model hyperparameter dictionary. |
required |
logger
|
Logger or None
|
Logger object to log prints. If None print to terminal. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Source code in src/medpipe/models/Predictor.py
fit(X_train, y_train, weights=None)
Fits the predictor to the training data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X_train
|
array-like of shape (n_samples, n_features)
|
Training data. |
required |
y_train
|
array-like of shape (n_samples,)
|
Prediction labels. |
required |
weights
|
array-like of shape (n_samples,) or None
|
Weights to address class imbalance. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Source code in src/medpipe/models/Predictor.py
predict(X)
Predicts labels from input data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like of shape (n_samples, n_features)
|
Training data. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
labels |
array-like of shape (n_samples,)
|
Predicted labels. |
Source code in src/medpipe/models/Predictor.py
predict_proba(X)
Predicts probabilities from input data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like of shape (n_samples, n_features)
|
Training data. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
probabilities |
np.array of shape (n_samples, 2)
|
Predicted probabilities. |
Source code in src/medpipe/models/Predictor.py
medpipe.models.core
Models functions module.
This module provides functions to core functions for models and pipelines.
Functions: - create_model: Creates a new model. - test_model: Tests a model on some test data. - save_pipeline: Pickles a pipeline. - load_pipeline: Loads a pickled pipeline. - get_positive_proba: Returns just the positive label probabilities of the each class. - get_full_proba: Returns probabilities for both labels.
create_model(model_type, logger=None, quiet=False, **config_params)
Creates a AI model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_type
|
(hgb - c, logistic, isotonic)
|
Type of model to create. hgb-c: histogram gradient boosting classifier. logistic: logistic regression. isotonic: isotonic regression. |
"hgb-c"
|
quiet
|
bool
|
Flag to create a model without printing. |
False
|
**config_params
|
Configuration parameters for the model. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
model |
HistGradBoostingClassifier
|
Created model. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If model_type is not a str. If an unexpected keyword argument is present. |
ValueError
|
If model_type is not "hgb-c", "logistic" or "isotonic". |
Source code in src/medpipe/models/core.py
get_full_proba(pos_proba)
Returns probabilities for both labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pos_proba
|
array-like of shape (n_samples, n_classes)
|
Probabilities of the positive labels for each class. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
probabilities |
array-like of shape (n_classes, (n_samples, 2))
|
Probabilities for each class. |
Source code in src/medpipe/models/core.py
get_positive_proba(probabilities)
Returns just the positive label probabilities of the each class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
probabilities
|
array-like of shape (n_classes, (n_samples, 2))
|
Probabilities for each class. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
pos_proba |
array-like of shape (n_samples, n_classes)
|
Probabilities of the positive labels for each class. |
Source code in src/medpipe/models/core.py
load_pipeline(load_file)
Loads a saved Pipeline from a .pkl file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
load_file
|
str
|
Path to the file to load the Pipeline from. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
pipeline |
Pipeline
|
Loaded pipeline. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If load_file is not a str. |
FileNotFoundError
|
If load_file does not exist. |
IsADirectoryError
|
If load_file is a directory. |
ValueError
|
If load_file extension is not .pkl file. |
Source code in src/medpipe/models/core.py
save_pipeline(pipeline, save_file, extension='.pkl')
Saves a Pipeline to file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pipeline
|
Pipeline
|
Pipeline to save. |
required |
save_file
|
str
|
Path to the file to save the model. |
required |
extension
|
str
|
Extension of the save file. |
".pkl"
|
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If save_file is not a str. |
FileNotFoundError
|
If save_file does not exist. |
IsADirectoryError
|
If save_file is a directory. |
ValueError
|
If save_file extension is not extension. |
Source code in src/medpipe/models/core.py
test_model(y_test, y_pred, y_pred_proba)
Computes different metrics to test the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_test
|
array-like of shape (n_samples, n_classes)
|
Ground truth test labels. |
required |
y_pred
|
array-like of shape (n_samples, n_classes)
|
Predicted labels. |
required |
y_pred_proba
|
np.array (n_classes,) of arrays (n_samples, 2)
|
Predicted probabilities. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
metric_dict |
dict[str, dict[str, list[float or tuple(array-like)]]
|
Dictionary of the model performance for one fold. Keys are the metric name and values are the metric value. The test metrics used are: - accuracy - f1 - precision - recall - log_loss - roc (Receiver Operator Characteristic) - auroc (Area Under Receiver Operator Characteristic) - prc (Precision-Recall Curve) - ap (Average Precision) |
Raises:
| Type | Description |
|---|---|
TypeError
|
If X_test or y_test are not an array-like. |
ValueError
|
If X_test and y_test do not have the same dimensions. |