|
| 1 | +defmodule LearnKit.Preprocessing do |
| 2 | + @moduledoc """ |
| 3 | + Module for data preprocessing |
| 4 | + """ |
| 5 | + |
| 6 | + alias LearnKit.Preprocessing |
| 7 | + |
| 8 | + use Preprocessing.Normalize |
| 9 | + |
| 10 | + @type row :: [number] |
| 11 | + @type matrix :: [row] |
| 12 | + |
| 13 | + @doc """ |
| 14 | + Normalization of data set |
| 15 | +
|
| 16 | + ## Parameters |
| 17 | +
|
| 18 | + - 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 |
| 24 | +
|
| 25 | + ## Examples |
| 26 | +
|
| 27 | + iex> LearnKit.Preprocessing.normalize([[1, 2], [3, 4], [5, 6]]) |
| 28 | + [ |
| 29 | + [0.0, 0.0], |
| 30 | + [0.5, 0.5], |
| 31 | + [1.0, 1.0] |
| 32 | + ] |
| 33 | +
|
| 34 | + iex> LearnKit.Preprocessing.normalize([[1, 2], [3, 4], [5, 6]], [type: "z_normalization"]) |
| 35 | + [ |
| 36 | + [-1.224744871391589, -1.224744871391589], |
| 37 | + [0.0, 0.0], |
| 38 | + [1.224744871391589, 1.224744871391589] |
| 39 | + ] |
| 40 | +
|
| 41 | + """ |
| 42 | + @spec normalize(matrix) :: matrix |
| 43 | + @spec normalize(matrix, list) :: matrix |
| 44 | + |
| 45 | + def normalize(features) when is_list(features) do |
| 46 | + normalize(features, [type: "minimax"]) |
| 47 | + end |
| 48 | + |
| 49 | + def normalize(features, options) when is_list(features) and is_list(options) do |
| 50 | + options = Keyword.merge([type: "minimax"], options) |
| 51 | + case Keyword.get(options, :type) do |
| 52 | + "z_normalization" -> z_normalization(features) |
| 53 | + _ -> minimax_normalization(features) |
| 54 | + end |
| 55 | + end |
| 56 | +end |
0 commit comments