【发布时间】:2016-08-06 02:42:37
【问题描述】:
我想表示 Isomap 算法的结果。但我也希望用户选择他想要保留的组件数量。 我为此创建了一个滑块对象,但问题是回调函数在 Javascript 中。因此我不能使用 scikit learn 来更新我的数据。 这是我的代码,有人可以给出一些想法吗? 谢谢
import numpy as np
from bokeh.io import vform
from bokeh.models import CustomJS, ColumnDataSource
from bokeh.plotting import figure, output_file, show
from bokeh.models.widgets import Slider
from sklearn import manifold
output_file("test.html")
X = np.random.randn(1000,20)
Y = np.random.randn(1000,20)
X_isomap = manifold.Isomap(n_neighbors=10, n_components=2).fit_transform(X)
X1 = X_isomap[:,0]
X2 = X_isomap[:,1]
IsoSource = ColumnDataSource(data=dict(x=X1, y=X2,DATA=X))
plot1 = figure(plot_width=400, plot_height=400,tools = "pan,wheel_zoom,box_zoom,reset,resize")
plot1.circle('x', 'y',source=IsoSource,size=7, color="navy")
#sliderCompMDS = Slider(title="n_components MDS",value=2,start=2,end=20,step=1)
callback = CustomJS(args=dict(source=IsoSource),code="""
var data = source.get('data');
var f = cb_obj.get('value')
x = data['x']
y =data['y']
X = data['DATA']
donnees = manifold.Isomap(n_neighbors=10, n_components=f).fit_transform(X)
x = donnees[:,0]
y = donnees[:,1]
source.trigger('change');
""")
sliderCompIso = Slider(title="n_components Isomap",value=2,start=1,end=20,step=1,callback=callback)
layout = vform(sliderCompIso, plot1)
show(layout)
【问题讨论】:
-
如果你想从用户交互中触发 python 代码,你需要创建一个 Bokeh 服务器应用。正如您所指出的,
CustomJS回调只能执行 JavaScript,不能执行 python。您可以在此处查看几个示例:github.com/bokeh/bokeh/tree/master/examples/app(单击图像以转到应用程序的实时运行版本)以及在此处找到大量文档:bokeh.pydata.org/en/latest/docs/user_guide/server.html -
非常感谢您的链接。它帮助我解决了我的问题
-
@clémentabinader 那么,你呢......分享你的见解?没有什么比看到有人问完全相同的问题但保留所有自己得到的知识更令人沮丧的了。
标签: javascript python dynamic visualization bokeh