Skip to content

Instantly share code, notes, and snippets.

@ghl3
Created October 16, 2015 16:13
Show Gist options
  • Save ghl3/f33a1c18852e5386ea39 to your computer and use it in GitHub Desktop.
Save ghl3/f33a1c18852e5386ea39 to your computer and use it in GitHub Desktop.
Plot a table of numeric and nominal features
def plot_table(features, targets):
for idx, feature in enumerate(features.columns):
grouped = loans.groupby(targets)
if idx % 2==0:
fig = plt.figure(figsize=(16,4))
plt.subplot(1, 2, idx % 2 + 1)
vals = loans[feature][pd.notnull(loans[feature])]
dtype = grouped[feature].obj.dtype
if dtype == 'float64':
left = min(vals)
right = vals.quantile(0.99)
if left == right:
right = left + 0.5
left = left - 0.5
delta = (right-left)/10
bins = np.arange(left, right, delta)
for name, srs in grouped[feature]:
srs.hist(alpha=0.5, bins=bins, label=name, normed=True,
color='b' if name=='GOOD' else 'g')
else:
for name, srs in grouped[feature]:
values = srs.value_counts(normalize=True)
values[:10].plot(alpha=0.5, kind='bar', label=name,
color='b' if name=='GOOD' else 'g')
plt.legend(loc='best')
plt.xlabel("{} ({})".format(feature, 'NUMERIC' if dtype=='float64' else 'NOMINAL'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment