0 votes
1 view
in Python by (20.3k points)

I have two arrays, for example:

array1=numpy.array([1.1, 2.2, 3.3])

array2=numpy.array([1, 2, 3])

How can I find the difference between these two arrays in Python, to give:

[0.1, 0.2, 0.3]

As an array as well?

Sorry if this is an amateur question - but any help would be greatly appreciated!

2 Answers

0 votes
by (40.3k points)

This is actually very simple with numpy, You just have to subtract the arrays like this:

diffs = array1 - array2

And You'll get this:

diffs == array([ 0.1,  0.2,  0.3])

0 votes
by (106k points)

To differentiate between two numpy arrays you can also use numpy.subtract

It has the advantage over the difference operator, -, that you do not have to transform the sequences list or tuples into a numpy arrays you save the two commands:

array1 = np.array([1.1, 2.2, 3.3]) 

array2 = np.array([1, 2, 3])


import numpy as np 

result = np.subtract([1.1, 2.2, 3.3], [1, 2, 3]) 

print ('the difference =', result)

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers !