Reputation:
I want to divide an array, which has 1000 data points into the bins of 100 data points each. Then, I want to calculate the mean of these bins separately.
Can someone suggest how to divide the data which is in a numpy array into the bins? This is what I've tried.
import numpy as np
f = np.random.random((1000))
bin_1 = f[0:100]
mean_1= bin_1.mean()
print(bin_1, mean_1)
Upvotes: 1
Views: 3087
Reputation: 1059
If your list has always a constant size of 1000 data points, you can simply write a for loop from 0 to 1000. This loop will increment the main index of 100 at every iteration. You can compute the mean result of a your list[i-100:i]
.
For instance:
f = np.loadtxt('ising_T2.dat',usecols=(0))
for (i = 100; i < 1000; i += 100) {
// mean = f[i - 100:i].mean()
}
Something like this. The trick is that you can crop a list using variables.
Upvotes: 0
Reputation: 1691
import numpy as np
f = np.loadtxt('ising_T2.dat',usecols=(0))
chunk_begin, chunk_end = 0, 100
splitted_bins = []
splitted_means = []
for _ in range(9):
bin = f[chunk_begin:chunk_end]
mean = bin.mean()
splitted_bins.append(bin)
splitted_means.append(mean)
chunk_begin, chunk_end = chunk_begin + 100, chunk_end + 100
print(splitted_bins, splitted_means)
Not sure if that is what you want
Upvotes: 2