I know about np.eye
which generates identity matrix. This question is about the algorithm rather than about the final result.
In Q (kdb+ language) I can generate identity matrix using the following code:
`float${x =\: x} til 12000
Python numpy equivalent is more or less this:
import numpy as np
r=np.arange(12000)
np.asarray([i==r for i in r]).astype(float)
With Python approach there's an unnecessary data copying during np.asarray
to convert Python array back into numpy array. Is there a way to avoid this copying? I.e. I want to perform [i==r for i in r]
without getting out of numpy
domain.