Data Science Last Project: Data Science - Pandas Pandas Pandas | Sololearn: Learn to code for FREE!

0

Data Science Last Project: Data Science - Pandas Pandas Pandas

My solution gives 4 correct out of 5 test cases, can someone help me out? 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.

8/19/2021 12:13:36 AM

Shivam Panchal

4 Answers

New Answer

+2

Please like copy my answer

+1

import numpy as np def distance(l1,l2): x1, y1 = l1 x2, y2 = l2 dis = np.sqrt(((x1-x2)**2)+((y1-y2)**2)) return dis def new_Centroid(pC): final_cen=[] for idx,cen in enumerate(pC): if len(cen)>1 and (None not in cen): centx=0 centy=0 for jt in cen: centx += jt[0] centy += jt[1] final_cen.append([centx/len(cen), centy/len(cen)]) elif None in cen: final_cen.append(None) else: final_cen.append(cen[0]) return final_cen[0], final_cen[1] centroids = [[0,0], [2,2]] n = int(input()) nums=[[float(j) for j in input().split()] for i in range(n)] output= [[distance(centroid,no) for centroid in centroids] for no in nums ] pC1=[] pC2=[] for ind,dat in enumerate(output): if (dat[0]< dat[1]) or (dat[0]==dat[1]): pC1.append(nums[ind]) else: pC2.append(nums[ind]) if len(pC1)<1: pC1.append(None) elif len(pC2)<1: pC2.append(None) ans = np.round(new_Centroid([pC1, pC2]),2) print(ans[0]) print(ans[1])

+1

Update : I got it, here is added part for anyone looking for an answer: ans = new_Centroid([pC1, pC2]) if None in ans: nval = ans.index(None) a1=np.round(ans[1-nval],2) a2=ans[nval] print(a1) print(a2) else: ans = np.round(new_Centroid([pC1, pC2]),2) print(ans[0]) print(ans[1])

+1

#Thanks John Robotane n = int(input()) import numpy as np def eudt(lista,listb): lista,listb=np.array(lista), np.array(listb) diff=lista-listb return ((diff[0]**2)+(diff[1]**2))**0.5 w=np.zeros((n,2)) for i in range(n): w[i,]=[float(j) for j in input().split()] distances=np.zeros((n,2)) for i in range(n): distances[i,]=[eudt(w[i,],[0,0]),eudt(w[i,],[2,2])] team0=w[(distances[:,0]<=distances[:,1]),] team2=w[(distances[:,0]>distances[:,1]),] if sum(distances[:,0]<=distances[:,1])!=0: zeromn=np.around(team0.mean(axis=0),2) if sum(distances[:,0]>distances[:,1])!=0: twoavg=np.around(team2.mean(axis=0),2) if sum(distances[:,0]<=distances[:,1])==0: zeromn=None elif sum(distances[:,0]>distances[:,1])==0: twoavg=None else: pass print(zeromn) print(twoavg)