Angela
Angela

Reputation: 235

Why isn't the Bokeh Plot changing?

Can somebody help me find the mistake? I trained a regression model on the mpg dataset using only the weight and horsepower variables. Now I want to make a plot with Bokeh where the user can choose the weight via a slider, and the plot shows the predictions for increasing horsepower. This is my code:

import matplotlib.pyplot as plt

import numpy as np
from numpy import linspace

import pandas as pd

import random

import sklearn
from sklearn import linear_model, model_selection
from sklearn.metrics import mean_squared_error, r2_score

from bokeh.plotting import figure 
from bokeh.io import output_notebook, show

output_notebook() # Output im Jupyter-Notebook.

from bokeh.sampledata.autompg import autompg
auto=autompg[['hp','weight']]
mpg=autompg['mpg']

X_train, X_test, Y_train, Y_test = model_selection.train_test_split(auto, mpg, test_size=0.2)

mod = linear_model.LinearRegression()
model=mod.fit(X_train,Y_train)
#print(model.score(X_test,Y_test))

from bokeh.layouts import column
from bokeh.models import CustomJS, ColumnDataSource, Slider
from bokeh.plotting import Figure, output_file, show


# Datenerzeugung:
wtm=np.mean(auto['weight'])

hpmin=0.75*min(auto['hp'])
hpmax=1.25*max(auto['hp'])
hprange=[hpmin+(i/100)*(hpmax-hpmin) for i in range(100)]
l=len(hprange)

newinput=pd.DataFrame(data={'weight':np.repeat(wtm,l), 'hp':hprange})

preds=model.predict(newinput)
#print(list(X_train.columns.values))
#print(model.coef_)
#print(model.intercept_)

intc=model.intercept_
hpcoef=model.coef_[0]
wtcoef=model.coef_[1]


source = ColumnDataSource(data=dict(hprange=hprange, preds=preds, intc=np.repeat(intc,l), hpcoef=np.repeat(hpcoef,l), wtcoef=np.repeat(wtcoef,l)))

plot = Figure(plot_width=400, plot_height=400)

plot.circle(hprange, preds, size=7, color="firebrick", alpha=0.5)

callback = CustomJS(args=dict(source=source), code="""
    var data = source.data;
    var wt = cb_obj.value
    var intc= data['intc']
    var hprange= data['hprange']
    var hpcoef= data['hpcoef']
    var wtcoef= data['wtcoef']
    var preds = data['preds']
    for (var i = 0; i < preds.length; i++) {
        preds[i] = intc[0]+hpcoef[0]*hprange[i]+wtcoef[0]*wt
    }
    source.change.emit();
""")


slider = Slider(start=1500, end=5500, value=3000, step=100, title="Weight")
slider.js_on_change('value', callback)

layout = column(slider, plot)

show(layout)

When I run it, it shows the plot, but it doesn't change when I move the slider. I want to add that I've never worked with javascript, so sorry if the reason is a really stupid one. Thanks!

Upvotes: 0

Views: 92

Answers (1)

Angela
Angela

Reputation: 235

For anyone in the future: I fixed it by doing

plot.circle(x='hprange', y='preds', size=7, color="firebrick", alpha=0.5, 
            source=source)

The problem was that the plot wasn't referring to the CDS (I think).

Thanks bigreddot for the comment though, that helped fix many other lesser bugs :)

Upvotes: 1

Related Questions