Source

ml4hackers / ch5-death.py

Full commit
#!/usr/bin/env python2

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from statsmodels.nonparametric import KDE

df = pd.read_csv('longevity.csv')
d0 = KDE(df[df['Smokes'] == 0]['AgeAtDeath'].astype(np.double))
d0.fit()
d1 = KDE(df[df['Smokes'] == 1]['AgeAtDeath'].astype(np.double))
d1.fit()

fig = plt.figure()
ax = fig.add_subplot(211)
ax.fill(d1.support, d1.density, color='steelblue', alpha=.7, label='Smoker')
ax.legend(frameon=False)
am = d1.support[d1.density.argmax()]
ax.axvline(am, linestyle = '--')

ax = fig.add_subplot(212, sharex=ax)
ax.fill(d0.support, d0.density, color='red', alpha=.7, label='Non Smoker')
ax.legend(frameon=False)
am = d0.support[d0.density.argmax()]
ax.axvline(am, linestyle = '--')

xs = np.append(d0.support, d1.support)
ax.set_xlim(np.min(xs), np.max(xs))
ys = np.append(d0.density, d1.density)
ax.set_ylim(0, np.max(ys))

ax.set_ylabel('Density')
ax.set_xlabel('Age At Death')
ax.legend(frameon=False)
fig.savefig('smokers.png')