I have the following code:
from itertools import product
from joblib import Parallel, delayed
from operator import mul
import numpy as np
lst = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
arr = np.array(lst)
flat = np.ravel(arr).tolist()
lst = (list(a) for a in product(flat, repeat=2))
results = Parallel(n_jobs=-1)(delayed(mul)(x, y) for x, y in lst)
rows = arr.shape[0]
cols = arr.shape[1]
arr3d = np.array(results).reshape(rows, cols, rows, cols)
def stacking():
final_arr = np.empty((1, cols*cols)) # Initialize final_arr
for i in range(0,rows):
first_block = arr3d[i,0] # Initialize first_block
for j in range(1,cols):
first_block = np.column_stack((first_block, arr3d[i,j]))
final_arr = np.row_stack((final_arr, first_block))
yield np.delete(final_arr, 0, axis=0)
next(stacking())
I wish to convert both the for loops (i's and j's) to a generator. When I tried to do this for the j loop:
def stacking():
final_arr = np.empty((1, cols*cols)) # Initialize final_arr
for i in range(0,rows):
first_block = arr3d[i,0] # Initialize first_block
first_block = (np.column_stack((first_block, arr3d[i,j])) for j in range(1,cols))
final_arr = np.row_stack((final_arr, first_block))
yield np.delete(final_arr, 0, axis=0)
next(stacking())
I get the following error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-35-ba342fd13303> in <module>
----> 1 next(stacking())
<ipython-input-34-303528d26770> in stacking()
4 first_block = arr3d[i,0] # Initialize first_block
5 first_block = (np.column_stack((first_block, arr3d[i,j])) for j in range(1,cols))
----> 6 final_arr = np.row_stack((final_arr, first_block))
7 yield np.delete(final_arr, 0, axis=0)
<__array_function__ internals> in vstack(*args, **kwargs)
~\Anaconda3\lib\site-packages\numpy\core\shape_base.py in vstack(tup)
281 if not isinstance(arrs, list):
282 arrs = [arrs]
--> 283 return _nx.concatenate(arrs, 0)
284
285
<__array_function__ internals> in concatenate(*args, **kwargs)
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 9 and the array at index 1 has size 1
Is there a way to use np.column_stack/np.row_stack together with generators?
I am basically trying to make this piece of code run faster and use less memory, as I am dealing with large arrays. If there is another way to tweak this piece of code to do this, please let me know! Many thanks.