How to add and subtract between matrices using numpy in python

This recipe helps you add and subtract between matrices using numpy in python

Recipe Objective

Have you tried to add or substract matrices in python?

So this is the recipe on how we can add and subtract between matrices.

Explore the BERT Variants - ALBERT vs DistilBERT

Step 1 - Importing Library

import numpy as np

We have only imported numpy which is needed.

Step 2 - Creating Matrices

We have created two matrices on which we will perform operations. matrixA = np.array([[2, 3, 23], [5, 6, 25], [8, 9, 28]]) matrixB = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

Step 3 - Adding and Substracting Matices

First we are adding the two matrices using the numpy function np.add. After that we are performing substraction by using the numpy function np.subtract, in both the function we have passed the matrices. print(np.add(matrixA, matrixB)) print(np.subtract(matrixA, matrixB)) So the output comes as

[[ 3  5 26]
 [ 9 11 31]
 [15 17 37]]

[[ 1  1 20]
 [ 1  1 19]
 [ 1  1 19]]

Download Materials


What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

FEAST Feature Store Example for Scaling Machine Learning
FEAST Feature Store Example- Learn to use FEAST Feature Store to manage, store, and discover features for customer churn prediction machine learning project.

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.

Build a Langchain Streamlit Chatbot for EDA using LLMs
In this LLM project, you will build a Streamlit Chatbot integrated with Langchain technology for natural language interactions with a SQL database, facilitating real-time visualization and insightful insights, streamlining data exploration and analysis.

CycleGAN Implementation for Image-To-Image Translation
In this GAN Deep Learning Project, you will learn how to build an image to image translation model in PyTorch with Cycle GAN.

Build an Image Segmentation Model using Amazon SageMaker
In this Machine Learning Project, you will learn to implement the UNet Architecture and build an Image Segmentation Model using Amazon SageMaker

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

Deep Learning Project for Text Detection in Images using Python
CV2 Text Detection Code for Images using Python -Build a CRNN deep learning model to predict the single-line text in a given image.

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.