aprendizaje_maquina 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0f6636ede43358eb9dc98e1d2c102b7ebf5833e4
4
+ data.tar.gz: a2e2c4ef6bf6cf919926cee5688c5da25e523de1
5
+ SHA512:
6
+ metadata.gz: f6419ba629850cbdebbd1bf2100fede3fb23f7dd64632a52f034e1f531936f76bc06f53b42ba7487c0fc9afc8546637e1e3741cc56711f3e27e4ba7d4e038f67
7
+ data.tar.gz: 78265b9ac2746f3ddda229b9a7afb5b303f54668fd51f1b8a53e0c94ef7f33039aaeb3dadda36faf9af8fcb01ead1983c61dd3c6f4ad4449bb0fc08d32820ffe
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.16.0
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in aprendizaje_maquina.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ aprendizaje_maquina (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.7.0)
12
+ rspec-core (~> 3.7.0)
13
+ rspec-expectations (~> 3.7.0)
14
+ rspec-mocks (~> 3.7.0)
15
+ rspec-core (3.7.0)
16
+ rspec-support (~> 3.7.0)
17
+ rspec-expectations (3.7.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.7.0)
20
+ rspec-mocks (3.7.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.7.0)
23
+ rspec-support (3.7.0)
24
+
25
+ PLATFORMS
26
+ x86-mingw32
27
+
28
+ DEPENDENCIES
29
+ aprendizaje_maquina!
30
+ bundler (~> 1.16)
31
+ rake (~> 10.0)
32
+ rspec (~> 3.0)
33
+
34
+ BUNDLED WITH
35
+ 1.16.0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 erickson
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # AprendizajeMaquina
2
+
3
+ Aprendizaje maquina is a gem that help us to write ruby machine learning algorithms.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'aprendizaje_maquina'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install aprendizaje_maquina
18
+
19
+ ## Usage
20
+
21
+ for make predictions with the linear regression model
22
+
23
+ first
24
+
25
+ require 'aprendizaje_maquina'
26
+
27
+ load data from a CSV file
28
+
29
+ load = AprendizajeMaquina::Cargar.new("file.csv")
30
+ y = load.to_vector(3) # specify the column that you want to store on a vector
31
+ matrix = load.to_matrix # this put all the data of the csv file in a matrix
32
+ # if you don't specify the column or range of columns
33
+ x = load.to_matrix(0) # create a matrix with the data in the column 0 of the csv file
34
+ # you can specify range like this load.to_matrix(0..4)
35
+ x_with_ones = x.add_ones # this add a column of ones to the matrix
36
+
37
+ create an instance of the class RegresionLineal
38
+
39
+ regresion_lineal = AprendizajeMaquina::RegresionLineal.new(x_matrix,y_vector)
40
+ regresion_lineal.encontrar_ecuacion # find the theta values => Vector[114.50684133915638, 0.8310043668122375]
41
+ m = Matrix[[1,95]]
42
+ p regresion_lineal.hacer_prediccion(m) # make predictions
43
+ # => Vector[193.45225618631895]
44
+
45
+ linear regresion with arrays
46
+
47
+ x = [74,92,63,72,58,78,85,85,73,62,80,72]
48
+ y = [168,196,170,175,162,169,190,186,176,170,176,179]
49
+
50
+ regresion_simple = AprendizajeMaquina::RegresionLineal.new(x,y)
51
+ regresion_simple.encontrar_ecuacion
52
+ p regresion_simple.ecuacion
53
+ p regresion_simple.hacer_prediccion(95)
54
+
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/TheNoskOneVzla/aprendizaje_maquina.
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+ task :test => :spec
@@ -0,0 +1,37 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ #$: << 'lib'
5
+ require "aprendizaje_maquina/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "aprendizaje_maquina"
9
+ spec.version = AprendizajeMaquina::VERSION
10
+ spec.author = "Erickson Morales"
11
+ spec.email = ["erickson_19_07@hotmail.com"]
12
+
13
+ spec.summary = "Machine learning gem / Una gema para el aprendizaje de maquinas."
14
+ spec.description = "This is a gem to help ruby developers to write machine learning algorithms easier and faster / Esta es una gema para ayudar a los desarrolladores de ruby a escribir algoritmos de aprendizaje automático más fácil y rápido."
15
+ spec.homepage = ""
16
+ spec.license = "MIT"
17
+
18
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
19
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
20
+ #if spec.respond_to?(:metadata)
21
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
22
+ #else
23
+ # raise "RubyGems 2.0 or newer is required to protect against " \
24
+ # "public gem pushes."
25
+ #end
26
+
27
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
28
+ f.match(%r{^(test|spec|features)/})
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_development_dependency "bundler", "~> 1.16"
35
+ spec.add_development_dependency "rake", "~> 10.0"
36
+ spec.add_development_dependency "rspec", "~> 3.0"
37
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "aprendizaje_maquina"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,46 @@
1
+ require 'csv'
2
+ require 'matrix'
3
+ module AprendizajeMaquina
4
+
5
+ class Cargar
6
+
7
+ def initialize(path_file)
8
+ @path_file = path_file
9
+ @csv_data = CSV.read(@path_file)
10
+ #@csv_data = CSV.read(File.join(Dir.pwd,@path_file))
11
+ @largo_colum = @csv_data[0].length
12
+ end
13
+
14
+ def to_matrix(columnas = nil)
15
+ if columnas == nil
16
+ array = @csv_data.map{|e| e.map{|o| o.to_i } }
17
+ matrix = Matrix.rows(array,copy=true)
18
+ matrix
19
+ elsif columnas.is_a?(Range)
20
+ if columnas.last >= @largo_colum
21
+ raise ArgumentError, "Number of columns don't exist"
22
+ else
23
+ array = @csv_data.map{|e| e[columnas].map{|i| i.to_i} }
24
+ matrix = Matrix.rows(array,copy=true)
25
+ matrix
26
+ end
27
+ elsif columnas.is_a?(Integer)
28
+ array = @csv_data.map { |e| e[columnas].to_i }
29
+ matrix = Matrix[array].transpose
30
+ matrix
31
+ else
32
+ raise ArgumentError, "Must be nil, range or integer"
33
+ end
34
+ end
35
+
36
+ def to_vector(columna)
37
+ if columna >= @largo_colum
38
+ raise ArgumentError, "Column don't exist"
39
+ else
40
+ array = @csv_data.map { |e| e[columna].to_i }
41
+ vector = Vector.elements(array,copy=true)
42
+ vector
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,7 @@
1
+ class Matrix
2
+ def add_ones
3
+ matrix = self.to_a.map{|i| i.insert(0,1)}
4
+ matrix = Matrix.rows(matrix,copy=true)
5
+ matrix
6
+ end
7
+ end
@@ -0,0 +1,73 @@
1
+ module AprendizajeMaquina
2
+
3
+ class RegresionLineal
4
+
5
+ attr_reader :m,:b, :ecuacion, :theta
6
+ attr_accessor :x,:y
7
+
8
+ def initialize(x,y)
9
+ @x = x
10
+ @y = y
11
+ if @x.is_a?(Array)
12
+ @n = @x.length
13
+ elsif @x.is_a?(Matrix)
14
+ @n = @x.column_count
15
+ end
16
+ end
17
+
18
+ def encontrar_ecuacion
19
+ if @x.is_a?(Array) && @y.is_a?(Array)
20
+ @m = ((@n*sumatoria(multiplicar(@x,@y))) - (sumatoria(@x)*sumatoria(@y))).to_f / ((@n*sumatoria(al_cuadrado(@x))) - (sumatoria(@x)**2)).to_f
21
+ @b = media(@y) - (@m * media(@x))
22
+ @ecuacion = "Y = #{@m.round(4)}X+#{@b.round(4)}"
23
+ @ecuacion
24
+ elsif @x.is_a?(Matrix) && @y.is_a?(Vector)
25
+ inversa = (1.to_f/(@x.transpose*@x).det)*((@x.transpose*@x).adjugate)
26
+ @theta = inversa * (@x.transpose * @y)
27
+ @theta
28
+ else
29
+ raise ArgumentError
30
+ end
31
+ end
32
+
33
+ def hacer_prediccion(x_a_predecir)
34
+ if x_a_predecir.is_a?(Numeric)
35
+ prediccion = (@m * x_a_predecir) + @b
36
+ prediccion
37
+ elsif x_a_predecir.is_a?(Matrix)
38
+ prediccion = x_a_predecir * @theta
39
+ prediccion
40
+ else
41
+ raise ArgumentError, "Must be a number or matrix 1xN"
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def multiplicar(array_1,array_2)
48
+ iter = @n - 1
49
+ xy = []
50
+ for i in 0..iter
51
+ xy << array_1[i]*array_2[i]
52
+ end
53
+ xy
54
+ end
55
+
56
+ def al_cuadrado(array)
57
+ iter = @n - 1
58
+ x_2 = []
59
+ for i in 0..iter
60
+ x_2 << array[i]**2
61
+ end
62
+ x_2
63
+ end
64
+
65
+ def sumatoria(array)
66
+ array.inject(0) { |elem1, elem2| elem1 + elem2 }
67
+ end
68
+
69
+ def media(array)
70
+ sumatoria(array).to_f / array.length
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,3 @@
1
+ module AprendizajeMaquina
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,18 @@
1
+ require "aprendizaje_maquina/version"
2
+ require "aprendizaje_maquina/cargar"
3
+ require "aprendizaje_maquina/regresion_lineal"
4
+ require "aprendizaje_maquina/matrixx"
5
+
6
+ module AprendizajeMaquina
7
+ #class RegresionLogistica
8
+ # Coming soon...
9
+ #end
10
+
11
+ #class RedNeuronal
12
+ # Coming soon...
13
+ #end
14
+
15
+ #class ArbolDecision
16
+ # Coming soon...
17
+ #end
18
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aprendizaje_maquina
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Erickson Morales
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-11-09 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: This is a gem to help ruby developers to write machine learning algorithms
56
+ easier and faster / Esta es una gema para ayudar a los desarrolladores de ruby a
57
+ escribir algoritmos de aprendizaje automático más fácil y rápido.
58
+ email:
59
+ - erickson_19_07@hotmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - Gemfile
68
+ - Gemfile.lock
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - aprendizaje_maquina.gemspec
73
+ - bin/console
74
+ - bin/setup
75
+ - lib/aprendizaje_maquina.rb
76
+ - lib/aprendizaje_maquina/cargar.rb
77
+ - lib/aprendizaje_maquina/matrixx.rb
78
+ - lib/aprendizaje_maquina/regresion_lineal.rb
79
+ - lib/aprendizaje_maquina/version.rb
80
+ homepage: ''
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.5.2
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Machine learning gem / Una gema para el aprendizaje de maquinas.
104
+ test_files: []