Skip to content

Commit 933fe1f

Browse files
committed
modify documentation, modify responses
1 parent fb1b233 commit 933fe1f

6 files changed

Lines changed: 65 additions & 25 deletions

File tree

lib/learn_kit/knn.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ defmodule LearnKit.Knn do
9090
## Examples
9191
9292
iex> classificator |> LearnKit.Knn.classify([feature: [-1, -2], k: 3, weight: "distance"])
93-
{:ok, "Feature has label a1 with weight 1.5"}
93+
{:ok, :a1}
9494
9595
"""
96-
@spec classify(%LearnKit.Knn{data_set: data_set}, [tuple]) :: {atom, String.t}
96+
@spec classify(%LearnKit.Knn{data_set: data_set}, [tuple]) :: {:ok, label}
9797

9898
def classify(%Knn{data_set: data_set}, options \\ []) do
9999
try do
@@ -103,8 +103,8 @@ defmodule LearnKit.Knn do
103103
# modification of options
104104
options = Keyword.merge([k: 3, algorithm: "brute", weight: "uniform"], options)
105105
# prediction
106-
{label, weight} = prediction(data_set, options)
107-
{:ok, "Feature has label #{label} with weight #{weight}"}
106+
{label, _} = prediction(data_set, options)
107+
{:ok, label}
108108
rescue
109109
error -> {:error, error.message}
110110
end

lib/learn_kit/naive_bayes/gaussian.ex

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,14 @@ defmodule LearnKit.NaiveBayes.Gaussian do
116116
## Examples
117117
118118
iex> classificator |> LearnKit.NaiveBayes.Gaussian.predict_proba([1, 2])
119-
[a1: 0.0359, a2: 0.0039]
119+
{:ok, [a1: 0.0359, a2: 0.0039]}
120120
121121
"""
122-
@spec predict_proba(%LearnKit.NaiveBayes.Gaussian{fit_data: fit_data}, feature) :: predictions
122+
@spec predict_proba(%LearnKit.NaiveBayes.Gaussian{fit_data: fit_data}, feature) :: {:ok, predictions}
123123

124124
def predict_proba(%Gaussian{fit_data: fit_data}, feature) do
125-
fit_data
126-
|> classify_data(feature)
125+
result = fit_data |> classify_data(feature)
126+
{:ok, result}
127127
end
128128

129129
@doc """
@@ -136,15 +136,14 @@ defmodule LearnKit.NaiveBayes.Gaussian do
136136
## Examples
137137
138138
iex> classificator |> LearnKit.NaiveBayes.Gaussian.predict([1, 2])
139+
{:ok, {:a1, 0.334545454}}
139140
140141
"""
141-
@spec predict(%LearnKit.NaiveBayes.Gaussian{fit_data: fit_data}, feature) :: prediction
142+
@spec predict(%LearnKit.NaiveBayes.Gaussian{fit_data: fit_data}, feature) :: {:ok, prediction}
142143

143144
def predict(%Gaussian{fit_data: fit_data}, feature) do
144-
fit_data
145-
|> classify_data(feature)
146-
|> Enum.sort_by(&(elem(&1, 1)))
147-
|> Enum.at(-1)
145+
result = fit_data |> classify_data(feature) |> Enum.sort_by(&(elem(&1, 1))) |> Enum.at(-1)
146+
{:ok, result}
148147
end
149148

150149
@doc """
@@ -159,13 +158,13 @@ defmodule LearnKit.NaiveBayes.Gaussian do
159158
iex> classificator = LearnKit.NaiveBayes.Gaussian.new([{:label1, [[-1, -1], [-2, -1], [-3, -2]]}, {:label2, [[1, 1], [2, 1], [3, 2], [-2, -2]]}])
160159
iex> classificator = classificator |> LearnKit.NaiveBayes.Gaussian.fit
161160
iex> classificator |> LearnKit.NaiveBayes.Gaussian.score
162-
0.857143
161+
{:ok, 0.857143}
163162
164163
"""
165-
@spec score(%LearnKit.NaiveBayes.Gaussian{data_set: data_set, fit_data: fit_data}) :: number
164+
@spec score(%LearnKit.NaiveBayes.Gaussian{data_set: data_set, fit_data: fit_data}) :: {:ok, number}
166165

167166
def score(%Gaussian{data_set: data_set, fit_data: fit_data}) do
168-
fit_data
169-
|> calc_score(data_set)
167+
result = fit_data |> calc_score(data_set)
168+
{:ok, result}
170169
end
171170
end

lib/learn_kit/naive_bayes/gaussian/classify.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ defmodule LearnKit.NaiveBayes.Gaussian.Classify do
1919
class_fraction = 1.0 / labels_count
2020
feature_bayes = feature_mult(feature, fit_results, 1.0, 0)
2121
feature_bayes * class_fraction
22+
|> Float.round(10)
2223
end
2324

2425
# multiply together the feature probabilities for all of the features in a label for given values

lib/learn_kit/naive_bayes/gaussian/score.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ defmodule LearnKit.NaiveBayes.Gaussian.Score do
2828
end
2929

