{"id":9872,"library":"kmeans-pytorch","title":"KMeans-PyTorch","description":"K-means-pytorch provides a K-means clustering algorithm implementation built on top of PyTorch, enabling GPU acceleration for faster computations. The current version is 0.3, with releases occurring infrequently, often driven by new feature additions or argument clarifications rather than a fixed schedule.","status":"active","version":"0.3","language":"python","source_language":"en","source_url":"https://github.com/subhadarship/kmeans_pytorch","tags":["machine-learning","clustering","pytorch","gpu"],"install":[{"cmd":"pip install kmeans-pytorch","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core dependency for tensor operations and GPU acceleration.","package":"torch","optional":false}],"imports":[{"wrong":"from kmeans_pytorch import kmeans","symbol":"kmeans","correct":"from kmeans_pytorch import kmeans"}],"quickstart":{"code":"import torch\nfrom kmeans_pytorch import kmeans\n\n# 0. Generate some random data\nnum_samples = 1000\nnum_features = 2\nX = torch.randn(num_samples, num_features, device='cpu', dtype=torch.float)\n\n# Add some clusters\nX[:300] += 5\nX[300:600] -= 5\nX[600:] += torch.tensor([0, 10], dtype=torch.float)\n\nnum_clusters = 3\ntolerance = 1e-4\nmax_iterations = 500\ndistance_metric = 'euclidean'\ndevice = 'cpu' # Change to 'cuda:0' if a GPU is available\n\n# 1. Run K-means\ncluster_ids_x, cluster_centers = kmeans(\n    X=X,\n    num_clusters=num_clusters,\n    distance=distance_metric,\n    tol=tolerance,\n    max_iter=max_iterations,\n    device=device\n)\n\nprint(f\"Cluster IDs shape: {cluster_ids_x.shape}\")\nprint(f\"Cluster Centers shape: {cluster_centers.shape}\")\nprint(f\"First 5 cluster IDs: {cluster_ids_x[:5]}\")\nprint(f\"Cluster centers:\\n{cluster_centers}\")","lang":"python","description":"This example demonstrates how to generate sample data, run the `kmeans` algorithm, and retrieve the cluster assignments and final cluster centers. Remember to adjust the `device` parameter ('cpu' or 'cuda:0') based on your hardware."},"warnings":[{"fix":"Pass `device='cuda:0'` to the `kmeans` function to leverage GPU acceleration. Ensure your input tensor `X` is also on the correct device (e.g., `X = X.to('cuda:0')`).","message":"Performance on large datasets will be significantly impacted if you forget to specify `device='cuda:0'` when a GPU is available. The default device is CPU, which is much slower for heavy computations.","severity":"gotcha","affected_versions":"0.2+"},{"fix":"Ensure your input tensor `X` has the same `dtype` (e.g., `torch.float` or `torch.double`) as expected by PyTorch operations within the library. Explicitly cast `X` if necessary: `X = X.to(dtype=torch.float)`.","message":"Mismatched data types (e.g., `torch.float` vs `torch.double`) between the input tensor `X` and internally generated tensors can cause `RuntimeError: Input type (Float) and weight type (Double) should be the same`.","severity":"gotcha","affected_versions":"0.2+"},{"fix":"Always explicitly pass all desired arguments (e.g., `num_clusters`, `distance`, `tol`, `max_iter`, `device`) rather than relying on assumed defaults to ensure consistent behavior across updates.","message":"While not explicitly documented as breaking changes, minor releases might introduce or clarify argument names, types, and default values for the `kmeans` function. For instance, the exact default values for `tol`, `max_iter`, or the `distance` metric might subtly change.","severity":"breaking","affected_versions":"Prior to 0.3, possibly minor revisions within 0.3."}],"env_vars":null,"search_vec":"'0.3':31 'acceler':23 'addit':41 'algorithm':14 'argument':43 'built':16 'clarif':44 'cluster':13,53 'comput':26 'current':28 'driven':37 'enabl':21 'faster':25 'featur':40 'fix':48 'gpu':22,55 'implement':15 'infrequ':35 'k':5,11 'k-mean':10 'k-means-pytorch':4 'kmean':2 'kmeans-pytorch':1 'learn':52 'machin':51 'machine-learn':50 'mean':6,12 'new':39 'occur':34 'often':36 'provid':8 'pytorch':3,7,20,54 'rather':45 'releas':33 'schedul':49 'top':18 'version':29","created_at":"2026-04-17T01:21:09.728510+00:00","updated_at":"2026-04-17T01:21:09.728510+00:00","problems":{"verify_error":"Traceback (most recent call last):\n  File \"<string>\", line 1, in <module>\n  File \"/tmp/tmpakh5_esr/venv/lib/python3.12/site-packages/kmeans_pytorch/__init__.py\", line 1, in <module>\n    import numpy as np\nModuleNotFoundError: No module named 'numpy'"},"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.3","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/subhadarship/kmeans_pytorch","docs":null,"changelog":null,"pypi":"https://pypi.org/project/kmeans-pytorch/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["ai-ml"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"import_fail","verified_at":"2026-07-03","last_verified":"2026-07-03","next_check":"2026-07-10","install_tag":null}}