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

0

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

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

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.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

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.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

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.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble 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.