33 AnswersNew Answer
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)**2).sum()) dist2 = np.sqrt(((data[i]-second)**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)
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
Hmm... you're right but maybe you should post your attempt so someone who had the same problem know what needs to be adjusted.
My code doesn't pass test 3 too. Can someone check this and help https://code.sololearn.com/c8DKMv2ZnIj1/?ref=app
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 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.
Test 3 checks "None" printing my code works with kmeans https://code.sololearn.com/cA25a62A23a2
Yes, it's hidden.. but different tests evaluate different situations .. someone who had the same problem knows what needs to be adjusted..
Adriano Lopes Godoy I tried with Pandas too, failing just test 3. Can not understand what's wrong
《 Nicko12 》 The challenge is as such https://code.sololearn.com/chdyvNqc68e6/?ref=app It won't require ML knowledge
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...
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..
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
So you solved it? I think the previous code already has that (else then print(None) ) or maybe its different.
Oh ok, but what will hapen if else: pass ??
Need use <= ... if tie... append centroid (0,0)
《 Nicko12 》 your code works too...
Yes.. I solved..
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)...
Check it! I’ve implemented kmeans algoritm and numpy to solve it! ;) https://code.sololearn.com/cN83G8vbPnKY/?ref=app
my code is like yours .. but i use pandas too .. same logic...