在下面的代码中,y1 和 y2 应该相等,但事实并非如此。 vectorize() 或 dot() 中是否存在错误?
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()
最佳答案
我不确定这是否适用于您的情况,但是 vectorize
有一些技巧。
如果您没有指定返回 dtype
,它会通过测试计算来确定它 - 对于您的第一个案例。如果您的函数返回标量整数,例如 0,则 vectorize
返回一个整数数组。因此,如果您需要一个 float ,请确保指定返回 dtype
。
另外 - vectorize
不是一个速度工具。这只是将广播应用于您的输入的一种便捷方式。它并不比显式循环您的输入快多少。
np.vectorize(fun, otypes=[float])
删除步骤。
===========
试试这个:
vfun = np.vectorize(fun, otypes=[float])
X = vfun(interval[:,None], pivots, truths)
print(X.shape) # (300,3)
y2 = np.dot(X, coeffs)
print(y2.shape) # (300,)
它更充分地利用了vectorize 的
广播。
我怀疑您的 fun
可以写成对整个 x
起作用,而无需像 vectorize
那样进行迭代。
将 fun
更改为使用 np.maximum
,允许我提供数组 x
:
def fun(x, pivot, truth):
if truth: return np.maximum(0, x - pivot)
else: return np.maximum(0, pivot - x)
然后我可以通过在 pivots
和 truths
的 3 个案例上循环计算 X
,计算所有 interval
值一次:
X = np.stack([fun(interval, p, t) for p, t in zip(pivots, truths)], axis=-1)
y2 = np.dot(X, coeffs)
另一种应用 3 个“案例”的方法
Xlist = [fun(interval, p, t)*c for p, t, c in zip(pivots, truths, coeffs)]
y2 = np.sum(Xlist, axis=0)
因为 np.dot(..., coeffs)
只是一个加权和。我不确定它是否更好。
关于NumPy vectorize() 或 dot() 出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41205597/