def test(file, max_n_components, n_classes): print('GaussianMixture for set: ' + file) dataset = utils.dataset_reader(file) X, y = utils.data_process(dataset) list_sse = [] list_nmi = [] for n_components in range(1, max_n_components + 1): gmm = GaussianMixture(n_components=n_components) gmm.fit(X) y_hat = gmm.predict(X) sse = utils.sum_of_squared_errors(X, y_hat, gmm.means_) nmi = utils.normalized_mutual_information(y, n_classes, y_hat, n_components) print('{0:2d} components, SSE: {1:.2f}, NMI: {2:.4f}'.format( n_components, sse, nmi)) # print('iterations: ', gmm.n_iter_) # print(gmm.means_, gmm.covariances_, gmm.weights_) # print(gmm.lower_bound_) list_sse.append(sse) list_nmi.append(nmi) utils.plot_measure_vs_k('SSE', list_sse, range(1, max_n_components + 1)) utils.plot_measure_vs_k('NMI', list_nmi, range(1, max_n_components + 1))
def testPredictClasses(self): """ Assert that torch.FloatTensor is handled correctly. """ x = torch.randn(4, 2) n_components = np.random.randint(1, 100) model = GaussianMixture(n_components, x.size(1)) model.fit(x) y = model.predict(x) # check that dimensionality of class memberships is (n) self.assertEqual(torch.Tensor(x.size(0)).size(), y.size())
def testPredictProbabilities(self): """ Assert that torch.FloatTensor is handled correctly when returning class probabilities. """ x = torch.randn(4, 2) n_components = np.random.randint(1, 100) model = GaussianMixture(n_components, x.size(1)) model.fit(x) # check that y_p has dimensions (n, k) y_p = model.predict(x, probs=True) self.assertEqual( torch.Tensor(x.size(0), n_components).size(), y_p.size())
def main(): n, d = 300, 2 # generate some data points .. data = torch.Tensor(n, d).normal_() # .. and shift them around to non-standard Gaussians data[:n//2] -= 1 data[:n//2] *= sqrt(3) data[n//2:] += 1 data[n//2:] *= sqrt(2) # Next, the Gaussian mixture is instantiated and .. n_components = 2 model = GaussianMixture(n_components, d) model.fit(data) # .. used to predict the data points as they where shifted y = model.predict(data) plot(data, y)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat Apr 14 19:53:43 2018 @author: Garrett """ from kmeans import KMeans #from sklearn.cluster import KMeans from gmm import GaussianMixture import numpy as np X = np.array([[2, 2], [3, 4], [1, 0], [101, 2], [102, 4], [100, 0]]) kmeans = KMeans(n_clusters=2).fit(X) #print(kmeans.labels_) #print(kmeans.predict(np.array([[0, 0], [4, 4]]))) #print(kmeans.cluster_centers_) gmm = GaussianMixture(n_components=2).fit(X) print('gmm predict ', gmm.predict(X)) #print(gmm.predict(np.array([[0, 0], [4, 4]]))) print('gmm.means_ ', gmm.means_) print('gmm.covariances_ ', gmm.covariances_) print('gmm.n_iter', gmm.n_iter_)