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
149 views
in Technique[技术] by (71.8m points)

python - How to broadcast np.take_along_axis?

My objective is to sort Y first (increasing, from 0 to 3), then sort the X according to the sorted Y. Note that X and Y are one to one.

Y = np.random.randint(0, 4, (2, 10,))
print("before Y", Y.shape) # before Y (2, 10)
print(Y)
print()

idx = np.argsort(Y, axis=1)

Y_ordered = np.take_along_axis(Y, idx, axis=1)
print("reordered Y", Y_ordered.shape) # reordered Y (2, 10)
print(Y_ordered)
print()

### 

X = np.random.rand(2, 10, 64, 64)

X_ordered = np.take_along_axis(X, idx, axis=1) # ValueError: `indices` and `arr` must have the same number of dimensions
print("reordered X", X_ordered.shape)
print(X_ordered)
print()

I got the Y_ordered that I want, but how to broadcast the same idx on another array X with different shape, but have the same size in the first 2 dimensions.

question from:https://stackoverflow.com/questions/66062344/how-to-broadcast-np-take-along-axis

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

1 Answer

0 votes
by (71.8m points)
Waitting for answers

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

...