简体   繁体   中英

Python: How to calculate the mean square error of a distribution?

I've fit the data with GMM with data, I want to calculate the mean square error of the model, how can I do it?

Here's the code to generate the data

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
from sklearn import mixture
import matplotlib as mpl

from matplotlib.patches import Ellipse
%matplotlib inline

n_samples = 300

# generate random sample, two components
np.random.seed(0)
shifted_gaussian = np.random.randn(n_samples, 2) + np.array([20, 5])
sample= shifted_gaussian 

# fit a Gaussian Mixture Model with two components
clf = mixture.GMM(n_components=2, covariance_type='full')
clf.fit(sample)

# Then how can I calculate the Mean square error of the fitted model?

In my thinking, I can first generate the kdensity function, and for every observation in sample , caluclate the kdensitity(x,y)-clf.score(x,y) . But I'm not sure if this is the right approach.

Look the documentation here:

http://scikit-learn.org/stable/modules/generated/sklearn.mixture.GaussianMixture.html

I think you can try:

covariances = clf.fit(sample).covariances_ MSE = np.diag(covariances)

this will give you the covariance matrix of the fit parameters. Then the diagonal values of the matrix will give you the mean square errore of the fitted model parameteres.

Or it could be work:

clf.fit(sample).get_params(deep=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM