Numpy Tricks for your Data Science Projects

Tricks to create array and matrix, perform matrix operations, solve linear algebra problems, and common data science methods you may not know

If you have been creating an array or matrix or perform matrix operations with for loop, or struggle with solving a system of equations or preprocessing the data for machine learning algorithms, this tutorial will introduce you to the methods available in Numpy that could automate your process and help you create and transform data with ease.

Source: indianairproduction

This tutorial covers how to:

  • Create an array
  • Create a matrix
  • Perform operations on matrices
  • Perform linear algebra operations
  • Perform common methods for data science

If you just care about a specific section to help you with your data science project, feel free to skip one or multiple sections and go straight into the section that fits your interest.

Start with importing NumPy library

import numpy as np

Create an Array

This section will show you how to create an array with

  • a specific type of data
  • specified range
  • the specified number of elements in the range.

Create a matrix

This section will show you how to create:

  • an empty/zeros/ones matrix
  • an identity matrix
  • a diagonal matrix
  • a random matrix with the specified range, dimension, and datatype
  • a copy of a matrix with the same dimension but different elements

Operations on matrices

This section will show you how to:

  • Perform element-wise multiplication and matrix multiplication
  • Find the transpose of a matrix (an operator that switches the rows and columns)
  • Find the trace of a matrix (the sum of the diagonal elements)
  • Reshape the matrix
  • Find the slice of a matrix with boolean arrays

Linear Algebra

This section will show you how to:

  • Solve the system of linear equations
  • Find the inverse of a matrix
  • Find matrix norm
  • Compute Single Value Decomposition and eigenvalues

Common Method for Data Science

This section will show you how to:

  • Flatten a matrix
  • Reshape the matrix
  • Change the type of elements
  • Concatenate 2 matrices
  • Create a copy of A with a specified range
  • Add the elements in the same rows or columns to create a new vector
  • Shuffle the elements in the array


I hope some techniques in this tutorial give you the “Aha” moment. Automating some of the basic procedures would help you focus on other advanced procedures in your data science projects. You may not be able to remember every method for now. In fact, I recommend you not to do so. Pick up some methods that are useful and apply them. You will gradually be familiar with these methods as you tackle more data science projects. This is a comprehensive cheat sheet that puts together the methods I cover in this tutorial. You could use this as a review or for your future reference.

Feel free to fork and play with the code for this article in this Github repo.

I like to write about basic data science concepts and play with different algorithms and data science tools. You could connect with me on LinkedIn and Twitter.

Star this repo if you want to check out the codes for all of the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these:

Written by

Data scientist. I share a little bit of goodness every day through articles and daily data science tips:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store