# Gram or Gramian Matrix – Explained with Example in Python

September 19, 2021 2021-10-04 7:24## Gram or Gramian Matrix – Explained with Example in Python

Learn everything about a Gram or Gramian matrix through mathematical examples and Python implementations while discovering its use in Neural Style Transfer.

Excited to start learning? Let’s start off by understanding what is a Gram Matrix or a Gramian Matrix.

## What is a Gram or Gramian Matrix?

In simple words, a Gram matrix (often referred to as a Gramian matrix) is a matrix created by multiplying a matrix with its own transpose.

For a matrix, , whose columns are the vectors, , its Gramian matrix is defined as,

The Gramian matrix provides a degree of correlation between the vectors of the matrix since we’re multiplying a matrix with its own transpose.

## Mathematical Example of a Gram or Gramian Matrix

Consider a matrix, , given as follows,

Then, the transpose of this matrix is computed as,

So, the gram or gramian matrix is calculated by taking the matrix product of the two matrices and as follows,

Here, the matrix is the Gram or Gramian matrix.

## Gram or Gramian Matrix implementation in Python using NumPy

The Gram or Gramian matrix can be implemented in Python using just 4 lines of code using the NumPy library.

NumPy, or Numerical Python, is an open-source Python library that helps you perform simple as well as complex computations on numerical data. It is the go-to scientific computation library for beginners as well as advanced Python programmers and it is used mostly by statisticians, data scientists, and engineers.

If you do not have the NumPy library installed, you can run the following command through your command line/terminal to install NumPy using Python’s package manager (pip),

pip install numpy

After you’ve successfully installed NumPy, you can import it in Python using the following line of code,

import numpy as np

Here, we’re importing the NumPy library/package as `np`

since it is a general convention to use the short-hand name. If you are unfamiliar with NumPy and would want to master it, you can take this NumPy for Scientific Computation with Python course.

Now, that you know how to install and import NumPy in Python, you can write the following lines of code to implement a Gram or Gramian matrix in Python,

# Importing the NumPy library as np import numpy as np # Creating a matrix V V = np.array([[1, 2], [3, 4]]) # Creating the Gram Matrix G G = V.T.dot(V) # Printing the matrix G print(G)

[[10 14] [14 20]]

Nice! You’ve successfully implemented the Gram or Gramian matrix in Python using NumPy.

## Gram or Gramian Matrix in Neural Style Transfer

The Gram or Gramian Matrix is used in Neural Style Transfer, a Machine Learning use case for transferring the style of image into another.

Since the Gram or Gramian matrix provides a degree of correlation between the vectors of a matrix, Machine Learning engineers can find the correlation between parameters of different Convolutional Filters in a Convolutional Neural Network.

You can watch this video to learn more about it in detail:

## In Conclusion

You now know what is a Gram or Gramian matrix and how to implement it in Python. If you have any questions, please feel free to write to us in the comments and we will get back to you.

*Are you interested in working with data? Get a free trial month of LinkedIn Learning and learn Data Science and Machine Learning courses using Python, R, and SQL from top instructors. *

** Enroll in any one of the courses from the following fields today on LinkedIn Learning and start your free month of learning:**

*Disclaimer: When you subscribe to LinkedIn Learning, we may earn a small commission as an affiliate. We love the platform so much so that we have been using it ourselves at The Click Reader. *