hacksider/Deep-Live-Cam · error · ValueError

max_k must be at least 1

Error message

max_k must be at least 1

What it means

ValueError raised by find_cluster_centroids in modules/cluster_analysis.py when the max_k argument is less than 1. max_k caps how many candidate cluster counts (K = 1..max_k) the elbow search evaluates, so 0 or negative values make the search range empty and meaningless. The function validates this up front and fails fast.

Source

Thrown at modules/cluster_analysis.py:11

import numpy as np
from sklearn.cluster import KMeans
from typing import Any


def find_cluster_centroids(embeddings, max_k=10) -> Any:
    n_samples = len(embeddings)
    if n_samples == 0:
        raise ValueError("embeddings must not be empty")
    if max_k < 1:
        raise ValueError("max_k must be at least 1")

    max_k = min(max_k, n_samples)
    if max_k == 1:
        kmeans = KMeans(n_clusters=1, random_state=0)
        kmeans.fit(embeddings)
        return kmeans.cluster_centers_

    inertia = []
    cluster_centroids = []
    K = range(1, max_k+1)

    for k in K:
        kmeans = KMeans(n_clusters=k, random_state=0)
        kmeans.fit(embeddings)
        inertia.append(kmeans.inertia_)
        cluster_centroids.append({"k": k, "centroids": kmeans.cluster_centers_})

    diffs = [inertia[i] - inertia[i+1] for i in range(len(inertia)-1)]

View on GitHub (pinned to 987f6b392b)

Solutions

  1. Pass an explicit max_k >= 1 (the default of 10 is safe; it is internally clamped via min(max_k, n_samples)).
  2. If max_k is computed, clamp it: max_k = max(1, min(max_k, n_samples)) before the call.
  3. Fix the config/CLI source that produced 0 or a negative value.

Example fix

# before
max_k = len(embeddings) // 10
centroids = find_cluster_centroids(embeddings, max_k)

# after
max_k = max(1, len(embeddings) // 10)
centroids = find_cluster_centroids(embeddings, max_k)
Defensive patterns

Strategy: validation

Validate before calling

max_k = max(1, min(max_k, len(embeddings))) if embeddings else 1
centroids = find_cluster_centroids(embeddings, max_k=max_k)

Try / catch

try:
    centroids = find_cluster_centroids(embeddings, max_k=k)
except ValueError as e:
    if "max_k must be at least 1" in str(e):
        k = max(1, k)
        centroids = find_cluster_centroids(embeddings, max_k=k)
    else:
        raise

Prevention

When it happens

Trigger: Calling find_cluster_centroids(embeddings, max_k=0) or with a negative max_k. Most often max_k is computed from data or config (e.g. max_k = n // 10, or a CLI/config value) and the computation yields 0 for a small n or a misconfigured setting.

Common situations: max_k derived as a fraction of sample count with small inputs (n=5, max_k=n//10 -> 0); a config file or CLI flag with an unset/zero k value; unit tests parameterized with edge-case k values; off-by-one when converting an inclusive/exclusive upper bound.

Related errors


AI-assisted analysis of hacksider/Deep-Live-Cam@987f6b392b (2026-08-14). Data as JSON: /api/errors/d7e23d432fb21f17. Report an issue: GitHub.