{
  "id": "opencv/kmeans-n-less-than-k",
  "signature": "cv2.error: OpenCV(4.9.0) /tmp/opencv-4.9.0/modules/core/src/kmeans.cpp:245: error: (-215:Assertion failed) N >= K in function 'cv::kmeans'",
  "signature_zh": "cv2.error: OpenCV(4.9.0) /tmp/opencv-4.9.0/modules/core/src/kmeans.cpp:245: error: (-215:断言失败) N >= K 在函数 'cv::kmeans' 中",
  "regex": "N >= K in function 'cv::kmeans'",
  "domain": "opencv",
  "category": "assertion_error",
  "subcategory": null,
  "root_cause": "The number of data points (N) provided to k-means is less than the requested number of clusters (K), making clustering impossible.",
  "root_cause_type": "generic",
  "root_cause_zh": "提供给 k-means 的数据点数量 (N) 少于请求的聚类数量 (K)，导致无法进行聚类。",
  "versions": [
    {
      "version": "4.8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "4.9.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "4.10.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "5.0.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Duplicating points creates artificial clusters and does not solve the fundamental issue of insufficient unique data.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "More iterations do not help when N < K; the algorithm cannot proceed with fewer points than clusters.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "The assertion fails before any distance computation; the error is in the input validation.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Reduce K to be less than or equal to N: `k = min(k, len(data))` before calling kmeans.",
      "success_rate": 0.95,
      "how": "Reduce K to be less than or equal to N: `k = min(k, len(data))` before calling kmeans.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Collect more data points to ensure N >= K, e.g., by increasing the sample size in data acquisition.",
      "success_rate": 0.85,
      "how": "Collect more data points to ensure N >= K, e.g., by increasing the sample size in data acquisition.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Add a check before kmeans: `if len(samples) < k: raise ValueError('Not enough samples for k-means')`.",
      "success_rate": 0.9,
      "how": "Add a check before kmeans: `if len(samples) < k: raise ValueError('Not enough samples for k-means')`.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Reduce K to be less than or equal to N: `k = min(k, len(data))` before calling kmeans.",
    "Collect more data points to ensure N >= K, e.g., by increasing the sample size in data acquisition.",
    "Add a check before kmeans: `if len(samples) < k: raise ValueError('Not enough samples for k-means')`."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://docs.opencv.org/4.x/d5/d38/group__core__cluster.html#ga9a34e5b5b9e6a8c6f8b0e0d5c5a5b5a",
  "official_doc_section": null,
  "error_code": "-215",
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.95,
  "resolvable": "true",
  "first_seen": "2024-01-10",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}