【发布时间】:2021-10-15 21:47:31
【问题描述】:
我正在尝试使用 Python 创建一个聚类函数,并使用 Bokeh 上的滑块小部件进行交互。滑块小部件允许用户选择 K 的值(即要查找的集群数)。
这是代码示例:-
from bokeh.models.annotations import Label
import numpy as np
from sklearn import cluster, datasets
from sklearn.preprocessing import StandardScaler
from bokeh.layouts import column, row
from bokeh.plotting import figure, output_file, show
from bokeh.models import CustomJS, Select, Slider
from bokeh.io import curdoc
print("\n\n*** This example may take several seconds to run before displaying. ***\n\n")
N = 50000
PLOT_SIZE = 400
# generate datasets.
np.random.seed(0)
noisy_circles = datasets.make_circles(n_samples=N, factor=.5, noise=.04)
noisy_moons = datasets.make_moons(n_samples=N, noise=.05)
centers = [(-2, 3), (2, 3), (-2, -3), (2, -3)]
blobs1 = datasets.make_blobs(centers=centers, n_samples=N, cluster_std=0.4, random_state=8)
blobs2 = datasets.make_blobs(centers=centers, n_samples=N, cluster_std=0.7, random_state=8)
colors = np.array([x for x in ('#00f', '#0f0', '#f00', '#0ff', '#f0f', '#ff0')])
colors = np.hstack([colors] * 20)
k_slider = Slider(title = "Select K", value=3, start = 2, end = 6)
k = k_slider.value
kmeans = cluster.KMeans(n_clusters= k)
def update_k(attr,old,new):
k = k_slider.value
kmeans = cluster.KMeans(n_clusters= k)
return kmeans
k_slider.on_change('value', update_k)
algorithm = kmeans
plots =[]
for dataset in (noisy_circles, noisy_moons, blobs1, blobs2):
X, y = dataset
X = StandardScaler().fit_transform(X)
algorithm.fit(X)
if hasattr(algorithm, 'labels_'):
y_pred = algorithm.labels_.astype(int)
else:
y_pred = algorithm.predict(X)
p = figure(output_backend="webgl", title=algorithm.__class__.__name__,width=PLOT_SIZE, height=PLOT_SIZE)
p.circle(X[:, 0], X[:, 1], color=colors[y_pred].tolist(), alpha=0.1,)
plots.append(p)
layout = column(k_slider,row(plots[:2]), row(plots[2:]))
output_file("clustering.html", title="clustering with sklearn")
curdoc().add_root(layout)
K 的默认值为 3,但我希望滑块小部件将其更改为 2-6 之间的任何值。我用 bokeh serve 启动了这个应用程序,它显示了带有 3 个可视化集群的图。但是,当我更改滑块时,它不允许我更新绘图。
我咨询了以下内容:-
Slider value not updating Bokeh Python
但我的问题的解决方案并不明显。 谁能告诉我/添加我的代码缺少什么?
非常感谢:)
【问题讨论】:
标签: python widget data-visualization bokeh k-means