Numpy Assignment Problem (Problems 6 & 8) - Sachin's average score

I wanted to address this to the professor or whoever is good at Python coding. I am a beginner in coding and welcome to feedback.

Problem 6
In the solutions section, firstly for question 6 to find sachin’s average score for each quartile of India’s score. I was able to code a faster solution as below without using specific indices and reshaping.

Syntax

india_data = cric_data[:, 2]
Q1 = np.percentile(india_data, 25)
Q2 = np.percentile(india_data, 50)
Q3 = np.percentile(india_data, 75)
Q4 = np.percentile(india_data, 100)
np.mean(sachin_data[india_data<Q1])
np.mean(sachin_data[india_data<Q2])
np.mean(sachin_data[india_data<Q3]
np.mean(sachin_data[india_data<Q4])

The logic was simple here and we made use of broadcasting and it actually worked without reshaping arrays. This becomes much more useful in problem 8, where the solution is a 2 liner.

Problem 8

To find average of sachin’s score after he scores a given x

Syntax

def sachin_avg(x):
    return np.mean(sachin_data[sachin_data>=x]) - x

This can return value for any x entered. Request your thoughts, suggestions and explanation.

1 Like

Hi @Karthik_Pillai,
The solution seems valid and legitimate.
But suppose if we have more such partitions (instead of quartile) there will be more lines of code required.
Remember that simplicity is writing compact code.

Hi Ishvinder,

How about the code for problem 8?

The one I have suggested is fairly shorter and can be applicable for any “x” and not just for values which for a given dataset.

Yes, for Problem 8 it looks good.
The above comment was in reference to Problem 6. :slight_smile:

Thanks for your feedback!