我试图通过阅读Data Science from Scratch by Joel Grus一书来学习Python,在第94页,他们描述了如何使用下面的代码来近似f=x^2的导数
def difference_quotient(f, x, h):
return (f(x + h) - f(x)) / h
def square(x):
return x * x
def derivative(x):
return 2 * x
derivative_estimate = partial(difference_quotient, square, h=0.00001)
# plot to show they're basically the same
import matplotlib.pyplot as plt
x = range(-10,10)
plt.title("Actual Derivatives vs. Estimates")
plt.plot(x, map(derivative, x), 'rx', label='Actual')
plt.plot(x, map(derivative_estimate, x), 'b+', label='Estimate')
plt.legend(loc=9)
plt.show()
一切正常,但是当我将行derivative_estimate = partial(difference_quotient, square, h=0.00001)
更改为derivative_estimate = partial(difference_quotient, f=square, h=0.00001)
(因为我认为这更容易阅读)时,会出现以下错误
Traceback (most recent call last):
File "page_93.py", line 37, in <module>
plt.plot(x, map(derivative_estimate, x), 'b+', label='Estimate')
TypeError: difference_quotient() got multiple values for keyword argument 'f'
这是怎么回事?
目前没有回答
相关问题 更多 >
编程相关推荐