import numpy as np import matplotlib.pyplot as plt from GMM import GMM if __name__ == '__main__': group_a = np.random.normal(loc=(20.00, 14.00), scale=(4.0, 4.0), size=(1000, 2)) group_b = np.random.normal(loc=(15.00, 8.00), scale=(2.0, 2.0), size=(1000, 2)) group_c = np.random.normal(loc=(30.00, 40.00), scale=(2.0, 2.0), size=(1000, 2)) group_d = np.random.normal(loc=(25.00, 32.00), scale=(7.0, 7.0), size=(1000, 2)) data = np.concatenate((group_a, group_b, group_c, group_d)) g = GMM(n_components=4) eval_train = g.train(data) for c in g.components: print '*****' print c.mean print c.cov plt.plot(eval_train) plt.show()
chapter9 EM algorithm for Gaussian Misture Model Using iris dataset for clustering """ import DatasetUtil as DS from HTMLTable import HTMLTable import re from GMM import GMM if __name__ == "__main__": print("\t============ Chap9 EM for GMM ============") ds = DS.DATAUtil() x_train, y_train = ds.load(True, r".\dataset.dat") model = GMM() model.train(x_train) y_pred = model.predict(x_train) y_train = ds.y_int2str(y_train) table = HTMLTable(caption='Iris Data Cluster') table.append_header_rows(( ('No.', 'A1', 'A2', 'A3', 'A4', 'Classification', ''), ('', '', '', '', '', 'Label-C', 'Predict-C'), )) table[0][0].attr.rowspan = 2 table[0][1].attr.rowspan = 2 table[0][2].attr.rowspan = 2 table[0][3].attr.rowspan = 2 table[0][4].attr.rowspan = 2 table[0][5].attr.colspan = 2
marker='o') ax.scatter(real_data[0][(N[0] + N[1]):, 0], real_data[0][(N[0] + N[1]):, 1], real_data[0][(N[0] + N[1]):, 2], c='orange', marker='o') ax.text2D(0.05, 0.95, "Original Clusters", transform=ax.transAxes) ax.set_xlabel('x') ax.set_ylabel('y') ax.set_zlabel('z') plt.show() #Fitting model GMM_model = GMM(dim=3, num_clusters=3) GMM_model.train(real_data[0], 10**(-10)) #Showing the predicted clusters assignments = (real_data[0], np.argmax(np.apply_along_axis(GMM_model.resp, 1, real_data[0]), axis=1)) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(assignments[0][np.where(assignments[1] == 0), 0], assignments[0][np.where(assignments[1] == 0), 1], assignments[0][np.where(assignments[1] == 0), 2], c='green', marker='o') ax.scatter(assignments[0][np.where(assignments[1] == 1), 0],