K-Means python implementation with usage example. Coded for learning purposes.
This project contains two main files:
- kmeans module can be used in code by incorporating it in the project and usual importing.
- clustering_example.py is a small interactive console application created for demonstration purposes.
kmeans.py
uses the numpy package, therefore you need to have it installed in your environment before running the module from your code or interpreter.
To run the k-means algorithm from this module in your code or intepreter session just call the function:
kmeans(k, data, min_vals, max_vals, max_iter=50)
with following parameters:
- k - number of clusters,
- d - data set, should be ndarray of shape (n, d) where n is the number of data entries, and d is the number of dimensions of each data entry,
- min_vals, max_vals - array like lists that store min and max values for each dimension,
- max_iter - maximal number of iterations.
kmeans() returns ndarray of shape (n, d) containing labels for every entry from the data set.
To see the example of clustering that utilizes kmeans module, run:
clustering_example.py
To run this example project, Python 3.6 with numpy and PIL packages installed is needed.
Copyright 2018, Ada Piekarska, All rights reserved