Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between two numpy arrays in python

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!

like image 504
user3263816 Avatar asked Feb 02 '14 20:02

user3263816


People also ask

How do you find the difference between two NumPy arrays?

Step 1: Import numpy. Step 2: Define two numpy arrays. Step 3: Find the set difference between these arrays using the setdiff1d() function. Step 4: Print the output.

How do you subtract two NumPy arrays in Python?

Subtracting two matrices in NumPy is a pretty common task to perform. The most straightforward way to subtract two matrices in NumPy is by using the - operator, which is the simplification of the np. subtract() method - NumPy specific method designed for subtracting arrays and other array-like objects such as matrices.

What is the difference between Python arrays and NumPy arrays?

Numpy arrays is a typed array, the array in memory stores a homogenous, densely packed numbers. Python list is a heterogeneous list, the list in memory stores references to objects rather than the number themselves.


1 Answers

This is pretty simple with numpy, just subtract the arrays:

diffs = array1 - array2 

I get:

diffs == array([ 0.1,  0.2,  0.3]) 
like image 163
jonrsharpe Avatar answered Sep 24 '22 18:09

jonrsharpe