Pio*_*dal 6 python arrays numpy map
我想要numpy.array从NxM到NxMx3的映射a ,其中三个元素的向量是原始条目的函数:
lambda x: [f1(x), f2(x), f3(x)]
Run Code Online (Sandbox Code Playgroud)
但是,像numpy.vectorize不允许更改尺寸的事情.当然,我可以创建一个零数组并创建一个循环(这就是我现在正在做的事情),但它既没有Pythonic也没有效率(就像Python中的每个循环一样).
有没有更好的方法在numpy.array上执行元素操作,为每个条目生成一个向量?
现在我看到了您的代码,对于大多数简单的数学运算,您可以让 numpy 进行循环,这通常称为向量化:
def complex_array_to_rgb(X, theme='dark', rmax=None):
'''Takes an array of complex number and converts it to an array of [r, g, b],
where phase gives hue and saturaton/value are given by the absolute value.
Especially for use with imshow for complex plots.'''
absmax = rmax or np.abs(X).max()
Y = np.zeros(X.shape + (3,), dtype='float')
Y[..., 0] = np.angle(X) / (2 * pi) % 1
if theme == 'light':
Y[..., 1] = np.clip(np.abs(X) / absmax, 0, 1)
Y[..., 2] = 1
elif theme == 'dark':
Y[..., 1] = 1
Y[..., 2] = np.clip(np.abs(X) / absmax, 0, 1)
Y = matplotlib.colors.hsv_to_rgb(Y)
return Y
Run Code Online (Sandbox Code Playgroud)
这段代码应该比你的运行得快得多。