aprendizaje_maquina 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0f6636ede43358eb9dc98e1d2c102b7ebf5833e4
4
- data.tar.gz: a2e2c4ef6bf6cf919926cee5688c5da25e523de1
3
+ metadata.gz: b0a8b0a74d8f2e8f8b22a89bd9b6a5f8d1d36ef7
4
+ data.tar.gz: d631cfcae79261763f72b1c1f8ac94b1c8b80f56
5
5
  SHA512:
6
- metadata.gz: f6419ba629850cbdebbd1bf2100fede3fb23f7dd64632a52f034e1f531936f76bc06f53b42ba7487c0fc9afc8546637e1e3741cc56711f3e27e4ba7d4e038f67
7
- data.tar.gz: 78265b9ac2746f3ddda229b9a7afb5b303f54668fd51f1b8a53e0c94ef7f33039aaeb3dadda36faf9af8fcb01ead1983c61dd3c6f4ad4449bb0fc08d32820ffe
6
+ metadata.gz: f3065a235bb3cca7c5d325290d0b8b62d2991743d8f4321bc0eae82080b96942e4a4b323b18deab58cd01bf04c6e9e438bad8fd3183517e0f2f2a1eabe3caa8a
7
+ data.tar.gz: c7ca538696ab235ce326415eb6ebb70e7c97d51d15f33fff7c1f5b1c0f3f6425f74cfffabc8083999a0a92d582bbbc9887ca58eaf92366483240a74689cc35a8
data/README.md CHANGED
@@ -1,62 +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).
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).
@@ -8,6 +8,7 @@ module AprendizajeMaquina
8
8
  def initialize(x,y)
9
9
  @x = x
10
10
  @y = y
11
+ @trained = false
11
12
  if @x.is_a?(Array)
12
13
  @n = @x.length
13
14
  elsif @x.is_a?(Matrix)
@@ -17,11 +18,13 @@ module AprendizajeMaquina
17
18
 
18
19
  def encontrar_ecuacion
19
20
  if @x.is_a?(Array) && @y.is_a?(Array)
21
+ @trained = true
20
22
  @m = ((@n*sumatoria(multiplicar(@x,@y))) - (sumatoria(@x)*sumatoria(@y))).to_f / ((@n*sumatoria(al_cuadrado(@x))) - (sumatoria(@x)**2)).to_f
21
23
  @b = media(@y) - (@m * media(@x))
22
24
  @ecuacion = "Y = #{@m.round(4)}X+#{@b.round(4)}"
23
25
  @ecuacion
24
26
  elsif @x.is_a?(Matrix) && @y.is_a?(Vector)
27
+ @trained = true
25
28
  inversa = (1.to_f/(@x.transpose*@x).det)*((@x.transpose*@x).adjugate)
26
29
  @theta = inversa * (@x.transpose * @y)
27
30
  @theta
@@ -31,15 +34,23 @@ module AprendizajeMaquina
31
34
  end
32
35
 
33
36
  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
37
+ if @trained == true
38
+ if x_a_predecir.is_a?(Numeric)
39
+ prediccion = (@m * x_a_predecir) + @b
40
+ prediccion
41
+ elsif x_a_predecir.is_a?(Matrix)
42
+ prediccion = x_a_predecir * @theta
43
+ prediccion
44
+ else
45
+ raise ArgumentError, "Must be a number or matrix 1xN"
46
+ end
40
47
  else
41
- raise ArgumentError, "Must be a number or matrix 1xN"
48
+ return "There is not a equation to make predictions (first, run encontrar_ecuacion method)"
49
+ <<<<<<< HEAD
42
50
  end
51
+ =======
52
+ end
53
+ >>>>>>> c8f8271ff1de1334797f9c6f048289c908e9dcb5
43
54
  end
44
55
 
45
56
  private
@@ -70,4 +81,4 @@ module AprendizajeMaquina
70
81
  sumatoria(array).to_f / array.length
71
82
  end
72
83
  end
73
- end
84
+ end
@@ -1,3 +1,3 @@
1
1
  module AprendizajeMaquina
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aprendizaje_maquina
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erickson Morales
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-09 00:00:00.000000000 Z
11
+ date: 2017-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler