更新时间:2021-11-23 23:53:27
下面是我曾尝试 - 利用减少,地图。我不知道有多快,这是 - ?但是这是你正在尝试做的。
Here's what I have tried - using reduce, map. I am not sure how fast this is - but is this what you are trying to do?
编辑4:最简单,最可读 - 让→
A numpy的数组,然后大大简化了,其中
Edit 4: Simplest and most readable - Make l
a numpy array and then greatly simplifies where
.
import numpy as np
import time
l = np.array([1.0, 2.0, 3.0])
def posFunc(x,y):
return x*y
def negFunc(x,y):
return x*(y+1)
def myFunc(x, y):
if x > 0:
return posFunc(x, y)
if x < 0:
return negFunc(x, y)
else:
return 1.0
myArray = np.array([
[ 0.,0.,0.],
[ 0.32, -6.79, 0.],
[ 0.,0.,0.],
[ 0.,1.5,0.],
[ 0.,0., -1.71]])
t1 = time.time()
a = np.array([reduce(lambda x, (y,z): x*myFunc(z,l[y]), enumerate(x), 1) for x in myArray])
t2 = time.time()
print (t2-t1)*1000000
print a
基本上就让我们来看看最后一行它说:累计繁殖事情枚举(XX)
,从1开始(最后一个参数减少
)。 myFunc的只是需要@指数排myArray的(行)的元素和元素在→
并乘以需要。
Basically let's just look at last line it says cumulatively multiply things in enumerate(xx)
, starting with 1 (last parameter to reduce
). myFunc simply takes the element in myArray(row) and element @ index row in l
and multiplies them as needed.
我的输出不一样的你 - 所以我不知道这是否正是你想要的,但可能是你可以遵循的逻辑。
My output is not same as yours - so I am not sure whether this is exactly what you want, but may be you can follow the logic.
另外,我不太知道如何快,这将是巨额的阵列。
Also I am not so sure how fast this will be for huge arrays.
编辑:下面是一个纯numpy的方式来做到这一点。
edit: Following is a 'pure numpy way' to do this.
my = myArray # just for brevity
t1 = time.time()
# First set the positive and negative values
# complicated - [my.itemset((x,y), posFunc(my.item(x,y), l[y])) for (x,y) in zip(*np.where(my > 0))]
# changed to
my = np.where(my > 0, my*l, my)
# complicated - [my.itemset((x,y), negFunc(my.item(x,y), l[y])) for (x,y) in zip(*np.where(my < 0))]
# changed to
my = np.where(my < 0, my*(l+1), my)
# print my - commented out to time it.
# Now set the zeroes to 1.0s
my = np.where(my == 0.0, 1.0, my)
# print my - commented out to time it
a = np.prod(my, axis=1)
t2 = time.time()
print (t2-t1)*1000000
print a
让我试图解释拉链(* np.where(我的!= 0))
部分是***的,我可以。 np.where
只需返回两个numpy的数组第一个数组是行索引,第二个数组是符合条件的列的索引(我的!= 0)
在这种情况下。我们把这些指标的元组,然后使用 array.itemset
和 array.item
,值得庆幸的是,列索引可用免费给我们,所以我们可以只取元素@该索引列表中的→
。这应该比previous更快(按数量级可读!!)。需要 timeit
来看看它是否的确是。
Let me try to explain the zip(*np.where(my != 0))
part as best as I can. np.where
simply returns two numpy arrays first array is an index of row, second array is an index of column that matches the condition (my != 0)
in this case. We take a tuple of those indices and then use array.itemset
and array.item
, thankfully, column index is available for free to us, so we can just take the element @ that index in the list l
. This should be faster than previous (and by orders of magnitude readable!!). Need to timeit
to find out whether it indeed is.
编辑2:不必对正面和负面的单独调用可以用一个调用来完成 np.where(!我= 0)
Edit 2: Don't have to call separately for positive and negative can be done with one call np.where(my != 0)
.