1

I'm trying to create a new array out of two other arrays. I already tried multiple np.append() statements along multiple axis. Here is some code:

arr1 = np.zeros(2, 3)
arr2 = np.zeros(2, 2)
new_arr = np.append(arr1, arr2)
print(new_arr)

Desired output:

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

Actual output:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
asked Jan 20, 2021 at 7:20
2

2 Answers 2

1

try this

import numpy as np
arr1 = np.array([0, 0, 0])
arr2 = np.array([0, 0, 0])
final_arr = np.concatenate((arr1, arr2))
print(final_arr)

Refer this --> https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html

Dharman
33.9k27 gold badges103 silver badges153 bronze badges
answered Jan 20, 2021 at 7:22
Sign up to request clarification or add additional context in comments.

1 Comment

That only works if both arrays have the same shape. Otherwise you get the following: ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 3 and the array at index 1 has size 2
0

You can do it this way:

np.asarray([list(arr1),list(arr2)], dtype = 'O')

The dtype = 'O' means Object type.

answered Jan 20, 2021 at 7:32

2 Comments

Why is it so complicated? Why does numpy append arrays so wierdly?
@melonmarlon because it isn't made to work this way. From the docs: "An array object represents a multidimensional, homogeneous array of fixed-size items" (numpy.org/doc/stable/reference/generated/numpy.ndarray.html)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.