我正在尝试并行运行Kmeans的多个实例(来自sklearn(。我有以下代码:
with concurrent.futures.ProcessPoolExecutor() as executor:
kmeans_per_k = [executor.submit(KMeans(n_clusters=k, random_state=42).fit(features)) for k in range(1,16)]
for f in concurrent.futures.as_completed(kmeans_per_k):
f = f.result()
这给了我以下错误
TypeError: 'KMeans' object is not callable
这是因为fit函数返回KMeans对象。ProcessPoolExecutor在提交方法中需要一个函数。您所做的基本上是在准备要提交的参数时进行拟合。你可能想要一些类似的东西
with concurrent.futures.ProcessPoolExecutor() as executor:
kmeans_per_k = [executor.submit(KMeans(n_clusters=k, random_state=42).fit, features) for k in range(1,16)]
for f in concurrent.futures.as_completed(kmeans_per_k):
f = f.result()