0

Is there a numpy function which can combine a 2d numpy array into a 1d numpy array. I want to do it without using a for loop.

Example:

[[1 0 0 0 0], [0 1 0 0 0]] => [1 1 0 0 0]
0

1 Answer 1

1

Just use the ndarray method sum along row axis:

arr2d = np.array([[1, 3, 8, 2, 0], [0, 1, 0, 5, 1]])

arr1d = arr2d.sum(axis=0)

>>> array([1, 4, 8, 7, 1])
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.