I have the following numpy arrays:
# doors = [[0,1,2],[0,1,2],....,[0,1,2]
doors = np.broadcast_to(np.array([0, 1, 2]),(max_samples, 3))
# first_choice = [ [2],[1],....,[2] ]
first_choice = np.random.randint(3, size=(max_samples, 1))
Without using any kind of loops (for, while, etc.) i would like to create a new numpy array called result who contains doors with suppressed first choice.
Example:
first_choice = [ [2], [1], [1], [0] ....]
result = [ [0,1],[0,2],[0,2],[1,2] .....]
How can i do that ? Use of any libraries if accepted if necessary, i think numpy can do that request but don't know how.
1 Answer 1
Here is one possibility:
import numpy as np
max_samples = 10
np.random.seed(100)
doors = np.broadcast_to(np.array([0, 1, 2]),(max_samples, 3))
first_choice = np.random.randint(3, size=(max_samples, 1))
print(first_choice[:, 0])
# [0 0 0 2 2 0 2 1 2 2]
# Take non-matches and reshape
result = doors[doors != first_choice].reshape((-1, 2))
print(result)
# [[1 2]
# [1 2]
# [1 2]
# [0 1]
# [0 1]
# [1 2]
# [0 1]
# [0 2]
# [0 1]
# [0 1]]
Obviously for this to work each value in first_choice must be present once (and only once) in each corresponding row of doors.
answered Nov 27, 2018 at 12:07
javidcf
59.9k7 gold badges87 silver badges134 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py