Python: Too many indices

三世轮回 提交于 2019-12-11 23:13:24

问题


I am trying to select amounts from an array given a criteria and reject the other amounts that do not fit. The criteria is: if amount[i,:]> x*amount[i-1,:] keep, otherwise keep prior amount. just like a treshold basically. And I am filling all these amount selected within a new array Array1. In the end the curve array takes it all and draws a curve. Now, the curve somehow always give me the same curve no matter what treshold i put in. This leads me to think that something is wrong with my Array1 code.

I am filling this array with a where function: amount is a 2 dim array taking in a date in the first index and an amount in the second index. I size up the Array1 to the same size and shape.

x=2
def (i,curve):
    Array1 = zeros(shape=(amount.shape[0],amount.shape[1]))

    Array1 = where(amount[i,:]>x*Array1[i-1,:],amount[i,:],where(amount[i,:]*x<Array1[i-1,:],amount[i,:],Array1[i-1,:]))

    #this function below just takes in Array1 and gives a curve.
    curve[:] = Array1[i,:]-Array1[i-1,:]

Now, when I run it, I get a "too many indices" error. Anyone care to help? How can I get an element of Array1 without specifying its index? I've been at it for 2 days now.

This is basically what iam trying to do with loops, but this is very slow, this is why i use the where function.

def curve(i, curve):
  Array1 = zeros(shape=(amount.shape[0],amount.shape[1])) 
  for u in xrange(5000):
   for i in xrange(5000):
    if amount[i,u] > 1.031*amount[i-1,u]:
      Array1[i]=amount[i,u]  
    else:
      Array1[i]=Array1[i-1,u] 

      curve[:] =Array1[i,u]-Array1[i-1,u]

来源:https://stackoverflow.com/questions/27024702/python-too-many-indices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!