【发布时间】:2016-12-19 21:06:10
【问题描述】:
在下面的代码中,y1 和 y2 应该相等,但它们不是。 vectorize() 或 dot() 会不会有 bug?
import numpy as np
interval = np.arange(0, 30, 0.1)
y1 = [- 1.57 * max(0, x - 10) - 0.72 * max(0, 15 - x)
- 1.09 * max(0, 20 - x) for x in interval]
def fun(x, pivot, truth):
if truth: return max(0, x - pivot)
else: return max(0, pivot - x)
pivots = [10, 15, 20]
truths = [ 1, 0, 0]
coeffs = [-1.57, -0.72, -1.09]
y2 = [np.dot(np.vectorize(fun)(x, pivots, truths), coeffs) for x in interval]
import matplotlib.pyplot as plt
plt.plot(interval, y1, interval, y2)
plt.show()
【问题讨论】:
标签: numpy types vectorization