Search code examples
pythonrnumpydiagonal

Equivalent diag R function in numpy python


When I do with R diag(0.03,2) I obtain :

     [,1] [,2]
[1,] 0.03 0.00
[2,] 0.00 0.03

How can I have the same result in Python ? I tried numpy.diag(0.03,2) but I get an error : ValueError: Input must be 1- or 2-d. Same with numpy.diagonal(0.03,2)

Thanks for any help


Solution

  • Do numpy.diag([.03]*2).

    numpy.diag() takes in a list of the diagonal components, and you can multiply lists in python to repeat elements.