I am following this sample to do density estimation for the Bayesian Gaussian mixture model below:
JavaScript
x
2
1
bgmm = BayesianGaussianMixture(n_components=10, random_state=7, max_iter=5000).fit(data)
2
in which data
(as a dataframe) includes 20 columns of numeric data.
I can simply plot the model for two features of bgmm
by
JavaScript
1
15
15
1
x = np.linspace(-20.0, 30.0)
2
y = np.linspace(-20.0, 40.0)
3
X, Y = np.meshgrid(x, y)
4
XX = np.array([X.ravel(), Y.ravel()]).T
5
Z = -bgmm.score_samples(XX)
6
Z = Z.reshape(X.shape)
7
8
CS = plt.contour(
9
X, Y, Z, norm=LogNorm(vmin=1.0, vmax=1000.0), levels=np.logspace(0, 3, 10)
10
)
11
CB = plt.colorbar(CS, shrink=0.8, extend="both")
12
plt.scatter(data[:, 0], data[:, 1], 0.8)
13
14
plt.show()
15
But, how can I plot all the clusters in the form of density contours?
Advertisement
Answer
I believe you need to get your data into one big two-column array before fitting, so define a new X_train
that combines all ten pairs of columns into one big pair of columns.
First, convert data
into an array:
JavaScript
1
2
1
data_array = data.to_numpy()
2
And then reshape into two columns:
JavaScript
1
2
1
X_train = np.reshape(data_array, (10*data_array.shape[0], 2))
2
and then call the mixture.fit
method with that instead of data
. Then just continue following the sample, using X_train
as they do (and of course use bgmm
instead of clf
).