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]
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

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.

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)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.