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?

2 Answers 2

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.                ]])
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

But I think there is only one -1 in each row. However, there are three -1 in the first row.
@joanne maybe is-floating-point-math-broken explains that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.