Skip to content

Instantly share code, notes, and snippets.

@GCBallesteros
Last active July 23, 2020 21:36
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save GCBallesteros/1498b9d703a1f182f48421558e9cda18 to your computer and use it in GitHub Desktop.
Regression error plot with holoviews
import numpy as np
import pandas as pd
import holoviews as hv
from holoviews import opts
from bokeh.models import HoverTool
from holoviews import streams
hv.extension("bokeh")
def errors_plot(y_true, y_pred):
error = y_pred - y_true
opts.defaults(opts.Histogram(framewise=True))
data = pd.DataFrame(
{
"Error": y_true - y_pred,
"True Value": y_true,
"Prediction": y_pred,
"Index": np.arange(len(y_pred)),
}
)
# Declare distribution of Points
points = hv.Points(data, ["True Value", "Error"]).opts(
ylabel="Error", xlabel="True Value", tools=["hover"]
)
# Declare points selection selection
sel = streams.Selection1D(source=points)
# Declare DynamicMap computing mean y-value of selection
mean_sel = hv.DynamicMap(
lambda index: hv.HLine(points["Error"][index].mean() if index else -10),
kdims=[],
streams=[sel],
)
# Declare a Bounds stream and DynamicMap to get box_select geometry and draw it
box = streams.BoundsXY(source=points, bounds=(0, 0, 0, 0))
bounds = hv.DynamicMap(lambda bounds: hv.Bounds(bounds), streams=[box])
# Horizontal line
hline = hv.HLine(0)
# Declare DynamicMap to apply bounds selection
dmap = hv.DynamicMap(
lambda bounds: points.select(
True_Value=(bounds[0], bounds[2]), Error=(bounds[1], bounds[3])
),
streams=[box],
)
# Compute histograms of selection along x-axis and y-axis
yhist = hv.operation.histogram(
dmap,
bin_range=points.range("Error"),
dimension="Error",
dynamic=True,
normed=False,
)
xhist = hv.operation.histogram(
dmap,
bin_range=points.range("True Value"),
dimension="True Value",
dynamic=True,
normed=False,
)
# Combine components and display
return (points * mean_sel * bounds * hline).opts(width=700) << yhist << xhist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment