Skip to content

Instantly share code, notes, and snippets.

@dlyapun
Created September 20, 2019 09:34
Show Gist options
  • Save dlyapun/08f9e5a65d92926f286d538049be8557 to your computer and use it in GitHub Desktop.
Save dlyapun/08f9e5a65d92926f286d538049be8557 to your computer and use it in GitHub Desktop.
lab1
from math import sqrt
import matplotlib.pyplot as plt
import numpy as np
RANDOM_POINTS_COUNT = 10
def get_euclidean_distance(point_one, point_two):
dx = (point_one[0] - point_two[0]) ** 2
dy = (point_one[1] - point_two[1]) ** 2
return sqrt(dx + dy)
def get_average_class_value(point, array_points):
average_value = 0.0
for array_point in array_points:
average_value += get_euclidean_distance(point, array_point)
return average_value / len(array_points)
def get_random_point():
array = [float('%.2f' % i) for i in np.random.rand(2)]
return np.array(array)
def main():
class_red = np.array([[0.05, 0.91],
[0.14, 0.96],
[0.16, 0.9],
[0.07, 0.7],
[0.2, 0.63]])
class_blue = np.array([[0.49, 0.89],
[0.34, 0.81],
[0.36, 0.67],
[0.47, 0.49],
[0.52, 0.53]])
p1 = plt.plot(class_red[:, 0], class_red[:, 1], '*r')
p2 = plt.plot(class_blue[:, 0], class_blue[:, 1], '+b')
plt.legend([p1, p2], ["classred", "classblue"])
plt.grid(True)
for i in range(RANDOM_POINTS_COUNT):
random_point = get_random_point()
red_average = get_average_class_value(random_point, class_red)
blue_average = get_average_class_value(random_point, class_blue)
color = 'r' if red_average < blue_average else 'b'
plt.scatter(random_point[0], random_point[1], color=color)
plt.pause(0.2)
plt.show()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment