You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@mahout.apache.org by Han Fan <vi...@gmail.com> on 2014/06/13 02:49:42 UTC

Mahout SingularValueDecomposition gives wrong answer

import org.apache.mahout.math.DenseMatrix;
import org.apache.mahout.math.Matrix;
import org.apache.mahout.math.SingularValueDecomposition;

public class testMPInverse {
	public void test() {
		Matrix matrix = new DenseMatrix(new double[][] {
				{1,2},
				{3,4},
				{5,6}
			  });
		SingularValueDecomposition svd = new 
SingularValueDecomposition(matrix);
		 Matrix u = svd.getU();
		 Matrix v = svd.getV();
		 Matrix s = svd.getS();
		 System.out.println(u);
		 System.out.println(v);
		 System.out.println(s);
	}

	public static void main(String[] args) {
		testMPInverse t = new testMPInverse();
		t.test();
	}
}

v is

{
  0  =>	{0:0.42866713354862623,1:-0.8059639085892977}
  1  =>	{0:0.5663069188480352,1:-0.1123824140965937}
  2  =>	{0:0.7039467041474443,1:0.5811990803961101}
}

MATLAB gives a different answer

x=[1,2,3;4,5,6];
[u s v]=svd(x);
v
v =

   -0.4287    0.8060    0.4082
   -0.5663    0.1124   -0.8165
   -0.7039   -0.5812    0.4082

Why the last column of v produced by Mahout SingularValueDecomposition is 0?

Thanks for your time.


Re: Mahout SingularValueDecomposition gives wrong answer

Posted by Han Fan <vi...@gmail.com>.
Thanks Chris and Ted. I see where the problem is now.
And thanks to Chris for your reminder of the transposes.


