Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以矢量化这个Python代码吗?_Python_Python 2.7_Pandas_Vectorization - Fatal编程技术网

我可以矢量化这个Python代码吗?

我可以矢量化这个Python代码吗?,python,python-2.7,pandas,vectorization,Python,Python 2.7,Pandas,Vectorization,我试图创建一个函数,该函数将数据帧作为参数,并返回一个满足阈值y的股票价格跳跃列表,在前瞻期x内。我想把这些观察结果放到另一个数据框中(现在,我只是把每个股价“跳跃”放到一个列表列表中),但我无法在合理的时间内运行此代码,因为我使用的数据框是4000ColumnSX3000行。有没有办法将这些循环矢量化以加快程序的速度?谢谢 def jumpFilter(df, y=.1,x=1): lastPrice=1000 #initialization listOfJumps=[] #t

我试图创建一个函数,该函数将数据帧作为参数,并返回一个满足阈值y的股票价格跳跃列表,在前瞻期x内。我想把这些观察结果放到另一个数据框中(现在,我只是把每个股价“跳跃”放到一个列表列表中),但我无法在合理的时间内运行此代码,因为我使用的数据框是4000ColumnSX3000行。有没有办法将这些循环矢量化以加快程序的速度?谢谢

def jumpFilter(df, y=.1,x=1):
    lastPrice=1000 #initialization
    listOfJumps=[] #ticker, date, jump threshold passed, lookahead period, pos/neg, percentJump, avgdailyrateofchange, counter
    y=.2 #threshold-percent change in stock price
    x=1 #lookahead period
    for i in range(1,len(df.columns)):
        counter=0 #counts how many days of consistent jumps have been occurring
        for j in range(len(df.values[:])):
            if (j+x)<len(df.values):
                if type(df.values[j][i])!=str:
                    if math.isnan(float(df.values[j][i]))==False:
                        if df.values[j][i]!=0:
                            currentPrice=df.values[j][i]
                            futurePrice=df.values[j+x][i]
                            if futurePrice>currentPrice:
                                posneg=1
                            elif futurePrice<currentPrice:
                                posneg=0
                            percentJump=abs((currentPrice-futurePrice)/currentPrice)
                            if (currentPrice-futurePrice)/currentPrice>=y:
                                counter+=1
                                avgDailyRateChange=percentJump/(x+counter-1)
                            elif (currentPrice-futurePrice)/currentPrice<y and counter>=1:
                                listOfJumps.append([df.columns[i],df.values[j][0], y, x, posneg, percentJump, avgDailyRateChange, counter])
                                counter=0
    return listOfJumps
def跳线过滤器(df,y=0.1,x=1):
lastPrice=1000#初始化
listOfJumps=[]#股票代码、日期、通过的跳转阈值、前瞻期、pos/neg、百分比跳转、avgdailyrateofchange、计数器
y=0.2#股票价格变化的临界百分比
x=1#前瞻期
对于范围内的i(1,len(df.列)):
计数器=0#统计连续跳跃发生的天数
对于范围内的j(len(df.值[:]):
如果(j+x)当前价格:
posneg=1
elif futurePrice=y:
计数器+=1
avgDailyRateChange=跳跃百分比/(x+计数器-1)
elif(当前价格未来价格)/currentPrice=1:
追加([df.columns[i],df.values[j][0],y,x,posneg,percentJump,avgDailyRateChange,counter])
计数器=0
返回跳转列表

关于改进工作代码的问题最好在上完成哦,谢谢,对不起,我认为这是正确的地方。不用担心,我期待在代码审查中看到您的代码。我建议您发布一个小的工作数据集,以及至少包含两列和30-40行的预期结果。如果这些数据是可剪切和粘贴的python代码,则效果最好。@RobertGrote有一些重叠。但总的来说,高度特定的编程问题也是如此,而CR则是一般性的评论。谢谢,我们实际上只是通过谷歌搜索解决了这个问题!