• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / NumPy / Linear Algebra with NumPy / 9 Basic Linear Algebra Operations with NumPy

9 Basic Linear Algebra Operations with NumPy

June 27, 2019 by cmdlinetips

Linear algebra is one of the most important mathematical topics that is highly useful to do a good data science. Learning the basics of linear algebra adds a valuable tool set to your data science skill.

Python’s NumPy has fast efficient functions for all standard linear albegra/matrix operations. Here we will see 9 important and useful linear algebra operations using NumPy and linalg module in NumPy.

1. How to Compute Dot product of two vectors?

Let us create two 1d-arrays using np.array function.

x = np.array([1,2,3])
y = np.array([4,5,6])

And we can print to see the content of the two arrays.

print(x)
print(y)
[1 2 3]
[4 5 6]

We can compute dot product of the two NumPy arrays using np.dot() function that takes the two 1d-array as inputs. Note that the order input arguments does not matter for the dot product of two vectors.

print(np.dot(x,y))
print(np.dot(y,x))

32
32

2. How To Compute Dot product of a Matrix and vector?

We can also compute dot product of a vector and a matrix using np.dot() function. Let us create a 2×3 matrix with NumPy’s array function.

a = np.array([[1,2,3], [4,5,6]])
print(a)
[[1 2 3]
 [4 5 6]]

We can NumPy’s dot function to compute dot product of a matrix and vector. And the result would be a vector.

np.dot(a,x)
array([14, 32])

3. How to Compute transpose of a matrix?

NumPy’s transpose() function changes rows with columns. In this example, we start with 2 x 3 matrix.

print(a)
[[1 2 3]
 [4 5 6]]

After transposing, we get 3 x 2 matrix.

np.transpose(a)
array([[1, 4],
       [2, 5],
       [3, 6]])

4. How to Create Identity Matrix?

Identity matrix is matrix with ones along the diagnola and zeroes in all off-diagonal elements. NumPy’s eye() function creates identity matrix. We just need to provide the dimension of identity matrix we want.

np.eye(3)

array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

5. How to Compute Inverse of a Matrix?

Computing inverse of a matrix is one of the important concepts and has many useful applications. Not all matrices can have inverse. Finding an inverse of a matrix A is find a matrix B such that the product of A with B is the identity matrix.

Let us create a 3 x 3 NumPy array.

b = np.array([[1.,2.,3.], [10.,5,6.],[7.,8.,9.]])
print(b)

[[ 1.  2.  3.]
 [10.  5.  6.]
 [ 7.  8.  9.]]

NumPy’s linalg module has the function inv.

b_inv = np.linalg.inv(b)
print(b_inv)

[[-0.08333333  0.16666667 -0.08333333]
 [-1.33333333 -0.33333333  0.66666667]
 [ 1.25        0.16666667 -0.41666667]]

6. How to Perform Matrix Multiplication?

Matrix multiplication or product of matrices is one of the most common operations we do in linear algebra. We can use NumPy’s dot() function to compute matrix multiplication.

Let us see a example of matrix multiplication using the previous example of computing matrix inverse. We noted that, if we multiply a Matrix and its inverse, we get identity matrix as the result.

Here we use NumPy’ dot() function with a matrix and its inverse. We get output that looks like a identity matrix.

np.dot(b,np.linalg.inv(b) )

array([[ 1.00000000e+00, -2.77555756e-17, -1.11022302e-16],
       [ 0.00000000e+00,  1.00000000e+00,  2.22044605e-16],
       [ 0.00000000e+00, -8.32667268e-17,  1.00000000e+00]])

Let us check if the matrix we have is close to identical matrix. NumPy’s allclose() function can help us with that.

np.allclose(np.dot(b, b_inv), np.eye(3))

True

7. How to Compute Trace of a Matrix?

Let us create a matrix a.

print(a)

[[1 2 3]
 [4 5 6]
 [7 8 9]]

We can compute trace of a matrix using NumPy’s trace() function..

print(np.trace(a))

8. How to Compute Determinant of a Matrix?

Let us create 2×2 square matrix.

a = np.array([[3.,4.],[5,6]])
print(a)

[[3. 4.]
 [5. 6.]]

We can compute determinant of a matrix in NumPy with linalg module’s det() function.

np.linalg.det(a)

-2.0000000000000004

9. How to Compute Eigen Value and Eigen Vector of a Matrix with NumPy?

Let us create diagonal matrix. We can create a diagonal matrix with diag() function in NumPy.

a = np.diag((3, 4, 5))
a

array([[3, 0, 0],
       [0, 4, 0],
       [0, 0, 5]])

We can use linalg module’s eig() function to computer eigen vector and eigen values of a matrix. In our example, since our input matrix is a diagonal matrix, the eigen vectors make a identity matrix and the eigen values are simply the diagonal elements of the input matrix.

e_val,e_vec = np.linalg.eig(a)
print(e_val)

[3. 4. 5.]
print(e_vec)

[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

NumPy is the fundamental package for scientific computing with Python. It contains among other things: a powerful N-dimensional array object sophisticated (broadcasting) functions tools for integrating C/C++ and Fortran code useful linear algebra, Fourier transform, and random number capabilities10 Basic Arithmetic Operations with NumPy array Linear Regression fit with Matrix Multiplication in PythonLinear Regression Using Matrix Multiplication in Python Using NumPy Default Thumbnail3 Basic Commands to Manipulate NumPy 2d-arrays Default Thumbnail12 Basic Commands with NumPy Array

Filed Under: Linear Algebra with NumPy, numpy eye Tagged With: Linear Algebra with NumPy, numpy eye, numpy matrix inverse

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version