Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DiCE exaple
# seeding random numbers for reproducability
from numpy.random import seed
seed(1)
tf.random.set_seed(2)
sess = tf.compat.v1.InteractiveSession()
d = dice_ml.Data(dataframe=df, continuous_features=['age', 'fare'], outcome_name='survived')
train, _ = d.split_data(d.normalize_data(d.one_hot_encoded_data))
X_train = train.loc[:, train.columns != 'survived']
y_train = train.loc[:, train.columns == 'survived']
X_train.head()
ann_model = keras.Sequential()
ann_model.add(keras.layers.Dense(20, input_shape=(X_train.shape[1],),
kernel_regularizer=keras.regularizers.l1(0.001),
activation=tf.nn.relu))
ann_model.add(keras.layers.Dense(1, activation=tf.nn.sigmoid))
ann_model.compile(loss='binary_crossentropy',
optimizer=tf.keras.optimizers.Adam(0.01),
metrics=['accuracy'])
ann_model.fit(X_train, y_train, validation_split=0.20, epochs=100, verbose=0, class_weight={0:1,1:2})
# provide the trained ML model to DiCE's model object
backend = 'TF'+tf.__version__[0]
m = dice_ml.Model(model=ann_model, backend=backend)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment