SKLearn KMeans 收敛警告[重复]

2024-02-04

我在一维数据集上使用 SKLearn 的 KMeans 聚类。我遇到的错误是,当我运行代码时,我得到一个ConvergenceWarning:

ConvergenceWarning: Number of distinct clusters (<some integer n>) found smaller than n_clusters (<some integer bigger than n>). Possibly due to duplicate points in X.
  return_n_iter=True)

除了源代码之外,我找不到任何相关内容,源代码并没有表明到底出了什么问题。我相信我的错误要么是因为我有一个一维数据结构,要么是因为我在 SKLearn 中使用一维数组的方式出了问题。这是有问题的代码:

def cluster_data(data_arr):
    """clusters the uas for a specific site"""
    d = 1.0
    k = 1
    inertia_prev = 1.0
    while k <= MAX and d > DELTA: 
        #max is the size of the input array, delta is .05
        kmean = KMeans(n_clusters=k)
        prediction = kmean.fit_predict(data_arr.reshape(-1, 1))
        #bug could be in the reshape!
        inertia_curr = kmean.inertia_
        d = abs(1 - (inertia_curr / inertia_prev))
        inertia_prev = inertia_curr
        k += 1

一些演示 IO: 输入示例:

[(11.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,) ( 7.,) ( 0.,) ( 4.,) ( 7.,)
( 7.,) (13.,) ( 2.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,)
 ( 7.,) ( 2.,) ( 0.,) ( 0.,) (11.,) ( 7.,) ( 7.,) ( 0.,) ( 2.,) ( 1.,)
 ( 0.,) ( 0.,) ( 0.,) ( 7.,) ( 5.,) ( 0.,) ( 0.,) ( 4.,) ( 0.,) ( 0.,)
 ( 0.,) ( 0.,) ( 8.,) ( 0.,) ( 4.,) (10.,) ( 0.,) (11.,) (13.,) (11.,)
 (11.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,) ( 7.,) ( 7.,)
 ( 5.,) ( 5.,) ( 5.,) ( 5.,) ( 5.,) (10.,) (16.,) (15.,) (13.,) ( 2.,)
 ( 5.,) ( 5.,) ( 5.,) ( 5.,) ( 5.,) ( 5.,) ( 5.,) ( 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) ( 0.,) ( 2.,) ( 2.,) ( 2.,) ( 2.,)
 ( 2.,) ( 0.,) (13.,) ( 5.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,) ( 2.,)
 ( 0.,) ( 0.,) ( 0.,) (13.,) ( 0.,) ( 5.,) ( 5.,) ( 5.,) ( 5.,) ( 5.,)
 ( 7.,) ( 5.,) ( 5.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,) ( 8.,)
 ( 0.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,) ( 2.,) (11.,) (10.,)
 ( 2.,) ( 7.,) ( 0.,) ( 2.,) ( 0.,) ( 0.,) ( 5.,) ( 2.,) ( 5.,) ( 2.,)
 ( 5.,) ( 0.,) ( 0.,) ( 2.,) ( 2.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,) ( 2.,)
 ( 0.,) ( 2.,) ( 0.,) ( 2.,) ( 5.,) ( 5.,) ( 1.,) ( 0.,) ( 0.,) ( 0.,)
 ( 0.,) (11.,) ( 5.,) ( 2.,) ( 0.,) ( 0.,) (13.,) ( 0.,) ( 5.,) (15.,)
 ( 7.,) ( 5.,) (11.,) (11.,) (16.,) (15.,) ( 7.,) (16.,) (11.,) (15.,)
 (16.,) (11.,) (17.,) (15.,) (17.,) (15.,) (11.,) ( 7.,) (11.,) ( 7.,)
 ( 7.,) (15.,) (15.,) (15.,) (16.,) (16.,) (16.,) (16.,) (16.,) (16.,)
 (17.,) (16.,) (15.,) (13.,) (14.,) (15.,) (15.,) ( 7.,) (16.,) (15.,)
 (11.,) (15.,) (17.,) (11.,) (11.,) ( 7.,) (15.,) (15.,) (11.,) (11.,)
 (15.,) (15.,) (15.,) (16.,) (11.,) ( 7.,) (16.,) (11.,) (11.,) (15.,)
 (11.,) (15.,) ( 5.,) (16.,) (11.,) (11.,) ( 7.,) (15.,) (15.,) (15.,)]

示例输出:

ConvergenceWarning: Number of distinct clusters (14) found smaller than n_clusters (15). Possibly due to duplicate points in X. return_n_iter=True)

预期输出:

no warning

您可能会注意到输入有很多重复值。这是预期的,我想知道如何更好地对这些数据进行聚类,这样我就不会得到具有重复质心的重复聚类。


理想情况下,指定的簇数不应超过唯一数据点的数量。如果您可以相应地调整质心计数,则不会引发警告。

Sklearn 使用warning模块发出警告。我们可以抑制警告,如下所示。

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    cluster_data(data_arr)

所有警告都被抑制在with块,因此应谨慎使用此功能。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SKLearn KMeans 收敛警告[重复] 的相关文章

  • 为神经网络打乱两个 numpy 数组

    我有两个 numpy 数组用于输入数据 X 和输出数据 y X np array 2 3 sample 1 x 16 4 dtype float sample 2 x y np array 1 0 sample 1 y 0 1 dtype
  • 如何更改默认的Python版本?

    我已经在我的 Mac 上安装了 Python 3 2 我跑完之后 Applications Python 3 2 Update Shell Profile command 当我输入时 这很令人困惑Python V在终端它说Python 2
  • minAreaRect OpenCV 返回的裁剪矩形 [Python]

    minAreaRectOpenCV 中返回一个旋转的矩形 如何裁剪矩形内图像的这部分 boxPoints返回旋转矩形的角点的坐标 以便可以通过循环框内的点来访问像素 但是在 Python 中是否有更快的裁剪方法 EDIT See code在
  • 如何检索分配给 Django 中的组的所有权限

    我正在执行一项任务来检索分配给 Django 中的组的一组权限 我可以使用以下代码获取创建的组 但无法使用它来获取分配给它们的权限 from django contrib auth models import Group Permissio
  • 如何在 numpy 数组中查找并保存重复的行?

    我有一个数组 例如 Array 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 1 1 1 2 2 2 我想要输出以下内容的东西 Repeated 1 1 1 2 2 2 保留重复行的数量也可以 例如 Repeated 1 1
  • 将多索引转换为行式多维 NumPy 数组。

    假设我有一个类似于以下示例的 MultiIndex DataFrame多索引文档 http pandas pydata org pandas docs stable advanced html gt gt gt df 0 1 2 3 fir
  • 烧瓶 - 404 未找到

    我是烧瓶开发的新手 这是我在烧瓶中的第一个程序 但它向我显示了这个错误 在服务器上找不到请求的 URL 如果您输入了网址 请手动检查拼写并重试 这是我的代码 from flask import Flask app Flask name ap
  • 样本()和r样本()有什么区别?

    当我从 PyTorch 中的发行版中采样时 两者sample and rsample似乎给出了类似的结果 import torch seaborn as sns x torch distributions Normal torch tens
  • 如何将 Pyspark Dataframe 标题设置到另一行?

    我有一个如下所示的数据框 col1 col2 col3 id name val 1 a01 X 2 a02 Y 我需要从中创建一个新的数据框 使用 row 1 作为新的列标题并忽略或删除 col1 col2 等行 新表应如下所示 id na
  • 同一台机器上有多个Python版本?

    Python 网站上是否有关于如何在 Linux 上的同一台计算机上安装和运行多个版本的 Python 的官方文档 我可以找到无数的博客文章和答案 但我想知道是否有 标准 官方方法可以做到这一点 或者这一切都取决于操作系统 我认为它是完全独
  • Bokeh 中单独的节点和边缘悬停工具?

    我正在尝试为 Bokeh 中的节点和边缘获取单独的悬停工具提示 但未能使其正常工作 有人可以指出我做错了什么吗 我相信代码应该如下所示 from bokeh io import show output notebook from bokeh
  • Jupyter笔记本突然变得很慢

    我以前在anaconda环境下运行jupyter运行得很好 显示警告后 IOPub data rate exceeded The notebook server will temporarily stop sending output to
  • django 中的身份验证方法返回 None

    你好 我在 django 中做了一个简单的注册和登录页面 当想要登录时 登录视图中的身份验证方法不返回任何内容 我的身份验证应用程序 模型 py from django db import models from django contri
  • 无法将matplotlib安装到pycharm

    我最近开始使用Python速成课程学习Python编程 我陷入困境 因为我无法让 matplotlib 在 pycharm 中工作 我已经安装了pip 我已经通过命令提示符使用 pip 安装了 matplotlib 现在 当我打开 pych
  • 在 for 循环中访问 itertools 产品的元素

    我有一个列表列表 是附加 itertools 产品的一些其他结果的结果 我想要的是能够使用 for 循环访问列表列表中列表的每个元素 但我无法访问所有元素 我只能访问最后一个列表的元素 结果是一个非常巨大的列表列表 例如 1 2 4 3 6
  • 如何使用logging.conf文件使用RotatingFileHandler将所有内容记录到文件中?

    我正在尝试使用RotatingHandler用于 Python 中的日志记录目的 我将备份文件保留为 500 个 这意味着我猜它将创建最多 500 个文件 并且我设置的大小是 2000 字节 不确定建议的大小限制是多少 如果我运行下面的代码
  • 避免“散点/点/蜂群”图中的数据点重叠

    使用绘制点图时matplotlib 我想偏移重叠的数据点以使它们全部可见 例如 如果我有 CategoryA 0 0 3 0 5 CategoryB 5 10 5 5 10 我想要每一个CategoryA 0 数据点并排设置 而不是彼此重叠
  • OSError: [WinError 193] %1 不是有效的 Win32 应用程序,同时使用 CTypes 在 python 中读取自定义 DLL

    我正在尝试编写用 python 封装 C 库的代码 我计划使用 CTypes 来完成此操作 并使用 Visual Studio 来编译我的 DLL 我从一个简单的函数开始 在 Visual Studio 内的标头中添加了以下内容 然后将其构
  • 当训练和测试的特征数量不同时,如何处理生产环境中的One-Hot Encoding?

    在做某些实验时 我们通常在 70 上进行训练 在 33 上进行测试 但是 当您的模型投入生产时会发生什么 可能会发生以下情况 训练集 Ser Type Of Car 1 Hatchback 2 Sedan 3 Coupe 4 SUV 经过
  • 防止 Ada DLL 中的名称损坏

    有没有一种简单的方法可以防止在创建 Ada DLL 时 Ada 名称被破坏 这是我的 adb 代码 with Ada Text IO package body testDLL is procedure Print Call is begin

随机推荐