0
Data Science with python ( Pandas Pandas Pandas ) Project test case #3
test case #3 always fails. checked everything. older discussions are saying case #3 checks the 'None' output, which works perfectly in my code. Can anyone spot the bug? Thank you! https://code.sololearn.com/ca11A18a38a4
6 Answers
+ 7
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(
+ 3
#try this code credit to Namrata Dattani
import numpy as np
first = np.array([[0., 0.]])
second = np.array([[2., 2.]])
n = int(input())
data = []
for i in range(n):
data.append([float(i) for i in input().split()])
data = np.array(data).reshape((-1,2))
for i in range(n):
dist1 = np.sqrt(((data[i]-first[0])**2).sum())
dist2 = np.sqrt(((data[i]-second[0])**2).sum())
if (dist1) <= (dist2):
first = np.vstack((first,data[i]))
else:
second = np.vstack((second,data[i]))
if first.size > 2:
mean1 = np.mean(first[1:], axis=0)
print(np.around(mean1, decimals=2))
else:
print(None)
if second.size > 2:
mean2 = np.mean(second[1:], axis=0)
print(np.around(mean2, decimals=2))
else:
print(None)
0
shortest answer :
https://code.sololearn.com/ca19a6a186A1/?ref=app
0
Also:
https://www.sololearn.com/compiler-playground/cEIuqF5925EB
It takes the ideas from Jonnie Sagarino and Mardin D & combines into one. Doesn't require matrix multiplication as in NimAA's post, althugh that is the shortest solution. Hope this is elegant n' readable for someone (like me) new to Python coding. With in-code comments.
0
This solves all cases. For case #3, use 'print(None)' will do.
>>>>
import numpy as np
import pandas as pd
# return the displacement betwen two points
def dp(pt, cpt):
return ((pt[0]-cpt[0])**2 + (pt[1]-cpt[1])**2)**0.5
# list all points with their cooridinates
def pts():
plist = []
n = int(input())
for i in range(n):
plist.append([float(x) for x in input().split()])
return plist
# find out which cluster a given point is assigned to
def srt():
cls = []
for i in points:
dps = []
for j in range(len(centroids)):
dps.append(dp(i, centroids[j]))
cls.append(dps.index(min(dps)))
return cls
# calculate the new centroid
def findnew():
df = pd.DataFrame({"points":points, "clusters":srt()})
for i in range(len(centroids)):
cat = df['points'][df['clusters']==i].tolist()
if cat:
print(np.mean(cat, axis=0).round(2))
else:
print(None)
# __main__
centroids = ((0,0), (2,2))
points = pts()
findnew()
- 1
Jonnie Sagarino thank you! Worked like a charm