ruby-svd 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.markdown +36 -0
  2. data/lib/lsa.rb +4 -0
  3. data/lib/svd_matrix.rb +2 -1
  4. metadata +8 -16
  5. data/README +0 -6
@@ -0,0 +1,36 @@
1
+ Ruby SVD
2
+ ========
3
+ Singular Value Decomposition for Ruby with no dependency on GSL or LAPACK.
4
+
5
+ About
6
+ -----
7
+ Ruby SVD provides an interface to the Numerical Recipies C implementation of an SVD matrix decomposer. It also includes an extension to the native Ruby Matrix class with a simple LSA
8
+ method (decomposes the matrix, transposes matrix V, diagonalises the S array into a matrix, then removes all but the two leading terms in S to compress the original matrix to two dimensions).
9
+
10
+ Sample Usage
11
+ ------------
12
+ ```ruby
13
+ require 'ruby-svd'
14
+
15
+ tdm = SVDMatrix.new(4, 2)
16
+ tdm.set_row(0, [1,0])
17
+ tdm.set_row(1, [1,0])
18
+ tdm.set_row(2, [0,1])
19
+ tdm.set_row(3, [0,1])
20
+
21
+ puts "== Term document matrix:"
22
+ p tdm
23
+
24
+ puts "\n== Decomposing matrix:"
25
+ lsa = LSA.new(tdm)
26
+ p lsa
27
+
28
+ puts "\n== Classifying new column vector: [1, 0.5, 0, 0.5]"
29
+ puts "Format is [column, similarity]"
30
+ ranks = lsa.classify_vector([1,0.5,0,0.5])
31
+ p ranks
32
+
33
+ sorted_ranks = ranks.sort_by(&:last).reverse
34
+ puts "\n== Vector most similar to column #{sorted_ranks.first[0]}"
35
+ p tdm.column(sorted_ranks.first[0])
36
+ ```
data/lib/lsa.rb CHANGED
@@ -7,6 +7,10 @@ class LSA
7
7
  @u, @s, @v = matrix.decompose(2)
8
8
  end
9
9
 
10
+ def inspect
11
+ "U:\n#{@u.inspect}\n\nS:\n#{@s.inspect}\n\nV:\n#{@v.inspect}"
12
+ end
13
+
10
14
  # Return a distance (cosine similarity) between a new vector,
11
15
  # and all the clusters (columns) used in the original matrix.
12
16
  # Returns a sorted list of indexes and distances,
@@ -7,6 +7,7 @@ class SVDMatrix < Matrix
7
7
  # Create a new SVD Matrix with m rows, n columns
8
8
  def initialize(m, n)
9
9
  @rows = Array.new(m)
10
+ @column_size = n
10
11
  m.times {|i| @rows[i] = Array.new(n)}
11
12
  end
12
13
 
@@ -22,7 +23,7 @@ class SVDMatrix < Matrix
22
23
 
23
24
  # Nicely formatted inspect string for the matrix
24
25
  def inspect
25
- @rows.collect {|row| "#{row.inspect}\n"}
26
+ @rows.collect {|row| row.inspect}.join("\n")
26
27
  end
27
28
 
28
29
  # Perform SVD and decompose the matrix into three matrices:
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-svd
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 5
8
- - 0
9
- version: 0.5.0
4
+ prerelease:
5
+ version: 0.5.1
10
6
  platform: ruby
11
7
  authors:
12
8
  - Will Cannings
@@ -14,7 +10,7 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2010-09-30 00:00:00 +10:00
13
+ date: 2011-05-30 00:00:00 +10:00
18
14
  default_executable:
19
15
  dependencies: []
20
16
 
@@ -26,7 +22,7 @@ extensions:
26
22
  - ext/extconf.rb
27
23
  extra_rdoc_files:
28
24
  - LICENSE
29
- - README
25
+ - README.markdown
30
26
  files:
31
27
  - ext/extconf.rb
32
28
  - ext/nrutil.h
@@ -36,14 +32,14 @@ files:
36
32
  - lib/ruby-svd.rb
37
33
  - lib/svd_matrix.rb
38
34
  - LICENSE
39
- - README
35
+ - README.markdown
40
36
  has_rdoc: true
41
37
  homepage: http://github.com/willcannings/ruby-svd
42
38
  licenses: []
43
39
 
44
40
  post_install_message:
45
- rdoc_options:
46
- - --charset=UTF-8
41
+ rdoc_options: []
42
+
47
43
  require_paths:
48
44
  - lib
49
45
  - ext
@@ -52,21 +48,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
52
48
  requirements:
53
49
  - - ">="
54
50
  - !ruby/object:Gem::Version
55
- segments:
56
- - 0
57
51
  version: "0"
58
52
  required_rubygems_version: !ruby/object:Gem::Requirement
59
53
  none: false
60
54
  requirements:
61
55
  - - ">="
62
56
  - !ruby/object:Gem::Version
63
- segments:
64
- - 0
65
57
  version: "0"
66
58
  requirements: []
67
59
 
68
60
  rubyforge_project:
69
- rubygems_version: 1.3.7
61
+ rubygems_version: 1.6.2
70
62
  signing_key:
71
63
  specification_version: 3
72
64
  summary: SVD for Ruby
data/README DELETED
@@ -1,6 +0,0 @@
1
- Singular Value Decomposition for Ruby with no dependency on GSL or LAPACK. Uses a C extension
2
- to the Numerical Recipies C implementation of an SVD matrix decomposer. Also includes an
3
- extension to the native Ruby Matrix class with a simple LSA method (decomposes the matrix,
4
- transposes matrix V, diagonalises the S array into a matrix, then removes all but the two
5
- leading terms in S to compress the original matrix to two dimensions).
6
-