I don't know why the syntax is different when I take out a part of the list.

Asked 2 years ago, Updated 2 years ago, 51 views

I wrote the following code:

dy[0][0:1] failed to retrieve the first number in the list, so dy_sum can be shaped once and dy_sum[0:1] can retrieve the first number in the list, but for some reason dx[0][0:1] must be .

I'm in trouble because I don't know the reason.Could someone explain it to me?

import numpy as np

D,N = 8,7
x = np.random.randn(1,D)
y=np.repeat(x,N,axis=0)

dy=np.random.randn(N,D)
dx=np.sum (dy,axis=0, keepdims=True)

dy_sum=np.sum(dy,axis=0)

print(dy_sum[0:1])
print(dx[0][0:1])

python python3 numpy

2022-09-30 17:40

1 Answers

keepdims=True, so the dimensions of dx (ndim) are 2.
If keepdims=False or keepdims is not specified, dx[0] can retrieve the first value.

dx[0] was taken out when ndim set 2→False to 1.

This post is a comment from @ladleDear @yokomizo-groove,I posted it as a community wiki based on the comments in .


2022-09-30 17:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.