class_means_dict = dict()
preds_dict = dict()
k=0
# K means for different values.
for K in range(2,6):
pred = np.zeros(shape = (N,K))
class_means = np.zeros(shape=(K,alpha))
# Initialize Class Means from different values from the dataset.
#First get the random indices.
initial_indices = np.random.choice(N,K)
#Pick the random values from the dataset based on random initialize indices.
for k in range(K):
class_means[k,:] = Z[initial_indices[k],:]
old_class_means = np.zeros(shape=(K,alpha))
while np.linalg.norm(old_class_means-class_means)/np.linalg.norm(class_means) > 0.001:
old_class_means = np.array(class_means)
for i in range(N): # Iterate over the dataset.
distances_to_mean = np.zeros(K)
# For each point in dataset compute the distance from the means.
for k in range(K):
distances_to_mean[k] = np.linalg.norm(class_means[k]-Z[i])
nearest_mean = np.argmin(distances_to_mean)
#Re-intialize the means
pred[i,:]=np.zeros(K)
# Update the means.
pred[i,nearest_mean]=1
#compute the new class means.
for k in range(K):
class_means[k] = np.mean(Z[np.where(pred[:,k]==1)],axis=0)
class_means_dict[K] = class_means
preds_dict[K] = pred
np.set_printoptions(precision=2)
probabilities = np.ndarray((3,K))
print K
for predicted in range(K):
for true_label in range(3):
probabilities[true_label,predicted]=np.intersect1d(np.where(labels[:,true_label]==1),np.where(pred[:,predicted]==1)).size/float(np.where(labels[:,true_label]==1)[0].size)
print "%.2f" %probabilities[true_label,predicted],
print '\n'