Как то так?
def nd_id(n, d):
out = np.zeros( (n,) * d )
out[ tuple([np.arange(n)] * d) ] = 1
return out
Тестирование
nd_id(3,3)
Out[]:
array([[[ 1., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]],
[[ 0., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 0.]],
[[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 1.]]])