text_html_output_1.png
import lightgbm as lgb

import numpy as np

import pandas as pd

import shap

import numpy as np
import pandas as pd
import lightgbm as lgb
import graphviz as graphviz
import shap
train = pd.read_csv('../input/titanic/train.csv')
train['FamilyMembers'] = train['SibSp'] + train['Parch']
train['Female'] = train['Sex'].map({'male': 0, 'female': 1}).astype(np.int8)
train['Pclass_cat'] = (train['Pclass'] - 1).astype(np.int8)
train['Adult'] = (train['Age'] > 16).astype(np.int8)
train['Adult'].values[train['Age'].isna()] = 1
train['Age'] = train.groupby(['Pclass', 'Female'])['Age'].transform(lambda x: x.fillna(x.median()))

new_data = pd.DataFrame({'Person': ['Rose', 'Jack'], 'Pclass': [1, 3], 'Age': [17, 20], 'FamilyMembers': [1, 0], 'Female': [1, 0]})
new_data

continuous_features = ['Pclass', 'Age', 'FamilyMembers']
discrete_features = ['Female']
model_columns = discrete_features + continuous_features
ids_of_categorical = [0]
X = np.array(train[model_columns])
y = np.array(train['Survived']).flatten()
dtrain = lgb.Dataset(X, label=y)
params = {'objective': 'binary', 'metric': 'binary_error', 'verbosity': -1, 'boosting_type': 'gbdt', 'seed': 538, 'learning_rate': 1, 'num_leaves': 4, 'feature_fraction': 1.0, 'bagging_fraction': 1.0, 'bagging_freq': 15, 'min_child_samples': 5}
lgbfit = lgb.train(params, dtrain, categorical_feature=ids_of_categorical, verbose_eval=True, num_boost_round=3)

predictions = pd.DataFrame({'Person': ['Rose', 'Jack'], 'Log-odds': [3.362, -1.861]})
predictions['Probability of survival'] = np.exp(predictions['Log-odds']) / (1 + np.exp(predictions['Log-odds']))
predictions

lgbfit.predict(np.array(new_data[model_columns]))

alldata = pd.concat([train[model_columns], new_data[model_columns]]).reset_index(drop=True)
explainer = shap.TreeExplainer(lgbfit)
shap_values = explainer.shap_values(alldata)

shap.initjs()

shap.dependence_plot('Female', shap_values[1], alldata[model_columns])