Evaluate absolute value of array element in function

refle

I defined a function

def softthresh(u, LAMBDA):
    if np.fabs(u) <= LAMBDA:
        return 0
     else:
        return ((np.fabs(u) - LAMBDA) * u / np.fabs(u))   

u is a numpy array, and np.fabs will check the relations for each array element (np.fabs(u_i)). It gives me the following error:

 The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()


Follow up Question:

Strange behaviour in simple function.

def softthresh(u,LAMBDA):
    for i in u:
        if np.fabs(i)<=LAMBDA:
            return 0
        else:
            return ((np.fabs(i)-LAMBDA)*u/np.fabs(i)) 
   ll = 5.0
   xx = np.arange(-10,11)
   yy = softthresh(xx,ll)

What I get is not what I expect. for u (=xx ) array-elements that are smaller than 5 i should get zero. But i don't. Why?

mtrw

You are calling return from inside the inner loop. Therefore, your function returns just after it evaluates the first member of u.

Since you are using NumPy, you should take advantage of NumPy's ability to operate on the whole array at once, and also of NumPy's smart indexing.

def softthreshold(u, LAMBDA):
    notzero = np.fabs(u) > LAMBDA # find the indeces of elements that need to be scaled
    rr = np.zeros_like(u) # an array the same size/type as u, already initialized to 0
    rr[notzero] = (np.fabs(u[notzero])-LAMBDA)*u[notzero]/np.fabs(u[notzero]) # scale each of the members that aren't zero
    return rr

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Evaluate absolute value of array element in function

From Dev

Conditionally evaluate an array element to return

From Dev

CasperJS Evaluate function not returning Array

From Dev

Is there a function for retrieving last array's element value?

From Dev

How to compare the value of an array element passed to a function

From Dev

click() function is missing on DOM element in evaluate() in CasperJS

From Dev

How to evaluate and add string to numpy array element

From Dev

Using Jquery wrap function with absolute placed element

From Dev

Sort a list of tuples by their second element and absolute value

From Dev

Why does my array element retrieval function return random value?

From Dev

Error with assigning a value to an element of an array of characters once initialised inside a function

From Dev

Can variance be replaced by absolute value in this objective function?

From Dev

Is there a function in Excel to find the maximum absolute value of a range?

From Dev

Find absolute max value in Javascript array

From Dev

How to sort numpy array by absolute value of a column?

From Dev

How to calculate the absolute value for an array in python?

From Dev

Element Array + 1 value

From Dev

undefined value for array element

From Dev

How to return index and value of the specific element from array on callback using any JavaScript array function?

From Dev

Does a function in Haskell always evaluate its return value?

From Dev

VBA Evaluate function and array formula returning range of values

From Dev

How does this function compute the absolute value of a float through a NOT and AND operation?

From Dev

Cost function using absolute value and division by decision variables

From Dev

How does this function compute the absolute value of a float through a NOT and AND operation?

From Dev

Function result as array value

From Dev

Passing Array to a function by Value

From Dev

CSS: absolute element inside an another absolute element

From Dev

invoke function for each element in array

From Dev

Function to verify if an element present in an array

Related Related

  1. 1

    Evaluate absolute value of array element in function

  2. 2

    Conditionally evaluate an array element to return

  3. 3

    CasperJS Evaluate function not returning Array

  4. 4

    Is there a function for retrieving last array's element value?

  5. 5

    How to compare the value of an array element passed to a function

  6. 6

    click() function is missing on DOM element in evaluate() in CasperJS

  7. 7

    How to evaluate and add string to numpy array element

  8. 8

    Using Jquery wrap function with absolute placed element

  9. 9

    Sort a list of tuples by their second element and absolute value

  10. 10

    Why does my array element retrieval function return random value?

  11. 11

    Error with assigning a value to an element of an array of characters once initialised inside a function

  12. 12

    Can variance be replaced by absolute value in this objective function?

  13. 13

    Is there a function in Excel to find the maximum absolute value of a range?

  14. 14

    Find absolute max value in Javascript array

  15. 15

    How to sort numpy array by absolute value of a column?

  16. 16

    How to calculate the absolute value for an array in python?

  17. 17

    Element Array + 1 value

  18. 18

    undefined value for array element

  19. 19

    How to return index and value of the specific element from array on callback using any JavaScript array function?

  20. 20

    Does a function in Haskell always evaluate its return value?

  21. 21

    VBA Evaluate function and array formula returning range of values

  22. 22

    How does this function compute the absolute value of a float through a NOT and AND operation?

  23. 23

    Cost function using absolute value and division by decision variables

  24. 24

    How does this function compute the absolute value of a float through a NOT and AND operation?

  25. 25

    Function result as array value

  26. 26

    Passing Array to a function by Value

  27. 27

    CSS: absolute element inside an another absolute element

  28. 28

    invoke function for each element in array

  29. 29

    Function to verify if an element present in an array

HotTag

Archive