@@ -2,6 +2,9 @@ defmodule LearnKit.Knn.Classify do
22 @ moduledoc """
33 Module for knn classify functions
44 """
5+
6+ alias LearnKit.Math
7+
58 defmacro __using__ ( _opts ) do
69 quote do
710 defp prediction ( data_set , options ) do
@@ -34,7 +37,8 @@ defmodule LearnKit.Knn.Classify do
3437 defp calc_feature_weights ( features , options ) do
3538 features
3639 |> Enum . map ( fn feature ->
37- Tuple . append ( feature , calc_feature_weight ( Keyword . get ( options , :weight ) , elem ( feature , 0 ) ) )
40+ feature
41+ |> Tuple . append ( calc_feature_weight ( Keyword . get ( options , :weight ) , elem ( feature , 0 ) ) )
3842 end )
3943 end
4044
@@ -78,9 +82,7 @@ defmodule LearnKit.Knn.Classify do
7882 features
7983 |> Enum . reduce ( [ ] , fn feature , acc ->
8084 distance = feature |> calc_distance_between_features ( current_feature )
81- if distance == 0 do
82- raise "Feature exists in train data set with label #{ key } "
83- end
85+ if distance == 0 , do: raise "Feature exists in train data set with label #{ key } "
8486 acc = [ { distance , key } | acc ]
8587 end )
8688 end
@@ -92,19 +94,15 @@ defmodule LearnKit.Knn.Classify do
9294 defp calc_distance_between_points ( acc , feature_from_data_set , feature , current_index , size ) when current_index <= size do
9395 Enum . at ( feature_from_data_set , current_index ) - Enum . at ( feature , current_index )
9496 |> :math . pow ( 2 )
95- |> summ ( acc )
97+ |> Math . summ ( acc )
9698 |> calc_distance_between_points ( feature_from_data_set , feature , current_index + 1 , size )
9799 end
98100
99- defp calc_distance_between_points ( acc , _feature_from_data_set , _feature , _current_index , _size ) do
101+ defp calc_distance_between_points ( acc , _ , _ , _ , _ ) do
100102 acc
101103 |> :math . sqrt
102104 end
103105
104- defp summ ( a , b ) do
105- a + b
106- end
107-
108106 defp calc_feature_weight ( weight , distance ) do
109107 case weight do
110108 "uniform" -> 1
@@ -117,7 +115,7 @@ defmodule LearnKit.Knn.Classify do
117115 acc
118116 end
119117
120- defp accumulate_weight_of_labels ( [ { _distance , key , weight } | tail ] , acc ) do
118+ defp accumulate_weight_of_labels ( [ { _ , key , weight } | tail ] , acc ) do
121119 previous = if Keyword . has_key? ( acc , key ) , do: Keyword . get ( acc , key ) , else: 0
122120 acc = Keyword . put ( acc , key , previous + weight )
123121 accumulate_weight_of_labels ( tail , acc )
0 commit comments