5

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

Nd4j 中是否包含任何有助于此操作的方法?

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

1  2
8 12

在这里使用 Nd4j 构建:

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);

应该

24.5  35.0
35.0  50.0

这可以使用 pandas 的 DataFramecov方法轻松完成,如下所示:

>>> pandas.DataFrame([[1, 2],[8, 12]]).cov()
      0     1
0  24.5  35.0
1  35.0  50.0

有没有办法使用 Nd4j 做到这一点?

4

1 回答 1

3

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

    /**
     * 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包中找到。

于 2018-05-24T08:31:18.773 回答