+ 3
Pandas Pandas Pandas challenge.. what test 3 checks??
Pandas Pandas Pandas Challenge
38 ответов
+ 11
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)
+ 8
Adriano Lopes Godoy
I removed the colons on the columns (which is sometimes the cause of error) and because the output will be the same anyway. This is just a possibility. This is from Pranjal Tambe 's code.
https://code.sololearn.com/c81T363EgrNn/?ref=app
EDIT: Namrata Dattani Bruh! you just copied my code bit then pasted it here as your answer. At first, I thought that was yours but I noticed that it is an exact copy of my previous code (also OP's code), even spaces and variables. Solve and help on your own dude. That's not a nice thing to do! [original code: https://code.sololearn.com/c81T363EgrNn/?ref=app]
+ 5
Aside from colons, others are just minor adjustment or improvement of readability such as:
1.) Instead of [0, 0] and [2, 2] it is [0., 0.] and [2., 2.] for it to be float-type from the start.
2.) Instead of input().split(' '), just input().split() cause whitespace is the default parameter
3.) Just some whitespaces for readability.
And just to clarify I didnt remove colons, rather I remove columns because for example I want to get all the columns of the 3rd row:
array[2, :] is the same with array[2]
So I think it is safer to only have one number if you will access a row that includes all columns, but I also dont know whats the reason behind this.
+ 3
Hmm... you're right but maybe you should post your attempt so someone who had the same problem know what needs to be adjusted.
+ 3
My code doesn't pass test 3 too. Can someone check this and help
https://code.sololearn.com/c8DKMv2ZnIj1/?ref=app
+ 3
import numpy as np
n = int(input())
d = []
for i in range(n):
d.append([float(i) for i in input().split()])
data = np.array(d)
r1 = [0, 0]
r2 = [2, 2]
c1 = []
c2 = []
for i in range(n):
euclidean_dist1 = np.sqrt(((data[i]-r1)**2).sum())
euclidean_dist2 = np.sqrt(((data[i]-r2)**2).sum())
if euclidean_dist1 <= euclidean_dist2:
c1.append(data[i])
else:
c2.append(data[i])
if len(c1)==0:
print('None')
else:
print((np.array(c1).mean(axis=0)).round(2))
if len(c2)==0:
print('None')
else:
print((np.array(c2).mean(axis=0)).round(2))
+ 2
I solved.. add else and print(None)
Solution:
else:
print(None)
Or
if(len(array)==0):
print(None)
else:
[Code]
This solve test 3...
this is the tip I needed
+ 1
Adriano Lopes Godoy I tried with Pandas too, failing just test 3. Can not understand what's wrong
+ 1
《 Nicko12 》 The challenge is as such
https://code.sololearn.com/chdyvNqc68e6/?ref=app
It won't require ML knowledge
+ 1
distance calculation
#initial centroid
p1 = np.array([0.,0.])
p2 = np.array([2.,2.])
#loop for... for each point input.. p = new point
d1 = np.sqrt(((p-p1)**2).sum())
d2 = np.sqrt(((p-p2)**2).sum())
if d2>=d1:
c0.append(p)
else:
c2.append(p)
mean use pandas to calculate and round
this fail in test 3...
+ 1
separated into 2 arrays for each centroid... c0 nearst p(0, 0) and c2 nearst p(2,2)...
use pandas to calculate mean and round
print(c0.mean().round(2).values)
print(c2.mean().round(2).values)
this work for test 1,2,4 and 5... fail in test 3..
+ 1
So you solved it?
I think the previous code already has that (else then print(None) ) or maybe its different.
+ 1
Oh ok, but what will hapen if
else:
pass
??
+ 1
Need use <= ... if tie... append centroid (0,0)
+ 1
《 Nicko12 》 your code works too...
+ 1
Yes.. I solved..
+ 1
My final code is the same as the first.. When i use print(None)... i use split(' ')... this fail test 3 too... always test 3 to fail.... When use split()... not use print(None).. i try too print()... fail test 3 too.. 2 small details... only need use split() and print(None)...
+ 1
Test 3 checks "None" printing
my code works with kmeans
https://code.sololearn.com/cA25a62A23a2
+ 1
i've tried some input , and i've found out that if the list is empty , you should print None
example input:
2
0 0
1 1
the output must be:
[0.5 , 0.5]
None
Hope it helps :D
+ 1
Here is my solution, I hope this is helpful.
Take a look so you can have another point of view.
https://code.sololearn.com/cO7ZsO5Y1LCK/#py
Please upvote if it was useful.