{"id":21373238,"url":"https://github.com/joeylemon/python-kmeans","last_synced_at":"2026-05-21T04:06:50.539Z","repository":{"id":85081600,"uuid":"412105553","full_name":"joeylemon/python-kmeans","owner":"joeylemon","description":"an implementation of the K-means clustering algorithm in Python","archived":false,"fork":false,"pushed_at":"2021-10-06T00:33:08.000Z","size":1561,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-16T09:13:05.340Z","etag":null,"topics":["k-means-clustering","matplotlib","ml","numpy","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joeylemon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-09-30T14:50:08.000Z","updated_at":"2021-10-06T00:33:11.000Z","dependencies_parsed_at":null,"dependency_job_id":"1b357fed-aa96-460c-bf5c-5fe118117876","html_url":"https://github.com/joeylemon/python-kmeans","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/joeylemon/python-kmeans","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joeylemon%2Fpython-kmeans","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joeylemon%2Fpython-kmeans/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joeylemon%2Fpython-kmeans/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joeylemon%2Fpython-kmeans/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joeylemon","download_url":"https://codeload.github.com/joeylemon/python-kmeans/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joeylemon%2Fpython-kmeans/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33288189,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-21T02:57:32.698Z","status":"ssl_error","status_checked_at":"2026-05-21T02:57:31.990Z","response_time":62,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["k-means-clustering","matplotlib","ml","numpy","python"],"created_at":"2024-11-22T08:26:38.873Z","updated_at":"2026-05-21T04:06:50.509Z","avatar_url":"https://github.com/joeylemon.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# python-kmeans\n\nAn implementation of the [K-means clustering](https://en.wikipedia.org/wiki/K-means_clustering) unsupervised machine learning algorithm used to reduce the number of colors required to represent an image.\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://user-images.githubusercontent.com/8845512/135511666-b6965ffc-4baf-4d83-9c50-718a4b9db037.png\" /\u003e\n\u003c/p\u003e\n\n## Motivation\n\nAs students in COSC425: Introduction to Machine Learning at the [University of Tennessee](https://utk.edu/), we were tasked with implementing the K-means algorithm from scratch in Python. We were then to use the algorithm to determine the best set of RGB colors to represent a given image. Finally, we had to analyze the performance of our algorithm by observing how quickly the clusters reached a centroid convergence and the distribution of pixels and their corresponding clusters. We performed our algorithms on images with K values of 4, 16, or 32. We set a max iteration count of 24, and we determined convergence with a max RGB value delta of 1.\n\n## Analysis\n\nFor the image above of [Smokey](https://en.wikipedia.org/wiki/Smokey_(mascot)), the mascot of the University of Tennessee, we can plot how quickly a set of K values reach a cluster convergence. We can also observe the distribution of pixels and their corresponding clusters:\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://user-images.githubusercontent.com/8845512/135512789-297fdbe2-77c9-4eb6-9936-384cc03f074e.png\" /\u003e\n\u003c/p\u003e\n\nAs a further step to understanding how our K-means algorithm reaches a conclusion, we can plot the image's RGB values in 3D and observe how the cluster centroids converge as the algorithm iterates. The below figure observes a separate run of the algorithm with K=4 on the image of Smokey:\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://user-images.githubusercontent.com/8845512/135518453-caeb6851-e6f4-4c35-8485-5cf1700f301c.jpg\" /\u003e\n\u003c/p\u003e\n\n## How to Run\n\nTo run the program on other images and K values, edit the main function of `kmeans.py` with the appropriate values:\n\n```py\nif __name__ == \"__main__\":\n    k_values = [\n        {\"K\": 4, \"color\": \"blue\", \"xticks\": [1, 2, 3, 4]},\n        {\"K\": 16, \"color\": \"red\", \"xticks\": [1, 8, 16]},\n        {\"K\": 32, \"color\": \"green\", \"xticks\": [1, 8, 16, 32]}\n    ]\n\n    perform_comparison(\"images/baboon.jpeg\", k_values)\n    perform_comparison(\"images/rocket.jpeg\", k_values)\n    perform_comparison(\"images/smokey.jpeg\", k_values)\n    perform_comparison(\"images/truck.jpeg\", k_values)\n```\n\nThen, execute the script:\n```sh\n\u003e python kmeans.py\n```\n\nThe program is accompanied by a unit test to ensure the image reduction is working correctly. You can run the test with:\n```sh\n\u003e python test.py\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoeylemon%2Fpython-kmeans","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoeylemon%2Fpython-kmeans","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoeylemon%2Fpython-kmeans/lists"}