How to create a n by n identity matrix in numpy?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

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

print(identity(3))

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

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Inventory Demand Forecasting using Machine Learning in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Creating your own embeddings using Glove and Word2vec
We all at some point in time wished to create our own language as a child! But what if certain words always cooccur with another in a corpus? Thus you can make your own model which will understand which word goes with which one, which words are often coming together etc. This all can be done by building a custom embeddings model which we create in this project

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.

Time Series Analysis Project in R on Stock Market forecasting
In this time series project, you will build a model to predict the stock prices and identify the best time series forecasting model that gives reliable and authentic results for decision making.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

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.

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.

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.

Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.