使用 Nd4j 计算协方差矩阵

Compute covariance matrix using Nd4j

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

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' DataFrame 的 cov 方法轻松完成,如下所示:

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

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

我希望你已经找到了解决方案,对于那些面临同样问题的人,这里是 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 source

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