production of a matrix and a tensor
now I have below two objects:
A = np.array(('a', 'b', 'c', 'd'), dtype=object)
x = np.array(range(1,9)).reshape(2,2,2).transpose(0,2,1)
With this setting, I want to have a 3D matrix as follows:
array([[['abb' 'aaabbbb'],
['cdd' 'cccdddd']],
[['aaaaabbbbbb' 'aaaaaaabbbbbbbb'],
['cccccdddddd' 'cccccccdddddddd']]]
I have tried np.tensordot(A, x, 1)
with some options, but I didn't have a desired result. Hope someone helps me.
Solution 1:
You can try:
A.reshape(2, 2)@x
It gives:
array([[['abb', 'aaabbbb'],
['cdd', 'cccdddd']],
[['aaaaabbbbbb', 'aaaaaaabbbbbbbb'],
['cccccdddddd', 'cccccccdddddddd']]], dtype=object)