Why np.sum() dosen't works if i want use this in lambda function

Question:

I’m terribly sorry for the stupid question,this is my first time using NumPy so I don’t know what I’m doing wrong
Lines with errors are marked #ERROR
I must be accessing the index incorrectly or getting the wrong type of value
Thank you in advance for your help

import numpy as np

p = {1:100, 2:120, 3:130, 4:100, 5:90,6:180}
M = {1:300, 2:120, 3:300}
I = [1,2,3,4,5,6]
J = [1,2,3]
st_per = {(1,1):1, (1,2):2, (1,3):3,
(2,1):4, (2,2):1, (2,3):1,
(3,1):5, (3,2):2, (3,3):4,
(4,1):3, (4,2):1, (4,3):2,
(5,1):1, (5,2):2, (5,3):1,
(6,1):0, (6,2):0, (6,3):0
}

st_per2p = np.empty([len(I), len(J)])

for i in range(len(I)):

 for j in range(len(J)):
  st_per2p[i,j] = st_per[i+1,j+1]
x0 = np.ones(len(st_per))*100

bounds = list((0,max(p.values())) for _ in range(st_per2p.size))

def ob(x):

 ob_func = sum(x[idx]*st_per2p[idx//len(J), idx%len(J)] for idx in range(st_per2p.size))

 return ob_func


def st_per1():
 tmp = []
 for idx in range(0, st_per2p.size, len(J)):
  tmp_constr = {'type': 'eq','fun': lambda x, idx: p[idx//len(J) + 1]–np.sum(x[idx: idx + len(J)]),'args': (idx,)} #ERROR
  tmp.append(tmp_constr)
 return tmp


def st_per2():
 tmp = []
 for idx in range(0, st_per2p.size, len(I)):
  tmp_constr = {'type': 'ineq','fun': lambda x, idx=idx: M[idx//len(I) + 1]–np.sum(x[idx: idx + len(I)])} #ERROR
  tmp.append(tmp_constr)
 return tmp
Asked By: ill_999

||

Answers:

You should have included the whole error message:

SyntaxError: invalid character '–' (U+2013)

The PROBLEM is that what you THINK is a minus sign in those two lines is not actually a minus sign — it is the Unicode "en dash" character. U+2013. This probably came from whatever tool you used to edit the text. You should go into a plain text editor (vim, nano, etc), and replace those two with a simple minus sign.

Answered By: Tim Roberts

np.sum() is a function from the NumPy library that calculates the sum of array elements along a specified axis. The reason it may not work in a lambda function is that np.sum() is not a built-in Python function, and it requires NumPy to be imported before it can be used.

Lambda functions are anonymous functions in Python that are defined using the lambda keyword. They are typically used for short, one-line functions that can be defined on-the-fly, without the need for a separate function definition. Since np.sum() is not a built-in function, it needs to be imported before it can be used, and this is not possible within the scope of a lambda function.

To use np.sum() in a lambda function, you would need to import NumPy first and define the lambda function to use the np.sum() function from NumPy. Here is an example:

import numpy as np

my_array = np.array([1, 2, 3, 4, 5])

# Define a lambda function that uses np.sum()
my_sum_lambda = lambda arr: np.sum(arr)

# Call the lambda function
result = my_sum_lambda(my_array)

print(result) # Output: 15

If you are need more ditels or you want to learn about it visite https://blogiflys.com/blog/why-npsum-dosent-works-if-i-want-use-this-in-lambda-function

Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.