-1

I would like to know, for this mixture of Gaussian distributions generated by the data we give ourselves, how do we figure out which component is more likely to belong to a new sample we are given?

I learned that Matlab seems to have functions that can be calculated directly, is there any in python? I haven't found an answer so far.

import matplotlib.pyplot as plt
import numpy as np
import random

# Bivariate example
dim = 2

# Settings
n = 500
NumberOfMixtures = 3

# Mixture weights (non-negative, sum to 1)
w = [0.5, 0.25, 0.25]

# Mean vectors and covariance matrices
MeanVectors = [ [0,0], [-5,5], [5,5] ]
CovarianceMatrices = [ [[1, 0], [0, 1]], [[1, .8], [.8, 1]], [[1, -.8], [-.8, 1]] ]

# Initialize arrays
samples = np.empty( (n,dim) ); samples[:] = np.NaN
componentlist = np.empty( (n,1) ); componentlist[:] = np.NaN

# Generate samples
for iter in range(n):
    # Get random number to select the mixture component with probability according to mixture weights
    DrawComponent = random.choices(range(NumberOfMixtures), weights=w, cum_weights=None, k=1)[0]
    # Draw sample from selected mixture component
    DrawSample = np.random.multivariate_normal(MeanVectors[DrawComponent], CovarianceMatrices[DrawComponent], 1)
    # Store results
    componentlist[iter] = DrawComponent
    samples[iter, :] = DrawSample

# Report fractions
print('Fraction of mixture component 0:', np.sum(componentlist==0)/n)
print('Fraction of mixture component 1:',np.sum(componentlist==1)/n)
print('Fraction of mixture component 2:',np.sum(componentlist==2)/n)

# Visualize result
plt.plot(samples[:, 0], samples[:, 1], '.', alpha=0.5)
plt.grid()
plt.show()
Leshui
  • 19
  • 4
  • Welcome to [Stack Overflow.](https://stackoverflow.com/ "Stack Overflow") This is not a code-writing or tutoring service. We help solve specific, technical problems, not open-ended requests for code or advice. Please edit your question to show what you have tried so far, and what specific problem you need help with. See the [How To Ask a Good Question](https://stackoverflow.com/help/how-to-ask "How To Ask a Good Question") page for details on how to best help us help you. – itprorh66 Oct 26 '22 at 14:12
  • ok,thanks,I have found a solution and am about to modify the problem. – Leshui Oct 26 '22 at 14:21

1 Answers1

-1

The problem has been sovled, the answer can refer in the link:

https://stackoverflow.com/questions/42971126/multivariate-gaussian-distribution-scipy
Leshui
  • 19
  • 4