Rob
Rob

Reputation: 3459

Why aren't the numpy matrices equal?

Why does this return False?

temp = np.array([[1,2,3],[4,5,6], [7,8,9]])

filter_indices = range(3)[1:2]
filter_indices2 = range(3)[1:]

print np.array_equal(temp[1:2,1:], temp[filter_indices, filter_indices2])

but these are equal:

np.array_equal(temp[1:2,1:], temp[filter_indices, 1:])

It looks like they'd be the same to me, but it seems like this second array is filtering differently than the first.

Upvotes: 1

Views: 60

Answers (1)

wflynny
wflynny

Reputation: 18521

Ultimately it is the product of your first slice, as there is a difference between indexing with slice(1, 2) and [1]. By indexing with 1:2, you produce

> temp[1:2]
array([[4, 5, 6]])

which has shape (1, 3), i.e. a row vector (1 row, 3 cols). Using filter_indices, you are producing essentially

> temp[1] #equivalent to temp[filter_indices]
array([4, 5, 6])

with shape (3,). To get the same behavior, index with

> temp[[1]] #equivalent to temp[[filter_indices]]
array([[4, 5, 6]])

Upvotes: 2

Related Questions