Skip to content

Commit

Permalink
visualization of points
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmad88me committed May 25, 2018
1 parent d03cda5 commit 089763b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
6 changes: 4 additions & 2 deletions fuzzycmeans/fuzzy_clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
56 changes: 33 additions & 23 deletions fuzzycmeans/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand All @@ -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")
Expand All @@ -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

Expand All @@ -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)

0 comments on commit 089763b

Please sign in to comment.