OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Indexing numpy array of shape `(A, B, C)` with `[[a, b], [c, d], :]` (`0 <= a, b < A`, `0 <= c, d < B`) produces shape `(2, C)` instead of `(2, 2, C)`

  • Thread starter Thread starter bzm3r
  • Start date Start date
B

bzm3r

Guest
Here's the example:

Code:
import numpy as np

A = np.random.randint(100)
B = np.random.randint(100)
C = np.random.randint(100)
print(f"{A=}, {B=}, {C=}")

x = np.random.random((A, B, C))
print(f"{x.shape=}")

a = np.random.randint(0, A)
b = np.random.randint(0, A)
print(f"{a=}, {b=}")

c = np.random.randint(0, B)
d = np.random.randint(0, B)
print(f"{c=}, {d=}")

print(f"{x[[a, b], [c, d], :].shape=}")
print(f"{x[[a, b]][:, [c, d]].shape=}")

Code:
A=7, B=40, C=57
x.shape=(7, 40, 57)
a=4, b=1
c=10, d=5
x[[a, b], [c, d], :].shape=(2, 57)
x[[a, b]][:, [c, d]].shape=(2, 2, 57)

I would have expected indexing with [[a, b], [c, d], :] to produce a shape (2, 2, C)?
<p>Here's the example:</p>
<pre class="lang-py prettyprint-override"><code>import numpy as np

A = np.random.randint(100)
B = np.random.randint(100)
C = np.random.randint(100)
print(f"{A=}, {B=}, {C=}")

x = np.random.random((A, B, C))
print(f"{x.shape=}")

a = np.random.randint(0, A)
b = np.random.randint(0, A)
print(f"{a=}, {b=}")

c = np.random.randint(0, B)
d = np.random.randint(0, B)
print(f"{c=}, {d=}")

print(f"{x[[a, b], [c, d], :].shape=}")
print(f"{x[[a, b]][:, [c, d]].shape=}")
</code></pre>
<pre><code>A=7, B=40, C=57
x.shape=(7, 40, 57)
a=4, b=1
c=10, d=5
x[[a, b], [c, d], :].shape=(2, 57)
x[[a, b]][:, [c, d]].shape=(2, 2, 57)
</code></pre>
<p>I would have expected indexing with <code>[[a, b], [c, d], :]</code> to produce a shape <code>(2, 2, C)</code>?</p>
 

Latest posts

J
Replies
0
Views
1
jbowerbir
J
Top