0

My goal is it to save 2 different arrays in a new numpy array.

I tried a few things and this worked for me:

state_vek = np.array([array1, array2])

array1 and array2 are have different dimensions.

But as i am working with classes i would need to initialize state_vek beforehand.

Then I tried this:

state_vek = np.zeros(2)
state_vek[0] = array1
state_vek[1] = array2

Which resulted in following error:

ValueError: setting an array element with a sequence.

So my question is, how am i supposed to initialize my state_vek so i can use this line state_vek[0] = array1?

4
  • What, exactly is the result you are looking for? If you want to be able to store any arbitrary array, just use a list. Commented Dec 26, 2020 at 14:02
  • In the first case what's the shape of the two arrays and the result? What's their dtypes? Commented Dec 26, 2020 at 15:19
  • @juanpa.arrivillaga My goal is it to use the array or list as input for a neural network. The first array is a (4,160,120,1) array which gets passed to an convolutional neural network as input The output of this CNN gets flattened into an long array and then i wanted to append array2 to that flattened array. The storing of the states is also done via numpy arrays that why i would like an solution with numpy array. Im aware of the existance of lists Commented Dec 26, 2020 at 16:06
  • An object dtype array is no better, and often worse, than a list with the same components. So far you problem description is incomplete. Commented Dec 26, 2020 at 17:28

1 Answer 1

1

If you know the dimensions of array1 and array2 beforehand, you can do something like this:

import numpy as np

arr1 = np.array([1,2,3])
arr2 = np.array([4,5,6])
b = np.zeros((2,3)) #np.zeros(shape, dtype = None, order = 'C')

b[0] = arr1
b[1] = arr2
print(b)

Output:

[[1. 2. 3.]
 [4. 5. 6.]]
Sign up to request clarification or add additional context in comments.

2 Comments

Hello, thanks for your answer. How would you do that if array1 had the shape of (4,160,120,1) and array2 had the shape of (1,)?
Just use python lists to store, and convert in numpy arrays using asarray() function when you want to perform any operations.

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.