How to Divide each element of a matrix by a numerical value?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

How to Divide each element of a matrix by a numerical value?

How to Divide each element of a matrix by a numerical value?

This recipe helps you Divide each element of a matrix by a numerical value

Recipe Objective

Have to tried to do any mathematical function on all the values of a feature. Doing it manually may be a hectic work.

So this is the recipe on how we can divide each element of a matrix by a numerical value.

Step 1 - Import the library

import numpy as np

We have only imported numpy which is needed.

Step 2 - Creating a matrix

We have created a matrix on which we will perform the operation. matrixA = np.array([[2, 3, 23], [5, 6, 25], [8, 9, 28]])

Step 3 - Dividing each elements

First we have created a lambda function which is just like loop as it will iterate the function assined to it to the all elements. Then we have converted the martix into a vector form and finally we have passed the matrix in the function. add_100 = lambda i: i / 9 vectorized_add_100 = np.vectorize(add_100) print(vectorized_add_100(matrixA)) So the output comes as

[[0.22222222 0.33333333 2.55555556]
 [0.55555556 0.66666667 2.77777778]
 [0.88888889 1.         3.11111111]]

Download Materials

Relevant Projects

Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

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.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Churn Prediction in Telecom using Machine Learning in R
Estimating churners before they discontinue using a product or service is extremely important. In this ML project, you will develop a churn prediction model in telecom to predict customers who are most likely subject to churn.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.

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.

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.