Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@yuyasugano
Created October 1, 2020 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuyasugano/33593603e2655e5a52c5bdc349ef6fb3 to your computer and use it in GitHub Desktop.
Save yuyasugano/33593603e2655e5a52c5bdc349ef6fb3 to your computer and use it in GitHub Desktop.
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