0

looking for bug in numpy code

So, I'm working on a SL learn exercise about Numpy functions, and I'm having some trouble. The exercise only wants a simple percentage of values that are within one standard deviation of the mean. I thought I'd provided that, but the hidden test case keeps showing up as a failure. I've tried converting the percent to an integer, and not converting it, and both times it comes up wrong. I ended up writing all this other code around the problem to see if I could find something wrong in one of the other values, but everything looks ok to me. Since they are all outputs from numpy functions and not from anything I've written myself, I'm not really sure how I could fix them anyway. Can anyone looking at this see any problems with the outputs? FYI, in the actual problem, of course all the print statements are commented out, except for the last one, which prints the percentage. Anyway, the code is below, and the code playground window is at: https://code.sololearn.com/ceSbl3xGUctY import numpy as np data = np.array( [150000, 125000, 320000, 540000, 200000, 120000, 160000, 230000, 280000, 290000, 300000, 500000, 420000, 100000, 150000, 280000]) print(np.sort(data)) print("=================") print("Lowest value: " + str(np.min(data))) print("Mean: " + str(np.mean(data))) print("Highest value: " + str(np.max(data))) print("=================") print("Variance: " + str(np.var(data))) print("SD: " + str(np.std(data))) lowerRange = np.mean(data) - np.std(data) upperRange = np.mean(data) + np.std(data) print("Lower range: " + str(lowerRange)) print("Upper range: " + str(upperRange)) withinSD = 0 for n in data: if n >= lowerRange and n <= upperRange: withinSD += 1 percentWithinSD = int((withinSD/data.size)*100) print("==================") print("# of data points: " + str(data.size)) print("#WithinSD: " + str(withinSD)) print("%WithinSD: " + str(percentWithinSD))

26th Jun 2022, 3:48 PM
Jimmy Tyrrell
Jimmy Tyrrell - avatar
7 Réponses
0
I noticed you use >= and <=, I think it is > and < if it is "within" range. Have you tried this?
26th Jun 2022, 5:50 PM
Lisa
Lisa - avatar
0
for calculating the percentage, I've used: percentWithinSD = ((withinSD/data.size)*100), or (10/16)*100 which outputs 62.5 percentWithinSD = int((withinSD/data.size)*100), or int((10/16)*100) which outputs 62 percentWithinSD = round((withinSD/data.size)*100), or round((10/16)*100) which also outputs 62, although isn't it supposed to output 63? To double-check on the rounding, I even tried this one: percentWithinSD = round(((withinSD/data.size)*100),0), , or round(((10/16)*100),0) which got me 62.0. Shouldn't it be 63.0?
26th Jun 2022, 5:26 PM
Jimmy Tyrrell
Jimmy Tyrrell - avatar
0
Can you please mention the course and the task number so we can check on it?
26th Jun 2022, 5:32 PM
Lisa
Lisa - avatar
0
@lisa, The exercise is called House Prices. It's the end of unit project for the numpy unit of Python for Data Science. the link for the exercise is: https://www.sololearn.com/learning/eom-project/1161/1156 This is the code I'm using in the exercise: import numpy as np data = np.array( [150000, 125000, 320000, 540000, 200000, 120000, 160000, 230000, 280000, 290000, 300000, 500000, 420000, 100000, 150000, 280000]) lowerRange = np.mean(data) - np.std(data) upperRange = np.mean(data) + np.std(data) withinSD = 0 for n in data: if n >= lowerRange and n <= upperRange: withinSD += 1 percentWithinSD = (withinSD/data.size)*100 print(percentWithinSD)
26th Jun 2022, 5:43 PM
Jimmy Tyrrell
Jimmy Tyrrell - avatar
0
@Lisa, ugh, that fixed it. I'm pretty dumb about statistics. I just assumed that equaling the bottom or top of the range was still within bounds. Thanks for the correction!
26th Jun 2022, 5:54 PM
Jimmy Tyrrell
Jimmy Tyrrell - avatar
0
Great that you could solve it!
26th Jun 2022, 5:55 PM
Lisa
Lisa - avatar
0
@Lisa, I just checked in my own code window, and noticed that changing those parameters didn't actually change the outputs. withinSD was still 10 and the percent was still 62.5. I'm going to try not to wonder too much about it though, and move on :) Thanks for the help!
26th Jun 2022, 6:00 PM
Jimmy Tyrrell
Jimmy Tyrrell - avatar