Python Forum
numpy subtraction of two arrays
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
numpy subtraction of two arrays
#1
Hi there

So I want to subtract two numpy arrays a and b:

a=[[ 1. 0.85979163 0. 0.11766047 0.19353699]
[ 0.8589698 1. 0.24111901 0. 0. ]
[ 0. 0.24554123 1. 0.09234979 0.07125199]
[ 0.31269982 0.22558714 0.29298401 1. 0.475543 ]
[ 0.18880995 0. 0.06580817 0.32276821 1. ]]

b=[[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]]

When I use the following command:
y=numpy.subtract(b,a)
I get an output array:
y= [[ -2.22044605e-16 1.40208370e-01 1.00000000e+00 8.82339528e-01
8.06463005e-01]
[ 1.41030195e-01 0.00000000e+00 7.58880995e-01 1.00000000e+00
1.00000000e+00]
[ 1.00000000e+00 7.54458767e-01 -2.22044605e-16 9.07650211e-01
9.28748013e-01]
[ 6.87300178e-01 7.74412855e-01 7.07015986e-01 0.00000000e+00
5.24457002e-01]
[ 8.11190053e-01 1.00000000e+00 9.34191829e-01 6.77231787e-01
0.00000000e+00]]


And it's really confusing me as to how it's outputting these values.

When I tried to troubleshoot and do:
y=b[0,0]-a[0,0]

I got y=0.. which makes sense because i would be subtracting 1-1. But in the output array I'm getting a value of -2.22044605e-16 instead.

Do you have any idea as to why this might be happening and what I can do to fix it?
Reply


Messages In This Thread
numpy subtraction of two arrays - by devenuro - Sep-24-2018, 12:30 AM
RE: numpy subtraction of two arrays - by Mekire - Sep-24-2018, 12:55 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Numpy] How to store different data type in one numpy array? water 7 288 Mar-26-2024, 02:18 PM
Last Post: snippsat
  Pandas dataframes and numpy arrays bytecrunch 1 1,292 Oct-11-2022, 08:08 PM
Last Post: Larz60+
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,528 Jul-19-2022, 06:31 AM
Last Post: paul18fr
  comparing floating point arrays to arrays of integers in Numpy amjass12 0 1,613 Jul-26-2021, 11:58 AM
Last Post: amjass12
  list subtraction d8a988 3 2,113 Oct-23-2020, 04:49 PM
Last Post: Gribouillis
  Corrupted numpy arrays when save to file. DreamingInsanity 2 3,183 Dec-14-2019, 12:12 PM
Last Post: DreamingInsanity
  Numpy arrays and compatability with Fortran arrays merrittr 0 1,849 Sep-03-2019, 03:54 AM
Last Post: merrittr
  Converting set of tuples to set of 2D numpy arrays ThemePark 0 2,566 Aug-04-2019, 05:07 PM
Last Post: ThemePark
  "erlarge" a numpy-matrix to numpy-array PhysChem 2 2,926 Apr-09-2019, 04:54 PM
Last Post: PhysChem
  How to do matrix subtraction SriRajesh 0 2,686 Jun-01-2018, 02:00 PM
Last Post: SriRajesh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020