How to subtract a 1d array from a 2d array where each item of 1d array subtracts from respective row in numpy

This recipe helps you subtract a 1d array from a 2d array where each item of 1d array subtracts from respective row in numpy

Recipe Objective

How to subtract a 1d array from a 2d array, where each item of 1d array subtracts from respective row? This is possible we just have to take 2 arrays, 1 is 1-dimensional and other is 2-dimensional array. After that we can use the simple substract symbol "-" to make the substraction, and in the 2 dimensional array just select all the rows and none of the columns for substraction.

Step 1 - Import library

import numpy as np

Step 2 - Take Sample data

first_1D_data = np.array([1,2,3]) second_2D_data = np.array([[4,5,6],[1,5,3]]) print("This is 1D array:","\n", first_1D_data, "\n") print("This is 2D array:","\n",second_2D_data)

This is 1D array: 
 [1 2 3] 

This is 2D array: 
 [[4 5 6]
 [1 5 3]]

Step 3 - Print Results

Result = first_1D_data - second_2D_data[:,None] print("This is the Result of Substraction between 1D array and 2D array:","\n",Result)

This is the Result of Substraction between 1D array and 2D array: 
 [[[-3 -3 -3]]

 [[ 0 -3  0]]]

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

Multi-Class Text Classification with Deep Learning using BERT
In this deep learning project, you will implement one of the most popular state of the art Transformer models, BERT for Multi-Class Text Classification

Classification Projects on Machine Learning for Beginners - 2
Learn to implement various ensemble techniques to predict license status for a given business.

AWS MLOps Project for Gaussian Process Time Series Modeling
MLOps Project to Build and Deploy a Gaussian Process Time Series Model in Python on AWS

Learn to Build an End-to-End Machine Learning Pipeline - Part 1
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, addressing a major challenge in the logistics industry.

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Linear Regression Model Project in Python for Beginners Part 1
Machine Learning Linear Regression Project in Python to build a simple linear regression model and master the fundamentals of regression for beginners.

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.

Build Multi Class Text Classification Models with RNN and LSTM
In this Deep Learning Project, you will use the customer complaints data about consumer financial products to build multi-class text classification models using RNN and LSTM.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.