Skip to content

Commit f679e1d

Browse files
committed
Resolve #8: Prevent ride clustering k > node count crash
1 parent 82b89f8 commit f679e1d

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

unsupervised/kmeans_rides.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
# Data is expected as a,b|c,d|e,f|
55
def clusterActivities(data, num_clusters):
66
input = data.split('|')
7+
8+
if data == '':
9+
return ''
10+
num_clusters = min(len(input), num_clusters)
11+
712
arr = []
813
for point in input:
914
pointArr = point.split(',')
@@ -13,4 +18,5 @@ def clusterActivities(data, num_clusters):
1318
kmeans = KMeans(n_clusters=num_clusters, random_state=0).fit(X)
1419
return '|'.join(str(v) for v in np.array(kmeans.labels_).tolist())
1520

21+
1622
# print clusterActivities('1,1|2,2|4,4|6,6|32132,13231|33443,3432|-322,-32123|-321,-43211', 3)

0 commit comments

Comments
 (0)