callidus 1.0.67

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7e8348b821e13ceed061213ebf3f311f32c52853
4
+ data.tar.gz: b9231b12ef42ef76f52129149c367d724d4280cf
5
+ SHA512:
6
+ metadata.gz: 46ac0cbdd0e5bf0f50475505a6ed89c60537ac0bfa9879872238ac269e460bebe6347e194c694f044d5aac371add9cfc28dba2f16d5787fec4704c03ed1d6834
7
+ data.tar.gz: 075e233a7eb2c697aea13d7966a548dcd8d310220d7db2d743d611658020e74c11ee488a15184cb3679799119be5d917af9839ba8355de90d2594a52b5379916
@@ -0,0 +1,3 @@
1
+ require_relative "./src/KNN.rb"
2
+ require_relative "./src/LinearRegression.rb"
3
+ require_relative "./src/Winnow.rb"
@@ -0,0 +1,28 @@
1
+ require_relative "../util/Comp"
2
+
3
+ module Callidus
4
+ class KNN
5
+ attr_accessor :input
6
+ attr_accessor :output
7
+
8
+ def initialize(ip = [], op = [])
9
+ @input = ip
10
+ @output = op
11
+ end
12
+
13
+ def predict(x, k = 1)
14
+ diffs = @input.map { |i| Math.sqrt(x.each_with_index.map { |val, ind| (i[ind] - val) ** 2 }.sum) }
15
+ tdiffs = diffs.dup
16
+
17
+ mins = []
18
+
19
+ k.times do |i|
20
+ ind = tdiffs.index(tdiffs.min)
21
+ mins << tdiffs[ind]
22
+ tdiffs.pop(ind)
23
+ end
24
+
25
+ return @output[diffs.index(Comp::Arrays.MostFrequent(mins))];
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,97 @@
1
+ require_relative "../util/Util"
2
+
3
+ module Callidus
4
+ class LinearRegression
5
+ attr_accessor :input
6
+ attr_accessor :output
7
+ attr_accessor :predicted_output
8
+
9
+ attr_reader :slope
10
+ attr_reader :y_intercept
11
+
12
+ attr_reader :correlation
13
+ attr_reader :standard_error
14
+
15
+ def initialize(ip = [], op = [])
16
+ @input = ip
17
+ @output = op
18
+
19
+ @trained = false
20
+ end
21
+
22
+ private def assert_trained(method = "train")
23
+ if !@trained
24
+ raise Util::UntrainedError.new("LinearRegression")
25
+ end
26
+ end
27
+
28
+ def formatted
29
+ assert_trained()
30
+
31
+ "f(x) = #{@slope}x + #{@y_intercept}"
32
+ end
33
+
34
+ def train
35
+ sum_x = 0
36
+ sum_y = 0
37
+ sum_xy = 0
38
+ sum_xx = 0
39
+
40
+ n = @input.size > @output.size ? @output.size : @input.size
41
+
42
+ @predicted_output = []
43
+
44
+ n.times do |i|
45
+ x = @input[i]
46
+ y = @output[i]
47
+
48
+ sum_x += x
49
+ sum_y += y
50
+ sum_xx += (x * x)
51
+ sum_xy += (x * y)
52
+ end
53
+
54
+ @slope = ((n * sum_xy - sum_x * sum_y).to_f / (n * sum_xx - sum_x * sum_x).to_f).round(3)
55
+ @y_intercept = ((sum_y / n).to_f - (@slope * sum_x).to_f / n.to_f).round(3)
56
+
57
+ @predicted_output = @input.map { |x| (x * @slope + @y_intercept).round(3) }
58
+
59
+ @trained = true
60
+
61
+ self.find_correlation.find_standard_error
62
+ end
63
+
64
+ def find_correlation
65
+ assert_trained()
66
+
67
+ mean = @output.sum/@output.size
68
+
69
+ diffYM = @output.map { |y| (y - mean) ** 2 };
70
+ diffPYM = @predicted_output.map { |y| (y - mean) ** 2 };
71
+
72
+ @correlation = (diffPYM.sum/diffYM.sum).round(3)
73
+
74
+ self
75
+ end
76
+
77
+ def find_standard_error
78
+ assert_trained()
79
+
80
+ n = @input.size > @output.size ? @output.size : @input.size
81
+
82
+ diffs = []
83
+
84
+ n.times do |i|
85
+ diffs << (@predicted_output[i] - @output[i]) ** 2
86
+ end
87
+
88
+ @standard_error = (Math.sqrt(diffs.sum/(n - 2.0))).round(3)
89
+
90
+ self
91
+ end
92
+
93
+ def predict(x)
94
+ x * @slope + @y_intercept
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,64 @@
1
+ require_relative "../util/Util"
2
+
3
+ module Callidus
4
+ class Winnow
5
+ attr_accessor :input
6
+ attr_accessor :output
7
+
8
+ attr_accessor :a
9
+ attr_accessor :mode
10
+
11
+ attr_reader :weights
12
+
13
+ def initialize(ip = [], op = [], options = {})
14
+ @input = ip
15
+ @output = op
16
+
17
+ @default_weight = options[:default_weight] || 1
18
+ @a = options[:a] || 2;
19
+ @mode = options[:mode] || 0 # 0 is demote, 1 is reset
20
+
21
+ @weights = []
22
+ @trained = false
23
+ end
24
+
25
+ private def assert_trained(method = "train")
26
+ if !@trained
27
+ raise Util::UntrainedError.new("Winnow")
28
+ end
29
+ end
30
+
31
+ def train(a = @a)
32
+ if (@input[0] and @input[0].size != @weights.size)
33
+ @weights = Array.new(@input[0].size, @default_weight)
34
+ end
35
+
36
+ @input.each_index do |i|
37
+ @input[i].each_index do |j|
38
+ if @input[i][j] == 1
39
+ @weights[j] *= @output[i] == 0 ? (@mode == 0 ? 1 / a : 0) : a
40
+ end
41
+ end
42
+ end
43
+
44
+ @trained = true
45
+
46
+ return self
47
+ end
48
+
49
+ def predict(x, options = {})
50
+ assert_trained()
51
+
52
+ bias = options[:bias] || 0
53
+ threshold = options[:threshold] || (@input.size / 2).to_i
54
+
55
+ probability = 0;
56
+
57
+ x.each_index do |i|
58
+ probability += @weights[i] * x[i] + bias
59
+ end
60
+
61
+ return (probability > threshold ? 1 : 0)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,53 @@
1
+ class ::Array
2
+ def sum
3
+ s = 0
4
+
5
+ self.each do |i|
6
+ s += i
7
+ end
8
+
9
+ s
10
+ end
11
+
12
+ def max
13
+ m = self[0]
14
+
15
+ self.each do |i|
16
+ if i > m
17
+ m = i
18
+ end
19
+ end
20
+
21
+ m
22
+ end
23
+
24
+ def min
25
+ m = self[0]
26
+
27
+ self.each do |i|
28
+ if i < m
29
+ m = i
30
+ end
31
+ end
32
+
33
+ m
34
+ end
35
+ end
36
+
37
+ module Comp
38
+ class Arrays
39
+ def self.MostFrequent(arr)
40
+ freqs = {}
41
+
42
+ arr.each do |i|
43
+ if freqs.has_key?(i)
44
+ freqs[i] = freqs[i] + 1
45
+ else
46
+ freqs[i] = 1
47
+ end
48
+ end
49
+
50
+ arr[freqs.values.index(freqs.values.max)];
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,7 @@
1
+ module Util
2
+ class UntrainedError < StandardError
3
+ def initialize(modelType, method = "train")
4
+ super("Model must be trained (hint: Call the `" + method + "` method of your " + modelType + " instance)")
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: callidus
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.67
5
+ platform: ruby
6
+ authors:
7
+ - Hayden Higginbotham
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-01-21 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A collection of machine learning algorithms implemented in a very simple,
14
+ concise, and easy-to-use way.
15
+ email: hayden@higg.me
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/callidus.rb
21
+ - lib/src/KNN.rb
22
+ - lib/src/LinearRegression.rb
23
+ - lib/src/Winnow.rb
24
+ - lib/util/Comp.rb
25
+ - lib/util/Util.rb
26
+ homepage: https://github.com/haydenhigg/Callidus
27
+ licenses:
28
+ - MIT
29
+ metadata:
30
+ documentation_uri: https://github.com/haydenhigg/Callidus#Callidus
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 2.2.2
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Callidus
51
+ test_files: []