K MEANS E AND M STEP (image segmentation also)
import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import make_blobs X,y = make_blobs(n_samples=500,n_features=2,centers=5,random_state=13) print(X.shape,y.shape) plt.figure(0) plt.scatter(X[:,0],X[:,1],cmap=y) plt.grid(True) plt.show() #this is an unsupervised learning algorithm we are not going to give y values but only x #we will give y later for accuracy color = ['red','green','yellow','orange','blue'] k=5 clusters={} for kx in range(k): center = 10*(2*np.random.random((X.shape[1],))-1) #this generates 5 vectors in the range(-10,10) points = [] cluster = { 'center':center, 'color':color[kx], 'points':points } clusters[kx] = cluster def distance(v1,v2): return np.sqrt(np.sum((v1-v2)**2)) """ K means is an special case of expectati...