Skip to content
This repository has been archived by the owner on Nov 14, 2022. It is now read-only.

Commit

Permalink
Added figure labels
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik3003 committed Dec 14, 2020
1 parent 95e18b6 commit b816109
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions kMeansClustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def calculate_means(self):

def cluster_data(self):
# Ordnet die Daten den Clustern zu
# Gibt True zurück, wenn die neu berechnete Ordnung der alten entspricht (Abbruchbedingung)
# Gibt False zurück, wenn die neu berechnete Ordnung der alten entspricht (Abbruchbedingung)
new_groups = np.zeros(len(self.array))
for i in range(len(self.array)):
min_distance = -1
Expand All @@ -36,9 +36,9 @@ def cluster_data(self):
min_distance = distance
new_groups[i] = m
if np.array_equal(new_groups, self.groups):
return True
return False
self.groups = new_groups
return False
return True

def visualize(self):
# Zeichnen der Plots
Expand All @@ -49,6 +49,15 @@ def visualize(self):
plt.plot(self.array[i][n], self.array[i][n+1], marker='o', color=self.get_color(category))
for i in range(self.k):
plt.plot(self.means[i][n], self.means[i][n+1], marker='x', color=self.get_color(i))
if n == 0:
plt.title("Sepal size")
plt.ylabel("Sepal width (cm)")
plt.xlabel("Sepal length (cm)")
if n == 2:
plt.title("Petal size")
plt.ylabel("Petal width (cm)")
plt.xlabel("Petal length (cm)")

plt.show()

def get_color(self, n):
Expand All @@ -64,7 +73,7 @@ def get_color(self, n):
cluster = KMeansClustering(3, data_array)

# K-Means Clustering Algorithmus nach Lloyd
while not cluster.cluster_data():
while cluster.cluster_data():
cluster.calculate_means()

cluster.visualize()

0 comments on commit b816109

Please sign in to comment.