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

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

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

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

Build a Customer Churn Prediction Model using Decision Trees
Develop a customer churn prediction model using decision tree machine learning algorithms and data science on streaming service data.

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

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

MLOps Project to Build Search Relevancy Algorithm with SBERT
In this MLOps SBERT project you will learn to build and deploy an accurate and scalable search algorithm on AWS using SBERT and ANNOY to enhance search relevancy in news articles.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

Build a Review Classification Model using Gated Recurrent Unit
In this Machine Learning project, you will build a classification model in python to classify the reviews of an app on a scale of 1 to 5 using Gated Recurrent Unit.

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.