Reputation: 63
np.array([3, 2, 3]).T == np.array([[3],[2],[1]])
outputs:
[[ True False True]
[False True False]
[False False False]]
Why isn't this equal and what does this output mean?
Upvotes: 0
Views: 120
Reputation:
So you have two arrays: np.array([3, 2, 3]).T
(which is identical to the non-tranposed version: np.array([3, 2, 3])
), and np.array([[3],[2],[1]])
. Let's look at each one:
>>> a = np.array([3, 2, 3])
>>> a
array([3, 2, 3])
>>> b = np.array([[3],[2],[1]])
>>> b
array([[3],
[2],
[1]])
Note how the first (a
) is 1D, while the second (b
) is 2D. Since they have different dimensions, trying to compare them will do what's called "numpy broadcasting", and it's a really cool feature.
To break it down:
>>> a == b
array([[ True, False, True],
[False, True, False],
[False, False, False]])
Basically what the does, is for every item E
in b
, it checks if all the items in a
are equal to E
. To prove that:
>>> a == b[0]
array([ True, False, True])
>>> a == b[1]
array([False, True, False])
>>> a == b[2]
array([False, False, False])
Notice how the above arrays are identical to the whole array made by a == b
. That's because a == b
is a short, efficient form of doing the above.
Upvotes: 2