How to create an identity matrix with numpy? Is there a simpler syntax than
numpy.matrix(numpy.identity(n))
Here is a simpler syntax:
np.matlib.identity(n)
And here is an even simpler syntax that runs much faster:
In [1]: n = 1000 In [2]: timeit np.matlib.identity(n) 100 loops, best of 3: 8.78 ms per loop In [3]: timeit np.matlib.eye(n) 1000 loops, best of 3: 695 us per loop
I do not think there is a simpler solution. You can do this somewhat more efficiently though:
numpy.matrix(numpy.identity(n), copy=False)
This avoids unnecessary data copying.
In addition, np.eye can be used to create an array of identifiers (In).
For instance,
>>> np.eye(2, dtype=int) array([[1, 0], [0, 1]]) >>> np.eye(3, k=1) array([[ 0., 1., 0.], [ 0., 0., 1.], [ 0., 0., 0.]])
Source: https://habr.com/ru/post/917541/More articles:C # datatable select statement with dates - c #Truncate Chinese text - phpcreating a folder on sd with subfolders - androidAn example of accepting / implementing a quick enumeration for my class? - objective-cHow can I get an email address from the Google Plus API as soon as I get a token - javaSuppress `Expected identifier and instead see 'default' (reserved word)` in JSLint with Mongoose - javascripthttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/917543/in-ios-using-arc-is-it-sufficient-to-set-all-ivars-and-properties-to-nil-and-release-context-image-color-space-in-viewdidunload&usg=ALkJrhjhXm42XOd6cYbhZQ9EJpMF4quBdwIE 8 and 9 ignoring div width - how to make this example work? - cssCan I specify the location of the circuit in a compact RELAX NG circuit? - xmlHow can I control which version of the msbuild file is used between .NET4 and 4.5RC? - visual-studio-2012All Articles