1

I have been trying to copy the individual elements from one 2D array to another. My code is as follows:

tp_matrix = np.array(tp_matrix)
my_array = np.empty(shape = (tp_matrix.shape))

for x in range(tp_matrix.shape[0]):
    for y in range(tp_matrix.shape[1]):
        my_array[x][y] = tp_matrix[x][y]


if(np.array_equal(my_array, tp_matrix)):
    print('Equal')
else:
    print('Not equal')

However the two arrays are not equal for some reason. What is the problem here and what can I do to solve it?

I cannot use numpy's copy function as I want to make modifications later to some of the elements from my_array later with the other values being the same as that of my_matrix.

Edit: On running the code I get the following message: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison Does this mean there is something wrong with the dataset (tp_matrix)?

Edit 2: I have tried the allclose and isclose functions but I get this error: TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'' The data is stored as floats. Also it is a bit large (399 x 5825).

Edit 3: Solved. I had to reinstall python.

6
  • 2
    Are the elements floats? Comparing floats can have such effects. Try docs.scipy.org/doc/numpy-1.10.0/reference/generated/…. In general, one should never test floats for equality. Commented Apr 24, 2016 at 19:47
  • Try ´numpy.allclose()´ to give a tolerance to your equality sentence. Also if you give some more information about your data (perhaps an example) and what you intend to do with it there might be solutions to create a "best copy". Commented Apr 24, 2016 at 19:50
  • I get "Equal" when I run your code, using a list of list of integers and floats as the tp_matrix. What is tp_matrix you are using? Commented Apr 24, 2016 at 19:50
  • Your code works for me. Can you provide some of your data? Commented Apr 24, 2016 at 19:50
  • The problems with this code are too many to list really; try googling a numpy primer. Commented Apr 24, 2016 at 19:57

2 Answers 2

3

Use np.allclose to test the (almost) equality of float arrays, because of the way float numbers are represented in a computer.

For more details, you could read for instance "What Every Computer Scientist Should Know About Floating-Point Arithmetic"

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

2 Comments

what is my_array.dtype ?
Try my_array = my_array.astype(np.float64) and same for tp_matrix
0

I tried to mimic what you are experiencing and did the following:

one = np.array([1,2,3])
two = np.array([one,one,one])
three = np.empty(shape=(two.shape))
for x in range(two.shape[0]):
    for y in range(two.shape[1]):
        three[x][y] = two[x][y]

Printing the contents of 'two' and 'three' gives the following result

print(three)
array([[ 1.,  2.,  3.],
       [ 1.,  2.,  3.],
       [ 1.,  2.,  3.]])
print(two)
array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])

Although for this small example numpy returns True if I test equality using np.array_equal, it is possible that rounding errors cause the test to be False in your case.

A workaround for this could be the following test:

sum(sum(two==three)) == two.shape[0]*three.shape[1]

Although there are probably more efficient ways.

Comments

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.