I am working with the titanic dataset and using decision trees for analyzing the age covariate. I'd like just to see whether kids are more likely to survive than adults. I implemented my own Gini coefficient and I had plot the coefficient by age: dataset here titanic ds

import pandas as pd
import seaborn
import matplotlib.pyplot as plt
from sklearn import tree
import graphviz
import numpy as np

def gini_by_age(df, t):
    df['age_group'] = df['age'].apply(lambda row : 0 if row <= t else 1)
    kids = df[df['age_group'] == 0]
    kids0 = kids[kids['survived'] == 0]
    kids1 = kids[kids['survived'] == 1]
    adults = df[df['age_group'] == 1]
    adults0 = adults[adults['survived'] == 0]
    adults1 = adults[adults['survived'] == 1] 
    gk = 1 - (len(kids0)**2 + len(kids1)**2)/float(len(kids))**2
    ga = 1 - (len(adults0)**2 + len(adults1)**2)/float(len(adults))**2

    return gk + ga

def plot_gini_by_age(df):    
    ages = range(2,25)
    y = [gini_by_age(df, a) for a in ages]
    plt.plot(ages, y)

def use_tree(df):
    X = np.array(df['age']).reshape((len(df['age']),1))
    y = df['survived']
    clf = tree.DecisionTreeClassifier(max_depth=1).fit(X,y)    
    dot_data = tree.export_graphviz(clf, out_file=None)
    graph = graphviz.Source(dot_data)

titanic_df = pd.read_csv("titanic_ds.csv")
ages_cov = titanic_df[['age', 'survived']].dropna()
print gini_by_age(ages_cov, 5)
print gini_by_age(ages_cov, 8.5)
print gini_by_age(ages_cov, 15)

output: 0.925844132419 0.937732003001 0.963875889772 I see from the plot that gini coefficient has local minima at roughly 5, 8 and 15 years old and the best is at 5. But scikit gives me 8.5 years old as the best split. What is wrong here?

  • $\begingroup$ If you want to Visualize what's your tree doing, use graphviz $\endgroup$ – Aditya Mar 12 '18 at 11:16
  • $\begingroup$ I used graphviz as you can see on the function use_tree. $\endgroup$ – Juan Chô Mar 12 '18 at 13:15

I got it thanks to the scikit team, I put the answer here for the people to come. The split used in scikit uses weights in calculating the Gini coefficient, just add the following lines before returning: .... gk *= len(kids)/len(df) ga *= len(adults)/len(df)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.