Skip to content

Instantly share code, notes, and snippets.

@eugeneyan
Last active February 21, 2021 19:11
Show Gist options
  • Save eugeneyan/2a32a474ae0e0d37b07d1498ee6e77b3 to your computer and use it in GitHub Desktop.
Save eugeneyan/2a32a474ae0e0d37b07d1498ee6e77b3 to your computer and use it in GitHub Desktop.
Test directional expectation
def test_dt_directional_expectation(dummy_titanic_dt, dummy_passengers):
model = dummy_titanic_dt
_, p2 = dummy_passengers
# Get original survival probability of passenger 2
test_df = pd.DataFrame.from_dict([p2], orient='columns')
X, y = get_feats_and_labels(prep_df(test_df))
p2_prob = model.predict(X)[0] # 1.0
# Change gender from female to male
p2_male = p2.copy()
p2_male['Name'] = ' Mr. John'
p2_male['Sex'] = 'male'
test_df = pd.DataFrame.from_dict([p2_male], orient='columns')
X, y = get_feats_and_labels(prep_df(test_df))
p2_male_prob = model.predict(X)[0] # 0.56
# Change class from 1 to 3
p2_class = p2.copy()
p2_class['Pclass'] = 3
test_df = pd.DataFrame.from_dict([p2_class], orient='columns')
X, y = get_feats_and_labels(prep_df(test_df))
p2_class_prob = model.predict(X)[0] # 0.0
# Lower fare from 71.2833 to 5
p2_fare = p2.copy()
p2_fare['Fare'] = 5
test_df = pd.DataFrame.from_dict([p2_fare], orient='columns')
X, y = get_feats_and_labels(prep_df(test_df))
p2_fare_prob = model.predict(X)[0] # 0.85
assert p2_prob > p2_male_prob, 'Changing gender from female to male should decrease survival probability.'
assert p2_prob > p2_class_prob, 'Changing class from 1 to 3 should decrease survival probability.'
assert p2_prob > p2_fare_prob, 'Changing fare from 72 to 5 should decrease survival probability.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment