首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Nd4j计算协方差矩阵

用Nd4j计算协方差矩阵
EN

Stack Overflow用户
提问于 2018-03-16 14:31:59
回答 1查看 388关注 0票数 5

给定一个二维矩阵,我想计算相应的协方差矩阵。

在Nd4j中是否包含了任何方法来促进这一操作?

例如,从以下矩阵计算的协方差矩阵

代码语言:javascript
复制
1  2
8 12

在这里使用Nd4j构造:

代码语言:javascript
复制
INDArray array1 = Nd4j.zeros(2, 2);  
array1.putScalar(0, 0, 1);
array1.putScalar(0, 1, 2);
array1.putScalar(1, 0, 8);
array1.putScalar(1, 1, 12);

应该是

代码语言:javascript
复制
24.5  35.0
35.0  50.0

使用熊猫的DataFrame的cov方法可以很容易地做到如下所示:

代码语言:javascript
复制
>>> pandas.DataFrame([[1, 2],[8, 12]]).cov()
      0     1
0  24.5  35.0
1  35.0  50.0

是否有任何使用Nd4j的方法来做到这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-24 08:31:18

我希望您已经找到了一个解决方案,对于那些面临同样问题的人,下面是ND4J中计算协方差矩阵的一种方法:

代码语言:javascript
复制
    /**
     * Returns the covariance matrix of a data set of many records, each with N features.
     * It also returns the average values, which are usually going to be important since in this
     * version, all modes are centered around the mean.  It's a matrix that has elements that are
     * expressed as average dx_i * dx_j (used in procedure) or average x_i * x_j - average x_i * average x_j
     *
     * @param in A matrix of vectors of fixed length N (N features) on each row
     * @return INDArray[2], an N x N covariance matrix is element 0, and the average values is element 1.
     */
public static INDArray[] covarianceMatrix(INDArray in)

GitHub源

此方法可在org.nd4j.linalg.dimensionalityreduction.PCA包中找到。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49323200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档