小编Nip*_*per的帖子

将 lambda 表达式应用于数组元素时出现 ValueError

目前我在处理 numpy.array - 4x1 - 即时遇到错误

[[-1.96113883]
 [-3.46144244]
 [ 5.075857  ]
 [ 1.77550086]]
Run Code Online (Sandbox Code Playgroud)

与 lambda 函数f = lambda x: x if (x > 0) else (x * 0.01)

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

我在 stackoverflow.com 上搜索了不同的主题,但没有找到任何令人满意的问题解释和适合我的案例(许多对运算符and、矢量化代码等的不清楚引用)。

处理数组后我期望得到一个与输入维度相同的数组,并且根据函数修改每个单个值,例如:

[[-0.0196113883]
 [-0.0346144244]
 [ 5.075857  ]
 [ 1.77550086]]
Run Code Online (Sandbox Code Playgroud)

最后,有人可以为我提供解决方案并解释为什么会出现此错误。谢谢你的建议。

python arrays lambda runtime-error numpy

5
推荐指数
1
解决办法
1951
查看次数

scipy.minimize - “TypeError: numpy.float64' 对象不可调用运行”

运行 scipy.minimize 函数“我得到 TypeError: 'numpy.float64' object is not callable”。具体在执行过程中:

    .../scipy/optimize/optimize.py", line 292, in function_wrapper
return function(*(wrapper_args + args))
Run Code Online (Sandbox Code Playgroud)

我已经在这里查看了以前的类似主题,通常会出现此问题,因为 .minimize 的第一个输入参数不是函数。我很难弄清楚,因为“a”是函数。你怎么认为?

    ### "data" is a pandas data frame of float values
    ### "w" is a numpy float array i.e. [0.11365704 0.00886848 0.65302202 0.05680696 0.1676455 ]

    def a(data, w):
        ### Return a negative float value from position [2] of an numpy array of float values calculated via the "b" function i.e -0.3632965490830499 
        return -b(data, w)[2]

    constraint = ({'type': 'eq', 'fun': lambda …
Run Code Online (Sandbox Code Playgroud)

python compiler-errors numpy minimize scipy

5
推荐指数
1
解决办法
3019
查看次数