Skip to content

Commit b0bf269

Browse files
committed
modify tests
1 parent ae5ded5 commit b0bf269

6 files changed

Lines changed: 88 additions & 31 deletions

File tree

lib/learn_kit/math.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ defmodule LearnKit.Math do
136136
## Examples
137137
138138
iex> LearnKit.Math.division(10, 2)
139-
5
139+
5.0
140140
141141
"""
142142
@spec division(number, number) :: number
@@ -154,7 +154,7 @@ defmodule LearnKit.Math do
154154
5.5
155155
156156
"""
157-
@spec covariance(list,list) :: number
157+
@spec covariance(list, list) :: number
158158

159159
def covariance(x, y) when length(x) == length(y) do
160160
mean_x = mean(x)

lib/learn_kit/regression/linear.ex

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ defmodule LearnKit.Regression.Linear do
8383
## Examples
8484
8585
iex> predictor |> LearnKit.Regression.Linear.predict([4, 8, 13])
86-
[14.5, 30.5, 50.5]
86+
{:ok, [14.5, 30.5, 50.5]}
8787
8888
"""
89-
@spec predict(%LearnKit.Regression.Linear{coefficients: coefficients}, list) :: list
89+
@spec predict(%LearnKit.Regression.Linear{coefficients: coefficients}, list) :: {:ok, list}
9090

9191
def predict(%Linear{coefficients: coefficients}, samples) do
92-
samples
93-
|> Enum.map(fn sample -> predict_sample(sample, coefficients) end)
92+
result = samples |> Enum.map(fn sample -> predict_sample(sample, coefficients) end)
93+
{:ok, result}
9494
end
9595

9696
@doc """
@@ -103,12 +103,12 @@ defmodule LearnKit.Regression.Linear do
103103
## Examples
104104
105105
iex> predictor |> LearnKit.Regression.Linear.score
106-
0.9876543209876543
106+
{:ok, 0.9876543209876543}
107107
108108
"""
109-
@spec score(%LearnKit.Regression.Linear{factors: factors, results: results, coefficients: coefficients}) :: number
109+
@spec score(%LearnKit.Regression.Linear{factors: factors, results: results, coefficients: coefficients}) :: {:ok, number}
110110

111111
def score(%Linear{factors: factors, results: results, coefficients: coefficients}) do
112-
calculate_score(coefficients, factors, results)
112+
{:ok, calculate_score(coefficients, factors, results)}
113113
end
114114
end

test/learn_kit/knn_test.exs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule LearnKit.KnnTest do
22
use ExUnit.Case
33

4-
alias LearnKit.{Knn}
4+
alias LearnKit.Knn
55

66
test "create new knn classificator with empty data set" do
77
assert %Knn{data_set: data_set} = Knn.new
@@ -19,15 +19,15 @@ defmodule LearnKit.KnnTest do
1919
end
2020

2121
test "classify new feature" do
22-
classificator = LearnKit.Knn.new
23-
|> LearnKit.Knn.add_train_data({:a1, [-1, -1]})
24-
|> LearnKit.Knn.add_train_data({:a1, [-2, -1]})
25-
|> LearnKit.Knn.add_train_data({:a1, [-3, -2]})
26-
|> LearnKit.Knn.add_train_data({:a2, [1, 1]})
27-
|> LearnKit.Knn.add_train_data({:a2, [2, 1]})
28-
|> LearnKit.Knn.add_train_data({:a2, [3, 2]})
29-
|> LearnKit.Knn.add_train_data({:a2, [-2, -2]})
22+
classificator = Knn.new
23+
|> Knn.add_train_data({:a1, [-1, -1]})
24+
|> Knn.add_train_data({:a1, [-2, -1]})
25+
|> Knn.add_train_data({:a1, [-3, -2]})
26+
|> Knn.add_train_data({:a2, [1, 1]})
27+
|> Knn.add_train_data({:a2, [2, 1]})
28+
|> Knn.add_train_data({:a2, [3, 2]})
29+
|> Knn.add_train_data({:a2, [-2, -2]})
3030

31-
assert {:ok, :a1} = LearnKit.Knn.classify(classificator, [feature: [-1, -2], k: 3, weight: "distance"])
31+
assert {:ok, :a1} = Knn.classify(classificator, [feature: [-1, -2], k: 3, weight: "distance"])
3232
end
3333
end

test/learn_kit/math_test.exs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule LearnKit.MathTest do
22
use ExUnit.Case
33

4-
alias LearnKit.{Math}
4+
alias LearnKit.Math
55

66
test "calculate mean" do
77
assert 2.0 = Math.mean([1, 2, 3])
@@ -22,4 +22,16 @@ defmodule LearnKit.MathTest do
2222
test "calculate standard deviation from variance" do
2323
assert 1.118033988749895 = Math.standard_deviation_from_variance(1.25)
2424
end
25+
26+
test "calculate division" do
27+
assert 5.0 = Math.division(10, 2)
28+
end
29+
30+
test "calculate covariance" do
31+
assert 5.5 = Math.covariance([1, 2, 3], [14, 17, 25])
32+
end
33+
34+
test "calculate correlation" do
35+
assert 0.9672471299049061 = Math.correlation([1, 2, 3], [14, 17, 25])
36+
end
2537
end

test/learn_kit/naive_bayes/gaussian_test.exs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ defmodule LearnKit.NaiveBayes.GaussianTest do
1919
end
2020

2121
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
22+
classificator = Gaussian.new([{:label1, [[-1, -1], [-2, -1], [-3, -2]]}, {:label2, [[1, 1], [2, 1], [3, 2], [-2, -2]]}])
23+
%Gaussian{fit_data: fit_data} = classificator |> Gaussian.fit
2424

2525
assert fit_data == [
2626
label1: [
@@ -35,26 +35,26 @@ defmodule LearnKit.NaiveBayes.GaussianTest do
3535
end
3636

3737
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
38+
classificator = Gaussian.new([{:label1, [[-1, -1], [-2, -1], [-3, -2]]}, {:label2, [[1, 1], [2, 1], [3, 2], [-2, -2]]}])
39+
classificator = classificator |> Gaussian.fit
4040

41-
assert {:ok, result} = classificator |> LearnKit.NaiveBayes.Gaussian.predict_proba([1, 2])
41+
assert {:ok, result} = classificator |> Gaussian.predict_proba([1, 2])
4242
assert result == [label1: 0.0, label2: 0.017199571]
4343
end
4444

4545
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
46+
classificator = Gaussian.new([{:label1, [[-1, -1], [-2, -1], [-3, -2]]}, {:label2, [[1, 1], [2, 1], [3, 2], [-2, -2]]}])
47+
classificator = classificator |> Gaussian.fit
4848

49-
assert {:ok, result} = classificator |> LearnKit.NaiveBayes.Gaussian.predict([1, 2])
49+
assert {:ok, result} = classificator |> Gaussian.predict([1, 2])
5050
assert result == {:label2, 0.017199571}
5151
end
5252

5353
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
54+
classificator = Gaussian.new([{:label1, [[-1, -1], [-2, -1], [-3, -2]]}, {:label2, [[1, 1], [2, 1], [3, 2], [-2, -2]]}])
55+
classificator = classificator |> Gaussian.fit
5656

57-
assert {:ok, result} = classificator |> LearnKit.NaiveBayes.Gaussian.score
57+
assert {:ok, result} = classificator |> Gaussian.score
5858
assert result == 0.857143
5959
end
6060
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
defmodule LearnKit.Regression.LinearTest do
2+
use ExUnit.Case
3+
4+
alias LearnKit.Regression.Linear
5+
6+
setup_all do
7+
{:ok, predictor: Linear.new([1, 2, 3, 4], [3, 6, 10, 15])}
8+
end
9+
10+
test "create new linear predictor with empty data set" do
11+
assert %Linear{factors: factors, results: results, coefficients: coefficients} = Linear.new
12+
13+
assert factors == []
14+
assert results == []
15+
assert coefficients == []
16+
end
17+
18+
test "create new linear predictor with data", state do
19+
assert %Linear{factors: factors, results: results, coefficients: coefficients} = state[:predictor]
20+
21+
assert factors == [1, 2, 3, 4]
22+
assert results == [3, 6, 10, 15]
23+
assert coefficients == []
24+
end
25+
26+
test "fit data set", state do
27+
%Linear{coefficients: coefficients} = state[:predictor] |> Linear.fit
28+
29+
assert coefficients == [-1.5, 4.0]
30+
end
31+
32+
test "return prediction using the linear model", state do
33+
predictor = state[:predictor] |> Linear.fit
34+
35+
assert {:ok, result} = predictor |> Linear.predict([4, 8, 13])
36+
assert result == [14.5, 30.5, 50.5]
37+
end
38+
39+
test "returns coefficient of determination R^2 of the prediction", state do
40+
predictor = state[:predictor] |> Linear.fit
41+
42+
assert {:ok, result} = predictor |> Linear.score
43+
assert result == 0.9876543209876543
44+
end
45+
end

0 commit comments

Comments
 (0)