0

Data Science latest module excerise Pandas Pandas Pandas

I'm trying to solve the latest code project Pandas Pandas Pandas. I'm able to pass question 1, 2, 4, and 5, but failed at 3 every time, and the playground doesn't give any feedback. I tried my code on PC, simulating all data points belong to the same cluster and leaving 2nd centroid empty, it doesn't give any error. Can anyone give me some pointer by telling me what's wrong without giving any code? I saw other person shared their code and decided not to read it. I want to do it myself. Thanks. n = int(input()) # this line is not provided in the exercise X = [float(x) for i in range(n) for x in input().split()] # reshape to 2d array for easier euclidean compution import numpy as np X = np.array(X).reshape(-1, 2) # init centroid clusterA = np.array([0,0]) clusterB = np.array([2,2]) # compute euclidean and store the datapoints to the nearest group as a list groupA = [] groupB = [] import numpy as np for i in range(len(X)): ga = np.sqrt(((clusterA - X[i])**2).sum()) gb = np.sqrt(((clusterB - X[i])**2).sum()) if ga <= gb: groupA.append(X[i]) else: groupB.append(X[i]) # reshape to 2d array for easier compution, store result in res groupA = np.array(groupA).reshape(-1,2) groupB = np.array(groupB).reshape(-1,2) res = [] # if the group is empty, append 2 None, otherwise append the mean of x and y (cluster centroid) if len(groupA)==0: res.append(None) res.append(None) else: res.append(groupA[:,0].mean().round(2)) res.append(groupA[:,1].mean().round(2)) if len(groupB)==0: res.append(None) res.append(None) else: res.append(groupB[:,0].mean().round(2)) res.append(groupB[:,1].mean().round(2)) # reshape res to 2d array and use index to print out the answer res = np.array(res).reshape(-1,2) print(res[0]) print(res[1])

1st Sep 2023, 10:40 AM
Wong Hei Ming
Wong Hei Ming - avatar
7 Answers
+ 1
Sorry I’m not fully understand how to integrate your code to mine. To print the result I transform res into a numpy array. If follow your code it print None if one group is empty while other not (it does because all data point must assign to a centroid), the size of res will become 2 (a centroid position), and after transforming it will be broken apart and 3 lines are printed in total (None, centroid x position, centroid y position). However your code does inspire me to rethink how to output the result. In task description it says “If none of the data points were assigned to the given centroid, return None.” So I assume there is an extra None in my output. My new approach is to make a list of lists, it consists of [centroid position x, centroid position y] or [None], and print out the inner list. Sadly it failed all cases without any error message. And a closer look on the expected output of case 1, it is [0.5 0.25] [4. 0.] An numpy array, and my output is [0.5, 0.25] [4.0, 0.0] Clearly not the same data type. And it comes another question. If the output requires a numpy data type and return ONE None if no data point is assigned to one of the given centroid, size of res will become 3 and cannot be transformed.
2nd Sep 2023, 2:48 AM
Wong Hei Ming
Wong Hei Ming - avatar
1st Sep 2023, 12:48 PM
**🇦🇪|🇦🇪**
**🇦🇪|🇦🇪** - avatar
+ 1
I finally made it! Instead of putting the expected result into res and get transform, I print the new centroid one at a time. Here is the concept. If length of group is larger than 0, calculate the new centroid x and y, transform it into a 1d numpy array and print it out. If length is 0, print a plain None. It pass all the cases!
2nd Sep 2023, 2:53 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
It doesn't work, still failed at question 3. I see you get the cert. of Data Science. Would you kindly try your code in the code project and see what is it up to?
1st Sep 2023, 1:32 PM
Wong Hei Ming
Wong Hei Ming - avatar
0
code project is not like codeplayground for some cases : I think because of append(None) it modifies the list in place instead of of returning None or just simply print(None)
1st Sep 2023, 2:11 PM
**🇦🇪|🇦🇪**
**🇦🇪|🇦🇪** - avatar
0
res was declared as an empty list at the beginning, and numpy float64 object are appended as I see in case 1 and 2. I deliberately convert them into a classic float before appending, it got pass the questions without problem. And then I replace the first 2 items with None, and indeed they are Nonetype as I checked. Then I go further to change the value type to np.float64 when transforming res, the None changed to NaN. Of course, it still doesn't pass question 3. So, I don't think it modifies the list in place when appending.
1st Sep 2023, 3:15 PM
Wong Hei Ming
Wong Hei Ming - avatar
0
try to replace res.append(None) if len(groupA)==0: print(None) && if len(groupB)==0: print(None) and see still not passing case 3 ? and here is a reference about how append modifies in place https://realpython.com/JUMP_LINK__&&__python__&&__JUMP_LINK-append/
1st Sep 2023, 4:17 PM
**🇦🇪|🇦🇪**
**🇦🇪|🇦🇪** - avatar