Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy: Set array element to another array

I'm trying to set an element of a Numpy array to be another Numpy array. I'm not sure on how to do this since every time I try I get ValueError: setting an array element with a sequence.

I know this is possible with Python's list since I can append the new array to list and it will work.

This is an example of what I'm trying to do:

import numpy as np

finalArray = np.zeros(3)
finalList = []

a = np.arange(128).reshape(32,4)
b = np.arange(124).reshape(31,4)
c = np.arange(120).reshape(30,4)

# This works
finalList.append(a)
finalList.append(b)
finalList.append(c)

# This doesn't work
finalArray[0] = a
finalArray[1] = b
finalArray[2] = c

Any ideas on how to do this?

like image 371
Xavier Merino Avatar asked Oct 18 '22 08:10

Xavier Merino


1 Answers

Numpy arrays cannot be filled with arbitrary types. They are more like the arrays of C or Java. To create a two dimensional array, feed a two dimensional list into the np.array function. For example,

x = np.array([[0,0,0],[0,0,0],[0,0,0]])

creates a 3x3 two dimensional array filled with zeros.

like image 138
Algorithmic Canary Avatar answered Oct 21 '22 04:10

Algorithmic Canary