Skip to content

Commit f9bbe1a

Browse files
committed
modify normalization algorithm
1 parent 973be81 commit f9bbe1a

2 files changed

Lines changed: 55 additions & 50 deletions

File tree

lib/learn_kit/preprocessing.ex

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,11 @@ defmodule LearnKit.Preprocessing do
1111
@type matrix :: [row]
1212

1313
@doc """
14-
Normalization of data set
14+
Normalize data set with minimax normalization
1515
1616
## Parameters
1717
1818
- features: list of features for normalization
19-
- options: keyword list with options
20-
21-
## Options
22-
23-
- type: minimax/z_normalization, default is minimax, optional
2419
2520
## Examples
2621
@@ -31,6 +26,27 @@ defmodule LearnKit.Preprocessing do
3126
[1.0, 1.0]
3227
]
3328
29+
"""
30+
@spec normalize(matrix) :: matrix
31+
32+
def normalize(features) when is_list(features) do
33+
normalize(features, [type: "minimax"])
34+
end
35+
36+
@doc """
37+
Normalize data set
38+
39+
## Parameters
40+
41+
- features: list of features for normalization
42+
- options: keyword list with options
43+
44+
## Options
45+
46+
- type: minimax/z_normalization, default is minimax, optional
47+
48+
## Examples
49+
3450
iex> LearnKit.Preprocessing.normalize([[1, 2], [3, 4], [5, 6]], [type: "z_normalization"])
3551
[
3652
[-1.224744871391589, -1.224744871391589],
@@ -39,18 +55,13 @@ defmodule LearnKit.Preprocessing do
3955
]
4056
4157
"""
42-
@spec normalize(matrix) :: matrix
4358
@spec normalize(matrix, list) :: matrix
4459

45-
def normalize(features) when is_list(features) do
46-
normalize(features, [type: "minimax"])
47-
end
48-
4960
def normalize(features, options) when is_list(features) and is_list(options) do
5061
options = Keyword.merge([type: "minimax"], options)
5162
case Keyword.get(options, :type) do
52-
"z_normalization" -> z_normalization(features)
53-
_ -> minimax_normalization(features)
63+
"z_normalization" -> normalization(features, "z_normalization")
64+
_ -> normalization(features, "minimax")
5465
end
5566
end
5667
end

lib/learn_kit/preprocessing/normalize.ex

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,43 @@ defmodule LearnKit.Preprocessing.Normalize do
77

88
defmacro __using__(_opts) do
99
quote do
10-
defp minimax_normalization(features) do
10+
defp normalization(features, type) do
1111
features_by_index = Math.transpose(features)
12-
list_of_params =
13-
features_by_index
14-
|> Enum.map(fn list ->
15-
{
16-
Enum.min(list),
17-
Enum.max(list)
18-
}
19-
end)
12+
list_of_params = Enum.map(features_by_index, fn list -> return_params(list, type) end)
2013
features_by_index
2114
|> Enum.with_index()
22-
|> Enum.map(fn {feature, index} ->
23-
params_for_point = Enum.at(list_of_params, index)
24-
range = elem(params_for_point, 1) - elem(params_for_point, 0)
25-
feature
26-
|> Enum.map(fn point ->
27-
(point - elem(params_for_point, 0)) / range
28-
end)
29-
end)
15+
|> Enum.map(fn {feature, index} -> transform_feature(feature, Enum.at(list_of_params, index), type) end)
3016
|> Math.transpose()
3117
end
3218

33-
defp z_normalization(features) do
34-
features_by_index = Math.transpose(features)
35-
list_of_params =
36-
features_by_index
37-
|> Enum.map(fn list ->
38-
{
39-
Math.mean(list),
40-
Math.standard_deviation(list)
41-
}
42-
end)
43-
features_by_index
44-
|> Enum.with_index()
45-
|> Enum.map(fn {feature, index} ->
46-
params_for_point = Enum.at(list_of_params, index)
47-
feature
48-
|> Enum.map(fn point ->
49-
(point - elem(params_for_point, 0)) / elem(params_for_point, 1)
50-
end)
51-
end)
52-
|> Math.transpose()
19+
defp return_params(list, "minimax") do
20+
{
21+
Enum.min(list),
22+
Enum.max(list)
23+
}
24+
end
25+
26+
defp return_params(list, "z_normalization") do
27+
{
28+
Math.mean(list),
29+
Math.standard_deviation(list)
30+
}
31+
end
32+
33+
defp transform_feature(feature, params_for_point, type) do
34+
divider = define_divider(params_for_point, type)
35+
case divider do
36+
0 -> feature
37+
_ -> Enum.map(feature, fn point -> (point - elem(params_for_point, 0)) / divider end)
38+
end
39+
end
40+
41+
defp define_divider(params_for_point, "minimax") do
42+
elem(params_for_point, 1) - elem(params_for_point, 0)
43+
end
44+
45+
defp define_divider(params_for_point, "z_normalization") do
46+
elem(params_for_point, 1)
5347
end
5448
end
5549
end

0 commit comments

Comments
 (0)