3030
defp check_feature(feature, fit_data, label) do
31-
{predicted_label, _} = Gaussian.predict(%Gaussian{fit_data: fit_data}, feature)
31+
{:ok, {predicted_label, _}} = Gaussian.predict(%Gaussian{fit_data: fit_data}, feature)
3232
if predicted_label == label, do: 1, else: 0
3333
end
3434
end

test/learn_kit/knn_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ defmodule LearnKit.KnnTest do
2828
|> LearnKit.Knn.add_train_data({:a2, [3, 2]})
2929
|> LearnKit.Knn.add_train_data({:a2, [-2, -2]})
3030

31-
assert {:ok, "Feature has label a1 with weight 1.5"} = LearnKit.Knn.classify(classificator, [feature: [-1, -2], k: 3, weight: "distance"])
31+
assert {:ok, :a1} = LearnKit.Knn.classify(classificator, [feature: [-1, -2], k: 3, weight: "distance"])
3232
end
3333
end
Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,60 @@
11
defmodule LearnKit.NaiveBayes.GaussianTest do
22
use ExUnit.Case
33

4-
alias LearnKit.{NaiveBayes}
4+
alias LearnKit.NaiveBayes.Gaussian
55

66
test "create new knn classificator with empty data set" do
7-
assert %NaiveBayes.Gaussian{data_set: data_set} = NaiveBayes.Gaussian.new
7+
assert %Gaussian{data_set: data_set} = Gaussian.new
88

99
assert data_set == []
1010
end
1111

1212
test "add train data to classificator" do
13-
%NaiveBayes.Gaussian{data_set: data_set} = NaiveBayes.Gaussian.new
14-
|> NaiveBayes.Gaussian.add_train_data({:a1, [1, 2]})
15-
|> NaiveBayes.Gaussian.add_train_data({:a1, [1, 3]})
16-
|> NaiveBayes.Gaussian.add_train_data({:b1, [2, 3]})
13+
%Gaussian{data_set: data_set} = Gaussian.new
14+
|> Gaussian.add_train_data({:a1, [1, 2]})
15+
|> Gaussian.add_train_data({:a1, [1, 3]})
16+
|> Gaussian.add_train_data({:b1, [2, 3]})
1717

1818
assert data_set == [b1: [[2, 3]], a1: [[1, 3], [1, 2]]]
1919
end
20+
21+
test "fit data set" do
22+
classificator = LearnKit.NaiveBayes.Gaussian.new([{:label1, [[-1, -1], [-2, -1], [-3, -2]]}, {:label2, [[1, 1], [2, 1], [3, 2], [-2, -2]]}])
23+
%LearnKit.NaiveBayes.Gaussian{fit_data: fit_data} = classificator |> LearnKit.NaiveBayes.Gaussian.fit
24+
25+
assert fit_data == [
26+
label1: [
27+
%{mean: -2.0, standard_deviation: 0.816496580927726, variance: 0.6666666666666666},
28+
%{mean: -1.3333333333333333, standard_deviation: 0.4714045207910317, variance: 0.2222222222222222}
29+
],
30+
label2: [
31+
%{mean: 1.0, standard_deviation: 1.8708286933869707, variance: 3.5},
32+
%{mean: 0.5, standard_deviation: 1.5, variance: 2.25}
33+
]
34+
]
35+
end
36+
37+
test "return probability estimates for the feature" do
38+
classificator = LearnKit.NaiveBayes.Gaussian.new([{:label1, [[-1, -1], [-2, -1], [-3, -2]]}, {:label2, [[1, 1], [2, 1], [3, 2], [-2, -2]]}])
39+
classificator = classificator |> LearnKit.NaiveBayes.Gaussian.fit
40+
41+
assert {:ok, result} = classificator |> LearnKit.NaiveBayes.Gaussian.predict_proba([1, 2])
42+
assert result == [label1: 0.0, label2: 0.017199571]
43+
end
44+
45+
test "return exact prediction for the feature" do
46+
classificator = LearnKit.NaiveBayes.Gaussian.new([{:label1, [[-1, -1], [-2, -1], [-3, -2]]}, {:label2, [[1, 1], [2, 1], [3, 2], [-2, -2]]}])
47+
classificator = classificator |> LearnKit.NaiveBayes.Gaussian.fit
48+
49+
assert {:ok, result} = classificator |> LearnKit.NaiveBayes.Gaussian.predict([1, 2])
50+
assert result == {:label2, 0.017199571}
51+
end
52+
53+
test "returns the mean accuracy on the given test data and labels" do
54+
classificator = LearnKit.NaiveBayes.Gaussian.new([{:label1, [[-1, -1], [-2, -1], [-3, -2]]}, {:label2, [[1, 1], [2, 1], [3, 2], [-2, -2]]}])
55+
classificator = classificator |> LearnKit.NaiveBayes.Gaussian.fit
56+
57+
assert {:ok, result} = classificator |> LearnKit.NaiveBayes.Gaussian.score
58+
assert result == 0.857143
59+
end
2060
end

0 commit comments

Comments
 (0)