0

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]
uniXVanXcel
  • 807
  • 1
  • 10
  • 25
  • 1
    `where` handles the looping and indexing internally, just like adding two numpy arrays without specifying the indices. See page 13 of this document for an example of its use http://www.engr.ucsb.edu/~shell/che210d/numpy.pdf (it has two modes of operation, which can be confusing). Your question as it stands is not ideal because there are many variables that are undefined, like `amount`. It's best if you can post a self-contained example. – YXD Nov 19 '14 at 19:24
  • 2
    Please add a minimal, complete, running (in this case throwing this error) example to your post. This code isn't even valid Python code, and we don't know the shapes and contents of the arrays. Your problem probably lies with setting Array1 to a 1-D array, when you later expect it to be 2-D. Maybe on creating a complete, running example you'll figure out the problem yourself. – w-m Nov 19 '14 at 19:30
  • I did some error copying the code to the website here; ive fixed them now; the issue remains though. I cannot write down the entire code here so that it would be run. the amount array takes in data from outside also. basically amount and Array1 have the same shape – uniXVanXcel Nov 19 '14 at 19:42
  • to Mr E i understand that where handles looping and indexing internally without needing to specify indices, but however how would you compare rows without having some sort of index to show which one you re selecting like what i did in my snippet? thanks – uniXVanXcel Nov 19 '14 at 19:50
  • @Rmything I don't quite understand what you are trying to work out in your code. What you should do is write a small piece of code that produces the result you want - do it with loops and indices the "normal" way. [Make it a complete and minimal example](http://stackoverflow.com/help/mcve), like this recent question: http://stackoverflow.com/q/27000092/553404 . As it stands it's very hard to guess what you want to do – YXD Nov 19 '14 at 22:27
  • @ Mr E I tried to make it more understandable following your input. let me know if you can help thanks – uniXVanXcel Nov 20 '14 at 01:55
  • 1
    Which line has the error? If it is the long one with `where`, you may need to evaluate the pieces to pin down exactly which indexing is wrong. It `Array1` after the `where` line still 2d? – hpaulj Nov 20 '14 at 04:24

0 Answers0