Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

python - Iterating a numpy array in chunks

How can I arrange list_ into groups of numbers using the numpy.where() or the numpy.select(). I want to break the data into groups of 3 and then calculate the standard deviation std() of those functions. So the program will take in 457.334015,424.440002,394.795990 for the first values of standard dev to be calculated and the will take 424.440002,394.795990, 408.903992 and calculate the standard deviation etc. It will keep on going like this till the end it reaches the end of the list. I want the first chunk 457.334015,424.440002,394.795990 to be deleted before it calculates the second chunk 424.440002,394.795990, 408.903992. I want to delete chunks from the memory so i do not have a memory error. Would this be possible with numpy and without using a for loop.

number = 3
list_= np.array([457.334015,424.440002,394.795990,408.903992,398.821014,402.152008,435.790985,423.204987,411.574005,
404.424988,399.519989,377.181000,375.467010,386.944000,383.614990,375.071991,359.511993,328.865997,
320.510010,330.079010,336.187012,352.940002,365.026001,361.562012,362.299011,378.549011,390.414001,
400.869995,394.773010,382.556000])
question from:https://stackoverflow.com/questions/65837362/iterating-a-numpy-array-in-chunks

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From this post:

from numpy.lib.stride_tricks import as_strided
def strided_app(a, L, S ):  # Window len = L, Stride len/stepsize = S
    nrows = ((a.size-L)//S)+1
    n = a.strides[0]
    return np.lib.stride_tricks.as_strided(a, shape=(nrows,L), strides=(S*n,n))
list_= np.array([457.334015,424.440002,394.795990,408.903992,398.821014,402.152008,435.790985,423.204987,411.574005,
404.424988,399.519989,377.181000,375.467010,386.944000,383.614990,375.071991,359.511993,328.865997,
320.510010,330.079010,336.187012,352.940002,365.026001,361.562012,362.299011,378.549011,390.414001,
400.869995,394.773010,382.556000])
np.std(strided_app(list_, 3, 1), axis=1)

However, this code does not delete any elements from the array. Also, keep in mind that the function used here comes with a warning from the numpy docs!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...