+ 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
#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()