waikho / FeatureSaliencyHMM-Updates

0 stars 0 forks source link

AttributeError: 'GaussianHMM' object has no attribute 'k' #1

Closed Vishu92 closed 1 year ago

Vishu92 commented 1 year ago

Hi Waikho, thank you for updating the code. I am facing issue while running the code. Please find below the error message.

Output exceeds the [size limit](command:workbench.action.openSettings?[). Open the full output data [in a text editor](command:workbench.action.openLargeOutput?8cbcfdc3-2695-4e6a-8461-f9120e195028)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File c:\Users\vkapoor\Anaconda3\lib\site-packages\IPython\core\formatters.py:973, in MimeBundleFormatter.__call__(self, obj, include, exclude)
    970     method = get_real_method(obj, self.print_method)
    972     if method is not None:
--> 973         return method(include=include, exclude=exclude)
    974     return None
    975 else:

File c:\Users\vkapoor\Anaconda3\lib\site-packages\sklearn\base.py:614, in BaseEstimator._repr_mimebundle_(self, **kwargs)
    612 def _repr_mimebundle_(self, **kwargs):
    613     """Mime bundle used by jupyter kernels to display estimator"""
--> 614     output = {"text/plain": repr(self)}
    615     if get_config()["display"] == "diagram":
    616         output["text/html"] = estimator_html_repr(self)

File c:\Users\vkapoor\Anaconda3\lib\site-packages\sklearn\base.py:279, in BaseEstimator.__repr__(self, N_CHAR_MAX)
    271 # use ellipsis for sequences with a lot of elements
    272 pp = _EstimatorPrettyPrinter(
    273     compact=True,
    274     indent=1,
    275     indent_at_name=True,
    276     n_max_elements_to_show=N_MAX_ELEMENTS_TO_SHOW,
    277 )
--> 279 repr_ = pp.pformat(self)
...
--> 210     value = getattr(self, key)
    211     if deep and hasattr(value, "get_params"):
    212         deep_items = value.get_params().items()

AttributeError: 'GaussianHMM' object has no attribute 'k'
Output exceeds the [size limit](command:workbench.action.openSettings?[). Open the full output data [in a text editor](command:workbench.action.openLargeOutput?06242b7c-db83-425c-89d0-98879c7e568d)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File c:\Users\vkapoor\Anaconda3\lib\site-packages\IPython\core\formatters.py:707, in PlainTextFormatter.__call__(self, obj)
    700 stream = StringIO()
    701 printer = pretty.RepresentationPrinter(stream, self.verbose,
    702     self.max_width, self.newline,
    703     max_seq_length=self.max_seq_length,
    704     singleton_pprinters=self.singleton_printers,
    705     type_pprinters=self.type_printers,
    706     deferred_pprinters=self.deferred_printers)
--> 707 printer.pretty(obj)
    708 printer.flush()
    709 return stream.getvalue()

File c:\Users\vkapoor\Anaconda3\lib\site-packages\IPython\lib\pretty.py:410, in RepresentationPrinter.pretty(self, obj)
    407                         return meth(obj, self, cycle)
    408                 if cls is not object \
    409                         and callable(cls.__dict__.get('__repr__')):
--> 410                     return _repr_pprint(obj, self, cycle)
    412     return _default_pprint(obj, self, cycle)
    413 finally:

File c:\Users\vkapoor\Anaconda3\lib\site-packages\IPython\lib\pretty.py:778, in _repr_pprint(obj, p, cycle)
    776 """A pprint that just redirects to the normal repr function."""
    777 # Find newlines and replace them with p.break_()
...
--> 210     value = getattr(self, key)
    211     if deep and hasattr(value, "get_params"):
    212         deep_items = value.get_params().items()

AttributeError: 'GaussianHMM' object has no attribute 'k'

Below is my code for running the code.

model = fsshmm.GaussianHMM(n_components = n_components, 

        covariance_type = "diag", 

        n_iter = 10000, random_state = 42,

        k=k)   #k is the cost
model.fit(X)

where, X = an array with shape (550, 5)

Can you please assist?

waikho commented 1 year ago

Hi @Vishu92 , I notice that your error message reads AttributeError: 'GaussianHMM' object has no attribute 'k', but in line 100 of my FSHMM.py, the def init() has included a k variable:

def __init__(self, n_components=1, covariance_type='diag',
                 min_covar=1e-3,
                 startprob_prior=1.0, transmat_prior=1.0,
                 means_prior=0, means_weight=0,
                 covars_prior=1e-2, covars_weight=1,
                 algorithm="viterbi", random_state=None,
                 n_iter=10, tol=1e-2, verbose=False,
                 params="stmc", init_params="stmc",
                 implementation="log",
                 ### NEW ###
                 k=50):

Did you do import FSHMM and save it in the correct directory? And if you've imported as I suggested, did you try instantiating the model with FSHMM.GaussianHMM() instead?

Vishu92 commented 1 year ago

Hi Waikho, Thank you for replying. Does the FSHMM need to be saved with the hmmlearn library? I have saved it in the same folder as my current directory. Please correct me if this is not the case. I am pasting my code below. Please let me know if you find any issue there.

import pandas as pd
import numpy as np
import hmmlearn
import FSHMM

n_components = 2
k = 50

fsshm_model = FSHMM.GaussianHMM(n_components = n_components, 

                      covariance_type = "diag", 

                      n_iter = 10000, random_state = 42,

                      k=k)   #k is the cost

fsshm_model.fit(X = X)

where X is an array if shape (500,2)

waikho commented 1 year ago

I was using Colab. I saved FSHMM.py in the same directory as my notebook, not with the hmmlearn library. It may be just a path issue, i.e., you may need to save it in a directory where you can import without interfering with the hmmlearn library.

And for the fit statement, I just did fsshm_model.fit(X_train). Then the fitted model should be able to run the methods provided in FSHMM.py, such as fsshm_model.score(X_test), fsshm_model.monitor_.converged @Vishu92

Vishu92 commented 1 year ago

I too have saved the FSHMM.py file in the same directory as my notebook. I will check at my side and confirm once I discover the issue. Thanks. @waikho

Vishu92 commented 1 year ago

I couldn't figure out the reason for the error. I have removed the k from the initialization and put it constant. Closing the issue. Many thanks for the code. @waikho

waikho commented 1 year ago

I took further look into your error message. My read is "AttributeError: 'GaussianHMM' object has no attribute 'k'" means you're trying to get an attribute "k" from the GaussianHMM object. I'm not sure if your code is trying to get the same "k", but in line 102 of FSHMM.py, you'll see that the value of "k" in the GaussianHMM object can be returned by self.k_factor_

Good luck and feel free to discuss more! @Vishu92

Vishu92 commented 1 year ago

Hi @waikho Thanks for taking the effort to solve the issue. I am not trying to get the attribute from the object. I am initiating the module and calling model.fit() with the training data. I have spend considerable time to understand the issue but failed to do so. Happy to discuss further and solve it. @waikho