首页 > 解决方案 > ValueError:在进行聚类时,输入包含 NaN、无穷大或对于 dtype('float64') 来说太大的值

问题描述

当我尝试执行肘部方法时,我得到了问题中提到的错误,我正在这样做以进行 KMeans 聚类。

我为解决问题而采取的步骤:

1.检查 NaN 和无穷大:

np.isnan(df3.any())输出=假

np.isfinite(df3.all())输出=真

2.尝试使用以下方法减少我的 float64 数字的值round()

df4 = df3.round({'VolumeRatio_Log':4, 'DollarMillionRatio_Log':4})

这没有解决错误消息

3.将数据类型从float64转换为int64:

df3.column = df3.column.astype('Int64')我收到以下错误

TypeError:不能安全地将非等效 float64 转换为 int64

df3.column= df3.column.astype(np.int64)我收到以下错误

ValueError:无法将非有限值(NA 或 inf)转换为整数

无法转换并且不知道它是否会解决我的主要错误(在问题的标题中)

4.尝试做层次聚类而不是 KMeans - 而不是肘法,我使用树状图并得到以下错误:

MemoryError: Unable to allocate 722. GiB for an array with shape (96845105253,) and data type float64

我不知道我能做些什么来解决错误消息并使肘部方法的代码工作。

提醒一下,我正在尝试解决的错误消息:ValueError:输入包含 NaN、无穷大或对于 dtype('float64') 来说太大的值。

我的数据样本:( 记录的列)

  col1    col2
  1.4494  4.2302
  0.3561  4.5876
  1.9148  4.8567
 ...
 -1.3172  8.2511
 -0.5452  6.5569
 -0.6241  4.8539

我用于肘部方法的代码

wcss = []
for i in range(1, 11):
    kmeans = KMeans(n_clusters = i, init = 'k-means++', random_state = 42)
    kmeans.fit(X)
    wcss.append(kmeans.inertia_)
plt.plot(range(1, 11), wcss)
plt.title('The Elbow Method')
plt.xlabel('Number of clusters')
plt.ylabel('WCSS')
plt.show()

我要解决的错误:ValueError:输入包含 NaN、无穷大或对于 dtype('float64')来说太大的值。

我用于树状图的代码:

dendrogram = sch.dendrogram(sch.linkage(X, method = 'ward'))
plt.title('Dendrogram')
plt.xlabel('Observation points')
plt.ylabel('Euclidean distances')
plt.show()

我得到的错误:MemoryError: Unable to allocate 722. GiB for an array with shape (96845105253,) and data type float64

任何帮助,将不胜感激。

标签: pythonpandasnumpy

解决方案


推荐阅读