Skip to content

Commit dc6198f

Browse files
committed
normalize train data for Gaussian NB
1 parent 740f5c5 commit dc6198f

5 files changed

Lines changed: 90 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77
## Unreleased
88
### Added
99
- preprocessing normalization for data set
10+
- normalize train data for Gaussian NB
1011

1112
### Modified
1213
- normalization can be selected for KNN

lib/learn_kit/knn/classify.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ defmodule LearnKit.Knn.Classify do
3131
defp check_normalization(data_set, options) do
3232
type = Keyword.get(options, :normalization)
3333
case type do
34-
t when t in ["minimax", "z_normalization"] -> normalize(data_set, options, type)
34+
t when t in ["minimax", "z_normalization"] -> normalize(data_set, type)
3535
_ -> data_set
3636
end
3737
end
@@ -72,7 +72,7 @@ defmodule LearnKit.Knn.Classify do
7272
end
7373

7474
# normalize each feature
75-
defp normalize(data_set, options, type) do
75+
defp normalize(data_set, type) do
7676
coefficients = find_coefficients_for_normalization(data_set, type)
7777
Enum.map(data_set, fn {key, features} ->
7878
{

lib/learn_kit/naive_bayes/gaussian.ex

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ defmodule LearnKit.NaiveBayes.Gaussian do
77

88
alias LearnKit.NaiveBayes.Gaussian
99

10+
use Gaussian.Normalize
1011
use Gaussian.Fit
1112
use Gaussian.Classify
1213
use Gaussian.Score
@@ -78,6 +79,29 @@ defmodule LearnKit.NaiveBayes.Gaussian do
7879
%Gaussian{data_set: data_set}
7980
end
8081

82+
@doc """
83+
Normalize train data
84+
85+
## Parameters
86+
87+
- classifier: %LearnKit.NaiveBayes.Gaussian{}
88+
- type: none/minimax/z_normalization, default is none, optional
89+
90+
## Examples
91+
92+
iex> classifier = classifier |> LearnKit.NaiveBayes.Gaussian.normalize_train_data("minimax")
93+
%LearnKit.NaiveBayes.Gaussian{
94+
data_set: [a1: [[0.6666666666666666, 0.8], [1.0, 1.0]], b1: [[0.0, 0.0]]],
95+
fit_data: []
96+
}
97+
98+
"""
99+
@spec normalize_train_data(%Gaussian{data_set: data_set}, String.t()) :: %Gaussian{data_set: data_set, fit_data: fit_data}
100+
101+
def normalize_train_data(%Gaussian{data_set: data_set}, type \\ "none") when is_binary(type) do
102+
%Gaussian{data_set: normalize_data(data_set, type), fit_data: []}
103+
end
104+
81105
@doc """
82106
Fit train data
83107
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
defmodule LearnKit.NaiveBayes.Gaussian.Normalize do
2+
@moduledoc """
3+
Module for fit functions
4+
"""
5+
6+
alias LearnKit.Preprocessing
7+
8+
defmacro __using__(_opts) do
9+
quote do
10+
defp normalize_data(data_set, type) do
11+
case type do
12+
t when t in ["minimax", "z_normalization"] -> normalize(data_set, type)
13+
_ -> data_set
14+
end
15+
end
16+
17+
# normalize each feature
18+
defp normalize(data_set, type) do
19+
coefficients = find_coefficients_for_normalization(data_set, type)
20+
Enum.map(data_set, fn {key, features} ->
21+
{
22+
key,
23+
Enum.map(features, fn feature -> Preprocessing.normalize_feature(feature, coefficients, type) end)
24+
}
25+
end)
26+
end
27+
28+
# find coefficients for normalization
29+
defp find_coefficients_for_normalization(data_set, type) do
30+
Enum.reduce(data_set, [], fn {_, features}, acc ->
31+
Enum.reduce(features, acc, fn feature, acc -> [feature | acc] end)
32+
end)
33+
|> Preprocessing.coefficients(type)
34+
end
35+
end
36+
end
37+
end

test/learn_kit/naive_bayes/gaussian_test.exs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,39 @@ defmodule LearnKit.NaiveBayes.GaussianTest do
1414
end
1515

1616
test "add train data to classificator" do
17-
%Gaussian{data_set: data_set} = Gaussian.new
18-
|> Gaussian.add_train_data({:a1, [1, 2]})
19-
|> Gaussian.add_train_data({:a1, [1, 3]})
20-
|> Gaussian.add_train_data({:b1, [2, 3]})
17+
%Gaussian{data_set: data_set} =
18+
Gaussian.new
19+
|> Gaussian.add_train_data({:a1, [1, 2]})
20+
|> Gaussian.add_train_data({:a1, [1, 3]})
21+
|> Gaussian.add_train_data({:b1, [2, 3]})
2122

2223
assert data_set == [b1: [[2, 3]], a1: [[1, 3], [1, 2]]]
2324
end
2425

26+
test "normalize data set", state do
27+
%Gaussian{data_set: data_set} = state[:classificator] |> Gaussian.normalize_train_data("minimax")
28+
29+
assert data_set ==
30+
[
31+
label1: [[0.3333333333333333, 0.25], [0.16666666666666666, 0.25], [0.0, 0.0]],
32+
label2: [ [0.6666666666666666, 0.75], [0.8333333333333334, 0.75], [1.0, 1.0], [0.16666666666666666, 0.0]]
33+
]
34+
end
35+
2536
test "fit data set", state do
2637
%Gaussian{fit_data: fit_data} = state[:classificator] |> Gaussian.fit
2738

28-
assert fit_data == [
29-
label1: [
30-
%{mean: -2.0, standard_deviation: 0.816496580927726, variance: 0.6666666666666666},
31-
%{mean: -1.3333333333333333, standard_deviation: 0.4714045207910317, variance: 0.2222222222222222}
32-
],
33-
label2: [
34-
%{mean: 1.0, standard_deviation: 1.8708286933869707, variance: 3.5},
35-
%{mean: 0.5, standard_deviation: 1.5, variance: 2.25}
36-
]
37-
]
39+
assert fit_data ==
40+
[
41+
label1: [
42+
%{mean: -2.0, standard_deviation: 0.816496580927726, variance: 0.6666666666666666},
43+
%{mean: -1.3333333333333333, standard_deviation: 0.4714045207910317, variance: 0.2222222222222222}
44+
],
45+
label2: [
46+
%{mean: 1.0, standard_deviation: 1.8708286933869707, variance: 3.5},
47+
%{mean: 0.5, standard_deviation: 1.5, variance: 2.25}
48+
]
49+
]
3850
end
3951

4052
test "return probability estimates for the feature", state do

0 commit comments

Comments
 (0)