How to subtract a 1d array from a 2d array where each item of 1d array subtracts from respective row?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

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

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

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

0

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]]]

Relevant Projects

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.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

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

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

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.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

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.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.