给定一个二维矩阵,我想计算相应的协方差矩阵。
在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使用熊猫的DataFrame的cov方法可以很容易地做到如下所示:
>>> pandas.DataFrame([[1, 2],[8, 12]]).cov()
0 1
0 24.5 35.0
1 35.0 50.0是否有任何使用Nd4j的方法来做到这一点?
发布于 2018-05-24 08:31:18
我希望您已经找到了一个解决方案,对于那些面临同样问题的人,下面是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)此方法可在org.nd4j.linalg.dimensionalityreduction.PCA包中找到。
https://stackoverflow.com/questions/49323200
复制相似问题