uranai 0.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +47 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/uranai/data.rb +42 -0
- data/lib/uranai/fitting_parameter.rb +31 -0
- data/lib/uranai/linear_regression.rb +63 -0
- data/lib/uranai/version.rb +3 -0
- data/lib/uranai.rb +2 -0
- data/uranai.gemspec +25 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8a799e4a46726580878537ac459dfcf49da77b94
|
4
|
+
data.tar.gz: abc37c0a3ad0669e60e1974ccacbbc5001b208bf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 292c257928957bcc13b6dcbab17084784808225f5c4f3a01cbaa21101e6c0d268fde3f2ea5693674fc0450121781ce4fda1c27c1a53cfdc32ee7c71a122b0d99
|
7
|
+
data.tar.gz: 4f038e3a64712bbee9b6a157b360a83abf308f1dc72adda9ed68ffd25600ffca57b4d357a2890bf79427e4671a85f8b4548bf5703867105d8b160a365be1a565
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Teerawat Lamanchart
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# Uranai
|
2
|
+
|
3
|
+
A library for solving **Supervised Learning** (***regression*** & ***classification***) problems.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
```ruby
|
9
|
+
gem 'uranai'
|
10
|
+
```
|
11
|
+
And then execute:
|
12
|
+
```shell
|
13
|
+
$ bundle
|
14
|
+
```
|
15
|
+
Or install it yourself as:
|
16
|
+
```shell
|
17
|
+
$ gem install uranai
|
18
|
+
```
|
19
|
+
|
20
|
+
## Linear Regression Example:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
# Instantiates a linear regression instance.
|
24
|
+
linear_regression = Uranai::LinearRegression.new("data.csv")
|
25
|
+
|
26
|
+
# Computes cost (using cost function) with `default_theta`.
|
27
|
+
initial_cost = linear_regression.compute_cost
|
28
|
+
|
29
|
+
# Computes cost (using cost function) with custom theta.
|
30
|
+
custom_cost = linear_regression.compute_cost(theta: [0.5, 0.5])
|
31
|
+
|
32
|
+
# Finds fitting parameters.
|
33
|
+
fitting_params = linear_regression.fitting_params
|
34
|
+
|
35
|
+
# Predicts the result from given data.
|
36
|
+
result = fitting_params.predict([3.5])
|
37
|
+
```
|
38
|
+
|
39
|
+
## Development
|
40
|
+
|
41
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
42
|
+
|
43
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/teerawat1992/uranai.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "uranai"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/uranai/data.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require "csv"
|
2
|
+
|
3
|
+
module Uranai
|
4
|
+
class Data
|
5
|
+
def initialize(csv)
|
6
|
+
@data = CSV.read(csv).map { |arr| arr.map(&:to_f) }.transpose
|
7
|
+
end
|
8
|
+
|
9
|
+
def column_size
|
10
|
+
data.length
|
11
|
+
end
|
12
|
+
|
13
|
+
def row_size
|
14
|
+
data.transpose.length
|
15
|
+
end
|
16
|
+
alias :training_example_size :row_size
|
17
|
+
|
18
|
+
def feature_size
|
19
|
+
column_size - 1
|
20
|
+
end
|
21
|
+
|
22
|
+
def x
|
23
|
+
data[0...column_size-1].insert(0, [1] * row_size)
|
24
|
+
end
|
25
|
+
|
26
|
+
def matrix_x
|
27
|
+
Matrix.columns(x)
|
28
|
+
end
|
29
|
+
|
30
|
+
def y
|
31
|
+
[data[-1]]
|
32
|
+
end
|
33
|
+
|
34
|
+
def matrix_y
|
35
|
+
Matrix.columns(y)
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
attr_reader :data
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Uranai
|
2
|
+
class FittingParameter
|
3
|
+
attr_reader :params
|
4
|
+
alias :values :params
|
5
|
+
|
6
|
+
def initialize(params)
|
7
|
+
@params = params
|
8
|
+
end
|
9
|
+
|
10
|
+
def predict(input_data)
|
11
|
+
check(input_data) do
|
12
|
+
params
|
13
|
+
.zip(input_data.insert(0, 1))
|
14
|
+
.map { |n1,n2| n1 * n2 }
|
15
|
+
.reduce(:+)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def check(data)
|
22
|
+
raise "input data must be Array" unless data.kind_of?(Array)
|
23
|
+
raise "input data size must be equal to feature size (#{feature_size})" if data.length != feature_size
|
24
|
+
yield
|
25
|
+
end
|
26
|
+
|
27
|
+
def feature_size
|
28
|
+
params.length - 1
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require "matrix"
|
2
|
+
require "uranai/data"
|
3
|
+
require "uranai/fitting_parameter"
|
4
|
+
|
5
|
+
module Uranai
|
6
|
+
class LinearRegression
|
7
|
+
def initialize(csv)
|
8
|
+
@data = Uranai::Data.new(csv)
|
9
|
+
end
|
10
|
+
|
11
|
+
def compute_cost(theta: default_theta)
|
12
|
+
matrix_theta = Matrix.columns(normalize_theta(theta))
|
13
|
+
((matrix_x * matrix_theta) - matrix_y)
|
14
|
+
.map { |element| element ** 2 }
|
15
|
+
.inject(0.0) { |sum,i| sum + i }
|
16
|
+
./ (2 * data.training_example_size)
|
17
|
+
end
|
18
|
+
|
19
|
+
def fitting_params_with_gradient_descent(options = {})
|
20
|
+
options.keep_if { |k,_| [:alpha, :num_iters, :theta, :normalize].include?(k) }
|
21
|
+
FittingParameter.new(gradient_descent(options))
|
22
|
+
end
|
23
|
+
alias :fitting_params :fitting_params_with_gradient_descent
|
24
|
+
|
25
|
+
def fitting_params_with_normal_equations
|
26
|
+
FittingParameter.new(normal_equations)
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
attr_reader :data
|
32
|
+
|
33
|
+
def default_theta
|
34
|
+
[[0] * data.column_size]
|
35
|
+
end
|
36
|
+
|
37
|
+
def normalize_theta(theta)
|
38
|
+
[theta.flatten]
|
39
|
+
end
|
40
|
+
|
41
|
+
def matrix_x
|
42
|
+
data.matrix_x
|
43
|
+
end
|
44
|
+
|
45
|
+
def matrix_y
|
46
|
+
data.matrix_y
|
47
|
+
end
|
48
|
+
|
49
|
+
# TODO: Add an option for normalizing feature data (x data)
|
50
|
+
def gradient_descent(alpha: 0.01, num_iters: 1500, theta: default_theta, normalize: false)
|
51
|
+
matrix_theta = Matrix.columns(normalize_theta(theta))
|
52
|
+
num_iters.times do
|
53
|
+
matrix_theta -= (matrix_x.transpose * ((matrix_x * matrix_theta) - matrix_y)) * (alpha / data.training_example_size)
|
54
|
+
end
|
55
|
+
matrix_theta.to_a.flatten
|
56
|
+
end
|
57
|
+
|
58
|
+
def normal_equations
|
59
|
+
((matrix_x.transpose * matrix_x).inverse * matrix_x.transpose * matrix_y)
|
60
|
+
.to_a.flatten
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/lib/uranai.rb
ADDED
data/uranai.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'uranai/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "uranai"
|
8
|
+
spec.version = Uranai::VERSION
|
9
|
+
spec.authors = ["Teerawat Lamanchart"]
|
10
|
+
spec.email = ["teerawat.lamanchart@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Machine Learning for Ruby}
|
13
|
+
spec.description = %q{A library for solving Supervised Learning (regression & classification) problems}
|
14
|
+
spec.homepage = "https://github.com/teerawat1992/uranai"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: uranai
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Teerawat Lamanchart
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description: A library for solving Supervised Learning (regression & classification)
|
56
|
+
problems
|
57
|
+
email:
|
58
|
+
- teerawat.lamanchart@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/uranai.rb
|
72
|
+
- lib/uranai/data.rb
|
73
|
+
- lib/uranai/fitting_parameter.rb
|
74
|
+
- lib/uranai/linear_regression.rb
|
75
|
+
- lib/uranai/version.rb
|
76
|
+
- uranai.gemspec
|
77
|
+
homepage: https://github.com/teerawat1992/uranai
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.5.1
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Machine Learning for Ruby
|
101
|
+
test_files: []
|