1

i have a numpy array of 27 elements,Im trying concatenate or add all the elements inside the array,but i cant come up with anything right,

I tried,

for index,value in enumerate(array):
    np.concatenate(array[index],array[index])

but this throws

TypeError: only integer scalar arrays can be converted to a scalar index

I tried

array[1]+array[2]+array[3]

this works for me, but im not sure how to put this in a loop, Any suggestions on this front would be really helpful

Thanks in advance.

EDIT: array looks like this

array([[[0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0],
    ..., 
    [0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0]],

   [[0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0],
    ..., 
    [0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0]],

   [[0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0],
    ..., 
    [0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0]],

   ..., 
   [[0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0],
    ..., 
11
  • 1
    Do you just want the total sum of all of the elements in the array? Commented Jan 18, 2018 at 11:37
  • 1
    "but im not sure how to put this in a loop"... Try something like for x in range(0,100): previous_value = array[x] + previous_value. Obviously first you declare previous_value = 0 so that it "knows" where to start. Commented Jan 18, 2018 at 11:40
  • 1
    That array has more than 27 elements. What do you mean by "tied together"? It would help if you showed us a small input array and the exact output you want from that input. Commented Jan 18, 2018 at 11:40
  • 1
    Generally when working with Numpy you should try to avoid explicit loops. Numpy provides many functions and methods which perform the looping for you at compiled speed, which is much faster than a plain Python for loop. Commented Jan 18, 2018 at 11:43
  • 1
    I'm still not clear what you mean by "tied together", but one of the various Joining arrays functions may do what you want. Commented Jan 18, 2018 at 11:46

1 Answer 1

2

You want to sum over one axis (the first, I think). Like this:

array.sum(axis=0)
Sign up to request clarification or add additional context in comments.

Comments

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.