0

Pandas Pandas Pandas

I am outputting the correct centroids for the test cases, but they are not being taken as correct. Can someone advise? n = int(input()) import numpy as np X = [] for i in range(n): X.append([float(x) for x in input().split()]) #print(X) #calculate distance between points and the origin dist_origin = [] for i in range(n): y = np.sqrt((X[i][0])**2 + (X[i][1]**2)) dist_origin.append(float(y)) #calculate distance between points and second centroid dist_cen = [] for i in range(n): y = np.sqrt((X[i][0] - 2.0)**2 + (X[i][1] - 2.0)**2) dist_cen.append(float(y)) #assign points to centroid origin = [] cen = [] for i in range(n): if dist_origin[i] < dist_cen[i]: origin.append(X[i]) elif dist_origin[i] > dist_cen[i]: cen.append(X[i]) elif dist_origin[i] == dist_cen[i]: origin.append(X[i]) else: print(none) #calculate new centroids from assigned data points x = len(origin) xsum, ysum = 0,0 for i in range(x): xsum += origin[i][0] ysum += origin[i][1] xavg = round(xsum / x, 2) yavg = round(ysum / x, 2) cen1 = [xavg, yavg] x = len(cen) xsum, ysum = 0,0 for i in range(x): xsum += cen[i][0] ysum += cen[i][1] xavg = round(xsum / x, 2) yavg = round(ysum / x, 2) cen2 = [xavg, yavg] print(cen1) print(cen2)

7th Apr 2022, 7:24 PM
Delbert Bauldock
2 odpowiedzi
0
https://www.sololearn.com/compiler-playground/cvUcsMq6W81C import numpy as np n, k, datapoints = int(input()), 2, [] for i in range(n): datapoints.append([float(x) for x in input().split()]) datapoints = np.array(datapoints) centroids = np.array([[0, 0], [2, 2]]) def euclidean_distance(x, y): return np.sum(np.square(x - y)) ed = np.zeros((n, k)) for i, c in enumerate(centroids): for j, d in enumerate(datapoints): ed[j, i] = euclidean_distance(c, d) nearest_c = np.argmin(ed, axis=1) for c in range(k): dps = datapoints[np.asarray(c==nearest_c).nonzero()] if len(dps) == 0: print(None) continue new_centroid = dps.mean(axis=0).round(2) print(new_centroid)
22nd Feb 2023, 7:36 PM
kwesiquest