<jupyter_start><jupyter_text>Topic: K-Means ClusteringSaket TiwariDate: 30 Jun, 2019<jupyter_code>#K-means is unsupervised
#no of clusters =k
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
np.random.choice([10,20,30,409,50],3) #choses randomly from list
def Kmeans(X,K,max_iters=1000): #K- starting me kitne centroids honge
    
    #X ki dimension ? (1000x2) 2columns radius,weight
    
    
    centroids=X[np.random.choice(np.arange(len(X)),K), :]
    print(centroids)
    
    for i in range(max_iters):
        
        C=[]
        
        for x in X:
            
            distances =[] 
            
            for c in centroids:
                
                d=np.dot((x-c) , (x-c))
                distances.append(np.array(d))
                #print(distances)
            #c.append(np.argmin(distances))
            
            centroid=np.argmin(distances)
            C.append(centroid)
        C=np.array(C) #because we are going to mask and mask is done only in numpy array
        centroids= [X[C==k].mean(axis=0) for k in range (K)]
        
    #c=[([np.dot(x_i-y_k , x_i-y_k) for y_i in cendroids]np.argmin) for x_i in X]
    #C=[np.argmin([np.dot])]
    
    return np.array(centroids), C
m1,cov1=[9,8],[[1.5,2],[1,2]] #1.5 ->horizontally kitan spread h 2&1->upar aur niche kitan spread h 2-> vertically kitna spread h
m2,cov2=[5,13],[[2.5,-1.5],[-1.5,1.5]]
m3,cov3=[3,7],[[0.25,0.5],[-0.1,0.5]]

data1=np.random.multivariate_normal(m1,cov1,250)
data2=np.random.multivariate_normal(m2,cov2,180)
data3=np.random.multivariate_normal(m3,cov3,100)

X=np.vstack((data3, np.vstack((data1,data2))))
np.random.shuffle(X)
centroids,C=Kmeans(X,K=3)
plt.scatter(X[:,0],X[:,-1])
plt.plot(centroids[:,0],centroids[:,-1],'r*',markersize=20)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1641954969796815, 'fraction_numerical': 0.05326743547501373, 'mean_word_length': 2.860169491525424, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, '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': '602914', 'license': 'MIT', 'n_tokens_mistral': 778, 'n_tokens_neox': 735, 'n_words': 130, 'path': 'K-Means Clustering/K-means Clustering.ipynb', 'repo_name': 'saket-iiitR/Machine-Learning-Works', 'size': '1821'}