1

I am trying to implement the code for GMM manually following the guide at this link

for another dataset with text messages.

I am getting a LinAlgError because of singular matrix when calculating probability density function. Cannot find out where I am going wrong.

mu = initial_means
pi=initial_pi
sigma=initial_cov
N = X.shape[0] 
gamma = np.zeros((N,C))
const_c = np.zeros(C)

for c in range(C):
            # Posterior Distribution using Bayes Rule

    zz= mvn.pdf(X,mu[c,:],sigma[c,:])
    print(zz)
    gamma[:,c] = pi[c] * zz
        # normalize across columns to make a valid probability
    gamma_norm = np.sum(gamma, axis=1)[:,np.newaxis]
    gamma /= gamma_norm 
  • 3
    Your matrix does not have an inverse. Either the values you've entered are incorrect OR you need a solver like SVD that can be robust in the face of a singular matrix. – duffymo Sep 26 '19 at 17:16
  • 1
    Hi Divyasshree, what are the values of `initial_means` and `initial_cov`? It looks like you are trying to use a singular `sigma`, when it needs to be non-singular. – MattG Sep 26 '19 at 17:16
  • My initial_means is a ndarray that has rows that look like this:[4.74394705e+01 6.73623412e-01 6.12863678e-01 ... 6.81845570e-03 5.06020993e-06 1.25836846e-02] and it looks like my sigma is not invertible.I just checked – Divyasshree Sep 26 '19 at 17:30
  • probably pseudoinverse would solve the problem for me. scipy.linalg.pinv – Divyasshree Sep 26 '19 at 17:47

0 Answers0