给定2维矩阵,我想计算相应的协方差矩阵。
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做到这一点?
答案 0 :(得分:1)
我希望你已经找到了解决办法,对于那些面临同样问题的人来说,这是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)
包中找到。