How to make a histogram using sklearn with 1D Gaussian mixture?


Théré Hernandez

I want to do a histogram with mixed 1D Gaussian images.

enter image description here

Thanks Meng for the photos.

My histogram looks like this:

enter image description here

I have a file with a lot of data (4,000,000 numbers) in columns:

1.727182
1.645300
1.619943
1.709263
1.614427
1.522313

I'm using the following script to make modifications that Judge Meng and Rhodes do not:

from matplotlib import rc
from sklearn import mixture
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
import matplotlib.ticker as tkr
import scipy.stats as stats

x = open("prueba.dat").read().splitlines()

f = np.ravel(x).astype(np.float)
f=f.reshape(-1,1)
g = mixture.GaussianMixture(n_components=3,covariance_type='full')
g.fit(f)
weights = g.weights_
means = g.means_
covars = g.covariances_

plt.hist(f, bins=100, histtype='bar', density=True, ec='red', alpha=0.5)
plt.plot(f,weights[0]*stats.norm.pdf(f,means[0],np.sqrt(covars[0])), c='red')
plt.rcParams['agg.path.chunksize'] = 10000

plt.grid()
plt.show()

When I run the script, I have the following:

enter image description here

So I don't know how to determine the start and end point of all the Gaussians that must be present. I am new to python and I am confused about the way to use modules. Please, can you help me guide me on how to do this plot?

thank you very much

eng

It's all about remodeling. First, you need to reshape f. For pdf, reshape before using stats.norm.pdf. Again, sort and reshape before drawing.

from matplotlib import rc
from sklearn import mixture
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
import matplotlib.ticker as tkr
import scipy.stats as stats

# x = open("prueba.dat").read().splitlines()

# create the data
x = np.concatenate((np.random.normal(5, 5, 1000),np.random.normal(10, 2, 1000)))

f = np.ravel(x).astype(np.float)
f=f.reshape(-1,1)
g = mixture.GaussianMixture(n_components=3,covariance_type='full')
g.fit(f)
weights = g.weights_
means = g.means_
covars = g.covariances_

plt.hist(f, bins=100, histtype='bar', density=True, ec='red', alpha=0.5)

f_axis = f.copy().ravel()
f_axis.sort()
plt.plot(f_axis,weights[0]*stats.norm.pdf(f_axis,means[0],np.sqrt(covars[0])).ravel(), c='red')

plt.rcParams['agg.path.chunksize'] = 10000

plt.grid()
plt.show()

enter image description here

Related


How to make a histogram using sklearn with 1D Gaussian mixture?

Théré Hernandez I want to do a histogram with mixed 1D Gaussian images. Thanks Meng for the photos. My histogram looks like this: I have a file with a lot of data (4,000,000 numbers) in columns: 1.727182 1.645300 1.619943 1.709263 1.614427 1.522313 I'm using

Gaussian Mixture Model for Image Histogram

Dotted glass I am trying to do automatic image segmentation of different regions of a 2D MR image based on pixel intensity values. The first step is to implement a Gaussian mixture model on the histogram of the image. I need to plot the resulting Gaussian obta

Gaussian Mixture Model for Image Histogram

Dotted glass I am trying to do automatic image segmentation of different regions of a 2D MR image based on pixel intensity values. The first step is to implement a Gaussian mixture model on the histogram of the image. I need to plot the resulting Gaussian obta

Gaussian Mixture Model for Image Histogram

Dotted glass I am trying to do automatic image segmentation of different regions of a 2D MR image based on pixel intensity values. The first step is to implement a Gaussian mixture model on the histogram of the image. I need to plot the resulting Gaussian obta

Gaussian Mixture Model for Image Histogram

Dotted glass I am trying to do automatic image segmentation of different regions of a 2D MR image based on pixel intensity values. The first step is to implement a Gaussian mixture model on the histogram of the image. I need to plot the resulting Gaussian obta

Gaussian Mixture Model for Image Histogram

Dotted glass I am trying to do automatic image segmentation of different regions of a 2D MR image based on pixel intensity values. The first step is to implement a Gaussian mixture model on the histogram of the image. I need to plot the resulting Gaussian obta

Get PDF from Gaussian Mixture Model in sklearn

