Skip to content

Instantly share code, notes, and snippets.

@ghl3
Created October 16, 2015 16:13
Show Gist options
  • Select an option

  • Save ghl3/f33a1c18852e5386ea39 to your computer and use it in GitHub Desktop.

Select an option

Save ghl3/f33a1c18852e5386ea39 to your computer and use it in GitHub Desktop.

Revisions

  1. ghl3 created this gist Oct 16, 2015.
    34 changes: 34 additions & 0 deletions plot_table.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    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'))