Class: SVDMatrix
- Inherits:
-
Matrix
- Object
- Matrix
- SVDMatrix
- Defined in:
- lib/svd_matrix.rb
Instance Method Summary collapse
-
#[]=(i, j, val) ⇒ Object
Set the value of the cell i, j.
-
#decompose(reduce_dimensions_to = nil) ⇒ Object
Perform SVD and decompose the matrix into three matrices: U, W, and V.
-
#initialize(m, n) ⇒ SVDMatrix
constructor
Create a new SVD Matrix with m rows, n columns.
-
#inspect ⇒ Object
Nicely formatted inspect string for the matrix.
-
#reduce_dimensions(dimensions = 2) ⇒ Object
Reduce the number of dimensions of the data to dimensions.
-
#set_row(i, row) ⇒ Object
Set the value of a row to an array.
Constructor Details
#initialize(m, n) ⇒ SVDMatrix
Create a new SVD Matrix with m rows, n columns
8 9 10 11 |
# File 'lib/svd_matrix.rb', line 8 def initialize(m, n) @rows = Array.new(m) m.times {|i| @rows[i] = Array.new(n)} end |
Instance Method Details
#[]=(i, j, val) ⇒ Object
Set the value of the cell i, j
14 15 16 |
# File 'lib/svd_matrix.rb', line 14 def []=(i, j, val) @rows[i][j] = val end |
#decompose(reduce_dimensions_to = nil) ⇒ Object
Perform SVD and decompose the matrix into three matrices: U, W, and V. You can choose to reduce the dimensionality of the data by setting a number of diagonal cells to 0. For example, reduce_dimentions_to = 2 will set a 4x4 W matrix into:
- NUM, 0, 0, 0
- 0, NUM, 0, 0
- 0, 0, 0, 0
- 0, 0, 0, 0
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/svd_matrix.rb', line 37 def decompose(reduce_dimensions_to = nil) input_array = [] @rows.each {|row| input_array += row} u_array, w_array, v_array = SVD.decompose(input_array, row_size, column_size) # recompose U matrix u = SVDMatrix.new(row_size, reduce_dimensions_to || column_size) row_size.times {|i| u.set_row(i, u_array.slice!(0, column_size)[0...reduce_dimensions_to])} # recompose V matric v = SVDMatrix.new(column_size, reduce_dimensions_to || column_size) column_size.times {|i| v.set_row(i, v_array.slice!(0, column_size)[0...reduce_dimensions_to])} # diagonalise W array as a matrix if reduce_dimensions_to w_array = w_array[0...reduce_dimensions_to] end w = Matrix.diagonal(*w_array) [u, w, v] end |
#inspect ⇒ Object
Nicely formatted inspect string for the matrix
24 25 26 |
# File 'lib/svd_matrix.rb', line 24 def inspect @rows.collect {|row| "#{row.inspect}\n"} end |
#reduce_dimensions(dimensions = 2) ⇒ Object
Reduce the number of dimensions of the data to dimensions. Returns a back a recombined matrix (conceptually the original matrix dimensionally reduced). For example Latent Semantic Analysis uses 2 dimensions, and commonly tf-idf cell data. The recombined matrix, and the 3 decomposed matrices are returned.
65 66 67 68 |
# File 'lib/svd_matrix.rb', line 65 def reduce_dimensions(dimensions = 2) u, w, v = self.decompose(dimensions) [(u * w * v.transpose), u, w, v] end |
#set_row(i, row) ⇒ Object
Set the value of a row to an array
19 20 21 |
# File 'lib/svd_matrix.rb', line 19 def set_row(i, row) @rows[i] = row end |