ruby-svd 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/svd_matrix.rb +7 -8
- metadata +3 -3
data/lib/svd_matrix.rb
CHANGED
@@ -40,17 +40,16 @@ class SVDMatrix < Matrix
|
|
40
40
|
u_array, w_array, v_array = SVD.decompose(input_array, row_size, column_size)
|
41
41
|
|
42
42
|
# recompose U matrix
|
43
|
-
u = SVDMatrix.new(row_size, column_size)
|
44
|
-
row_size.times {|i| u.set_row(i, u_array.slice!(0,column_size))}
|
43
|
+
u = SVDMatrix.new(row_size, reduce_dimensions_to || column_size)
|
44
|
+
row_size.times {|i| u.set_row(i, u_array.slice!(0, reduce_dimensions_to || column_size))}
|
45
45
|
|
46
46
|
# recompose V matric
|
47
|
-
v = SVDMatrix.new(column_size, column_size)
|
48
|
-
column_size.times {|i| v.set_row(i, v_array.slice!(0,column_size))}
|
49
|
-
v = v.transpose
|
47
|
+
v = SVDMatrix.new(column_size, reduce_dimensions_to || column_size)
|
48
|
+
column_size.times {|i| v.set_row(i, v_array.slice!(0, reduce_dimensions_to || column_size))}
|
50
49
|
|
51
50
|
# diagonalise W array as a matrix
|
52
51
|
if reduce_dimensions_to
|
53
|
-
|
52
|
+
w_array = w_array[0...reduce_dimensions_to]
|
54
53
|
end
|
55
54
|
w = Matrix.diagonal(*w_array)
|
56
55
|
|
@@ -63,8 +62,8 @@ class SVDMatrix < Matrix
|
|
63
62
|
# Analysis uses 2 dimensions, and commonly tf-idf cell data.
|
64
63
|
# The recombined matrix, and the 3 decomposed matrices are
|
65
64
|
# returned.
|
66
|
-
def reduce_dimensions(dimensions)
|
65
|
+
def reduce_dimensions(dimensions = 2)
|
67
66
|
u, w, v = self.decompose(dimensions)
|
68
|
-
[(u * w * v), u, w, v]
|
67
|
+
[(u * w * v.transpose), u, w, v]
|
69
68
|
end
|
70
69
|
end
|