{"id":15049410,"url":"https://github.com/klimentlagrangiewicz/k-means-in-c","last_synced_at":"2025-09-02T13:41:26.100Z","repository":{"id":163222552,"uuid":"570445999","full_name":"KlimentLagrangiewicz/k-means-in-C","owner":"KlimentLagrangiewicz","description":"Implementation of k-means algorithm in C (standard C89/C90)","archived":false,"fork":false,"pushed_at":"2025-06-17T12:08:10.000Z","size":2621,"stargazers_count":0,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-17T13:24:35.275Z","etag":null,"topics":["ansi-c","c89","c90","clustering","clustering-algorithm","data-clustering","data-mining","k-means","k-means-algorithm"],"latest_commit_sha":null,"homepage":"","language":"C","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/KlimentLagrangiewicz.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,"zenodo":null}},"created_at":"2022-11-25T08:00:00.000Z","updated_at":"2025-06-17T12:08:14.000Z","dependencies_parsed_at":"2024-09-15T18:26:00.514Z","dependency_job_id":"a86855e0-c9b8-4db7-a3d4-1178768de0db","html_url":"https://github.com/KlimentLagrangiewicz/k-means-in-C","commit_stats":null,"previous_names":["klimentlagrangiewicz/k-means-in-c"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/KlimentLagrangiewicz/k-means-in-C","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KlimentLagrangiewicz%2Fk-means-in-C","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KlimentLagrangiewicz%2Fk-means-in-C/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KlimentLagrangiewicz%2Fk-means-in-C/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KlimentLagrangiewicz%2Fk-means-in-C/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KlimentLagrangiewicz","download_url":"https://codeload.github.com/KlimentLagrangiewicz/k-means-in-C/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KlimentLagrangiewicz%2Fk-means-in-C/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261238897,"owners_count":23128879,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["ansi-c","c89","c90","clustering","clustering-algorithm","data-clustering","data-mining","k-means","k-means-algorithm"],"created_at":"2024-09-24T21:20:14.499Z","updated_at":"2025-09-02T13:41:26.089Z","avatar_url":"https://github.com/KlimentLagrangiewicz.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# k-means-in-C\nImplementation of k-means clustering algorithm in C (standard C89/C90)\n\n## About k-means\nThe k-means algorithm is an iterative data clustering algorithm developed by Stuart Lloyd of Bell Labs in the 1950s as a technique for pulse-code modulation.  \nThe main idea of the algorithm is that at each iteration, based on the existing partitioning, the cluster centers are recalculated, then the instances are divided into clusters according to which of the new centers turned out to be closer to a specific instance according to a pre-selected metric.  \n#### Input data:\n  +  $X=\\mathrm{x}_{i=1,j=1}^{n,m}$ — description of instances, where *n* is number of instances, *m* is number of features;  \n  +  $k \\in \\left \\\\{1, \\ldots, n \\right \\\\}$ — number of clusters.  \n#### Output data:\n  +  $Y = \\left\\\\{ y_i|y_i\\in\\left\\\\{0,\\ldots,k-1\\right\\\\}, i = \\overline{\\left(1,n\\right)}\\right\\\\}$ — cluster labels.  \n#### Advantages of k-means:\n  +  Low algorithmic complexity;  \n  +  Easy to implement;  \n  +  The possibility for effective parallelization;  \n  +  The presence of many modifications.  \n#### Disadvantages of k-means:\n  +  Sensitivity to initial cluster centers;  \n  +  Algorithm k-means poorly separates closely spaced clusters with a complex structure;  \n  +  The need for preliminary determination of the number of clusters.  \n### Steps of k-means algorithm:\nStep 1. Data preparing (autoscaling): $x_{i,j}=\\frac{x_{i,j}-\\mathrm{E_{X^{j}}}}{\\sigma_{X^{j}}}$;  \nStep 2. Set initial cluster centers: $C = \\left\\\\{c_{i}| c_{i} \\in ℝ^{m}, i = \\overline{\\left(1,k\\right)} \\right\\\\}$;  \nStep 3. Calculate the initial partition: $y_{i} = \\arg\\min\\limits_{j}\\rho\\left(x_{i},c_{j} \\right)$;  \nStep 4. Calculate new cluster centers:  \n$$l_j=\\sum_{i=1}^{n}{\\left[y_i\\equiv j\\right]}$$  \n$$c_j=\\frac{1}{l_j} \\sum_{i=1}^{n}{\\left[y_i\\equiv j\\right]\\cdot x_i}$$  \nStep 5. Calculate a new split: $y_{i} = \\arg\\min\\limits_{j}\\rho\\left(x_{i},c_{j} \\right)$;  \nStep 6. Repeat steps 4, 5 until the split changes.  \nA visualisation of the first four iterations of the algorithm is shown in figure 1.  \n\u003cp align=\"center\"\u003e \u003cimg width=\"500\" height=\"500\" src=\"https://github.com/KlimentLagrangiewicz/k-means-in-C/assets/81409101/c91edbf3-5c59-4a41-b6d9-e3f57f0c6516\"\u003e \u003c/p\u003e  \n\u003cp align=\"center\"\u003eFigure 1 — Processing the Old Faithful Geyser dataset using the k-means algorithm\u003c/p\u003e  \n\n## Example of usage\nCloning project and changing current directory:\n```\ngit clone https://github.com/KlimentLagrangiewicz/k-means-in-C\ncd k-means-in-C\n```\nBuilding from source (Linux):\n```\nmake\n```\nBuilding from source (Windows):\n```\nmake windows\n```\nIf building was ok, you can find executable file in `bin` subdirectory.  \nRun the program:\n```\n./bin/k-means-in-C ./datasets/iris/data.txt 150 4 3 ./datasets/iris/new_result.txt ./datasets/iris/res.txt\n```\n```\n./bin/k-means-in-C ./datasets/wine/data.txt 178 13 3 ./datasets/wine/new_result.txt ./datasets/wine/res.txt\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fklimentlagrangiewicz%2Fk-means-in-c","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fklimentlagrangiewicz%2Fk-means-in-c","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fklimentlagrangiewicz%2Fk-means-in-c/lists"}