Pandas Pandas Pandas challenge.. what test 3 checks?? | Sololearn: Learn to code for FREE!

+2

Pandas Pandas Pandas challenge.. what test 3 checks??

Pandas Pandas Pandas Challenge

12/26/2020 1:00:29 AM

Adriano Lopes Godoy

33 Answers

New Answer

+8

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)

+5

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

+2

Hmm... you're right but maybe you should post your attempt so someone who had the same problem know what needs to be adjusted.

+2

My code doesn't pass test 3 too. Can someone check this and help https://code.sololearn.com/c8DKMv2ZnIj1/?ref=app

+2

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.

+2

Test 3 checks "None" printing my code works with kmeans https://code.sololearn.com/cA25a62A23a2

+1

Yes, it's hidden.. but different tests evaluate different situations .. someone who had the same problem knows what needs to be adjusted..

+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

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

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

Check it! I’ve implemented kmeans algoritm and numpy to solve it! ;) https://code.sololearn.com/cN83G8vbPnKY/?ref=app

0

my code is like yours .. but i use pandas too .. same logic...