text_html_output_1.png
import graphviz as graphviz

import lightgbm as lgb

import numpy as np

import pandas as pd

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()))

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)

def get_a_graph(tree_index=0):
    gv1 = lgb.create_tree_digraph(lgbfit, tree_index=tree_index, show_info='data_percentage')
    gv1s = gv1.source.replace('Column_0', 'Female').replace('Column_1', 'Pclass').replace('Column_2', 'Age').replace('Column_3', 'FamilyMembers')
    graph = graphviz.Source(gv1s)
    return graph

get_a_graph(tree_index=0)