Skip to main content

The trimmed k-means clustering method

Project description

trimkmeans

The trimmed k-means clustering method by Cuesta-Albertos, Gordaliza and Matran (1997). This optimizes the k-means criterion under trimming a portion of the points.

linting: pylint

example usage:

import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import make_blobs
from sklearn.preprocessing import StandardScaler

from trimkmeans.metrics import trimmed_kmeans_metric_unsupervised
from trimkmeans.trimkmeans import TrimKMeans

if __name__ == "__main__":

    # Create a dataset of 2D distributions
    CENTERS = 5
    X_train, true_labels = make_blobs(n_samples=100, centers=CENTERS, random_state=42)
    X_train = StandardScaler().fit_transform(X_train)
    # Fit centroids to dataset
    trimkmeans = TrimKMeans(n_clusters=CENTERS, verbose=1)
    trimkmeans.fit(X_train)
    # View results
    labels = trimkmeans.predict(X_train)
    print(f"optimal criterion value found is: {trimkmeans.crit_val}")
    print(f"sum of variances is: {trimmed_kmeans_metric_unsupervised(X_train, labels, 'sv')}")
    print(f"sum of standart deviations is : {trimmed_kmeans_metric_unsupervised(X_train, labels, 'sed')}")
    sns.scatterplot(x=[X[0] for X in X_train],
                    y=[X[1] for X in X_train],
                    hue=labels,
                    style=labels,
                    palette="deep",
                    legend=None
                    )
    plt.plot([x for x, _ in trimkmeans.cluster_centers_],
             [y for _, y in trimkmeans.cluster_centers_],
             'k+',
             markersize=10,
             )

    for idx, centroid in enumerate(trimkmeans.cluster_centers_):
        circle = plt.Circle(centroid, trimkmeans.opt_cutoff_ranges[idx], fill=False, color='r')
        plt.gca().add_patch(circle)
    plt.show()

MIT License

Copyright (c) 2023 Arthur Rieß, University of Vienna

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

trimkmeans-1.1.1.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

trimkmeans-1.1.1-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file trimkmeans-1.1.1.tar.gz.

File metadata

  • Download URL: trimkmeans-1.1.1.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.5

File hashes

Hashes for trimkmeans-1.1.1.tar.gz
Algorithm Hash digest
SHA256 4fda7d37254c328e8ebd899278c5c6fc52205d75ddb201e6c2c05393a0a02fc0
MD5 0471d27eafc86b3e5836bf85976c1f72
BLAKE2b-256 f31adb9df865da4793dc2e59b7f94cb132ef301f8a7c9e925e304e549e25f19c

See more details on using hashes here.

File details

Details for the file trimkmeans-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: trimkmeans-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.5

File hashes

Hashes for trimkmeans-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5c740d5623af181b0c6668fbe71107be51c0792259cb99c4d35a02efc9054f2d
MD5 e38cb551c1f8d565cd1737a0646f0a43
BLAKE2b-256 8022cbdefc9d9bcf379b0f005112bd7bba40f16849560a67f2333f69edb46606

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page