Skip to content

Instantly share code, notes, and snippets.

@n0obcoder
Last active June 20, 2019 20:34
Show Gist options
  • Save n0obcoder/049948ca6f1855d7bd5450f6642473e7 to your computer and use it in GitHub Desktop.
Save n0obcoder/049948ca6f1855d7bd5450f6642473e7 to your computer and use it in GitHub Desktop.
iterations = 100
stop=[False]*k
for n in range(iterations):
# iterate over the data points and assign them to one of the k clusters
print('iteration number {}'.format(n))
for i, (x, y) in enumerate(zip(x_data, y_data)):
cluster_index= assign_cluster(x, y, kcenters)
# updating the color_list for all the data points on the basis of the newly assigned clusters to them.
color_info_list[i] = color_list[cluster_index]
# plotting the data points with their colors representing their newly assigned clusters
plt.scatter(x_data, y_data, color= color_info_list)
# plotting the k cluster centers
display_kcenters(kcenters)
plt.title('k-Means iter {}'.format(n))
plt.xlabel('Some Feature x')
plt.ylabel('Some Feature y')
plt.grid()
plt.savefig(results_dir + '/iter{}.jpg'.format(n))
plt.show()
#calculate the new position of the centers
for i in range(len(kcenters)):
x_coords_list=[]
y_coords_list= []
for j in range(len(x_data)):
if color_info_list[j] == color_list[i]:
x_coords_list.append(x_data[j])
y_coords_list.append(y_data[j])
x_mean = np.mean(x_coords_list).astype(np.int32)
y_mean = np.mean(y_coords_list).astype(np.int32)
mean_shift_value = get_mean_shit_value(kcenters[i], [x_mean, y_mean])
if mean_shift_value == 0:
stop[i] = True
kcenters[i] = [x_mean, y_mean]
# check if all the cluster centers have converged
if sum(stop) == k:
print('all the {} cluster centers have converged on the {}th iteration'.format(k, n))
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment