@@ -9,24 +9,63 @@ defmodule LearnKit.Knn do
99
1010 use Knn.Classify
1111
12+ @ type label :: atom
13+ @ type feature :: [ integer ]
14+ @ type point :: { label , feature }
15+ @ type features :: [ feature ]
16+ @ type data_set :: [ { label , features } ]
17+
1218 @ doc """
1319 Creates classificator with empty data_set
20+
21+ ## Examples
22+
23+ iex> classificator = LearnKit.Knn.new
24+ %LearnKit.Knn{data_set: []}
25+
1426 """
27+ @ spec new ( ) :: % LearnKit.Knn { data_set: [ ] }
28+
1529 def new do
1630 [ ]
1731 |> Knn . new
1832 end
1933
2034 @ doc """
2135 Creates classificator with data_set
36+
37+ ## Parameters
38+
39+ - data_set: Keyword list with labels and features in tuples
40+
41+ ## Examples
42+
43+ iex> classificator = LearnKit.Knn.new([{:a1, [[1, 2], [2, 3]]}, {:b1, [[-1, -2]]}])
44+ %LearnKit.Knn{data_set: [a1: [[1, 2], [2, 3]], b1: [[-1, -2]]]}
45+
2246 """
47+ @ spec new ( data_set ) :: % LearnKit.Knn { data_set: data_set }
48+
2349 def new ( data_set ) do
2450 % Knn { data_set: data_set }
2551 end
2652
2753 @ doc """
2854 Add train data to classificator
55+
56+ ## Parameters
57+
58+ - classificator: %LearnKit.Knn{}
59+ - train data: tuple with label and feature
60+
61+ ## Examples
62+
63+ iex> classificator |> LearnKit.Knn.add_train_data({:a1, [-1, -1]})
64+ %LearnKit.Knn{data_set: [a1: [[-1, -1]]]}
65+
2966 """
67+ @ spec add_train_data ( % LearnKit.Knn { data_set: data_set } , point ) :: % LearnKit.Knn { data_set: data_set }
68+
3069 def add_train_data ( % Knn { data_set: data_set } , { key , value } ) do
3170 features = if Keyword . has_key? ( data_set , key ) , do: Keyword . get ( data_set , key ) , else: [ ]
3271 data_set = Keyword . put ( data_set , key , [ value | features ] )
@@ -35,12 +74,27 @@ defmodule LearnKit.Knn do
3574
3675 @ doc """
3776 Classify label of the new feature
38- Available options:
39- feature - feature for classification, required, example: [1, 2, 3]
40- k - number of nearest neighbours, default is 3, optional
41- algorithm - brute, optional
42- weight - uniform/distance, default is uniform, optional
77+
78+ ## Parameters
79+
80+ - classificator: %LearnKit.Knn{}
81+ - options: keyword list with options
82+
83+ ## Options
84+
85+ - feature: feature for classification, required, example: [1, 2, 3]
86+ - k: number of nearest neighbours, default is 3, optional
87+ - algorithm: brute, optional
88+ - weight: uniform/distance, default is uniform, optional
89+
90+ ## Examples
91+
92+ iex> classificator |> LearnKit.Knn.classify([feature: [-1, -2], k: 3, weight: "distance"])
93+ {:ok, "Feature has label a1 with weight 1.5"}
94+
4395 """
96+ @ spec classify ( % LearnKit.Knn { data_set: data_set } , [ tuple ] ) :: { atom , String . t }
97+
4498 def classify ( % Knn { data_set: data_set } , options \\ [ ] ) do
4599 try do
46100 unless Keyword . has_key? ( options , :feature ) do
0 commit comments