GaussianMixture initialization using component parameters - sklearn

2024/9/21 13:51:36

I want to use sklearn.mixture.GaussianMixture to store a gaussian mixture model so that I can later use it to generate samples or a value at a sample point using score_samples method. Here is an example where the components have the following weight, mean and covariances

import numpy as np
weights = np.array([0.6322941277066596, 0.3677058722933399])
mu = np.array([[0.9148052872961359, 1.9792961751316835], [-1.0917396392992502, -0.9304220945910037]])
sigma = np.array([[[2.267889129267119, 0.6553245618368836], [0.6553245618368835, 0.6571014653342457]], [[0.9516607767206848, -0.7445831474157608], [-0.7445831474157608, 1.006599716443763]]])

Then I initialised the mixture as follow

from sklearn import mixture
gmix = mixture.GaussianMixture(n_components=2, covariance_type='full')
gmix.weights_ = weights   # mixture weights (n_components,) 
gmix.means_ = mu          # mixture means (n_components, 2) 
gmix.covariances_ = sigma  # mixture cov (n_components, 2, 2) 

Finally I tried to generate a sample based on the parameters which resulted in an error:

x = gmix.sample(1000)
NotFittedError: This GaussianMixture instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.

As I understand GaussianMixture is intended to fit a sample using a mixture of Gaussian but is there a way to provide it with the final values and continue from there?

Answer

You rock, J.P.Petersen! After seeing your answer I compared the change introduced by using fit method. It seems the initial instantiation does not create all the attributes of gmix. Specifically it is missing the following attributes,

covariances_
means_
weights_
converged_
lower_bound_
n_iter_
precisions_
precisions_cholesky_

The first three are introduced when the given inputs are assigned. Among the rest, for my application the only attribute that I need is precisions_cholesky_ which is cholesky decomposition of the inverse covarinace matrices. As a minimum requirement I added it as follow,

gmix.precisions_cholesky_ = np.linalg.cholesky(np.linalg.inv(sigma)).transpose((0, 2, 1))
https://en.xdnf.cn/q/72053.html

Related Q&A

How to use geopy vicenty distance over dataframe columns?

I have a dataframe with location column which contains lat,long location as followsdeviceid location 1102ADb75 [12.9404578177, 77.5548244743]How to get the di…

Opening a postgres connection in psycopg2 causes python to crash

Im getting the following error message when I try to open up a connection to a postgres database. Perhaps its related to OpenSSL, but I cant understand the error message. Can anyone help?>>>…

Calling generated `__init__` in custom `__init__` override on dataclass

Currently I have something like this: @dataclass(frozen=True) class MyClass:a: strb: strc: strd: Dict[str, str]...which is all well and good except dicts are mutable, so I cant use my class to key anot…

Python Watchdog process existing files on startup

I have a simple Watchdog and Queue process to monitor files in a directory. Code taken from https://camcairns.github.io/python/2017/09/06/python_watchdog_jobs_queue.htmlimport time from watchdog.events…

Updating Text In Entry (Tkinter)

The piece of code below takes input from user through a form and then returns the input as multiplied by 2. What I want to do is, when a user types a number (for example 5) and presses the "Enter&…

Python prevent overflow errors while handling large floating point numbers and integers

I am working on a python program to calculate numbers in the Fibonacci sequence. Here is my code:import math def F(n):return ((1+math.sqrt(5))**n-(1-math.sqrt(5))**n)/(2**n*math.sqrt(5)) def fib(n):for…

Python selenium sending keys into textarea

Im using Python 3.4.4 to access a website (https://readability-score.com/) that has a textarea, which dynamically updates when new values are added. Im trying to input a string into that textarea box b…

how to run several executable using python?

I have an executable under linux. I have an 8 core processor. I want to run 8 different instances of the same executable with different arguments.I tried os.system("process_name args")It does…

How to retrieve only arabic texts from a string using regular expression?

I have a string which has both Arabic and English sentences. What I want is to extract Arabic Sentences only.my_string=""" What is the reason ذَلِكَ الْكِتَابُ لَا رَ…

Formatted output in OpenOffice/Microsoft Word with Python

I am working on a project (in Python) that needs formatted, editable output. Since the end-user isnt going to be technically proficient, the output needs to be in a word processor editable format. The …