2016-04-14 57 views
0

我想表示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) 
+0

如果您想從用戶交互中觸發* 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

+0

非常感謝您的鏈接。 它幫助我解決了我的問題 –

回答

0

首先,它始終是更好一次更新整個.data字典。所以,做到這一點:

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

而是使與數據newdict第一,然後做

source2.data = newdata 

除此之外,就很難說了。看起來您每次更新都會發送〜1000 x-y點數?這不是一個不合理的數字,許多例子顯示的不止這些。你確定計算本身沒有花費可觀的時間嗎?多久:

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

manifold.MDS(n_components=u).fit_transform(Y) 

拿地完成,對自己?

我的猜測是,那些只需要一些時間來計算。如果是這樣的話,Bokeh就沒有什麼能做的。在0.12應該有一個「忙」的消息添加到協議,所以應用程序可以直觀地表明,正在發生昂貴的計算。

+0

我檢查了MDS的計算時間,並意識到我的所有問題都來自於此。 (我需要36秒生成網頁和MDS需要34秒) 感謝您的幫助 –

+0

太棒了!正如我所提到的,'0.12'應該有一些新的視覺指標來幫助向用戶顯示事情「忙碌」 – bigreddot