Current section

8 Versions

Jump to

Compare versions

12 files changed
+195 additions
-95 deletions
  @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
4 4 The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5 5 and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6 6
7 + ## [0.1.3] - 2018-11-22
8 + ### Modified
9 + - Linear Regression, fit with gradient descent
10 +
7 11 ## [0.1.2] - 2018-11-22
8 12 ### Added
9 13 - CHANGELOG.md file
  @@ -19,7 +19,7 @@ by adding `learn_kit` to your list of dependencies in `mix.exs`:
19 19 ```elixir
20 20 def deps do
21 21 [
22 - {:learn_kit, "~> 0.1.2"}
22 + {:learn_kit, "~> 0.1.3"}
23 23 ]
24 24 end
25 25 ```
  @@ -33,12 +33,18 @@ Initialize predictor with data:
33 33 predictor = Linear.new([1, 2, 3, 4], [3, 6, 10, 15])
34 34 ```
35 35
36 - Fit data set:
36 + Fit data set with least squares method:
37 37
38 38 ```elixir
39 39 predictor = predictor |> Linear.fit
40 40 ```
41 41
42 + Fit data set with gradient descent method:
43 +
44 + ```elixir
45 + predictor = predictor |> Linear.fit([method: "gradient descent"])
46 + ```
47 +
42 48 Predict using the linear model:
43 49
44 50 ```elixir
  @@ -13,12 +13,11 @@
13 13 <<"lib/learn_kit/naive_bayes/gaussian/score.ex">>,
14 14 <<"lib/learn_kit/regression">>,<<"lib/learn_kit/regression/linear">>,
15 15 <<"lib/learn_kit/regression/linear.ex">>,
16 - <<"lib/learn_kit/regression/linear/fit.ex">>,
17 - <<"lib/learn_kit/regression/linear/predict.ex">>,<<".formatter.exs">>,
16 + <<"lib/learn_kit/regression/linear/calculations.ex">>,<<".formatter.exs">>,
18 17 <<"mix.exs">>,<<"README.md">>,<<"CHANGELOG.md">>]}.
19 18 {<<"licenses">>,[<<"MIT">>]}.
20 19 {<<"links">>,
21 20 [{<<"GitHub">>,<<"https://github.com/kortirso/elixir_learn_kit">>}]}.
22 21 {<<"name">>,<<"learn_kit">>}.
23 22 {<<"requirements">>,[]}.
24 - {<<"version">>,<<"0.1.2">>}.
23 + {<<"version">>,<<"0.1.3">>}.
  @@ -97,9 +97,7 @@ defmodule LearnKit.Knn do
97 97
98 98 def classify(%Knn{data_set: data_set}, options \\ []) do
99 99 try do
100 - unless Keyword.has_key?(options, :feature) do
101 - raise "Feature option is required"
102 - end
100 + unless Keyword.has_key?(options, :feature), do: raise "Feature option is required"
103 101 # modification of options
104 102 options = Keyword.merge([k: 3, algorithm: "brute", weight: "uniform"], options)
105 103 # prediction
  @@ -2,6 +2,9 @@ defmodule LearnKit.Knn.Classify do
2 2 @moduledoc """
3 3 Module for knn classify functions
4 4 """
5 +
6 + alias LearnKit.Math
7 +
5 8 defmacro __using__(_opts) do
6 9 quote do
7 10 defp prediction(data_set, options) do
  @@ -34,7 +37,8 @@ defmodule LearnKit.Knn.Classify do
34 37 defp calc_feature_weights(features, options) do
35 38 features
36 39 |> Enum.map(fn feature ->
37 - Tuple.append(feature, calc_feature_weight(Keyword.get(options, :weight), elem(feature, 0)))
40 + feature
41 + |> Tuple.append(calc_feature_weight(Keyword.get(options, :weight), elem(feature, 0)))
38 42 end)
39 43 end
40 44
  @@ -78,9 +82,7 @@ defmodule LearnKit.Knn.Classify do
78 82 features
79 83 |> Enum.reduce([], fn feature, acc ->
80 84 distance = feature |> calc_distance_between_features(current_feature)
81 - if distance == 0 do
82 - raise "Feature exists in train data set with label #{key}"
83 - end
85 + if distance == 0, do: raise "Feature exists in train data set with label #{key}"
84 86 acc = [{distance, key} | acc]
85 87 end)
86 88 end
  @@ -92,19 +94,15 @@ defmodule LearnKit.Knn.Classify do
92 94 defp calc_distance_between_points(acc, feature_from_data_set, feature, current_index, size) when current_index <= size do
93 95 Enum.at(feature_from_data_set, current_index) - Enum.at(feature, current_index)
94 96 |> :math.pow(2)
95 - |> summ(acc)
97 + |> Math.summ(acc)
96 98 |> calc_distance_between_points(feature_from_data_set, feature, current_index + 1, size)
97 99 end
98 100
99 - defp calc_distance_between_points(acc, _feature_from_data_set, _feature, _current_index, _size) do
101 + defp calc_distance_between_points(acc, _, _, _, _) do
100 102 acc
101 103 |> :math.sqrt
102 104 end
103 105
104 - defp summ(a, b) do
105 - a + b
106 - end
107 -
108 106 defp calc_feature_weight(weight, distance) do
109 107 case weight do
110 108 "uniform" -> 1
  @@ -117,7 +115,7 @@ defmodule LearnKit.Knn.Classify do
117 115 acc
118 116 end
119 117
120 - defp accumulate_weight_of_labels([{_distance, key, weight} | tail], acc) do
118 + defp accumulate_weight_of_labels([{_, key, weight} | tail], acc) do
121 119 previous = if Keyword.has_key?(acc, key), do: Keyword.get(acc, key), else: 0
122 120 acc = Keyword.put(acc, key, previous + weight)
123 121 accumulate_weight_of_labels(tail, acc)
Loading more files…