0

I would like to change a class sklearn.mixture.GaussianMixture to compute GMM with fixed means. In this github repository the function is stored, I assume it would be enough to change that means_ will not be computed in _m_step function within the class sklearn.mixture.GaussianMixture , but instead the values provided as initial values for means, i.e. mean_init parameter of the class would be used as fixed means. How to achieve this? I have tried changing it by heart by copy-paste the class and changing the part inside the function, but I can not overcome that there are dependencies which I can not load... Is it possible to change it from outside somehow?

So this is what I have tried, but I am not able to make it running:

import sklearn
def new_m_step(self, X, log_resp):
    sklearn.mixture.GaussianMixture._m_step(self, X, log_resp)
    self.means_ = self.means_init


my_gmm = sklearn.mixture.gaussian_mixture.GaussianMixture() 
#my_gmm._m_step =  types.MethodType(new_m_step, my_gmm) # alternative which has the same error
my_gmm._m_step = new_m_step.__get__(my_gmm, sklearn.mixture.gaussian_mixture.GaussianMixture)

and I get this error:

TypeError: 'GaussianMixture' object is not callable
pikachu
  • 690
  • 1
  • 6
  • 17
  • Does this answer your question? [Python: replacing a function within a class of a module](https://stackoverflow.com/questions/50599045/python-replacing-a-function-within-a-class-of-a-module) – Mike Scotty Nov 11 '20 at 09:46
  • I dont get how to use it in my case. I need to change part of the function within a class, not whole function. – pikachu Nov 11 '20 at 09:49
  • You've read through all the answers and examples of the linked question in just 2 minutes? Maybe read them again. Hints: ``somemodule=_gaussian_mixture, testMOD=GaussianMixture, testFunc=_m_step, alternativeFunc=`` – Mike Scotty Nov 11 '20 at 09:55
  • I have seen that question before, but it did not help me, because I do not want to build that function again, just change one part. Is there any way to do it or I just have to build it again? – pikachu Nov 11 '20 at 10:01
  • You can only replace the whole function, but you will not have to copy&paste the whole class. If you create a backup of the original function before you replace it, your new function could call that backup and then set ``self.means_ = `` after you've called the original function. That way you won't have to write a lot of code. – Mike Scotty Nov 11 '20 at 10:06
  • Thanks, that sounds good. Do you want to write it into answer? – pikachu Nov 11 '20 at 10:17
  • Feel free to answer your own question once you've got it working. You can then elaborate on the steps you've taken and show the final result to help future readers. Or - if you find your own solution is not that different from the answers in the question I've linked above - you can still decide to close your question as a duplicate. – Mike Scotty Nov 11 '20 at 10:23
  • 1
    I will write it down as an answer once it works, because for me it was not easy to understand how to use that code to solve my problem, might be thus useful also for someone else. Thanks. – pikachu Nov 11 '20 at 10:26

1 Answers1

0

Ok, so I finally solved it in this way:

from skleran.mixture import GaussianMixture
class My_GMM(GaussianMixture):
    def _m_step(self, X, log_resp):
        GaussianMixture._m_step(self, X, log_resp)
        self.means_ = self.means_init

and the original class is not overwritten within the script. If anyone has any better solution feel free to add it. If you feel the question is duplicate, vote for closing, however I did not understand from the post mentioned in comments how to add something to function inside a class, so maybe I am not alone and will leave it here.

pikachu
  • 690
  • 1
  • 6
  • 17