在java中使用Jama进行LSA的问题

发布于 2025-01-01 03:48:50 字数 1934 浏览 1 评论 0原文

我正在使用 jama 包来查找 LSA 。我被告知要减少维数,因此在本例中我将其减少到 3,并重建了矩阵。但生成的矩阵与我给系统的矩阵有很大不同,

这里是代码

    a = new Matrix(termdoc); // get the matrix here 
    a = a.transpose() ; // since the matrix is in the form of doc * terms i transpose it 
    SingularValueDecomposition sv =new SingularValueDecomposition(a) ; 
    u = sv.getU();
    v = sv.getV(); 
    s = sv.getS();
    uarray = u.getArray();
    sarray = s.getArray(); 
    varray = v.getArray(); 
    sarray_mod = new double[3][3]; //reducing dimension 
    uarray_mod = new double[uarray.length][3];
    varray_mod = new double[3][varray.length]; 
    move(sarray,3,3,sarray_mod); // my method to move the contents 
    move(uarray,uarray.length,3,uarray_mod); 
    move(varray,3,varray.length,varray_mod); 
    e = new Matrix(uarray_mod); 
    f = new Matrix(sarray_mod);
    g = new Matrix(varray_mod);
    Matrix temp  =e.times(f); 
    result = temp.times(g); 
    result = result.transpose(); 
    results = result.getArray() ; 
    System.out.println(" The array after svd : \n"); 
    print(results);// my method to print the array 

 private static void move(double[][] sarray2, int r, int c,
        double[][] sarrayMod) {
    // TODO Auto-generated method stub 
    for(int i=0;i<r;i++)
        for(int t=0;t<c;t++)
            sarrayMod[i][t]=sarray2[i][t];

}

A 示例输出,只有 3 个文件,其中两个文件相似

0.25 0 0 0 0 0 0 0 0.25 0 0.25 0.25 0 

0 0.083 0.083 0.083 0.083 0.083 0.083 0.083 0 0.083 0.083 0.167 0.083 

0.25 0 0 0 0 0 0 0 0.25 0 0.25 0.25 0 

svd 之后的数组:

0.225 0.029 0.029 0.029 0.029 0.029 0.029 0.029 0.225 0.029 0.253 0.282 0.029 

-0.121 0.077 0.077 0.077 0.077 0.077 0.077 0.077 -0.121 0.077 -0.044 0.033 0.077 

0.245 0.012 0.012 0.012 0.012 0.012 0.012 0.012 0.245 0.012 0.257 0.269 0.012 

i am making using of the jama package for finding the lsa . I was told to reduce the dimensionality and hence i have reduced it to 3 in this case and i reconstruct the matrix . But the resultant matrix is very different from the one i had given to the system

heres the code

    a = new Matrix(termdoc); // get the matrix here 
    a = a.transpose() ; // since the matrix is in the form of doc * terms i transpose it 
    SingularValueDecomposition sv =new SingularValueDecomposition(a) ; 
    u = sv.getU();
    v = sv.getV(); 
    s = sv.getS();
    uarray = u.getArray();
    sarray = s.getArray(); 
    varray = v.getArray(); 
    sarray_mod = new double[3][3]; //reducing dimension 
    uarray_mod = new double[uarray.length][3];
    varray_mod = new double[3][varray.length]; 
    move(sarray,3,3,sarray_mod); // my method to move the contents 
    move(uarray,uarray.length,3,uarray_mod); 
    move(varray,3,varray.length,varray_mod); 
    e = new Matrix(uarray_mod); 
    f = new Matrix(sarray_mod);
    g = new Matrix(varray_mod);
    Matrix temp  =e.times(f); 
    result = temp.times(g); 
    result = result.transpose(); 
    results = result.getArray() ; 
    System.out.println(" The array after svd : \n"); 
    print(results);// my method to print the array 

 private static void move(double[][] sarray2, int r, int c,
        double[][] sarrayMod) {
    // TODO Auto-generated method stub 
    for(int i=0;i<r;i++)
        for(int t=0;t<c;t++)
            sarrayMod[i][t]=sarray2[i][t];

}

A sample output with just 3 files of which two are the similar

0.25 0 0 0 0 0 0 0 0.25 0 0.25 0.25 0 

0 0.083 0.083 0.083 0.083 0.083 0.083 0.083 0 0.083 0.083 0.167 0.083 

0.25 0 0 0 0 0 0 0 0.25 0 0.25 0.25 0 

The array after svd :

0.225 0.029 0.029 0.029 0.029 0.029 0.029 0.029 0.225 0.029 0.253 0.282 0.029 

-0.121 0.077 0.077 0.077 0.077 0.077 0.077 0.077 -0.121 0.077 -0.044 0.033 0.077 

0.245 0.012 0.012 0.012 0.012 0.012 0.012 0.012 0.245 0.012 0.257 0.269 0.012 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝眸 2025-01-08 03:48:50

浏览示例 此处

在示例中,我们从 U,S 和 V 中获取前 2 列。然后我们将它们相乘。它不会给你相同的矩阵,但会提高相似性的性能。

如果你看过这个例子,你会发现用户和人类之间的相似度是-ve。但是在我们执行 SVD 后,相似度增加到接近 1 的 +ve 值。

我认为你移动的方式是正确的。只需浏览一次示例即可。

Go through the example Here

In the example, we take first 2 columns from U,S and V . And then we multiply them. It wont result to give you the same matrix but will enhance the performance in similarity.

If you have gone through the example, you will find that the similarity between user and human was in -ve. But after we performed SVD , similarity increased to a +ve value close to 1.

I think the way you are moving is correct. Just go through the example once.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文