Skip to content

Instantly share code, notes, and snippets.

@Olshansk
Last active May 25, 2020 23:25
Show Gist options
  • Select an option

  • Save Olshansk/9af5b101d7a27e1f17a75684458bd9dc to your computer and use it in GitHub Desktop.

Select an option

Save Olshansk/9af5b101d7a27e1f17a75684458bd9dc to your computer and use it in GitHub Desktop.
Joint Probability Matrices - Format Output Table
# https://stackoverflow.com/questions/38931566
def background_gradient(s, m=None, M=None, cmap='Reds', low=0, high=0):
if m is None:
m = s.min().min()
if M is None:
M = s.max().max()
rng = M - m
norm = colors.Normalize(m - (rng * low), M + (rng * high))
normed = s.apply(lambda x: norm(x.values))
cm = plt.cm.get_cmap(cmap)
c = normed.applymap(lambda x: colors.rgb2hex(cm(x)))
ret = c.applymap(lambda x: 'background-color: %s' % x)
return ret
jp_df.columns.name = 'Predicted'
jp_df_df.index.name = 'Ground Truth'
jp_df_df.style.set_caption("Joint Probability Matrix").apply(background_gradient, high=1, axis=None).format("{:.2f}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment