What is kmeans?
Tags: python, ai, machinelearning
Published: 2019-07-24 16:13:39

---
title: What is kmeans?
published: true
description: python, machine learning
tags: python, ai, ml
cover_image: https://proxy.duckduckgo.com/iu/?u=https%3A%2F%2Ftse4.mm.bing.net%2Fth%3Fid%3DOIP.2vzKvKrj3uyZCoWwIzObBgHaEI%26pid%3DApi&f=1
---
kmeans is a clustering algorithm for <a href="https://pythonprogramminglanguage.com/python-machine-learning/">Machine Learning</a> purposes. A cluster is another word for group.

So what do I mean with kmeans?

### Starting situation 

The goal of the algorithm is to find groups in datapoints.  In the context of machine learning, you measure data.

For instance, you could measure height and salary. Then you would have 2d vectors (height,salary) that you could plot on a 2d plot.


There are K groups, or formally, K clusters. There must be at least 2 groups, because 1 group doesn't need the algorithm to run.

<img src="https://thepracticaldev.s3.amazonaws.com/i/kfuoau5ga91ehrpx681x.png">

### End goal

Based on the previous input (the data points). Then the algorithm will find K groups, in this case K=3.

<img src="https://thepracticaldev.s3.amazonaws.com/i/3uklv1e2xs8xcez5tkba.png">

So now if you have a new data point, the group that's closest is the type. Which K is best? that's a bit of a searching quest.

<img src="https://proxy.duckduckgo.com/iu/?u=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FpnzUNWmqaPE4w%2Fgiphy.gif&f=1">

### The code

The program below uses Machine Learning and the modules sklearn, matplotlib to create the plots and run the kmeans algorithm. It creates the images shown in this article.

    #!/usr/bin/python3
    from sklearn.datasets import make_blobs
    from matplotlib import pyplot as plt
    from sklearn.cluster import KMeans
    
    X, y = make_blobs(n_samples=200,
                      n_features=2,
                      centers=4,
                      cluster_std=1,
                      center_box=(-10.0, 10.0),
                      shuffle=True,
                      random_state=1)
    
    plt.figure(figsize=(6, 4), dpi=144)
    plt.xticks(())
    plt.yticks(())
    plt.scatter(X[:, 0],X[:, 1], s=20, marker='o')
    plt.show()
    
    n_cluster = 3
    k_means = KMeans(n_clusters=n_cluster)
    k_means.fit(X)
    print("kmean: k={}, cost={}".format(n_cluster, int(k_means.score(X))))
    
    labels = k_means.labels_
    centers = k_means.cluster_centers_
    
    markers = ['o', '^', '*']
    colors = ['r', 'b', 'y']
    
    plt.figure(figsize=(6, 4), dpi=144)
    plt.xticks(())
    plt.yticks(())
    
    for c in range(n_cluster):
        cluster = X[labels == c]
        plt.scatter(cluster[:, 0], cluster[:, 1], marker=markers[c], s=20, c=colors[c])
    
    plt.scatter(centers[:, 0], centers[:, 1], marker='o', c='white', alpha=0.9, s=300)
    for i, c in enumerate(centers):
        plt.scatter(c[0], c[1], marker='$%d$' % i, s=50, c=colors[i])
    
    plt.show()


Related links:
* <a href="https://scikit-learn.org/stable/index.html">Sklearn machine learning module</a>
* <a href="https://pythonprogramminglanguage.com/python-machine-learning/">Machine Learning articles</a>
* <a href="https://pythonprogramminglanguage.com/python-machine-learning/">Text clustering with kmeans</a>
* <a href="https://gumroad.com/l/MnRYU">Machine Learning course and examples</a>