How to do matrix multiplication in R?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

How to do matrix multiplication in R?

How to do matrix multiplication in R?

This recipe helps you do matrix multiplication in R

0

Recipe Objective

How to do matrix multiplication in R? A matrix is a two-dimensional data structure i.e. a matrix contains rows and columns. Matrices are used for performing mathematical calculations.. Matrix multiplication produces a single matrix by multiplying two different given matrices. The recipe provides an example of matrix multiplication.

Step 1 - Create a matrix

Syntax- A matrix function takes the following input values : matrix(data,nrow,ncol,byrow,dimnames). data - is our input matrix value, nrow and ncol - are the number of rows and columns required, if byrow=TRUE , the input numbers are arranged by rows and if byrow=FALSE, then they are arranged by columns. dimnames - assigns names to rows and columns of a data frame.

m1 <- matrix(c(1:8), nrow = 4, ncol = 4, byrow = TRUE) print(m1)
"m1 matrix":
     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8
[3,]    1    2    3    4
[4,]    5    6    7    8

m2 <- matrix(c(11:18), nrow = 4, ncol = 4, byrow = TRUE) print(m2)
"m2 matrix":
     [,1] [,2] [,3] [,4]
[1,]   11   12   13   14
[2,]   15   16   17   18
[3,]   11   12   13   14
[4,]   15   16   17   18

Step 2 - Matrix Multiplication

print(m1*m2) # element wise multiplication
"Output of the code is ":
     [,1] [,2] [,3] [,4]
[1,]   11   24   39   56
[2,]   75   96  119  144
[3,]   11   24   39   56
[4,]   75   96  119  144
print(m1%*%m2) # inner product of the two
"Output of the code is ":

print(m1%*%m2)        # inner product of the two matrices
     [,1] [,2] [,3] [,4]
[1,]  134  144  154  164
[2,]  342  368  394  420
[3,]  134  144  154  164
[4,]  342  368  394  420

Relevant Projects

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

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.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

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.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

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.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.