Finding the next centroid Unsupervised learning algorithm clustering involves updating the centroid of each cluster. Here we fin | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Finding the next centroid Unsupervised learning algorithm clustering involves updating the centroid of each cluster. Here we fin

Task Assume that there are two clusters among the given two-dimensional data points and two random points (0, 0), and (2, 2) are the initial cluster centroids. Calculate the euclidean distance between each data point and each of the centroid, assign each data point to its nearest centroid, then calculate the new centroid. If there's a tie, assign the data point to the cluster with centroid (0, 0). If none of the data points were assigned to the given centroid, return None. Input Format First line: an integer to indicate the number of data points (n) Next n lines: two numeric values per each line to represent a data point in two dimensional space. Output Format Two lists for two centroids. Numbers are rounded to the second decimal place. Sample Input 3 1 0 0 .5 4 0 Sample Output [0.5 0.25] [4. 0.]

2nd May 2021, 10:56 AM
Fini Ardiani Br Damanik
Fini Ardiani Br Damanik - avatar
4 Answers
+ 11
After some hours: import math import numpy as np n = int(input()) ce1=[0,0] ce2=[2,2] cl1=np.empty([0,2], float) cl2=np.empty([0,2], float) for i in range(n): x = [float(j) for j in input().split()] d21 = (np.array(ce1)-np.array(x))**2 d1=math.sqrt(d21.sum()) d2 = math.sqrt(((np.array(ce2)-np.array(x))**2).sum()) if d1<=d2: cl1 = np.append(cl1, np.array([x]), axis=0) else: cl2 = np.append(cl2, np.array([x]), axis=0) if cl1.shape[0]!=0: print(np.mean(cl1,axis=0).round(2)) else: print(None) if cl2.shape[0]!=0: print(np.mean(cl2,axis=0).round(2)) else: print(None) And it working.
13th May 2021, 7:22 PM
oleg pyankov
oleg pyankov - avatar
+ 2
import math import numpy as np n = int(input()) ce1=[0,0] ce2=[2,2] cl1=np.empty([0,2], float) cl2=np.empty([0,2], float) for i in range(n): x = [float(j) for j in input().split()] d21 = (np.array(ce1)-np.array(x))**2 d1=math.sqrt(d21.sum()) d2 = math.sqrt(((np.array(ce2)-np.array(x))**2).sum()) if d1<=d2: cl1 = np.append(cl1, np.array([x]), axis=0) else: cl2 = np.append(cl2, np.array([x]), axis=0) if cl1.shape[0]!=0: print(np.mean(cl1,axis=0).round(2)) else: print(None) if cl2.shape[0]!=0: print(np.mean(cl2,axis=0).round(2)) else: print(None) This is the right code
16th Jun 2022, 7:09 AM
priyanka mahule
+ 1
I write this code: n = int(input()) from sklearn.cluster import KMeans import numpy as np X=[] for i in range(n): X.append([float(x) for x in input().split()]) kmeans=KMeans(n_clusters=2,random_state=0).fit(X) for x in kmeans.cluster_centers_: print(x.round(2)) But it works only for 1 and 2 test cases.
9th May 2021, 2:44 PM
oleg pyankov
oleg pyankov - avatar
0
oleg pyankov , thank you so much.... . your code works
24th Aug 2021, 7:32 AM
Deepesh. V
Deepesh. V - avatar