+ 1

finding centroid of cluster

i have done the final project of Data Science course. my code has solved 4 test cases and 1 gives error. unfortunately it's a hidden case, so i cannot understand what should I do. that is the problem: https://www.sololearn.com/learning/eom-project/1093/108 here is my code: import numpy as np n = int(input()) data = [[float(i) for i in input().split()] for j in range(n) ] def dist(a, b): x = a[0]-b[0] y = a[1]-b[1] res = np.sqrt(x**2+y**2) return res clus_1 = [] clus_2 = [] for point in data: if(dist(point,[0,0])<=dist(point,[2,2])): clus_1.append(point) else: clus_2.append(point) res_1 = np.array(clus_1, dtype=np.float32) res_1 = res_1.mean(axis=0) res_1 = np.round(res_1, decimals=2) res_2 = np.array(clus_2, dtype=np.float32) res_2 = res_2.mean(axis=0) res_2 = np.round(res_2, decimals=2) print(res_1) print(res_2)

28th Nov 2021, 9:38 AM
Morteza Hajji
8 Respuestas
+ 1
"If none of the data points were assigned to the given centroid, return None." ...print(None)
28th Nov 2021, 10:23 AM
Paul K Sadler
Paul K Sadler - avatar
+ 1
Tnx for answering Paul K Sadler . What does it mean by givencentroid? I tried every permutation of checking empty lists to print None, its not clear to me
28th Nov 2021, 11:20 AM
Morteza Hajji
+ 1
For example... import numpy as np first = np.array([[0., 0.]]) second = np.array([[2., 2.]]) n = int(input()) data = [] for i in range(n): data.append([float(i) for i in input().split()]) data = np.array(data).reshape((-1,2)) for i in range(n): dist1 = np.sqrt(((data[i]-first[0])**2).sum()) dist2 = np.sqrt(((data[i]-second[0])**2).sum()) if (dist1) <= (dist2): first = np.vstack((first,data[i])) else: second = np.vstack((second,data[i])) if first.size > 2: mean1 = np.mean(first[1:], axis=0) print(np.around(mean1, decimals=2)) else: print(None) if second.size > 2: mean2 = np.mean(second[1:], axis=0) print(np.around(mean2, decimals=2)) else: print(None)
28th Nov 2021, 11:26 AM
Paul K Sadler
Paul K Sadler - avatar
+ 1
Paul that's great, your code works very well. but i'm confused what's wrong with my modified code? can you see whats its problem? it made me frustrated. here it is: import numpy as np n = int(input()) data = [[float(i) for i in input().split()] for j in range(n) ] def dist(a, b): x = a[0]-b[0] y = a[1]-b[1] res = np.sqrt(x**2+y**2) return res clus_1 = [] clus_2 = [] for point in data: if(dist(point,[0,0])<=dist(point,[2,2])): clus_1.append(point) else: clus_2.append(point) res_1 = np.array(clus_1, dtype=np.float32) res_1 = res_1.mean(axis=0) res_1 = np.round(res_1, decimals=2) res_2 = np.array(clus_2, dtype=np.float32) res_2 = res_2.mean(axis=0) res_2 = np.round(res_2, decimals=2) if (len(clus_1)==0): print(None) else: print(res_1) if (len(clus_2)==0): print(None) else: print(res_2)
28th Nov 2021, 12:29 PM
Morteza Hajji
+ 1
I found my code bug, it was trying to get mean of empty array, if statement solves this bug, tnx Paul K Sadler again
28th Nov 2021, 4:23 PM
Morteza Hajji
+ 1
Glad you were able to resolve it, sorry I was not able to look before now.
28th Nov 2021, 4:27 PM
Paul K Sadler
Paul K Sadler - avatar
+ 1
Paul K Sadler U helped a lot
28th Nov 2021, 4:28 PM
Morteza Hajji
+ 1
finding centroid of cluster In this last project If test 3 is giving error Then include these below points in your code If any of the cluster is empty(either cluster 0 or cluster 1) Then your programme must print None for that cluster
12th Feb 2022, 7:18 PM
Shadab Alam
Shadab Alam - avatar