Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix types multidim scaling #646

Merged
merged 3 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions alibi/explainers/cfproto.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self,
shape: tuple,
kappa: float = 0.,
beta: float = .1,
feature_range: tuple = (-1e10, 1e10),
feature_range: Tuple[Union[float, np.ndarray], Union[float, np.ndarray]] = (-1e10, 1e10),
gamma: float = 0.,
ae_model: Optional[tf.keras.Model] = None,
enc_model: Optional[tf.keras.Model] = None,
Expand Down Expand Up @@ -178,7 +178,9 @@ def __init__(self,
self.max_iterations = max_iterations
self.c_init = c_init
self.c_steps = c_steps
self.feature_range = feature_range
self.feature_range = tuple([(np.ones(shape[1:]) * feature_range[_])[None, :]
if isinstance(feature_range[_], float) else feature_range[_]
for _ in range(2)])
self.update_num_grad = update_num_grad
self.eps = eps
self.clip = clip
Expand Down Expand Up @@ -754,13 +756,13 @@ def fit(self,

# multidim scaled distances
d_abs_abdm, _ = multidim_scaling(d_abdm, n_components=2, use_metric=True,
feature_range=self.feature_range,
feature_range=self.feature_range, # type: ignore
standardize_cat_vars=standardize_cat_vars,
smooth=smooth, center=center,
update_feature_range=False)

d_abs_mvdm, _ = multidim_scaling(d_mvdm, n_components=2, use_metric=True,
feature_range=self.feature_range,
feature_range=self.feature_range, # type: ignore
standardize_cat_vars=standardize_cat_vars,
smooth=smooth, center=center,
update_feature_range=False)
Expand All @@ -779,7 +781,7 @@ def fit(self,
self.feature_range = new_feature_range
else: # apply multidimensional scaling for the abdm or mvdm distances
self.d_abs, self.feature_range = multidim_scaling(d_pair, n_components=2, use_metric=True,
feature_range=self.feature_range,
feature_range=self.feature_range, # type: ignore
standardize_cat_vars=standardize_cat_vars,
smooth=smooth, center=center,
update_feature_range=update_feature_range)
Expand Down
8 changes: 4 additions & 4 deletions alibi/utils/distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def abdm(X: np.ndarray,


def multidim_scaling(d_pair: dict,
feature_range: tuple,
feature_range: Tuple[np.ndarray, np.ndarray],
n_components: int = 2,
use_metric: bool = True,
standardize_cat_vars: bool = True,
Expand All @@ -178,8 +178,8 @@ def multidim_scaling(d_pair: dict,
Dict with as keys the column index of the categorical variables and as values
a pairwise distance matrix for the categories of the variable.
feature_range
Tuple with `min` and `max` ranges to allow for perturbed instances. `Min` and `max` ranges can be `float` or
`numpy` arrays with dimension (`1 x nb of features`) for feature-wise ranges.
Tuple with `min` and `max` ranges to allow for perturbed instances. `Min` and `max` ranges are
`numpy` arrays with dimension (`1 x nb of features`).
n_components
Number of dimensions in which to immerse the dissimilarities.
use_metric
Expand Down Expand Up @@ -240,6 +240,6 @@ def multidim_scaling(d_pair: dict,
d_abs_scaled[k] = d_scaled # scaled distance from the origin for each category

if update_feature_range:
feature_range = new_feature_range
feature_range = new_feature_range # type: ignore

return d_abs_scaled, feature_range