0

I have two 3D Numpy array

win_combination = np.array([
[[0, 0], [0, 1], [0, 2]],
[[1, 0], [1, 1], [1, 2]],
[[2, 0], [2, 1], [2, 2]],
[[0, 0], [1, 0], [2, 0]],
[[0, 1], [1, 1], [2, 1]],
[[0, 2], [1, 2], [2, 2]],
[[0, 0], [1, 1], [2, 2]],
[[0, 2], [1, 1], [2, 0]]
])

and,

game_log = np.array([[1 1],[0 2],[1 0]])

I would like to compare if the game_log data matches any of the arrays in win_combination if it matches it may print True else print False.

Basically I want that if game_log == [[0, 0],[0, 1],[0, 2]] it may print True if it isn't then my code should compare another array game_log == [[1, 0], [1, 1], [1, 2]] if not then #another and so on till last and print false if there is no array that matches game_log and in this case it should print False

I have tried,

for comb in win_combination:
    if game_log == comb:
        print(True)
    else:
        print(False)
3
  • What happened when you tried it? What is your actual question? Commented Apr 7, 2020 at 18:51
  • You don't have two 3D arrays. You have a 2D and a 3D Commented Apr 7, 2020 at 18:51
  • Unindent the else. You don't necessarily want to return False if the first element is not matching. Commented Apr 7, 2020 at 19:04

3 Answers 3

1

Two arrays are equal if np.all(x == y) (or alternatively (x == y).all()). You have a 3D array of shape (N, A, B) and a 2D array of shape (A, B). In this case, N=8, A=3 and B=2. These shapes broadcast to the shape of the 3D array. If you apply np.all to the last two dimensions, you can get N True or False values for each of the combinations. np.any will then tell you if any of them are True.

all has an axis keyword. As of numpy 1.7.0, axis can specify more than one axis simultaneously, so you can do:

matches = np.all(win_combination == game_log, axis=(-1, -2))

For older versions of numpy, you would have to reshape to get a single axis, or apply all twice:

matches = np.all((win_combination == game_log).reshape(win_comination.shape[0], -1), axis=-1)

OR

matches = np.all(np.all(win_combination == game_log, axis=-1), axis=-1)

The final result is matches.any() or np.any(matches). You can write it as a one-liner:

np.all(win_combination == game_log, axis=(-1, -2)).any()
Sign up to request clarification or add additional context in comments.

Comments

0

IIUC:

ans = np.any(np.all(game_log == win_combination, axis=(1,2)))
print(ans)

4 Comments

You don't need to replicate game_log: broadcasting does it for you since the dimensions line up on the right. You don't need size either. Your entire solution is just the third line, but with game_log instead of games.
Here's a link you may find interesting: docs.scipy.org/doc/numpy/user/basics.broadcasting.html. It's what makes so much of the magic in numpy possible.
Thanks for the feedback! I'm not that good with broadcasting, maybe this link helps me @MadPhysicist
Here's another one that helped me visualize it better: docs.scipy.org/doc/numpy/user/theory.broadcasting.html. It's not a simple subject, but you get used to it after a while. The basic idea is to line up the shapes on the right.
0

Try this method:

   def in_array(game_log, array):
        for comb in array:
            for arr in comb:
                if game_log.all() == arr.all():
                    return True
        return False

Pass the game_log to the first argument and the 3D to the next one, based on what i understand it should print True if the combination is in the array and False otherwise.

Keep in mind that the truth value of an array with more than one element is ambiguous. So its best to use all() if both arrays are exact matches and any() if any element in one array exists in the other.

2 Comments

You should really return values instead of printing them. You can always print a return value, but you can't recover what you've printed without returning it.
Thanks for fixing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.