列表理解卷积 [英] list comprehension convolution

查看:121
本文介绍了列表理解卷积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的有效代码,但是速度很慢.

I have a working code like this, but it is rather slow.

def halfconvolution(g,w,dz):
    convo=np.zeros_like(g)
    for i in range(0,len(g)):
        sum=0
        for j in range(0,i):
            sum+=g[j]*w[(i-j)]*dz
        convo[i] = -sum
    return convo

我正在尝试将其转化为列表理解,但我一直在努力.
我试过了:

I am trying to turn it into a list comprehension, but I am struggling.
I tried:

convo=[-g*w[i-j] for i in g for j in w]

推荐答案

内部循环可以用sum函数代替(不要用同名变量覆盖)

The inner loop can be replaced by the sum function (don't override it with a variable of the same name)

然后将外部循环附加到该循环的末尾

Then you append the outer loop to the end of that

 [-sum(g[j]*w[i-j]*dz for j in range(i)) for i in range(len(g))]

这篇关于列表理解卷积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