How to calculate Diagonal of a Matrix?

How to calculate Diagonal of a Matrix?

How to calculate Diagonal of a Matrix?

This recipe helps you calculate Diagonal of a Matrix


Recipe Objective

Have to tried to calculate diagonal elements and sum of diagonal values.

So this is the recipe on how we can calculate Diagonal of a Matrix.

Step 1 - Import the library

import numpy as np

We have only imported numpy which is needed.

Step 2 - Creating a matrix

We have created a matrix on which we will perform the operation. matrix = np.array([[1, 2, 3, 23], [4, 5, 6, 25], [7, 8, 9, 28], [10, 11, 12, 41]])

Step 3 - Finding elements

We can find diagonal elements by the function diagonal and by using sum function we can find the sum of the elements. print(matrix.diagonal()) print(matrix.diagonal().sum()) So the output comes as

[ 1  5  9 41]


Relevant Projects

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.