I don't know if the title makes any sense. Normally an identity matrix is a 2D matrix like
In [1]: import numpy as np
In [2]: np.identity(2)
Out[2]:
array([[ 1., 0.],
[ 0., 1.]])
and there's no 3rd dimension.
Numpy can give me 3D matrix with all zeros
In [3]: np.zeros((2,2,3))
Out[3]:
array([[[ 0., 0., 0.],
[ 0., 0., 0.]],
[[ 0., 0., 0.],
[ 0., 0., 0.]]])
But I want a "3D identity matrix" in the sense that all diagonal elements along the first 2 dimensions are 1s. For example, for shape (2,2,3) it should be
array([[[ 1., 1., 1.],
[ 0., 0., 0.]],
[[ 0., 0., 0.],
[ 1., 1., 1.]]])
Is there any elegant way to generate this?