Skip to content

Commit 346c1ef

Browse files
committed
modify readme
1 parent b0bf269 commit 346c1ef

3 files changed

Lines changed: 65 additions & 30 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
- CHANGELOG.md file
10+
- Add simple Linear Regression predictor
1011

1112
## [0.1.1] - 2018-11-19
1213
### Added

README.md

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
Elixir package for machine learning
44

5-
Available algorithms:
5+
Available algorithms for prediction:
6+
7+
- Linear Regression
8+
9+
Available algorithms for classification:
610

711
- K-Nearest Neighbours
812
- Gaussian Naive Bayes
@@ -20,21 +24,50 @@ def deps do
2024
end
2125
```
2226

27+
### Linear Regression
28+
29+
Initialize predictor with data:
30+
31+
```elixir
32+
alias LearnKit.Regression.Linear
33+
predictor = Linear.new([1, 2, 3, 4], [3, 6, 10, 15])
34+
```
35+
36+
Fit data set:
37+
38+
```elixir
39+
predictor = predictor |> Linear.fit
40+
```
41+
42+
Predict using the linear model:
43+
44+
```elixir
45+
predictor |> Linear.predict([4, 8, 13])
46+
```
47+
samples - array of variables, required
48+
49+
Returns the coefficient of determination R^2 of the prediction:
50+
51+
```elixir
52+
predictor |> Linear.score
53+
```
54+
2355
### K-Nearest Neighbours classification
2456

2557
Initialize classificator with data set consists from labels and features:
2658

2759
```elixir
28-
classificator = LearnKit.Knn.new
29-
|> LearnKit.Knn.add_train_data({:a1, [-1, -1]})
30-
|> LearnKit.Knn.add_train_data({:a1, [-2, -1]})
31-
|> LearnKit.Knn.add_train_data({:a2, [1, 1]})
60+
alias LearnKit.Knn
61+
classificator = Knn.new
62+
|> Knn.add_train_data({:a1, [-1, -1]})
63+
|> Knn.add_train_data({:a1, [-2, -1]})
64+
|> Knn.add_train_data({:a2, [1, 1]})
3265
```
3366

3467
Predict label for new feature:
3568

3669
```elixir
37-
LearnKit.Knn.classify(classificator, [feature: [-1, -2], k: 3, weight: "distance"])
70+
Knn.classify(classificator, [feature: [-1, -2], k: 3, weight: "distance"])
3871
```
3972
feature - new feature for prediction, required
4073
k - number of nearest neighbors, optional, default - 3
@@ -46,36 +79,37 @@ Predict label for new feature:
4679
Initialize classificator with data set consists from labels and features:
4780

4881
```elixir
49-
classificator = LearnKit.NaiveBayes.Gaussian.new
50-
|> LearnKit.NaiveBayes.Gaussian.add_train_data({:a1, [-1, -1]})
51-
|> LearnKit.NaiveBayes.Gaussian.add_train_data({:a1, [-2, -1]})
52-
|> LearnKit.NaiveBayes.Gaussian.add_train_data({:a2, [1, 1]})
82+
alias LearnKit.NaiveBayes.Gaussian
83+
classificator = Gaussian.new
84+
|> Gaussian.add_train_data({:a1, [-1, -1]})
85+
|> Gaussian.add_train_data({:a1, [-2, -1]})
86+
|> Gaussian.add_train_data({:a2, [1, 1]})
5387
```
5488

55-
Fit data set
89+
Fit data set:
5690

5791
```elixir
58-
classificator = classificator |> LearnKit.NaiveBayes.Gaussian.fit
92+
classificator = classificator |> Gaussian.fit
5993
```
6094

61-
Return probability estimates for the feature
95+
Return probability estimates for the feature:
6296

6397
```elixir
64-
classificator = classificator |> LearnKit.NaiveBayes.Gaussian.predict_proba([1, 2])
98+
classificator |> Gaussian.predict_proba([1, 2])
6599
```
66100
feature - new feature for prediction, required
67101

68-
Return exact prediction for the feature
102+
Return exact prediction for the feature:
69103

70104
```elixir
71-
classificator = classificator |> LearnKit.NaiveBayes.Gaussian.predict([1, 2])
105+
classificator |> Gaussian.predict([1, 2])
72106
```
73107
feature - new feature for prediction, required
74108

75-
Returns the mean accuracy on the given test data and labels
109+
Returns the mean accuracy on the given test data and labels:
76110

77111
```elixir
78-
classificator = classificator |> LearnKit.NaiveBayes.Gaussian.score
112+
classificator |> Gaussian.score
79113
```
80114

81115
## Contributing

test/learn_kit/naive_bayes/gaussian_test.exs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ defmodule LearnKit.NaiveBayes.GaussianTest do
33

44
alias LearnKit.NaiveBayes.Gaussian
55

6+
setup_all do
7+
{:ok, classificator: Gaussian.new([{:label1, [[-1, -1], [-2, -1], [-3, -2]]}, {:label2, [[1, 1], [2, 1], [3, 2], [-2, -2]]}])}
8+
end
9+
610
test "create new knn classificator with empty data set" do
711
assert %Gaussian{data_set: data_set} = Gaussian.new
812

@@ -18,9 +22,8 @@ defmodule LearnKit.NaiveBayes.GaussianTest do
1822
assert data_set == [b1: [[2, 3]], a1: [[1, 3], [1, 2]]]
1923
end
2024

21-
test "fit data set" do
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
25+
test "fit data set", state do
26+
%Gaussian{fit_data: fit_data} = state[:classificator] |> Gaussian.fit
2427

2528
assert fit_data == [
2629
label1: [
@@ -34,25 +37,22 @@ defmodule LearnKit.NaiveBayes.GaussianTest do
3437
]
3538
end
3639

37-
test "return probability estimates for the feature" do
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
40+
test "return probability estimates for the feature", state do
41+
classificator = state[:classificator] |> Gaussian.fit
4042

4143
assert {:ok, result} = classificator |> Gaussian.predict_proba([1, 2])
4244
assert result == [label1: 0.0, label2: 0.017199571]
4345
end
4446

45-
test "return exact prediction for the feature" do
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
47+
test "return exact prediction for the feature", state do
48+
classificator = state[:classificator] |> Gaussian.fit
4849

4950
assert {:ok, result} = classificator |> Gaussian.predict([1, 2])
5051
assert result == {:label2, 0.017199571}
5152
end
5253

53-
test "returns the mean accuracy on the given test data and labels" do
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
54+
test "returns the mean accuracy on the given test data and labels", state do
55+
classificator = state[:classificator] |> Gaussian.fit
5656

5757
assert {:ok, result} = classificator |> Gaussian.score
5858
assert result == 0.857143

0 commit comments

Comments
 (0)