+ 1
My solution for the Codeproject "pandas pandas pandas" in the Module "clustering wine" of the course Data science doesn't work
2 Answers
+ 1
To be a little more specific: it works for all testcases but the Third :-(
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)