python - Selecting multiple slices from a numpy array at once -


i'm looking way select multiple slices numpy array @ once. have 1d data array , want extract 3 portions of below:

data_extractions = []  start_index in range(0, 3):     data_extractions.append(data[start_index: start_index + 5]) 

afterwards data_extractions be:

data_extractions = [     data[0:5],     data[1:6],     data[2:7] ] 

is there way perform above operation without loop? sort of indexing scheme in numpy let me select multiple slices array , return them many arrays, in n+1 dimensional array?


i thought maybe can replicate data , select span each row, code below throws indexerror

replicated_data = np.vstack([data] * 3) data_extractions = replicated_data[[range(3)], [slice(0, 5), slice(1, 6), slice(2, 7)] 

you can use the indexes select rows want appropriate shape. example:

 data = np.random.normal(size=(100,2,2,2))   # creating array of row-indexes  indexes = np.array([np.arange(0,5), np.arange(1,6), np.arange(2,7)])  # data[indexes] return element of shape (3,5,2,2,2). converting  # list happens along axis 0  data_extractions = list(data[indexes])   np.all(data_extractions[1] == s[1:6])  true 

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -