Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compute covariance matrix using Nd4j

Given a 2 dimensional matrix, I'd like to compute the corresponding covariance matrix.

Are there any methods included with Nd4j that would facilitate this operation?

For example, the covariance matrix computed from the following matrix

1  2
8 12

constructed using Nd4j here:

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

should be

24.5  35.0
35.0  50.0

This can easily be done using pandas' DataFrame's cov method like so:

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

Is there any way of doing this using Nd4j?

like image 655
pgoggijr Avatar asked Oct 16 '22 21:10

pgoggijr


1 Answers

I hope you already found a solution, for those who are facing the same problem, here is a method in ND4J that computes a covariance matrix:

    /**
     * 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

This method is found in the org.nd4j.linalg.dimensionalityreduction.PCA package.

like image 136
Be Chiller Too Avatar answered Oct 21 '22 00:10

Be Chiller Too