How to create a n by n identity matrix in numpy?

How to create a n by n identity matrix in numpy?

How to create a n by n identity matrix in numpy?

This recipe helps you create a n by n identity matrix in numpy


Recipe Objective

Identity matrix are ones having diagonal elements to 1 and rest of elemets to be 0.

So this recipe is a short example on how to create a n by n identity matrix. Let's get started.

Step 1 - Defining the identity function

def identity(n): m=[[0 for x in range(n)] for y in range(n)] for i in range(0,n): m[i][i] = 1 return m

In 2nd line, we have created a 0 matrix of size n by n. Thereby, using for loop, updating the diagonal elements.

Step 2 - Printing a 3 by 3 matrix


We are calling identiy function of 3 by 3 matrix. Input needed for the function was 3.

Step 3 - Let's look at our dataset now

Once we run the above code snippet, we will see:

[[1, 0, 0], [0, 1, 0], [0, 0, 1]]

Relevant Projects

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

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.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

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.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.