learner I have fitted a Gaussian Mixture Model (GMM) to the data series I have. Using GMM, I am trying to get the probability of another vector, element-wise. Matlab achieves this with the following lines of code. a = reshape(0:1:15, 14, 1); gm = fitgmdist(a,

Problems with sklearn.mixture.GMM (Gaussian Mixture Model)

Gabriele Pompa I'm fairly new to scikit-lear and GMM in general...I have some questions about the fit quality of Gaussian mixture models in python (scikit-learn). I have an array of data that you can find in DATA HERE to match a GMM of n=2 components . As a be

How to plot a Gaussian on a histogram

Noodle How can I plot a Gaussian on the histogram generated by my code? Here is the code below. It works perfectly and generates histograms, but since I'm just starting out with pyplot, I'm having trouble adding a gaussian curve. I'm still struggling with the

How to plot a Gaussian on a histogram

Noodle How can I plot a Gaussian on the histogram generated by my code? Here is the code below. It works perfectly and generates histograms, but since I'm just starting out with pyplot, I'm having trouble adding a gaussian curve. I'm still struggling with the

How can I make the Gaussian normal match the histogram?

Sam Sloan I was wondering if there is a good way to match Gaussian normals in the form of numpy arrays with a histogram np.histogram(array, bins). How can I draw such a curve on the same graph and adjust it in height and width according to the histogram? Andre

How can I make the Gaussian normal match the histogram?

Sam Sloan I was wondering if there is a good way to match Gaussian normals in the form of numpy arrays with a histogram np.histogram(array, bins). How can I draw such a curve on the same graph and adjust it in height and width according to the histogram? Andre

How can I make the Gaussian normal match the histogram?

Sam Sloan I was wondering if there is a good way to match Gaussian normals in the form of numpy arrays with a histogram np.histogram(array, bins). How can I draw such a curve on the same graph and adjust it in height and width according to the histogram? Andre

How can I make the Gaussian normal match the histogram?

Sam Sloan I was wondering if there is a good way to match Gaussian normals in the form of numpy arrays with a histogram np.histogram(array, bins). How can I draw such a curve on the same graph and adjust it in height and width according to the histogram? Andre

How can I make the Gaussian normal match the histogram?

Sam Sloan I was wondering if there is a good way to match Gaussian normals in the form of numpy arrays with a histogram np.histogram(array, bins). How can I draw such a curve on the same graph and adjust it in height and width according to the histogram? Andre

Clustering images using a Gaussian mixture model

ninja I want to cluster a binary image using GMM (Gaussian Mixture Model) and also want to plot the cluster centroids on the binary image itself. I used this as a reference : http://in.mathworks.com/help/stats/gaussian-mixture-models.html Here is my initial co

Clustering images using a Gaussian mixture model

ninja I want to cluster a binary image using GMM (Gaussian Mixture Model) and also want to plot the cluster centroids on the binary image itself. I used this as a reference : http://in.mathworks.com/help/stats/gaussian-mixture-models.html Here is my initial co

Implementing Gaussian Mixture MLE using optim() in R

User 2007598 I am trying to implement MLE for mixture of Gaussians in R using optim() using R's native dataset (Geyser from MASS). My code is as follows. The problem is that optim works fine, but returns the original parameters I passed to it, and also says it

Clustering images using a Gaussian mixture model

ninja I want to cluster a binary image using GMM (Gaussian Mixture Model) and also want to plot the cluster centroids on the binary image itself. I used this as a reference : http://in.mathworks.com/help/stats/gaussian-mixture-models.html Here is my initial co

Implementing Gaussian Mixture MLE using optim() in R

User 2007598 I am trying to implement MLE for mixture of Gaussians in R using optim() using R's native dataset (Geyser from MASS). My code is as follows. The problem is that optim works fine, but returns the original parameters I passed to it, and also says it

Implementing Gaussian Mixture MLE using optim() in R

User 2007598 I am trying to implement MLE for mixture of Gaussians in R using optim() using R's native dataset (Geyser from MASS). My code is as follows. The problem is that optim works fine, but returns the original parameters I passed to it, and also says it

Motion is recorded using only Gaussian mixture models

Genevieve I am using this example on a Gaussian mixture model . I have a video showing a car in motion, but it's on a less busy street. A few cars flew by every now and then, but for the most part, there was no movement in the background. It gets very tedious