-2

Consider the following code:

import numpy as np
np.random.seed(0)

x = np.random.randint(0,9,(10,10,3))
x[np.array([0,1]):np.array([5,6])]

I get the following error:

TypeError: only integer scalar arrays can be converted to a scalar index

I guess I would need to unpack the arrays that index x but can't figure out a way to do it since * doesn't work.

The goal is to have x[0:5] and x[1:6]

EDIT:

Due to requests I'm posting here a solution for the problem I was looking for:

import numpy as np
np.random.seed(0)

x = np.random.randint(0,9,(10,10,3))
arr1,arr2 = np.array([0,1]),np.array([5,6])

slices = []
for i,j in zip(arr1,arr2):
    slices.append(x[i:j])

print(slices)

This way I can have both slices as requested i.e. x[0:5] and x[1:6]. If anyone has a method on how to approach this problem without a for loop it would be very helpful. Thanks.

moth
  • 1,833
  • 12
  • 29
  • 1
    I have trouble to understand what you want to achieve. Can you add the result you would like to get? Should your slice `[[0,1]:[0,1]]` output a list with only the element at index `[0,1]`? – André Apr 12 '22 at 08:39
  • each element of the numpy array, that is np.array(x,y,z) should be unpacked to make a range. so first unpacking will be x[0:5] and second x[1:6] – moth Apr 12 '22 at 08:43
  • 1
    It is not clear. please prepare a smaller case with the expected output and explain more (may step by step) how to achieve this. The explained process is confusing for me. – Ali_Sh Apr 12 '22 at 09:46

3 Answers3

1

here is the another one,

import numpy as np
np.random.seed(0)

x = np.random.randint(0,9,(10,10,3))
[x[i:j] for i,j in zip(np.array([0,1]),np.array([5,6]))]
0

if I have understood the question right , you should not use ":" in x[np.array([0,1]):np.array([0,1])]

use "," instead x[np.array([0,1]),np.array([0,1])]

0

You could generate an index array similar to this answer.

import numpy as np

x = np.random.randint(0,9,(10,10,3))
idcs = np.array([
    np.arange(0,5),
    np.arange(1,6),
])

print(x.shape)
# (10, 10, 3)
print(x[idcs].shape)
# (2, 5, 10, 3)
André
  • 1,034
  • 9
  • 19
  • hum not really shape must remain the same. I really need to extract a single integer from the array one at a time from the array in order to get the slice. – moth Apr 12 '22 at 08:57
  • moth, what shape do you want? The link suggests several possibilities. You can't do this with just one slice. – hpaulj Apr 12 '22 at 10:53
  • if one loop for every element of the array it's possible to keep the shape `(10,10,3)` – moth Apr 12 '22 at 12:17
  • 1
    2*5 is 10. Just use `reshape`. Or `np.hstack` to create `idcs`. – hpaulj Apr 12 '22 at 14:20
  • @moth For me it's not clear what assumptions you have on the slices. For an arbitrary set of slices, it is generally not the case that the set of sliced `x` can come together to the same shape as `x`. Take e.g. the slices `[0:5], [1:6], [2:7]`, which will give you 15 elements along the first axis. I'm sorry but I really don't get the use-case of this and the implications to a potential solution. From how I understand the question, I don't understand how to improve this answer, since it, IMO, solves the task. – André Apr 12 '22 at 14:45