动态可视化bokeh

2024-10-04 05:24:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我想表示Isomap算法的结果。但我也希望用户选择他想要保留的组件数量。 我为此创建了一个slider对象,但问题是回调函数是用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)

Tags: fromimportsourcedatavaluenpcallbackbokeh
1条回答
网友
1楼 · 发布于 2024-10-04 05:24:40

首先,最好一次更新整个.datadict。所以,不要这样做:

source2.data['y1'] = Y_MDS[:,0]
source2.data['y2'] = Y_MDS[:,1]

取而代之的是先用数据生成一个newdict,然后再做

^{pr2}$

除此之外,很难说。看起来你每次更新都要发送大约1000个x-y点?这不是一个不合理的数字,而且许多例子显示了更多。你确定计算本身并没有花费多少时间吗?多长时间:

manifold.Isomap(n_components=w).fit_transform(X)

以及

^{4}$

独自完成?

我的猜测是这些只是需要一些时间来计算。如果是这样的话,博克就无能为力了。在0.12中,应该有一个“busy”消息添加到协议中,这样应用程序可以直观地指示正在进行的昂贵的计算。

相关问题 更多 >