kmeans-tjdwill


Namekmeans-tjdwill JSON
Version 1.0.3 PyPI version JSON
download
home_pageNone
SummaryA function-based implementation of k-means clustering that maintains data association.
upload_time2024-04-29 03:06:44
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseMIT License Copyright (c) 2024 tjdwill Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords clustering computer vision data analysis data processing k-means linear algebra robotics
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # K-Means Clustering

[![PyPI version](https://badge.fury.io/py/kmeans-tjdwill.svg)](https://badge.fury.io/py/kmeans-tjdwill)
[![Docs](https://github.com/tjdwill/kmeans/actions/workflows/sitebuild.yml/badge.svg)](https://tjdwill.github.io/kmeans) 


A repository documenting the implementation of k-Means clustering in Python. Usage examples can be found in the `tests` directory.


The thing that makes this k-means clustering module different from others is that it allows the user to specify the number of dimensions to use for the clustering operation.

For example, given some data where each element is of form 
```python
# Each element would actually be a Numpy array, but the following uses lists for readability.
[
  [1, 2, 3, 4, 5],
  [4, 6, 7, 8, 2],
  ...
]
```
specifying `ndim=3` will result in only the first three elements of each data point being used for each operation.

This is useful for maintaining data association where it otherwise would be shuffled. An example of this is found in my implementation of image segmentation (`segmentation.py`) in this same project.
Other examples of use could be for maintaining data association in object detection elements. Given some 
```python
[xmin, ymin, xmax, ymax, conf, label]  # [bounding box, conf, label]
```
we may want to cluster the data solely on bounding box information while also maintaining the confidence intervals for each detection for further processing.

---

## How it Works

Specifying the `k` value results in a `dict[int: NDArray]` where each `NDArray` contains the elements within the cluster. The keys of this dict range from `0` to `k-1`, allowing the key to also be used to index the corresponding cluster centroid from the centroid array.

Here is an example of the use of the `cluster` function:

```python
import numpy as np
from kmeans import cluster

data = np.random.random((10000, 7))

# Only cluster along first three elements of a given data point.
# Choose initial_means randomly (default)
clusters, centroids = cluster(data, k=4, ndim=3, tolerance=0.001)

for key in clusters:
  cluster = clusters[key]
  centroid = centroids[key]
  print(cluster, centroid, sep="\n")
```

---

## Features

- k-means clustering (no side-effects)
- k-means clustering w/ animation
  - (2-D & 3-D)
- image segmentation via `kmeans.segmentation.segment_img` function


### k-means Animation

Using the `view_clustering` function

#### 2-D Case (Smallest Tolerance Possible)

[kmeans2D_animate.webm](https://github.com/tjdwill/KMeans_Clustering/assets/118497355/0584a4d1-268d-4785-b05e-319d54a28de1)

#### 3-D Case (Tolerance = 0.001)

[kmeans3D_animate.webm](https://github.com/tjdwill/KMeans_Clustering/assets/118497355/a542b606-0844-427e-bfef-243e6f1ceffc)

### Image Segmentation

Perform image segmentation based on color groups specified by the user.

Two options:

#### Averaged Colors

k=4

![seg_groups04](https://github.com/tjdwill/KMeans_Clustering/assets/118497355/9b468213-6983-4c66-8f93-de6e58a736a1)

k=10

![seg_groups10](https://github.com/tjdwill/KMeans_Clustering/assets/118497355/91fc5e42-4c2e-49bf-a24f-9926565a1a6c)

#### Random Colors

k=4

![seg_rand_groups04_cpy](https://github.com/tjdwill/KMeans_Clustering/assets/118497355/33cee3ba-0a7d-4c12-9f34-7c140376f24b)

---

## Developed With
* Python (3.12.1)
* Numpy (1.26.2) 
* Matplotlib (3.8.4)

However, no features specific to Python 3.12 were used.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "kmeans-tjdwill",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "clustering, computer vision, data analysis, data processing, k-means, linear algebra, robotics",
    "author": null,
    "author_email": "Terrance Williams <tjdwill.gh@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/fa/36/c0b82dc68015503402592bc08def84a03ac0694d6bbc83e2e18e01daa0d8/kmeans_tjdwill-1.0.3.tar.gz",
    "platform": null,
    "description": "# K-Means Clustering\n\n[![PyPI version](https://badge.fury.io/py/kmeans-tjdwill.svg)](https://badge.fury.io/py/kmeans-tjdwill)\n[![Docs](https://github.com/tjdwill/kmeans/actions/workflows/sitebuild.yml/badge.svg)](https://tjdwill.github.io/kmeans) \n\n\nA repository documenting the implementation of k-Means clustering in Python. Usage examples can be found in the `tests` directory.\n\n\nThe thing that makes this k-means clustering module different from others is that it allows the user to specify the number of dimensions to use for the clustering operation.\n\nFor example, given some data where each element is of form \n```python\n# Each element would actually be a Numpy array, but the following uses lists for readability.\n[\n  [1, 2, 3, 4, 5],\n  [4, 6, 7, 8, 2],\n  ...\n]\n```\nspecifying `ndim=3` will result in only the first three elements of each data point being used for each operation.\n\nThis is useful for maintaining data association where it otherwise would be shuffled. An example of this is found in my implementation of image segmentation (`segmentation.py`) in this same project.\nOther examples of use could be for maintaining data association in object detection elements. Given some \n```python\n[xmin, ymin, xmax, ymax, conf, label]  # [bounding box, conf, label]\n```\nwe may want to cluster the data solely on bounding box information while also maintaining the confidence intervals for each detection for further processing.\n\n---\n\n## How it Works\n\nSpecifying the `k` value results in a `dict[int: NDArray]` where each `NDArray` contains the elements within the cluster. The keys of this dict range from `0` to `k-1`, allowing the key to also be used to index the corresponding cluster centroid from the centroid array.\n\nHere is an example of the use of the `cluster` function:\n\n```python\nimport numpy as np\nfrom kmeans import cluster\n\ndata = np.random.random((10000, 7))\n\n# Only cluster along first three elements of a given data point.\n# Choose initial_means randomly (default)\nclusters, centroids = cluster(data, k=4, ndim=3, tolerance=0.001)\n\nfor key in clusters:\n  cluster = clusters[key]\n  centroid = centroids[key]\n  print(cluster, centroid, sep=\"\\n\")\n```\n\n---\n\n## Features\n\n- k-means clustering (no side-effects)\n- k-means clustering w/ animation\n  - (2-D & 3-D)\n- image segmentation via `kmeans.segmentation.segment_img` function\n\n\n### k-means Animation\n\nUsing the `view_clustering` function\n\n#### 2-D Case (Smallest Tolerance Possible)\n\n[kmeans2D_animate.webm](https://github.com/tjdwill/KMeans_Clustering/assets/118497355/0584a4d1-268d-4785-b05e-319d54a28de1)\n\n#### 3-D Case (Tolerance = 0.001)\n\n[kmeans3D_animate.webm](https://github.com/tjdwill/KMeans_Clustering/assets/118497355/a542b606-0844-427e-bfef-243e6f1ceffc)\n\n### Image Segmentation\n\nPerform image segmentation based on color groups specified by the user.\n\nTwo options:\n\n#### Averaged Colors\n\nk=4\n\n![seg_groups04](https://github.com/tjdwill/KMeans_Clustering/assets/118497355/9b468213-6983-4c66-8f93-de6e58a736a1)\n\nk=10\n\n![seg_groups10](https://github.com/tjdwill/KMeans_Clustering/assets/118497355/91fc5e42-4c2e-49bf-a24f-9926565a1a6c)\n\n#### Random Colors\n\nk=4\n\n![seg_rand_groups04_cpy](https://github.com/tjdwill/KMeans_Clustering/assets/118497355/33cee3ba-0a7d-4c12-9f34-7c140376f24b)\n\n---\n\n## Developed With\n* Python (3.12.1)\n* Numpy (1.26.2) \n* Matplotlib (3.8.4)\n\nHowever, no features specific to Python 3.12 were used.\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024 tjdwill  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "A function-based implementation of k-means clustering that maintains data association.",
    "version": "1.0.3",
    "project_urls": {
        "Homepage": "https://github.com/tjdwill/kmeans",
        "Issues": "https://github.com/tjdwill/kmeans/issues"
    },
    "split_keywords": [
        "clustering",
        " computer vision",
        " data analysis",
        " data processing",
        " k-means",
        " linear algebra",
        " robotics"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3bfcf6882b63ddc6ac6f22e2eb0943585faa85dbb6f5079405f61435049813fc",
                "md5": "53d2ec758975326c61eecd4ef7acc5a1",
                "sha256": "0651dc529dbfb35478d45947cea9ab061e30ea994136407322fd037f0023c803"
            },
            "downloads": -1,
            "filename": "kmeans_tjdwill-1.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "53d2ec758975326c61eecd4ef7acc5a1",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 12322,
            "upload_time": "2024-04-29T03:06:42",
            "upload_time_iso_8601": "2024-04-29T03:06:42.882554Z",
            "url": "https://files.pythonhosted.org/packages/3b/fc/f6882b63ddc6ac6f22e2eb0943585faa85dbb6f5079405f61435049813fc/kmeans_tjdwill-1.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fa36c0b82dc68015503402592bc08def84a03ac0694d6bbc83e2e18e01daa0d8",
                "md5": "5c90d4ece83de3242f86d6273ec3d56c",
                "sha256": "2d0e00aab7de1c5ceeb8bf37a7fc69c8f07e9c5526ca32b1e69b4bfa9e4c27a8"
            },
            "downloads": -1,
            "filename": "kmeans_tjdwill-1.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "5c90d4ece83de3242f86d6273ec3d56c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 81881,
            "upload_time": "2024-04-29T03:06:44",
            "upload_time_iso_8601": "2024-04-29T03:06:44.424766Z",
            "url": "https://files.pythonhosted.org/packages/fa/36/c0b82dc68015503402592bc08def84a03ac0694d6bbc83e2e18e01daa0d8/kmeans_tjdwill-1.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-29 03:06:44",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "tjdwill",
    "github_project": "kmeans",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "kmeans-tjdwill"
}
        
Elapsed time: 0.30832s