Skip to content

Commit b201233

Browse files
committed
Update
1 parent 8a7bc66 commit b201233

File tree

4 files changed

+16
-19
lines changed

4 files changed

+16
-19
lines changed

EM/EM.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def SSE(C, mu):
123123
if len(D.shape) < 2:
124124
D = D.reshape((D.shape[0],1))
125125

126-
# Read in k centroid count
126+
# Read in k cluster count
127127
k = int(sys.argv[2])
128128

129129
# Read in mu mean matrix if given

SpectralClustering/kMeans.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,9 @@ def kMeans(D, k, mu=None, eps=0.0001):
5555
mu[i,:] = np.sum(C[i], axis=0) / len(C[i])
5656

5757
# Check for convergence
58-
check = np.linalg.norm(mu - prev_mu)
59-
if check <= eps:
58+
if np.linalg.norm(mu - prev_mu) <= eps:
6059
return C, labels, mu, iter
6160

62-
# Print update
63-
#print("Iteration " + str(iter) + ":")
64-
#for i in range(k):
65-
#print("c_" + str(i) + ":" + str(C[i]) + " mu_" + str(i) + ":" + str(mu[i,:]))
66-
#print("||mu - mu_prev||:" + str(check) + "\n")
67-
6861
# Update iteration count
6962
iter += 1
7063
# Update previous mu

datafile_1d.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2
2+
3
3+
4
4+
10
5+
11
6+
12
7+
20
8+
25
9+
30

kMeans/kMeans.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
# mu - kxd centroid matrix
1212
# k - cluster count
1313
# eps - convergence tolarence
14-
def kMeans(D, k, mu, eps=0.0001):
14+
def kMeans(D, k, mu=None, eps=0.0001):
1515
# Get dimensions of nxd D matrix
1616
n, d = D.shape
1717

1818
# If mu is not preset
19-
if np.array_equal(mu,np.zeros((k,d))):
19+
if mu is None:
2020
# Randomly intialize k centroids in kxd mu matrix
21+
mu = np.zeros((k,d))
2122
mu_list = np.random.choice(n,size=k,replace=False)
2223
for i,id in enumerate(mu_list):
2324
mu[i,:] = D[id,:]
@@ -55,13 +56,7 @@ def kMeans(D, k, mu, eps=0.0001):
5556

5657
# Check for convergence
5758
if np.linalg.norm(mu - prev_mu) <= eps:
58-
return C, labels, mu, iter
59-
60-
# Print update
61-
#print("Iteration " + str(iter) + ":")
62-
#for i in range(k):
63-
#print("c_" + str(i) + ":" + str(C[i]) + " mu_" + str(i) + ":" + str(mu[i,:]))
64-
#print("||mu - mu_prev||:" + str(check) + "\n")
59+
return C, labels, mu, iter
6560

6661
# Update iteration count
6762
iter += 1
@@ -98,7 +93,7 @@ def SSE(C, mu):
9893
if len(D.shape) < 2:
9994
D = D.reshape((D.shape[0],1))
10095

101-
# Read in k centroid count
96+
# Read in k cluster count
10297
k = int(sys.argv[2])
10398

10499
# Read in mu centroid id list if given

0 commit comments

Comments
 (0)