我想表示Isomap算法的結果。但我也希望用戶選擇他想保留的組件數量。 我爲此創建了一個滑塊對象,但事情是回調函數在Javascript中。因此我不能使用scikit學習來更新我的數據。 這是我的代碼,有人可以提供一些想法? 謝謝動態可視化景
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。你可以在這裏看到幾個例子:https://github.com/bokeh/bokeh/tree/master/examples/app(點擊一個圖片去實時運行的應用程序版本)以及在這裏找到很多文檔:http://bokeh.pydata.org/en/latest/docs/user_guide/server.html – bigreddot
非常感謝您的鏈接。 它幫助我解決了我的問題 –