I am using a data object that is a list of numpy arrays of different shapes. I would like to treat the data object as a single numpy object, such that I can add, subtract and scale these objects as if they were pure numpy arrays:
# Defining the data objects:
d1 = [numpy.random.rand(n**2).reshape(n, n) for n in range(1, 5)]
d2 = [numpy.random.rand(n**2).reshape(n, n) for n in range(1, 5)]
# The operation I want to perform:
d3 = [a1 + 2*a2 for a1, a2 in zip(d1, d2)]
# What I really would like to write:
d3 = d1 + 2*d2 # (not valid for lists of arrays)
What is the easiest way to define the objects d1 and d2 such that I can use the last line?
The reason I ask is because I want to reuse some code that assumes that d1 and d2 are simple numpy arrays that can be added and scaled.
shapeanddtypewhen creating an array from these lists. Since the subarrays differ in shape, the combination will be a 1d array of array objects, not a multidimensional array of floats. Some, but not all, numpy math propagates through those objects.