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?
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 ^^
+ 1
I updated my solution with counting cluster's weights.
Still only two tests.
+ 1
Another example: https://www.sololearn.com/Discuss/2713085
+ 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)
+ 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))
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(
0
Azmi Wijaksono your final print gets cut off. What shoild that be?
0
Azmi Wijaksono code doesnt work at all now? Or wont print the final numbers
0
Ok I'll check and correct it later
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"
- 1
Print()