Example #1
0
def _nearest_neighbor(new, points):
	classe = None
	# Compute the nearest neighbor
	c = (points[0], dist(new[0], points[0][0], new[1], points[0][1]))
	for p in points:
		distance = dist(new[0], p[0], new[1], p[1])
		if(distance < c[1]):
			c = (p, distance)
	points.remove(c[0])

	# If the NN and the new point are the same, their class is the same
	if(new[0] == c[0][0] and new[1] == c[0][1]):
		classe = c[0][2]

	return c, points, classe
Example #2
0
def main(argv):
	points = [
		[.2, .25, 'red',  0],
		[.9, .25, 'red',  1],
		[.25, .75, 'red', 2],

		[.35, .5, 'blue', 3],
		[.5, .25, 'blue', 4],
		[.75, .8, 'blue', 5],
	]

	new = [.5, .5]
	k = 5

	print("----- POINTS -----\n")
	for p in points:
		print(p)

	distances = []
	for p in points:
		distances.append([p[3], dist(p[0], new[0], p[1], new[1])])

	print("\n\n----- DISTANCES BETWEEN POINTS -----\n")
	for d in distances:
		print(d)

	distances.sort(key=lambda d: d[1])
	print("\n\n----- SORTED DISTANCES -----\n")
	for d in distances:
		print(d)

	print("\n\n----- K NEAREST POINTS (K="+ str(k) +") -----\n")
	distances = distances[:k]
	for d in distances:
		print(d)

	print("\n\n----- MOST REPRESENTED CLASS -----\n")
	cl = [points[d[0]][2] for d in distances]
	print(max(cl, key=lambda c: cl.count(c)))

	
	print("\n\n----- ALGO RESULT -----\n")
	print(knn.kmeans((.5, .5), points)[1])

	# DISPLAY
	# all pre-existing points
	plt.scatter([p[0] for p in points],
				[p[1] for p in points],
				c=[p[2] for p in points])
	# new point to classify
	plt.scatter(new[0], new[1], c="#000000")
	# nearest neighbors lines
	for d in distances:
		plt.plot([new[0], points[d[0]][0]], [new[1], points[d[0]][1]], c="#000000")
	plt.show()
Example #3
0
def _couples_creation(c, points):
	# dist(a, b)
	couples = []
	for ida, a in enumerate(points):
		for idb in range(ida):
			couples.append([a, points[idb], dist(a[0], points[idb][0], a[1], points[idb][1])])

	# dist(a, b) ~= dist(c, d)
	for couple in couples:
		couple[-1] = abs(couple[-1] - c[1])
	couples.sort(key=lambda c: c[2])
	couples.reverse()

	return couples