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

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

Relevant Projects

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.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

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.

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

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

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.

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.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.