Title: Getting The Diagonal Of A Matrix
Slug: getting_the_diagonal_of_a_matrix
Summary: How to get the diagonal of a matrix in Python.
Date: 2017-09-02 12:00
Category: Machine Learning
Tags: Vectors Matrices Arrays
Authors: Chris Albon

Preliminaries


In [1]:
# Load library
import numpy as np

Create Matrix


In [2]:
# Create matrix
matrix = np.array([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9]])

Get The Diagonal


In [3]:
# Return diagonal elements
matrix.diagonal()


Out[3]:
array([1, 5, 9])

Calculate The Trace


In [4]:
# Calculate the tracre of the matrix
matrix.diagonal().sum()


Out[4]:
15