5

I have a matrix which fails the singular test in which I am calculating for naive bayes classifier. I am handling the ln(det(sigma)) portion of the equation.

if np.linalg.cond(covarianceMatrix) < 1/sys.float_info.epsilon:
    return np.log(np.linalg.det(covarianceMatrix))
else:
    return a pseudo determinant

When the covariance matrix is singular, I must find the pseudo determinant. How can I do that?

kenny
  • 75
  • 1
  • 6

2 Answers2

4
  1. First compute the eigenvalues of your matrix

    eig_values = np.linalg.eig(covarianceMatrix)

  1. Then compute the product of the non-zero eigenvalues (this equals the pseudo-determinant value of the matrix),

    pseudo_determinent = np.product(eig_values[eig_values > 1e-12])

IssamLaradji
  • 6,637
  • 8
  • 43
  • 68
  • I am trying to implement this solution but I faced the error: '>' not supported between instances of 'tuple' and 'float'. Can I ask if you know how to solve it? it looks like I need to loop through the elements. – DRA Nov 08 '22 at 05:20
1

Could you use numpy's pinv to calculate the pseudo inverse and then use that to calculate the determinant? http://www.sosmath.com/matrix/inverse/inverse.html

TheOriginalAlex
  • 148
  • 1
  • 5