pandas 混淆矩阵的散景热图 [英] Bokeh heatmap from Pandas confusion matrix
问题描述
如何将熊猫DataFrame
显示为散景热图?
How can a Pandas DataFrame
be shown as a Bokeh heatmap?
https://docs.bokeh.org /en/latest/docs/user_guide/categorical.html#heat-maps 显示了一些示例,但是尝试进行修改总是只会给出一个空白图.
https://docs.bokeh.org/en/latest/docs/user_guide/categorical.html#heat-maps shows some example, but trying to modify always only gave an empty plot.
混淆矩阵示例:
df = pd.DataFrame([[10, 0, 1], [1, 10, 0], [1, 1, 9]],
columns=['A', 'B', 'C'],
index=['A', 'B', 'C'])
df.index.name = 'Treatment'
df.columns.name = 'Prediction'
推荐答案
首先导入软件包并准备data.frame:
First import packages and prepare data.frame :
import pandas as pd
from bokeh.io import output_file, show
from bokeh.models import BasicTicker, ColorBar, LinearColorMapper, ColumnDataSource, PrintfTickFormatter
from bokeh.plotting import figure
from bokeh.transform import transform
df = pd.DataFrame(
[[10, 0, 1], [1, 10, 0], [1, 1, 9]],
columns=['A', 'B', 'C'],
index=['A', 'B', 'C'])
df.index.name = 'Treatment'
df.columns.name = 'Prediction'
要使用我的解决方案,您必须堆叠 data.frame:
To use my solution you have to stack the data.frame :
# Prepare data.frame in the right format
df = df.stack().rename("value").reset_index()
现在,我们可以创建情节了:
Now, we can create the plot :
# here the plot :
output_file("myPlot.html")
# You can use your own palette here
colors = ['#d7191c', '#fdae61', '#ffffbf', '#a6d96a', '#1a9641']
# Had a specific mapper to map color with value
mapper = LinearColorMapper(
palette=colors, low=df.value.min(), high=df.value.max())
# Define a figure
p = figure(
plot_width=800,
plot_height=300,
title="My plot",
x_range=list(df.Treatment.drop_duplicates()),
y_range=list(df.Prediction.drop_duplicates()),
toolbar_location=None,
tools="",
x_axis_location="above")
# Create rectangle for heatmap
p.rect(
x="Treatment",
y="Prediction",
width=1,
height=1,
source=ColumnDataSource(df),
line_color=None,
fill_color=transform('value', mapper))
# Add legend
color_bar = ColorBar(
color_mapper=mapper,
location=(0, 0),
ticker=BasicTicker(desired_num_ticks=len(colors)))
p.add_layout(color_bar, 'right')
show(p)
*注意:我使用的是比从bokeh库中调用HeatMap
更为完整的解决方案,因为1)您可以像这样对参数进行更多控制,2)与Bokeh,Pandas等存在很多不兼容,这是唯一适用于我的配置的解决方案.
*Note : I use a more complete solution than just call HeatMap
from the bokeh library because 1) you have more control on parameters like this, 2) there are lot of incompatibilities with Bokeh, Pandas, etc and this is the only solution working with my configuration.
这篇关于 pandas 混淆矩阵的散景热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!