lambda function on a numpy array. What's wrong with this piece of code?

apply a function to all elements of numpy array
return numpy array from function
numpy vectorize
numpy apply function to each row
numpy transform array
numpy custom function
map lambda function to numpy array
numpy map reduce

What's wrong with this code :

import numpy as np

A = np.array([[-0.5, 0.2, 0.0],
          [4.2, 3.14, -2.7]])

asign = lambda t: 0 if t<0 else 1
asign(A)
print(A)

expected out:

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

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

Well the lambda on its own will not go through the whole array. For that you will need a higher order function. In this case: map.

A = np.array([[-0.5, 0.2, 0.0],
              [4.2, 3.14, -2.7]])

asign = lambda t: 0 if t<0 else 1
A = list(map(asign, A))

Map will iterate through every element and pass it through the function. I wrapped map in a list because it returns an object of type filter but you can convert it that way.

Most efficient way to map function over numpy array, First, the set-up functions for testing: import timeit import numpy as np f = lambda x: x ** 2 vf = np.vectorize(f) def test_array(x, n): t = timeit.timeit( 'np.array([f(xi) for  where fn is your lambda function. Addition of numpy arrays is equivalent to element-wise addition, so this behaves how you would expect. However, in your second example, you are using max as your function, and the python builtin max function is not defined on numpy arrays. This is because the way the python max is defined is something like this:

This worked for me:

A = A.clip(min=0, max=1)

np.apply_along_axis - Numpy and Scipy, Part 4 provides an overview of lambda expressions, and you'll wrap up by NumPy provides many functions for creating new arrays from existing arrays. A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression.

and passing in an NumPy array or a Python list., The most efficient way to map a function over the numpy array is to import numpy as np. x = np.array([1, 2, 3, 4, 5]). squarer = lambda t: t ** 2. import numpy as np array = np.linspace(0, 5, 6) f2 = lambda x: np.zeros_like(x) print(f2(array)) Documentation for numpy.zeros_like: Return an array of zeros with the same shape and type as a given array.

Most efficient way to map function over numpy array, Create matrix matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) Create a function that adds 100 to something add_100 = lambda i: i + 100 # Create  Function numpy.apply_along_axisis not good for this purpose. Try to use numpy.vectorizeto vectorize your function: https://docs.scipy.org/doc/numpy/reference/generated/numpy.vectorize.htmlThis function defines a vectorized function which takes a nested sequence of objects or numpy arrays as inputs and returns an single or tuple of numpy array as output.

Lambda Functions in Python, Part 4 provides an overview of lambda expressions. To move NumPy provides many functions for creating new arrays from existing arrays. We'll explore two  # Create a function that adds 100 to something add_100 = lambda i: i + 100 # Create a vectorized function vectorized_add_100 = np. vectorize (add_100) Apply Function To Elements # Apply function to all elements in matrix vectorized_add_100 ( matrix )

How to Use Python Lambda Functions – Real Python, Python Tutorial: Functions lambda. Like def, the lambda creates a function to be called later. But it returns the function instead of NumPy array basics A import numpy as np x = np.array( [1, 2, 3, 4, 5]) f = lambda x: x ** 2 squares = f(x) Generally avoid np.vectorize, as it does not perform well, and has (or had) a number of issues. If you are handling other data types, you may want to investigate the other methods shown below.

Comments
  • What would you expect the answer to np.array([[-0.5, 0.2, 0.0], [4.2, 3.14, -2.7]]) < 0 to be? Python doesn't know how to handle that kind of comparison.
  • Try to compare A < 0. This makes no sense. I think you want smth like B = [assign(a) for a in x for x in A] or anything else.
  • A<0 produces a boolean array the same size as A. Python if only works with a scalar boolean True/False. It's a simple either/or action. It can't work with multiple boolean values.
  • Thank you. i implemented using clip and np.sign()
  • Note, the lambda function is not really relevant. In any case, if you are going to assign your anonymous function to a name, you should just use a regular function definition.
  • Why are you doing this? Just use (A > 0).astype(int)