0

I have two Numpy matrices(N*N), I want to subtract them:

Matrix a (For simplicity i just posted here a[0]):

[ 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.]

Matrix l1 (For simplicity i just posted here l1[0]):

[ 0.99999327  0.07987602  0.03962965  0.99967095  0.12899137  0.00232801
  0.9353088   0.90658779  0.99650294  0.99992827]

With this code I want to subtract them:

l1_error = a[0]-l1[0]

but i get some strange numbers as result:

[  6.72779132e-06  -7.98760205e-02  -3.96296547e-02  -9.99670951e-01
  -1.28991369e-01  -2.32801220e-03  -9.35308795e-01  -9.06587790e-01
  -9.96502936e-01  -9.99928265e-01]

Why? subtracting 1.0 with 0.99999327 should give 6.72779132e-06 ? I checked data type of both and both of them were float64 and ndarray

And one thing i didn't understand. When i create a random matrix, with this code:

2*np.random.random((3,1))-1

if it has one column, i get in this format:

[[-0.16595599]
 [ 0.44064899]
 [-0.99977125]]

But if it has several columns i get in this format:

[[-0.16595599  0.44064899 -0.99977125 -0.30887855 -0.20646505
   0.07763347]
 [-0.16161097  0.370439   -0.5910955  0.11737966 -0.71922612
  -0.60379702]
 [ 0.60148914  0.93652315 -0.37315164 -0.92189043 -0.66033916
   0.75628501]]

Which there is no semicolons between items or brackets. What i'm missing here? this is related to my main problem?

Fcoder
  • 9,066
  • 17
  • 63
  • 100
  • One question at a time better. Concerning the first one, I don't have clear what are the differences between your result and what you expected. Is your question related to the number of decimals shown? – Jalo Nov 22 '16 at 09:00
  • Why is that result strange? If a[0] is a list of floats and you subtract another list of floats, why do you not expect a list of floats back? Where are you printing your matrix, commas and semicolons are omitted everywhere. That's not a problem though, only a visual representation I don't see in my console. – Sven Nov 22 '16 at 09:02
  • @Jalo: In fact the two questions are one, because i think related to each other. i want to do element-wise subtracting. for example subtracting 1.0-0.99999327 should give 0.00000673 as result but right now i get 6.72779132e-06. why? – Fcoder Nov 22 '16 at 09:04
  • @Sven: subtracting 1.0 - 0.99999327 should give 0.00000673 as result but right now i get 6.72779132e-06. why? – Fcoder Nov 22 '16 at 09:07
  • 3
    Bro the result is right just notations are different. what is 0.01 in terms of power? In fact beyond 0.0001 python will give answer in terms of power notation try it in python. 0.00001 = 1e-05 – Tanmaya Meher Nov 22 '16 at 09:08
  • 1
    @Fcoder This has to do with the floating point arithmetic, which is based on base 2 operations – Jalo Nov 22 '16 at 09:09
  • Possible duplicate of [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – juanpa.arrivillaga Nov 22 '16 at 09:12
  • @TanmayaMeher: Im doing Neural Network test, i need 0.00000673 for updating my weights and 6.72779132e-06 as result will broke all of my calculation – Fcoder Nov 22 '16 at 09:14
  • then you need to round up to a significant digit after decimal point? right? – Tanmaya Meher Nov 22 '16 at 09:15
  • @TanmayaMeher: Yes – Fcoder Nov 22 '16 at 09:16
  • @Fcoder use np.round(l1_error, 8) for an array with your desired number of decimals – Jalo Nov 22 '16 at 09:18
  • 1
    Then this might help. Don't worry about the power notation its rounded up. `round(1.0 - 0.99999327,8)` ; which will show as 6.73e-06. you can verify it by subtracting the answer from 1.0 . it will print 0.99999327 – Tanmaya Meher Nov 22 '16 at 09:20
  • @TanmayaMeher: But print round(1.0 - 0.99999327,8) gives 6.73e-06 as result. – Fcoder Nov 22 '16 at 09:32
  • @Jalo: i get the same result. there is no difference with np.round(l1_error, 8) – Fcoder Nov 22 '16 at 09:33
  • @TanmayaMeher: oh, i get it. as you say, i have to subtract result from 1.0. thank you. This is my hello world, i'm confused – Fcoder Nov 22 '16 at 10:02
  • I think you are telling something satiric or making a pun ;). well i didn't said it like that. I said it as a **verification** in case you doubt or so. I said **don't worry of notation** it will print 6.73e-06 when you do `round(1.0 - 0.99999327,8)`. For verification, use `1.0-6.73e-06` to find out the result as 0.99999327. That's just i am telling. Nothing to be confused. :) – Tanmaya Meher Nov 22 '16 at 10:31

0 Answers0