Data Science Last Project: Pandas Pandas Pandas. Need help! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Data Science Last Project: Pandas Pandas Pandas. Need help!

Project: https://www.sololearn.com/learning/eom-project/1093/108 My solution is correct and passing first two tests: ******* import numpy as np from sklearn.cluster import KMeans n = int(input()) if n <= 1: print("None") else: X = [] for i in range(n): X.append([float(x) for x in input().split()]) x = np.array(X) c = np.array([[0, 0],[2, 2]]) kmeans = KMeans(n_clusters=2, init=c, n_init=1) kmeans.fit(x) labels = kmeans.predict(x) # counting cluster weights c0 = np.sum(labels==0) c1 = np.sum(labels==1) if c0 == 0: print("None") else: print(kmeans.cluster_centers_[0].round(2)) if c1 == 0: print("None") else: print(kmeans.cluster_centers_[1].round(2)) ********** How to pass last 3 hidden tests? Could anyone help?

19th Mar 2021, 2:21 PM
Airat Halitov
Airat Halitov - avatar
13 Answers
+ 2
I don't know how sklearn.cluster.KMeans works, I just know, that you should handle cases where there's no centroids and you should output None instead of centroid list, because I've solved it by doing all with only numpy as import ^^
19th Mar 2021, 3:20 PM
visph
visph - avatar
+ 1
I updated my solution with counting cluster's weights. Still only two tests.
19th Mar 2021, 4:35 PM
Airat Halitov
Airat Halitov - avatar
20th Mar 2021, 5:05 AM
Airat Halitov
Airat Halitov - avatar
+ 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)
19th Aug 2021, 3:28 PM
Subhransu Sekhar Mohanty
Subhransu Sekhar Mohanty - avatar
+ 1
# neat solution using Numpy, though centroids not vectorised n = int(input()) import numpy as np v=np.empty([n,2]) for i in range(n): v[i,] = [float(x) for x in input().split()] v = np.array(v) c1 = np.array([0,0]) c2 = np.array([2,2]) d1 = np.sqrt( ( (v-c1)**2 ).sum(axis=1)) d2 = np.sqrt( ( (v-c2)**2 ).sum(axis=1)) c1mask = d1<=d2 c2mask = d1>d2 if np.all(c1mask == False): print("None") else: print(v[c1mask].mean(axis=0).round(2)) if np.all(c2mask == False): print("None") else: print(v[c2mask].mean(axis=0).round(2))
9th Sep 2021, 11:32 AM
Herve Bonnel
Herve Bonnel - avatar
0
import numpy as np n = int(input()) X=[] for i in range(n): X.append([float(x) for x in input().split()]) x1 = np.array([0, 0]) x2 = np.array([2, 2]) X=np.array(X) a=[] b=[] for i in range(n): if np.sqrt(((X[i]-x1)**2).sum()) <= np.sqrt(((X[i]-x2)**2).sum()): a.append(X[i]) elif np.sqrt(((X[i]-x1)**2).sum()) > np.sqrt(((X[i]-x2)**2).sum()): b.append(X[i]) a=np.array(a) b=np.array(b) sum_a_1=0 sum_a_2=0 sum_b_1=0 sum_b_2=0 for i in range(len(a)): sum_a_1+=a[i][0] sum_a_2+=a[i][1] for i in range(len(b)): sum_b_1+=b[i][0] sum_b_2+=b[i][1] if (len(a)!=0): sum_a_1/=len(a) sum_a_2/=len(a) sum_a_1 = sum_a_1.round(2) sum_a_2 = sum_a_2.round(2) if (len(b)!=0): sum_b_1/=len(b) sum_b_2/=len(b) sum_b_1 = sum_b_1.round(2) sum_b_2 = sum_b_2.round(2) c=[] c.append(sum_a_1) c.append(sum_a_2) d=[] d.append(sum_b_1) d.append(sum_b_2) c=np.array(c) d=np.array(d) if len(a)==0: print(None) else: print(c) if len(b)==0: print(
5th May 2021, 2:29 PM
Azmi Wijaksono
Azmi Wijaksono - avatar
0
Azmi Wijaksono your final print gets cut off. What shoild that be?
20th May 2021, 4:34 PM
Zach Z
0
Azmi Wijaksono code doesnt work at all now? Or wont print the final numbers
21st May 2021, 2:28 PM
Zach Z
0
Ok I'll check and correct it later
23rd May 2021, 10:45 AM
Azmi Wijaksono
Azmi Wijaksono - avatar
0
you have not factored in this part of problem"If none of the data points were assigned to the given centroid, return None. " meaning if a center has no point assigned to it you return the new center and "None"
11th Jun 2021, 7:45 PM
Mohammad Amin Khanpour
24th Aug 2021, 6:29 PM
Chi Jone Wong
Chi Jone Wong - avatar
- 1
Print()
20th May 2021, 4:45 PM
Azmi Wijaksono
Azmi Wijaksono - avatar