Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonatan CARRANZA committed Apr 18, 2024
1 parent 1bc0cfb commit f9580c7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mapie/conformity_scores/residual_conformity_scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from mapie._machine_precision import EPSILON
from mapie._typing import ArrayLike, NDArray
from mapie.conformity_scores import ConformityScore
from mapie.wrap_arrays import wrap_ndarray_and_dataframe


class AbsoluteConformityScore(ConformityScore):
Expand Down Expand Up @@ -377,6 +378,8 @@ def get_signed_conformity_scores(
(X, y, y_pred,
self.residual_estimator_,
random_state) = self._check_parameters(X, y, y_pred)
# Wrap numpy or pandas array transparently to handle indexing
X = wrap_ndarray_and_dataframe(X)

full_indexes = np.argwhere(
np.logical_not(np.isnan(y_pred))
Expand Down
26 changes: 26 additions & 0 deletions mapie/wrap_arrays.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from typing import Union
import numpy as np
import pandas as pd
from mapie._typing import NDArray


class wrap_ndarray_and_dataframe:

def __init__(self, X_array: Union[NDArray | pd.DataFrame]):
"""
This class is a wrapper for numpy arrays and pandas DataFrames.
It is used to handle the indexing access to the data in a consistent way.
:param X_array:
"""
self.X_array = X_array
if isinstance(X_array, pd.DataFrame):
self.X_array = pd.DataFrame(X_array, columns=X_array.columns)
self.X_array = self.X_array.astype(self.X_array.dtypes.to_dict())

def __getitem__(self, item):
if isinstance(self.X_array, pd.DataFrame):
return self.X_array.iloc[item].values
elif isinstance(self.X_array, np.ndarray):
return self.X_array[item]
else:
raise ValueError("Input must be a numpy array or pandas DataFrame")

0 comments on commit f9580c7

Please sign in to comment.