Giter Site home page Giter Site logo

gulraizchoudhary / random-swap-clustering-algorithm Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 552 KB

The "Random Swap" algorithm with a random dataset, visuals and example notebooks

Python 100.00%
clustering clustering-algorithm clustering-evaluation clustering-methods python python3 sklearn

random-swap-clustering-algorithm's Introduction

Random Swap Clustering Algorithm

Random swap is a kmeans variant that does not stuck in local optima and finds the optimal partitions by removing a random centroid and adding a random centroid. This strategy work pretty well. You just need to book keep the successful swaps where distortion value reduced further.

Example 1: Random swap relative SSE improvements over kmeans using random dataset

from rs import rs
import numpy as np
from sklearn.cluster import KMeans

#random 2D data set
X=np.random.rand(1000,2)

# number of centroids
k=100
swaps = 1000

for i in range(5):
    randomSwap = rs(n_clusters=k).fit(X, swaps)

    km = KMeans(n_clusters=k, init='random').fit(X)
    
    # relative SSE improvement of random swap over kmeans
    imp = 1 - randomSwap.inertia_/km.inertia_
    print(f"SSE improvement over k-means: {imp:.2%}")

Output

SSE improvement over k-means: 4.51%
SSE improvement over k-means: 3.10%
SSE improvement over k-means: 3.73%
SSE improvement over k-means: 1.93%
SSE improvement over k-means: 5.79%

Visuals

"Kmeans results on iteration: 0" "Random Swap results on iteration: 0"

"Kmeans results on iteration: 1" "Random Swap results on iteration: 1"

"Kmeans results on iteration: 2" "Random Swap results on iteration: 2"

"Kmeans results on iteration: 3" "Random Swap results on iteration: 3"

"Kmeans results on iteration: 4" "Random Swap results on iteration: 4"

Example 2: Random swap relative SSE improvements over kmeans++ using random dataset

from rs import rs
import numpy as np
from sklearn.cluster import KMeans

#random 2D data set
X=np.random.rand(1000,2)

# number of centroids
k=100
swaps = 20000

for i in range(5):
    randomSwap = rs(n_clusters=k).fit(X, swaps)

    km = KMeans(n_clusters=k, init='random').fit(X)
    
    # relative SSE improvement of random swap over kmeans
    imp = 1 - randomSwap.inertia_/km.inertia_
    print(f"SSE improvement over k-means: {imp:.2%}")

Output

SSE improvement over k-means++: 1.69%
SSE improvement over k-means++: -1.98%
SSE improvement over k-means++: 0.42%
SSE improvement over k-means++: 1.18%
SSE improvement over k-means++: -1.18%

Acknowledgements

Credit goes to Pasi Fränt, You may consider to read his paper for more understanding "Efciency of random swap clustering", also credit to the scikit-learn team for their excellent sklearn.cluster.KMeans class.

License

MIT

random-swap-clustering-algorithm's People

Contributors

gulraizchoudhary avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.