Home > Software engineering >  Sequentially take a window of rows from array (python)
Sequentially take a window of rows from array (python)

Time:02-11

I have an array of size nxm and want to take the first 10 rows and perform calculations, then take the next 10 rows perform calculations, etc. But this is hard coded, how can I make a loop?

Code Attempted:

import numpy as np
total = []
x= np.random.random((100,4))
a = np.average(x[:10])
total.append(a)
a = np.average(x[10:20])
total.append(a)
a = np.average(x[20:30])
....

Goal:

for *this array*:
# to something
# append value
# go back and get next 10 values

CodePudding user response:

import numpy as np
x = np.random.random((100,4))
a = 10
b = 100//a
c = 4

You want the array of average numbers of the first 10 * 4 part, the second 10 * 4 part,..., right?

reshape function can be really useful here.

x_splited = x.reshape((-1, a*c))
total = x_splited.mean(axis=1)

This is the answer you need. The reshape function let the first a*c elements in the original matrix become the first row of the new matrix. Then, mean(axis=1) help you get the average of the first row.

Also, you could try something like this:

 x_splited = x.reshape((-1, a, c))

You can do something more complicated than this question with it.

Just a tip: in python, it is prefered to avoid using loop because it is slow.

Second tip: if you are still not proficient in using loop in Python, you are encouraged to spend some time to practice it.

CodePudding user response:

It looks like you want the following.

import numpy as np
x = np.random.random((100,4))
L = 10
k = 100//L
total = [np.average(x[L*i:L*(i 1)]) for i in range(k)]

If you'd rather implement this using a loop rather than list comprehension,

import numpy as np
x = np.random.random((100,4))
L = 10
k = 100//L
total = []
for i in range(k):
    total.append(np.average(x[L*i:L*(i 1)]))

As an alternative, here's an approach using a 3-dimensional reshape.

x= np.random.random((100,4))

L = 10 #window length
n = x.shape[1] #number of columns
total = a.reshape(-1,10,4).mean(axis = (1,2))
  • Related