<jupyter_start><jupyter_text>K-means clusteringUnsupervised method- Initialize $k$ cluster centers- Associations: Find the points closest to each cluster center and form groups- Recalculate means: Set the cluster center to the mean of each group<jupyter_code>%matplotlib inline
import numpy as np
import pandas as pd
from IPython import display
import matplotlib.pylab as plt

import time
from IPython import display

N = 100
D = 2

# Generate a data set
X1 = np.random.randn(D,N/2)
X2 = 0.8*np.random.randn(D,N/2) + 3*np.ones((D, N/2))
X = np.hstack((np.mat(X1), np.mat(X2)))

K = 5;

mu = X[:,0:K]
#plt.plot(mu[0,:],mu[1,:],'ro')

# Number of epochs
EP = 20

fig = plt.figure(figsize=(8,10))
plt.plot(X[0,:], X[1,:],'kx')

ax = fig.gca()
ln = plt.Line2D(xdata=mu[0,:], ydata=mu[1,:], marker='o', color='r',linestyle=None,linewidth=0)
ax.add_line(ln)

for e in range(EP):
    
    dist = np.zeros((N,K))
    for i in range(N):
        for c in range(K):
            err = X[:,i]-mu[:,c]
            dist[i,c] = float(err.T*err)
    
    # Assignments
    a = np.argmin(dist, axis=1)
    
    mu = np.mat(np.zeros((D,K)))
    count = np.zeros((K))
    for i,c in enumerate(a):
        count[c] += 1
        mu[:,c] = (count[c]-1)/count[c]*mu[:,c] + 1./count[c]*X[:,i]
    
    ln.set_xdata(mu[0,:])
    ln.set_ydata(mu[1,:])
    
    #plt.subplot(EP,1,e+1)
    display.clear_output(wait=True)
    display.display(plt.gcf())
    time.sleep(0.1)
    plt.plot(X[0,:], X[1,:],'kx')
    #plt.plot(mu[0,:],mu[1,:],'ro')

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1967845659163987, 'fraction_numerical': 0.02765273311897106, 'mean_word_length': 3.4457142857142857, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '666519', 'license': 'MIT', 'n_tokens_mistral': 695, 'n_tokens_neox': 677, 'n_words': 138, 'path': 'Kmeans.ipynb', 'repo_name': 'bkoyuncu/notes', 'size': '1555'}