@swyx/

PracticalHarmfulOutsourcing

Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import math

def calcKNN(isEuclidean, q, k): 
  data = [
    {'x': [1, 6], 'y': 7},
    {'x': [2, 4], 'y': 8},
    {'x': [3, 7], 'y': 16},
    {'x': [6, 8], 'y': 44},
    {'x': [7, 1], 'y': 50},
    {'x': [8, 4], 'y': 68}
  ]

  # a and b are coordinate tuples
  def dist(a, b):
    ## euclidean
    if isEuclidean:
      fst = math.pow(a[0] - b[0], 2)
      snd = math.pow(a[1] - b[1], 2)
      return math.pow(fst + snd, 0.5)
    ## manhattan
    else:
      return abs(a[0] - b[0]) + abs(a[1] - b[1])

  # enhance with dist
  for bundle in data:
    bundle['dist'] = dist(bundle['x'], q)

  # sort
  data.sort(key = lambda x: x['dist'])

  # cut - ties are also included
  cutoff = data[:k][-1]['dist']
  data = list(filter(lambda x: x['dist'] <= cutoff, data))


  # calc
  def mean(data):
      # return float(sum(numbers)) / max(len(numbers), 1)
      numbers = list(map(lambda x: x['y'], data))
      return sum(numbers) / len(numbers)

  print('isEuclidean: ' + str(isEuclidean) + ' q = ' + str(q) + ' k = ' + str(k))
  return str(mean(data))

# vars
q = [4, 2]
k = 1
isEuclidean = False
print('mean: ' + calcKNN(isEuclidean, q, k))

k = 3
print('mean: ' + calcKNN(isEuclidean, q, k))

isEuclidean = True
k = 1
print('mean: ' + calcKNN(isEuclidean, q, k))

k = 3
print('mean: ' + calcKNN(isEuclidean, q, k))