python - SciPy 导数函数 - 参数解析失败

标签 python numpy scipy

我正在尝试使用 scipy 导数模块查找函数的导数。不幸的是,我无法在没有 TypeError 的情况下将我的参数解析为函数:

def f(x, *arg):
    beta = arg
    y = -x + beta * np.tanh(x)
    return y

param = (0,)
der_x0 = derivative(f, x0 = 0.0, dx = 1e-6, args = param) 

输出:

TypeError: 'numpy.float64' object cannot be interpreted as an integer

最佳答案

beta 是元组类型(例如,尝试在 beta = arg 之后添加 print(beta),您应该会看到类似 (0,) 作为打印输出而不是 0)。

尝试:

beta = arg[0]

相反,即

def f(x, *arg):
    beta = arg[0]
    y = -x + beta * np.tanh(x)
    return y

对于 der_x0,您应该得到 -1.0 的值。

关于python - SciPy 导数函数 - 参数解析失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48158042/

相关文章:

python - 如何将 "args=(...)"添加到我自己函数的参数中?

python - 为什么 numpy.zeros 占用空间小

python - INSERT 命令不按顺序使用 sqlalchemy 和 mysql 大行

python - 在 Python 中重置生成器对象

python - MySQLdb安装问题

python - numpy 中的 3d 矩阵乘法

python - 如何将数组从 .txt 文件导入到 numpy 数组?

python - 在 numpy 中创建一个特殊矩阵

python - 我如何修复/调试 scikit 学习中抛出的这个多进程终止的工作错误

python - 使用 numpy.apply