vulpes.utils package

Submodules

vulpes.utils.utils module

utils.py: Core functions and objects of the Vulpes package.

@Author: Adrien Carrel

vulpes.utils.utils.avg_precision(y: ndarray, y_pred: ndarray, **kwargs) float[source]

Micro-average precision for binary and multiclass.

Calculate metrics globally by considering each element of the label indicator matrix as a label.

Parameters:
  • y (np.ndarray) – True labels.

  • y_pred (np.ndarray) – Target scores (e.g. probability).

  • kwargs – optional keyword arguments of sklearn.metrics.average_precision_score

Returns:

Micro average precision score.

Return type:

float

Examples

>>> from vulpes import avg_precision
>>> y_true = np.array([0, 0, 1, 1])
>>> y_scores = np.array([0.1, 0.4, 0.35, 0.8])
>>> avg_precision(y_true, y_scores)
vulpes.utils.utils.create_model_2(model: Any) Any[source]

Create a parent class of the model “model” to add a .predict_proba method based on either the decision function, or pairwise distances

Parameters:

model (Any) – instance of model object

Returns:

extended class with a new method

Return type:

Any

Examples

>>> from vulpes import create_model_2
>>> from sklearn.neighbors import NearestCentroid
>>> model = NearestCentroid
>>> model = create_model_2(model)
>>> model.fit([[1, 2], [2, 3], [3, 4], [4, 5]], [0, 0, 1, 1])
>>> model.predict_proba([[2, 2]])
array([[0.90099855, 0.09900145]])
vulpes.utils.utils.pr_auc_score(y: ndarray, y_pred: ndarray, **kwargs) float[source]

Function to calculate the PR AUC Score for binary and multiclass classification.

Parameters:
  • y (np.ndarray) – True labels.

  • y_pred (np.ndarray) – Target scores (e.g. probability).

  • kwargs – optional keyword arguments of sklearn.metrics.precision_recall_curve

Returns:

Area under the ROC curve.

Return type:

float

Examples

>>> from vulpes import pr_auc_score
>>> y_true = np.array([0, 0, 1, 1])
>>> y_scores = np.array([0.1, 0.4, 0.35, 0.8])
>>> pr_auc_score(y_true, y_scores)
vulpes.utils.utils.r2_score_adj(y: ndarray, y_pred: ndarray, *, n: int, p: int, fit_intercept: bool = True) float[source]

Calculate Adjusted R2 Score.

Adapted from: https://stackoverflow.com/questions/69901671/ how-to-create-an-adjusted-r-squared-scorer-using-sklearn-metrics-make-scorer

Parameters:
  • y (np.ndarray) – True labels.

  • y_pred (np.ndarray) – Target scores (e.g. probability).

  • n (int) – Number of samples.

  • p (int) – Number of parameters.

  • fit_intercept (bool, optional) – Whether of not we fitted the intercept. Defaults to True.

Returns:

Adjusted R2 Score based on the true values and the predicted

values.

Return type:

float

Examples

>>> from vulpes import r2_score_adj
>>> y_true = np.array([0, 1, 2, 3, 4])
>>> y_scores = np.array([-0.01, 1.05, 1.98, 3.12, 3.93])
>>> r2_score_adj(y_true, y_scores, n=5, p=2, fit_intercept=True)
vulpes.utils.utils.sigmoid_(x: float) float[source]

Calculate the sigmoid of x

Parameters:

x (float) – float

Returns:

sigmoid(x)

Return type:

float

Examples:

vulpes.utils.utils.sigmoid_array(x: ndarray | List) ndarray[source]

Based on a list of values, calculate two-class probabilities using the sigmoid function.

Parameters:
  • x (Union[np.ndarray, List]) – value (e.g. result

  • function) (of the decision) –

Returns:

An array with probabilities for a two class classification problem

Return type:

np.ndarray

Examples:

Module contents