Skip to content

Commit 123cb73

Browse files
committed
modify docs
1 parent b319d87 commit 123cb73

4 files changed

Lines changed: 62 additions & 8 deletions

File tree

.DS_Store

6 KB
Binary file not shown.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def deps do
1515
end
1616
```
1717

18-
### K-Nearest Neighbors
18+
### K-Nearest Neighbours
1919

2020
Initialize classificator with data set consists from labels and features:
2121

lib/learn_kit/knn.ex

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/learn_kit/knn/classify.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule LearnKit.Knn.Classify do
22
@moduledoc """
3-
Module for knn prediction functions
3+
Module for knn classify functions
44
"""
55
defmacro __using__(_opts) do
66
quote do
@@ -113,7 +113,7 @@ defmodule LearnKit.Knn.Classify do
113113
end
114114
end
115115

116-
defp accumulate_weight_of_labels(features, acc) when features == [] do
116+
defp accumulate_weight_of_labels([], acc) do
117117
acc
118118
end
119119

0 commit comments

Comments
 (0)