2

I want to compare two variables input_items and temp for equality. To give you an idea of their datatype -

print input_items

prints -

[array([ 50., 1., 0., ..., 0., 0., 0.], dtype=float32), array([ 50., -2., 0., ..., 0., 0., 0.], dtype=float32)]

What's the best way to do that in Python?

0

2 Answers 2

4

I suppose that allclose good for your case because you need to compare floats

import numpy as np
a = np.arange(10)
print a
#array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
b = np.arange(10)
print b
#array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print np.allclose(a, b)
#True
b[1] = 10    
#array([ 0, 10,  2,  3,  4,  5,  6,  7,  8,  9])
print  np.allclose(a, b)
#False

To compare lists of arrays you can combine np.allclose with all

a = [np.array([1, 2, 3]), np.array([1, 2, 3])]
b = [np.array([1, 2, 3]), np.array([1, 2, 3])]
all([np.allclose(x, y) for x, y in zip(a, b)])#True
b = [np.array([1, 2, 3]), np.array([1, 2, 4])]
all([np.allclose(x, y) for x, y in zip(a, b)])#False

PS Sorry for my poor English

Sign up to request clarification or add additional context in comments.

Comments

2

As already suggested you should build 2-D arrays and use numpy.allclose.

import numpy
#lists of arrays
yourarray1 = [numpy.random.random(5) for i in range(3)]
yourarray2 = [numpy.random.random(5) for i in range(3)]

#2-D arrays with list elements as rows
nw2Darray1 = numpy.array(yourarray1).reshape((3,5))
nw2Darray2 = numpy.array(yourarray2).reshape((3,5))

numpy.allclose(nw2Darray1,nw2Darray2) #returns True/False

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.