if (arg.numRows() < arg.numCols()) {
       transpositionNeeded = true;
}
...
public Matrix getU() {
     if (transpositionNeeded) { //case numRows() < numCols()
       return new DenseMatrix(v);
     } else {
...

On 2014-6-14 0:27, Ted Dunning wrote:
> Chris is just right.
>
>
> See the compact SVD definition here:
>
> http://en.wikipedia.org/wiki/Singular_value_decomposition#Reduced_SVDs
>
>
>
>
> On Fri, Jun 13, 2014 at 4:47 AM, Chris Baker <cg...@gmail.com> wrote:
>
>> MATLAB's svd() function computes the singular value decomposition, with
>> orthogonal matrices U and V. Mahout computes a singular value
>> factorization, which does not contain the subspaces that do not contribute
>> to the data matrix. Compare against
>> svd(x,0) instead. Also, watch out for transposes.
>> On Jun 13, 2014 3:10 AM, "Han Fan" <vi...@gmail.com> wrote:
>>
>>>
>>> import org.apache.mahout.math.DenseMatrix;
>>> import org.apache.mahout.math.Matrix;
>>> import org.apache.mahout.math.SingularValueDecomposition;
>>>
>>> public class testMPInverse {
>>>          public void test() {
>>>                  Matrix matrix = new DenseMatrix(new double[][] {
>>>                                  {1,2},
>>>                                  {3,4},
>>>                                  {5,6}
>>>                            });
>>>                  SingularValueDecomposition svd = new
>>> SingularValueDecomposition(matrix);
>>>                   Matrix u = svd.getU();
>>>                   Matrix v = svd.getV();
>>>                   Matrix s = svd.getS();
>>>                   System.out.println(u);
>>>                   System.out.println(v);
>>>                   System.out.println(s);
>>>          }
>>>
>>>          public static void main(String[] args) {
>>>                  testMPInverse t = new testMPInverse();
>>>                  t.test();
>>>          }
>>> }
>>>
>>> v is
>>>
>>> {
>>>    0  => {0:0.42866713354862623,1:-0.8059639085892977}
>>>    1  => {0:0.5663069188480352,1:-0.1123824140965937}
>>>    2  => {0:0.7039467041474443,1:0.5811990803961101}
>>> }
>>>
>>> MATLAB gives a different answer
>>>
>>> x=[1,2,3;4,5,6];
>>> [u s v]=svd(x);
>>> v
>>> v =
>>>
>>>     -0.4287    0.8060    0.4082
>>>     -0.5663    0.1124   -0.8165
>>>     -0.7039   -0.5812    0.4082
>>>
>>> Why the last column of v produced by Mahout SingularValueDecomposition is
>>> 0?
>>>
>>> Thanks for your time.
>>>
>>>
>>
>



Re: Mahout SingularValueDecomposition gives wrong answer

Posted by Ted Dunning <te...@gmail.com>.
Chris is just right.


See the compact SVD definition here:

http://en.wikipedia.org/wiki/Singular_value_decomposition#Reduced_SVDs




On Fri, Jun 13, 2014 at 4:47 AM, Chris Baker <cg...@gmail.com> wrote:

> MATLAB's svd() function computes the singular value decomposition, with
> orthogonal matrices U and V. Mahout computes a singular value
> factorization, which does not contain the subspaces that do not contribute
> to the data matrix. Compare against
> svd(x,0) instead. Also, watch out for transposes.
> On Jun 13, 2014 3:10 AM, "Han Fan" <vi...@gmail.com> wrote:
>
> >
> > import org.apache.mahout.math.DenseMatrix;
> > import org.apache.mahout.math.Matrix;
> > import org.apache.mahout.math.SingularValueDecomposition;
> >
> > public class testMPInverse {
> >         public void test() {
> >                 Matrix matrix = new DenseMatrix(new double[][] {
> >                                 {1,2},
> >                                 {3,4},
> >                                 {5,6}
> >                           });
> >                 SingularValueDecomposition svd = new
> > SingularValueDecomposition(matrix);
> >                  Matrix u = svd.getU();
> >                  Matrix v = svd.getV();
> >                  Matrix s = svd.getS();
> >                  System.out.println(u);
> >                  System.out.println(v);
> >                  System.out.println(s);
> >         }
> >
> >         public static void main(String[] args) {
> >                 testMPInverse t = new testMPInverse();
> >                 t.test();
> >         }
> > }
> >
> > v is
> >
> > {
> >   0  => {0:0.42866713354862623,1:-0.8059639085892977}
> >   1  => {0:0.5663069188480352,1:-0.1123824140965937}
> >   2  => {0:0.7039467041474443,1:0.5811990803961101}
> > }
> >
> > MATLAB gives a different answer
> >
> > x=[1,2,3;4,5,6];
> > [u s v]=svd(x);
> > v
> > v =
> >
> >    -0.4287    0.8060    0.4082
> >    -0.5663    0.1124   -0.8165
> >    -0.7039   -0.5812    0.4082
> >
> > Why the last column of v produced by Mahout SingularValueDecomposition is
> > 0?
> >
> > Thanks for your time.
> >
> >
>

Re: Mahout SingularValueDecomposition gives wrong answer

Posted by Chris Baker <cg...@gmail.com>.
MATLAB's svd() function computes the singular value decomposition, with
orthogonal matrices U and V. Mahout computes a singular value
factorization, which does not contain the subspaces that do not contribute
to the data matrix. Compare against
svd(x,0) instead. Also, watch out for transposes.
On Jun 13, 2014 3:10 AM, "Han Fan" <vi...@gmail.com> wrote:

>
> import org.apache.mahout.math.DenseMatrix;
> import org.apache.mahout.math.Matrix;
> import org.apache.mahout.math.SingularValueDecomposition;
>
> public class testMPInverse {
>         public void test() {
>                 Matrix matrix = new DenseMatrix(new double[][] {
>                                 {1,2},
>                                 {3,4},
>                                 {5,6}
>                           });
>                 SingularValueDecomposition svd = new
> SingularValueDecomposition(matrix);
>                  Matrix u = svd.getU();
>                  Matrix v = svd.getV();
>                  Matrix s = svd.getS();
>                  System.out.println(u);
>                  System.out.println(v);
>                  System.out.println(s);
>         }
>
>         public static void main(String[] args) {
>                 testMPInverse t = new testMPInverse();
>                 t.test();
>         }
> }
>
> v is
>
> {
>   0  => {0:0.42866713354862623,1:-0.8059639085892977}
>   1  => {0:0.5663069188480352,1:-0.1123824140965937}
>   2  => {0:0.7039467041474443,1:0.5811990803961101}
> }
>
> MATLAB gives a different answer
>
> x=[1,2,3;4,5,6];
> [u s v]=svd(x);
> v
> v =
>
>    -0.4287    0.8060    0.4082
>    -0.5663    0.1124   -0.8165
>    -0.7039   -0.5812    0.4082
>
> Why the last column of v produced by Mahout SingularValueDecomposition is
> 0?
>
> Thanks for your time.
>
>