1

I have a list, I would like to find the min. values in each row and do calculations: row - row.min - 1.

This is what I tried

import numpy as np

list = [[1.2886089553007253e-15, 3.283665029781338e-16, 0.0, 3.4027301260438933e-16],\
        [3.047580716284324e-15, 1.3787915767152193e-15, 3.505982818951592e-16, 0.0]]

array = np.asarray(list)

result = array-array.min(axis=0)-1

print(result)

This is the result I got,

[[-1. -1. -1. -1.]
 [-1. -1. -1. -1.]]

But I hope to get

[[1.2886089553007253e-15 -0.0-1, 3.283665029781338e-16 -0.0-1, 0.0 -0.0-1, 3.4027301260438933e-16 -0.0-1], 
[3.047580716284324e-15 -0.0-1, 1.3787915767152193e-15 -0.0-1, 3.505982818951592e-16 -0.0-1, 0.0 -0.0-1]]

So it would be

[[-0.9999999999999987, -0.9999999999999997, -1, -0.9999999999999997],
 [-0.999999999999997, -0.9999999999999987, -0.9999999999999997, -1]]

How can I make it?

Joanne
  • 503
  • 1
  • 3
  • 15

2 Answers2

1

To take the minimum from each row you actually want to take the minimum across the columns, ie. axis=1. Building on what @Patrick has done, to apply the subtraction we need to do some transposing to get broadcasting to work:

import numpy as np
np.set_printoptions(precision=20)

list = [[1.2886089553007253e-15, 3.283665029781338e-16, 0.0, 3.4027301260438933e-16],\
        [3.047580716284324e-15, 1.3787915767152193e-15, 3.505982818951592e-16, 0.0]]

array = np.asarray(list)

# minimum across each row
row_min = array.min(axis=1)
row_min
>>> array([0., 0.])

# row_min.shape = (2,), array.shape = (2, 4)
# so we transpose to do the subtraction and then transpose back
result = (array.T - row_min - 1).T

result
>>> array([[-0.9999999999999987, -0.9999999999999997, -1.                ,
    -0.9999999999999997],
           [-0.999999999999997 , -0.9999999999999987, -0.9999999999999997,
    -1.                ]])
Paddy Harrison
  • 1,808
  • 1
  • 8
  • 21
0

You already have the correct values - you are just not printing them with enough precision:

import numpy as np
# set the printout precision
np.set_printoptions(precision=20)
list = [[1.2886089553007253e-15, 3.283665029781338e-16, 0.0, 3.4027301260438933e-16],\
        [3.047580716284324e-15, 1.3787915767152193e-15, 3.505982818951592e-16, 0.0]]

array = np.asarray(list)

result = array-array.min(axis=0)-1

print(result)

Output:

[[-1.                 -1.                 -1.                  -0.9999999999999997]
 [-0.9999999999999982 -0.999999999999999  -0.9999999999999997  -1.                ]]

See numpy.set_printoptions

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • But I think there is only one -1 in each row. However, there are three -1 in the first row. – Joanne May 09 '20 at 07:53
  • @joanne maybe [is-floating-point-math-broken](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) explains that – Patrick Artner May 09 '20 at 08:11