pythonscipyhmmlearn

How to solve ValueError: Expected 2D array, got scalar array instead error in python?


I have a nX2 dimension array and I have some HMM models defined. Now, for each of the HMM models present, I am trying to calculate the log-likelihood value for each of the values present in the nx2 array. For this, I am using the score function in hmmlearn package

LINK: https://hmmlearn.readthedocs.io/en/latest/api.html#hmmlearn.base._BaseHMM.score

For reference, these are the codes I have written:

Defining the one of the HMM models:

HMMmodel = hmm.GaussianHMM(n_components=3, covariance_type="full", n_iter=10)
HMMmodel.fit(Sdata2)

HMMpredict = HMMmodel.predict(Sdata2)

HMMmodel.transmat_

Then, the dataset used with the Score function:

[[-1.72914138 -1.63633714]
 [-1.72914138 -1.63633714]
 [-1.69620469 -1.63633714]
 ...
 [-1.72226929 -1.63633714]
 [-1.71539655 -1.63633714]
 [-1.72914138 -1.63633714]]

The code for finding the score:

score1 = list()
score2 = list()
score3 = list()
score4 = list()

for x in np.nditer(Sdata3):
    score1.append(HMMmodel.score(x))
    score2.append(HMMmodel2.score(x))
    score3.append(HMMmodel3.score(x))
    score4.append(HMMmodel4.score(x))

It is at this point the error is encountered:

ValueError: Expected 2D array, got scalar array instead:
array=-1.7291413774395343.

I have read some similar questions on this site and tried reshaping my array with (-1,1) and (1,-1) but it produces the same error.

Any techniques to solve the error will be appreciated. Thanks!


Solution

  • Okay, I think I have solved this.

    What I did was I took one [1x2] sequence from the dataset and then used the reshape function to convert it into a [2x1] sequence. Upon this conversion, the score function accepted it and the program ran smoothly.

    Here is the modified code:

    score1 = list()
    score2 = list()
    score3 = list()
    score4 = list()
    
    for x in Sdata3:
        y = np.reshape(x, (2,1))
        score1.append(HMMmodel.score(y))
        score2.append(HMMmodel2.score(y))
        score3.append(HMMmodel3.score(y))
        score4.append(HMMmodel4.score(y))
    

    I hope this helps anyone facing a similar problem.