In [1]:
d_small = np.loadtxt("data/small_2dclust.dat", delimiter=' ')
plt.figure(figsize=(10,8))
plt.scatter(x=d_small[:,1],
y=d_small[:,2],
color='blue',
marker='o',
s=200,
alpha=0.4
)
labels = ['{0}'.format(i) for i in range(d_small.shape[0])]
for label, x, y in zip(labels, d_small[:, 1], d_small[:, 2]):
plt.annotate(
label,
xy = (x, y), xytext = (-20, 20),
textcoords = 'offset points', ha = 'right', va = 'bottom',
bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5),
arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))
plt.title('A Toy Dataset', size=20)
plt.xlabel('$x_1$', size=25)
plt.ylabel('$x_2$', size=25)
plt.tick_params(axis='both', which='major', labelsize=18)
plt.show()
## Computing the densities
den_small = cal_density(d_small[:,1:3], 1.5)
# Computing the minimum distance to high density peaks
mdist2peaks_small = cal_minDist2Peaks(d_small, den_small)
import prettytable
pt = prettytable.PrettyTable
In [2]:
import prettytable
pt = prettytable.PrettyTable(["Index", "Density", "Min. Dist."])
for i in range(d_small.shape[0]):
pt.add_row([i, den_small[i], mdist2peaks_small[i]])
print(pt)
print(den_small)
print(mdist2peaks_small)
plot_decisionGraph(den_small, mdist2peaks_small, 1.7)
In [ ]:
def find_nearestNeigb(d, den_arr, centroids):
## finding the nearest neighbor of each point
n = d.shape[0]
nearestNeigb = np.repeat(-1, n)
for i in range(n):
if i in centroids:
nearestNeigb[i] = i
else:
mindist = 999
indxNeigb = -1
for j in range(n):
if i != j:
dist_ij = euclidean_dist(d[i,:], d[j,:])
if dist_ij <= mindist:
if den_arr[i] > den_arr[j]:
mindist = dist_ij
indxNeigb = j
elif den_arr[i] == den_arr[j]:
if j in centroids:
mindist = dist_ij
indxNeigb = j
#find_nearestNeigb(d_small, den_small, np.array())