You could perhaps use np.multiply.outer instead of np.outer to get the required outer product:
>>> a = np.arange(4)
>>> b = np.ones(5)
>>> mo = np.multiply.outer
Then we have:
>>> mo(mo(a, a), b).shape
(4, 4, 5)
A better way could be to use np.einsum (this avoids creating intermediate arrays):
>>> c = np.einsum('i,j,k->ijk', a, a, b)
>>> c.shape
(4, 4, 5)
Answer from Alex Riley on Stack OverflowCreate a 3d tensor from a pandas dataframe (pytorch) - Stack Overflow
Is it possible to create a 3D tensor from a .csv file in Python3?
Of course both are possible. The question is what are the performance requirements for that sparse tensor once you have it.
It can be as simple as:
import csv
with open('my_data.csv', 'r') as csvfile:
reader = csv.reader(csvfile)
# skip header
next(reader)
my_tensor = {(A, B, C): D for A, B, C, D in reader}
# example usage
print(my_tensor.get((dim1, dim2, dim3), 0.0))and a csvreader can be used to turn it back into a csv.
More on reddit.comHow to create 3-D Tensors in Python using Numpy - Stack Overflow
Creating 3d Tensor Array from 2d Array (Python) - Stack Overflow
Can I create a tensor with more than three dimensions in Python?
How to create a tensor in NumPy?
How to create a 4-dimensional tensor with PyTorch?
My .csv file has 4 columns (A,B,C,D). I want my 3D tensor to have the dimensions (A,B,C) with elements D_i, and all the empty elements will be 0 (sparse). How can I do this in Python3?
Additionally, is it possible to transform a 3D tensor with dimensions (A,B,C) and elements D into a .csv file?
If you do np.dstack((x, y)), which is the same as the more explicit np.stack((x, y), axis=-1), you are concatenating along the last, not the first axis (i.e., the one with size 2):
(x == d[..., 0]).all()
(y == d[..., 1]).all()
Ellipsis (...) is a python object that means ": as many times as necessary" when used in an index. For a 3D array, you can equivalently access the leaves as
d[:, :, 0]
d[:, :, 1]
If you want to access the leaves along the first axis, your array must be (2, 4, 4):
d = np.stack((x, y), axis=0)
(x == d[0]).all()
(y == d[1]).all()
Use np.stack instead of np.dstack:
>>> d = np.stack([y, x])
>>> np.all(d[0] == y)
True
>>> np.all(d[1] == x)
True
>>> d.shape
(2, 4, 4)