简体   繁体   中英

Find the decades with the mean, and minimum mean from an array

I have an array of length (324) and I split it into decades as follows

Array = np.random.randint(1,10,324)
D1 = Array[:60]
D2 = Array[61:120]
D3 = Array[121:180]

I then find the mean of each decade as and complete array follows:

m_D1 = np.mean(D1) = 5
m_D2 = np.mean(D2) = 3
m_D3 = np.mean(D3) = 8 
m_array = np.mean(Array) = 6

Now question is 1. how do I programmatically find the decade with the mean value closest to the mean of the whole array? 2. how to I find the decade with the lowest mean? in the case above:

m_D1 is the closet to m_array and m_D2 is the decade with the lowest mean.

Thanks in advance.

  1. Closest to whole array mean could be achieved by taking the absolute value of the difference between each of the decade means and the overall mean and seeing which is smallest - this can be achieved with abs() .

  2. Minimum mean can be achieved by utilising the min() function to find the minimum mean of each fo the decades.

If you want to keep association with the actual decades to reference, you could do this by storing the means in a dictionary and using the decade numbers as the keys, so there is an association between a decade number and the mean value, and then you can iteratively determine your answers while retaining a reference to the decade.

Array = np.random.randint(1,10,324)
D1 = Array[:60]
D2 = Array[61:120]
D3 = Array[121:180]
decades_mean = np.array([np.mean(D1), np.mean(D2), np.mean(D3)])
array_mean = np.mean(Array)
closest_mean = decades_mean[np.argmin(np.abs(decades_mean - array_mean))] # for Q1
lowest_mean = min(decades_mean) # for Q2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM