MiniBatchKMeans OverflowError:无法将浮点无穷大转换为整数? [英] MiniBatchKMeans OverflowError: cannot convert float infinity to integer?

查看:144
本文介绍了MiniBatchKMeans OverflowError:无法将浮点无穷大转换为整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

from sklearn.cluster import MiniBatchKMeans从 sklearn.feature_extraction.text 导入 HashingVectorizerdocs = ['你好猴子再见,谢谢你','再见,谢谢你你好','我要回家再见,谢谢','非常感谢你先生','天哪,我终于回家了']矢量化器 = HashingVectorizer()X = vectorizer.fit_transform(docs)对于范围内的 k(5):模型 = MiniBatchKMeans(n_clusters = k)模型拟合(X)

我收到此错误:

警告(来自警告模块):文件C:\Python34\lib\site-packages\sklearn\cluster\k_means_.py",第 1279 行0, n_samples - 1, init_size)弃用警告:不推荐使用此功能.请改为调用 randint(0, 4 + 1)回溯(最近一次调用最后一次):文件<pyshell#85>",第 3 行,在 <module> 中模型拟合(X)文件C:\Python34\lib\site-packages\sklearn\cluster\k_means_.py",第 1300 行,适合init_size=init_size)文件C:\Python34\lib\site-packages\sklearn\cluster\k_means_.py",第 640 行,在 _init_centroidsx_squared_norms=x_squared_norms)文件C:\Python34\lib\site-packages\sklearn\cluster\k_means_.py",第 88 行,在 _k_initn_local_trials = 2 + int(np.log(n_clusters))溢出错误:无法将浮点无穷大转换为整数

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

model = MiniBatchKMeans(n_clusters = 2)模型拟合(X)

即使运行不同的model 也能工作:

<预><代码>>>>模型 = KMeans(n_clusters = 2)>>>模型拟合(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,详细=0)

解决方案

让我们分析您的代码:

  • 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 没有意义!

I am trying to find the right number of clusters, k, according to silhouette scores using sklearn.cluster.MiniBatchKMeans.

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)

And I receive this error:

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

I know the type(k) is int, so I don't know where this issue is coming from. I can run the following just fine, but I can't seem to iterate through integers in a list, even though the type(2) is equal to k = 2; type(k)

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

Even running a different model works:

>>> 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)

解决方案

Let's analyze your code:

  • for k in range(5) returns the following sequence:
    • 0, 1, 2, 3, 4
  • model = MiniBatchKMeans(n_clusters = k) inits model with n_clusters=k
  • Let's look at the first iteration:
    • n_clusters=0 is used
    • Within the optimization-code (look at the output):
    • int(np.log(n_clusters))
    • = int(np.log(0))
    • = int(-inf)
    • ERROR: no infinity definition for integers!
    • -> casting floating-point value of -inf to int not possible!

Setting n_clusters=0 does not make sense!

这篇关于MiniBatchKMeans OverflowError:无法将浮点无穷大转换为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