From 089763b64c5a8b41954e40aa9116e9843c9551c6 Mon Sep 17 00:00:00 2001 From: Ahmad Alobaid Date: Fri, 25 May 2018 16:01:52 +0200 Subject: [PATCH] visualization of points --- fuzzycmeans/fuzzy_clustering.py | 6 ++-- fuzzycmeans/visualization.py | 56 +++++++++++++++++++-------------- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/fuzzycmeans/fuzzy_clustering.py b/fuzzycmeans/fuzzy_clustering.py index fe56f6d..f1f6792 100644 --- a/fuzzycmeans/fuzzy_clustering.py +++ b/fuzzycmeans/fuzzy_clustering.py @@ -234,14 +234,16 @@ def fit(self, X, y=None, hard=True): """ y = np.array(y) X = np.array(X) - if y is not None: + if y: if hard: self.set_membership_from_hard_cluster(X, y) if self.cluster_centers_ is None: do_compute_cluster_centers = True else: do_compute_cluster_centers = False - + if self.u is None: + num_of_points = X.shape[0] + self.init_membership_random(num_of_points) #self.init_membership(X.shape[0]) list_of_centers = [] membership_history = [] diff --git a/fuzzycmeans/visualization.py b/fuzzycmeans/visualization.py index c9b1506..384f2f1 100644 --- a/fuzzycmeans/visualization.py +++ b/fuzzycmeans/visualization.py @@ -6,9 +6,12 @@ from fuzzy_clustering import FCM import logging + def color_gen(): for c in itertools.cycle(pallette[10]): yield c + + colors = color_gen() @@ -27,20 +30,20 @@ def mtext(p, x, y, text): N = 10 - mscatter(p, random(N)+2, random(N)+1, "circle") - mscatter(p, random(N)+4, random(N)+1, "square") - mscatter(p, random(N)+6, random(N)+1, "triangle") - mscatter(p, random(N)+8, random(N)+1, "asterisk") + mscatter(p, random(N) + 2, random(N) + 1, "circle") + mscatter(p, random(N) + 4, random(N) + 1, "square") + mscatter(p, random(N) + 6, random(N) + 1, "triangle") + mscatter(p, random(N) + 8, random(N) + 1, "asterisk") - mscatter(p, random(N)+2, random(N)+4, "circle_x") - mscatter(p, random(N)+4, random(N)+4, "square_x") - mscatter(p, random(N)+6, random(N)+4, "inverted_triangle") - mscatter(p, random(N)+8, random(N)+4, "x") + mscatter(p, random(N) + 2, random(N) + 4, "circle_x") + mscatter(p, random(N) + 4, random(N) + 4, "square_x") + mscatter(p, random(N) + 6, random(N) + 4, "inverted_triangle") + mscatter(p, random(N) + 8, random(N) + 4, "x") - mscatter(p, random(N)+2, random(N)+7, "circle_cross") - mscatter(p, random(N)+4, random(N)+7, "square_cross") - mscatter(p, random(N)+6, random(N)+7, "diamond") - mscatter(p, random(N)+8, random(N)+7, "cross") + mscatter(p, random(N) + 2, random(N) + 7, "circle_cross") + mscatter(p, random(N) + 4, random(N) + 7, "square_cross") + mscatter(p, random(N) + 6, random(N) + 7, "diamond") + mscatter(p, random(N) + 8, random(N) + 7, "cross") mtext(p, 2.5, 0.5, "circle / o") mtext(p, 4.5, 0.5, "square") @@ -62,17 +65,24 @@ def mtext(p, x, y, text): show(p) # open a browser -def draw_model_2d(model, show=True): +def draw_model_2d(model, data=None, membership=None, show_figure=True): title = "draw FCM model" fig = figure(title=title, toolbar_location=None) fig.grid.grid_line_color = None fig.background_fill_color = "#eeeeee" output_p = None - for cc, color in zip(model.cluster_centers_, color_gen()): - print cc - fig = draw_points_2d(np.array([cc]), fig=fig, title=title, marker="circle", size=15, - line_color="navy", fill_color="orange", alpha=0.5) - if show: + for clus, cc_color in enumerate(zip(model.cluster_centers_, color_gen())): + cc, color = cc_color + fig = draw_points_2d(np.array([cc]), fig=fig, title=title, marker="diamond", size=15, + line_color="navy", fill_color=color, alpha=1.0) + if data is not None and membership is not None: + for idx, data_point in enumerate(data): + # print idx + # print clus + print membership[idx][clus] + fig = draw_points_2d(np.array([data_point]), fig=fig, title=title, marker="circle", size=10, + line_color="navy", fill_color=color, alpha=membership[idx][clus]) + if show_figure: show(fig) return fig @@ -84,20 +94,20 @@ def draw_points_2d(points, fig=None, title="figure 123", **kwargs): fig.background_fill_color = "#eeeeee" x, y = points.T fig.scatter(x, y, **kwargs) - output_file("output.html", title=title+" of outputfile") + output_file("output.html", title=title + " of outputfile") return fig # X = np.array([[1, 1], [1, 2], [2, 2], [9, 10], [10, 10], [10, 9], [9, 9]]) # fcm = FCM() # fcm.set_logger(tostdout=True, level=logging.DEBUG) -# fcm.fit(X, [0, 0, 0, 1, 1, 1, 1]) -# testing_data = np.array([[0, 1.9], [3, 3], [4, 4], [8, 9], [9.5, 6.5]]) +# # fcm.fit(X, [0, 0, 0, 1, 1, 1, 1]) +# fcm.fit(X) +# testing_data = np.array([[0, 1.9], [3, 3], [4, 4], [8, 9], [9.5, 6.5], [5, 5], [4.5, 4.5], [4.2, 4.2]]) # predicted_membership = fcm.predict(testing_data) # print "\n\ntesting data" # print testing_data # print "predicted membership" # print predicted_membership # print "\n\n" -# draw_model_2d(fcm) - +# draw_model_2d(fcm, data=testing_data, membership=predicted_membership)