cosine.py 637 B

123456789101112131415161718192021
  1. #!/usr/bin/env python3
  2. def cosine_sim(known_point, new_point):
  3. from math import sqrt
  4. up, d1, d2 = 0.0, 0.0, 0.0
  5. a, b = known_point, new_point
  6. for key in a.wifi_snr:
  7. d1 += a.wifi_snr[key] * a.wifi_snr[key]
  8. if key in b.wifi_snr:
  9. up += a.wifi_snr[key] * b.wifi_snr[key]
  10. for key in b.wifi_snr:
  11. d2 += b.wifi_snr[key] * b.wifi_snr[key]
  12. if up == 0:
  13. return None
  14. else:
  15. return up / sqrt(d1) / sqrt(d2)
  16. if __name__ == '__main__':
  17. from wifiloc import toplev
  18. toplev(infile = "basicdata.csv", sim = cosine_sim, bad_wifi_crit = 5000,
  19. sim_crit = 0.0)