Skip to content

Instantly share code, notes, and snippets.

import numpy as np
import cv2
import os, sys
from matplotlib import pyplot as plt
def gen_data(data_points,loc):
x_center = loc[0]
y_center = loc[1]
x = np.random.normal(size= data_points, loc= x_center)*100
y = np.random.normal(size= data_points, loc= y_center)*100
data= np.vstack((x,y))
data = data.astype(np.int32)
x,y = data
return x, y
results_dir= 'results'
if not os.path.exists(results_dir):
os.mkdir(results_dir)
x_data = np.array((),dtype=np.int32)
y_data = np.array((),dtype=np.int32)
#...................................
x, y = gen_data(100,[5,5]) # generating 100 data points around the center [5,5]
plt.scatter(x, y)
x_data = np.hstack((x_data, x))
# finding the extremes of the data points so that this information
# could be used to initialize the k cluster centers within these extremes.
x_min= np.min(x_data)
x_max= np.max(x_data)
y_min= np.min(y_data)
y_max= np.max(y_data)
# we define a list of colors which could be used to represent data points assigned to different clusters.
color_list = ['Blue', 'Green', 'Yellow', 'Pink', 'Purple']
def display_kcenters(kcenters):
for i in range(len(kcenters)):
plt.scatter(kcenters[i][0], kcenters[i][1], color='Red')
#plt.show()
# setting the color of all the data points to 'Gray' at the beginning
initial_data_point_color = 'Gray'
display_kcenters(kcenters)
plt.scatter(x_data, y_data, color = initial_data_point_color)
plt.title('before kMeans Clustering')
plt.xlabel('Some Feature x')
plt.ylabel('Some Feature y')
plt.grid()
color_info_list = [initial_data_point_color]*len(x_data)
def get_mean_shit_value(old_point, new_point):
mean_shift_value = np.sqrt( np.square(old_point[0] - new_point[0]) + np.square(old_point[1] - new_point[1]) )
return mean_shift_value
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.
class NeuralNet(nn.Module):
def __init__(self):
super(NeuralNet, self).__init__()
self.sequential = nn.Sequential(nn.Conv2d(1, 32, 5),
nn.Conv2d(32, 64, 5),
nn.Dropout(0.3))
self.layer1 = nn.Conv2d(64, 128, 5)
self.layer2 = nn.Conv2d(128, 256, 5)
self.fc = nn.Linear(256*34*34, 128)