0

I have a while statement that produces an array in numpy

while(i<int(sp)): 
   body=(np.bincount(un[i].ravel().astype('int'), weights=step_periods[i].ravel())) 
   print(body) 
   i=i+1

each iteration produces an array of like the following:

1st    [  0.   0.   0.  30.]

2nd    [  0.   0.  21.  18.  15.]

3rd    [  0.  24.  27.   0.   3.]

My first issue is that if the first array has "0" as the last value, it will leave it out of the array. Is there a way to convert it from:

[  0.   0.   0.  30.]

to:

[  0.   0.   0.  30.  0.]

From there I would like to simply append each array to a master array so that the final output is something similar to:

    [[  0.   0.   0.  30.  0.0],

    [  0.   0.  21.  18.  15.],

   [  0.  24.  27.   0.   3.]]

I've looked into appending and vstack, but can't get it to work in a "while" statement, or possibly because they aren't all the same size due to the ending "0" being ommited!

Thanks!

8
  • 1
    It would help to show the while loop that is generating these subarrays. Commented Jul 30, 2012 at 16:45
  • You should post that as an edit to the post. Putting it in comments makes it quite difficult to tell what is going on. Commented Jul 30, 2012 at 17:04
  • Sorry I'll do this right now, I'm new to these forums! I appreciate the help! Commented Jul 30, 2012 at 17:07
  • No problem. we're here to help :) Commented Jul 30, 2012 at 17:07
  • I'm not a numpy expert... but from itertools import izip_longest; np.vstack(izip_longest(a, b, c, fillvalue=0.)).T works... - requires Py2.6+ I believe... Commented Jul 30, 2012 at 17:13

1 Answer 1

1

One solution -- if you can guarantee that the length of body is always going to be less than (or equal to) 5, you can pre-allocate the array:

import numpy as np
my_array = np.zeros((int(sp)-i,5),dtype=int)
for ii in range(i,int(sp)):
    body=(np.bincount(un[i].ravel().astype('int'), weights=step_periods[i].ravel())) 
    my_array[ii,:len(body)]=body 

You could even add a check to see if body got too wide and create a new array, assigning the old portion into it ... But at that point, maybe a stacking operation is what is needed.

Sign up to request clarification or add additional context in comments.

8 Comments

Can you explain what ii is? and I get the error "operands could not be broadcast together with shapes (3) (4)"
Looking at the np.zeros i think that the 5, and the int(sp)-i should be switched. But when I do switch it, i get the error "invalid index"
after switching them, I realized my i=i+1 was not indented! THANK YOU! :) it works perfectly!
@EricEscobar -- You're right, they should have been switched. (sorry about that)
could you point me somewhere I could learn more about what this means "my_array[ii,:len(body)]=body " it works well for what I need I just want to understand it more. Thank you again!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.