norskregnesentral / mccepy Goto Github PK
View Code? Open in Web Editor NEWPython package to generate counterfactuals using Monte Carlo sampling of realistic counterfactual explanations
Python package to generate counterfactuals using Monte Carlo sampling of realistic counterfactual explanations
This problem was noticed while looking at examples/Examples_notebook_scratch.ipynb.
'postprocess' calls 'calculate_metrics', which later calls 'distance'. In the 'distance'-function, the data is inverse-transformed according to the 'inverse_transform'-function passed to the Dataset object, if the data is allowed higher cardinality than two levels per categorical feature. In the example, this object is constructed based on
class Dataset():
def __init__(self,
immutables,
target,
categorical,
immutables_encoded,
continuous,
features,
encoder,
scaler,
inverse_transform,
):
self.immutables = immutables
self.target = target
self.feature_order = feature_order
self.dtypes = dtypes
self.categorical = categorical
self.continuous = continuous
self.features = self.categorical + self.continuous
self.cols = self.features + [self.target]
self.immutables_encoded = immutables_encoded
self.encoder = encoder
self.scaler = scaler
self.inverse_transform = inverse_transform
where the inverse_transform function is defined as
def inverse_transform(df,
scaler,
encoder,
continuous,
categorical,
categorical_encoded,
):
df_categorical = pd.DataFrame(encoder.inverse_transform(df[categorical_encoded]), columns=categorical)
df_continuous = pd.DataFrame(scaler.inverse_transform(df[continuous]), columns=continuous)
return pd.concat([df_categorical, df_continuous], axis=1)
Now, the if-else statement in 'distance' fails if higher_card = True, i.e.
if higher_card:
cf_inverse_transform = dataset.inverse_transform(cfs.copy())
fact_inverse_transform = dataset.inverse_transform(fact.copy())
cfs_categorical = cf_inverse_transform[categorical].sort_index().to_numpy()
factual_categorical = fact_inverse_transform[categorical].sort_index().to_numpy()
else:
cfs_categorical = cfs[categorical_encoded].sort_index().to_numpy()
factual_categorical = fact[categorical_encoded].sort_index().to_numpy()
because the inverse_transform-function misses four arguments. These can be passed quite easily however. Thus, I believe this problem can be fixed by changing the if-else statement above to
if higher_card:
cf_inverse_transform = dataset.inverse_transform(cfs.copy(), dataset.scaler, dataset.encoder, continuous, categorical, categorical_encoded)
fact_inverse_transform = dataset.inverse_transform(fact.copy(), dataset.scaler, dataset.encoder, continuous, categorical, categorical_encoded)
cfs_categorical = cf_inverse_transform[categorical].sort_index().to_numpy()
factual_categorical = fact_inverse_transform[categorical].sort_index().to_numpy()
else:
cfs_categorical = cfs[categorical_encoded].sort_index().to_numpy()
factual_categorical = fact[categorical_encoded].sort_index().to_numpy()
I can send a PR later if this is interesting to repository owner.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.