Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. let get_smallest_elements_i input_array k =
  2. let n = Array.length input_array in
  3. let indices = Array.init n (fun x -> x) in
  4. for i = 0 to (k-1) do
  5. for j = (n-1) downto 1 do
  6. if input_array.(indices.(j-1)) > input_array.(indices.(j)) then begin
  7. let b = indices.(j-1) in
  8. indices.(j-1) <- indices.(j);
  9. indices.(j) <- b;
  10. end
  11. done;
  12. done;
  13. Array.sub indices 0 k ;;
  14.  
  15. let find_nearest_neighbours current_point all_points k distance =
  16. let distances = Array.map (fun x -> distance x current_point) all_points in
  17. get_smallest_elements_i distances k ;;
  18.  
  19. let predict nearest_neighbours labels =
  20. let sum a b = a +. b in
  21. let k = Array.length nearest_neighbours in
  22. if Array.fold_left sum 0. (Array.init k (fun i -> labels.(nearest_neighbours.(i)))) > 0. then 1. else ~-.1. ;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement