0

[Data Science] Clustering Wines Project - Pandas Pandas Pandas

Hi guys, I failed in test case 3 while passing the others. Don't know where did I go wrong.. can any master help me? Thank you very much :D import numpy as np n = int(input()) X = [] for i in range(n): X.append([float(x) for x in input().split()]) point_1 = np.array((0.,0.)) point_2 = np.array((2.,2.)) cluster_0 = [] cluster_1 = [] for i in X: if np.linalg.norm(np.array(tuple(i)) - point_1) <= np.linalg.norm(np.array(tuple(i)) - point_2): cluster_0.append(np.array(i)) else: cluster_1.append(np.array(i)) mean_1 = np.mean(cluster_0[:], axis = 0) mean_2 = np.mean(cluster_1[:], axis = 0) if np.array(cluster_0).size == 0: print(None) else: print(np.round(mean_1, 2)) if np.array(cluster_1).size == 0: print(None) else: print(np.round(mean_2, 2))

6th Jan 2022, 5:03 PM
æž—ćź‡ć‡Ą
æž—ćź‡ć‡Ą - avatar
1 Answer
0
Solved by John Robotane's solution in another discussion thread. Thanks!! (here's his suggestion: if n1 or n2 is null you will have an error on line 25 or line 26. I suggest you, before calculating the means to first test if those numbers are not null. I suppose that in this test, one of your variables n1 or n2 will be null.) My final code: import numpy as np n = int(input()) X = [] for i in range(n): X.append([float(x) for x in input().split()]) point_1 = np.array([0.,0.]) point_2 = np.array([2.,2.]) cluster_0 = [] cluster_1 = [] for i in X: if np.linalg.norm(np.array(i) - point_1) <= np.linalg.norm(np.array(i) - point_2): cluster_0.append(np.array(i)) else: cluster_1.append(np.array(i)) if np.array(cluster_0).size == 0: print(None) else: mean_1 = np.mean(cluster_0[:], axis = 0) print(np.round(mean_1, 2)) if np.array(cluster_1).size == 0: print(None) else: mean_2 = np.mean(cluster_1[:], axis = 0) print(np.round(mean_2, 2))
6th Jan 2022, 5:19 PM
æž—ćź‡ć‡Ą
æž—ćź‡ć‡Ą - avatar