numpy.diagonal#
- numpy.diagonal(a,offset=0,axis1=0,axis2=1)[source]#
Return specified diagonals.
Ifa is 2-D, returns the diagonal ofa with the given offset,i.e., the collection of elements of the form
a[i,i+offset]. Ifa has more than two dimensions, then the axes specified byaxis1andaxis2 are used to determine the 2-D sub-array whose diagonal isreturned. The shape of the resulting array can be determined byremovingaxis1 andaxis2 and appending an index to the right equalto the size of the resulting diagonals.In versions of NumPy prior to 1.7, this function always returned a new,independent array containing a copy of the values in the diagonal.
In NumPy 1.7 and 1.8, it continues to return a copy of the diagonal,but depending on this fact is deprecated. Writing to the resultingarray continues to work as it used to, but a FutureWarning is issued.
Starting in NumPy 1.9 it returns a read-only view on the original array.Attempting to write to the resulting array will produce an error.
In some future release, it will return a read/write view and writing tothe returned array will alter your original array. The returned arraywill have the same type as the input array.
If you don’t write to the array returned by this function, then you canjust ignore all of the above.
If you depend on the current behavior, then we suggest copying thereturned array explicitly, i.e., use
np.diagonal(a).copy()insteadof justnp.diagonal(a). This will work with both past and futureversions of NumPy.- Parameters:
- aarray_like
Array from which the diagonals are taken.
- offsetint, optional
Offset of the diagonal from the main diagonal. Can be positive ornegative. Defaults to main diagonal (0).
- axis1int, optional
Axis to be used as the first axis of the 2-D sub-arrays from whichthe diagonals should be taken. Defaults to first axis (0).
- axis2int, optional
Axis to be used as the second axis of the 2-D sub-arrays fromwhich the diagonals should be taken. Defaults to second axis (1).
- Returns:
- array_of_diagonalsndarray
Ifa is 2-D, then a 1-D array containing the diagonal and of thesame type asa is returned unlessa is a
matrix, in which casea 1-D array rather than a (2-D)matrixis returned in order tomaintain backward compatibility.If
a.ndim>2, then the dimensions specified byaxis1 andaxis2are removed, and a new axis inserted at the end corresponding to thediagonal.
- Raises:
- ValueError
If the dimension ofa is less than 2.
See also
Examples
>>>importnumpyasnp>>>a=np.arange(4).reshape(2,2)>>>aarray([[0, 1], [2, 3]])>>>a.diagonal()array([0, 3])>>>a.diagonal(1)array([1])
A 3-D example:
>>>a=np.arange(8).reshape(2,2,2);aarray([[[0, 1], [2, 3]], [[4, 5], [6, 7]]])>>>a.diagonal(0,# Main diagonals of two arrays created by skipping...0,# across the outer(left)-most axis last and...1)# the "middle" (row) axis first.array([[0, 6], [1, 7]])
The sub-arrays whose main diagonals we just obtained; note that eachcorresponds to fixing the right-most (column) axis, and that thediagonals are “packed” in rows.
>>>a[:,:,0]# main diagonal is [0 6]array([[0, 2], [4, 6]])>>>a[:,:,1]# main diagonal is [1 7]array([[1, 3], [5, 7]])
The anti-diagonal can be obtained by reversing the order of elementsusing either
numpy.flipudornumpy.fliplr.>>>a=np.arange(9).reshape(3,3)>>>aarray([[0, 1, 2], [3, 4, 5], [6, 7, 8]])>>>np.fliplr(a).diagonal()# Horizontal fliparray([2, 4, 6])>>>np.flipud(a).diagonal()# Vertical fliparray([6, 4, 2])
Note that the order in which the diagonal is retrieved varies dependingon the flip function.