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!
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.
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.
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.
This is pretty simple with numpy
, just subtract the arrays:
diffs = array1 - array2
I get:
diffs == array([ 0.1, 0.2, 0.3])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With