【问题标题】:MiniBatchKMeans OverflowError: cannot convert float infinity to integer?MiniBatchKMeans OverflowError:无法将浮点无穷大转换为整数?
【发布时间】:2016-10-14 20:04:02
【问题描述】:

我正在尝试根据使用sklearn.cluster.MiniBatchKMeans 的轮廓分数找到正确的集群数量k

from sklearn.cluster import MiniBatchKMeans
from sklearn.feature_extraction.text import HashingVectorizer

docs = ['hello monkey goodbye thank you', 'goodbye thank you hello', 'i am going home goodbye thanks', 'thank you very much sir', 'good golly i am going home finally']

vectorizer = HashingVectorizer()

X = vectorizer.fit_transform(docs)

for k in range(5):
    model = MiniBatchKMeans(n_clusters = k)
    model.fit(X)

我收到此错误:

Warning (from warnings module):
  File "C:\Python34\lib\site-packages\sklearn\cluster\k_means_.py", line 1279
    0, n_samples - 1, init_size)
DeprecationWarning: This function is deprecated. Please call randint(0, 4 + 1) instead
Traceback (most recent call last):
  File "<pyshell#85>", line 3, in <module>
    model.fit(X)
  File "C:\Python34\lib\site-packages\sklearn\cluster\k_means_.py", line 1300, in fit
    init_size=init_size)
  File "C:\Python34\lib\site-packages\sklearn\cluster\k_means_.py", line 640, in _init_centroids
    x_squared_norms=x_squared_norms)
  File "C:\Python34\lib\site-packages\sklearn\cluster\k_means_.py", line 88, in _k_init
    n_local_trials = 2 + int(np.log(n_clusters))
OverflowError: cannot convert float infinity to integer

我知道type(k)int,所以我不知道这个问题来自哪里。我可以很好地运行以下命令,但我似乎无法遍历列表中的整数,即使 type(2) 等于 k = 2; type(k)

model = MiniBatchKMeans(n_clusters = 2)
model.fit(X)

即使运行不同的model 也可以:

>>> model = KMeans(n_clusters = 2)
>>> model.fit(X)
KMeans(copy_x=True, init='k-means++', max_iter=300, n_clusters=2, n_init=10,
    n_jobs=1, precompute_distances='auto', random_state=None, tol=0.0001,
    verbose=0)

【问题讨论】:

    标签: python types scikit-learn infinity


    【解决方案1】:

    让我们分析一下你的代码:

    • for k in range(5) 返回以下序列:
      • 0, 1, 2, 3, 4
    • model = MiniBatchKMeans(n_clusters = k)n_clusters=k 初始化模型
    • 让我们看看第一次迭代:
      • 使用n_clusters=0
      • 在优化代码中(查看输出):
      • int(np.log(n_clusters))
      • = int(np.log(0))
      • = int(-inf)
      • 错误:整数没有无穷大定义!
      • -> 无法将 -inf 的浮点值转换为 int!

    设置n_clusters=0没有意义!

    【讨论】:

    • 出色的解释。感谢您带我一路回溯。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-16
    • 2019-11-25
    • 1970-01-01
    • 2018-04-30
    • 2020-08-14
    • 1970-01-01
    相关资源
    最近更新 更多