An Ultimate Cheat Sheet for Numpy

An Ultimate Cheat Sheet for Numpy

Numpy is an open-source Python library. This library is essential for data scientists who use python. Cheat Sheet for Numpy: Numpy Array Basics, Repeat, Mathematics, Statistics, Initializing Different Types of Arrays, Rearranging or Reorganizing Arrays, Indexing and Slicing of Array, Adding Rows or Columns, Append, Insert, Delete, and Sort, Random, File Import, Save, and, Load

Numpy is an open-source Python library. This library is essential for data scientists who use python. Some other essential libraries like Pandas, Scipy are built on the Numpy library. So I decided to make a cheat sheet. Here I included all the Numpy functions that I used so far. And I believe these functions will be enough for you to do your job in your everyday work life as a data scientist or a data analyst.

I will start with the very basic Numpy functions slowly moved towards the more advanced ones. But using Numpy is easy. You will not find any complicated coding trick here.

What is Numpy?

In Numpy documentation, Numpy is defined like this:

NumPy is the fundamental package for scientific computing in Python. It is a Python library that provides a multidimensional array object, various derived objects (such as masked arrays and matrices), and an assortment of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation and much more.

I use this library every day. Most data scientists do if they are python users. It is fast, easy to use, understandable, uncomplicated. I don’t want to write too much about how and why it is so good. Because you will see it for yourself as you read through this article.

My goal is to document Numpy’s every day used methods.

As the title says, it is a guide to Numpy. It can be used as a cheat sheet as well. If you use the Numpy library or planning to use in the future, or trying to learn, this page can be a great resource for your everyday life.

These are the topics that will be covered here:

  1. Numpy Array Basics
  2. Repeat
  3. Mathematics
  4. Statistics
  5. Initializing Different Types of Arrays
  6. Rearranging or Reorganizing Arrays
  7. Indexing and Slicing of Array
  8. Adding Rows or Columns
  9. Append, Insert, Delete, and Sort
  10. Random
  11. File Import, Save, and, Load

Let’s start!!


Numpy Array Basics

This section is

I used a Jupyter Notebook for this whole exercise. First import Numpy.

import numpy as np

Make a Numpy array. To do that we need to pass a Python list.

input:

a = np.array([1,2,3])
a

output:

array([1, 2, 3])

In the array ‘a’ I used all the integers. Now, make an array of floats:

input:

b = np.array([[9.0, 10.0, 6.0], [6.0,1.0,7.0]])
b

output:

array([[ 9., 10.,  6.],
       [ 6.,  1.,  7.]])

Let’s try to make an array with both ints and floats:

input:

np.array([1, 3.0, 0.004, -2])

output:

array([ 1\.   ,  3\.   ,  0.004, -2\.   ])

Notice, Numpy automatically converted the integers into floats!

Find out the dimensions of array a and b:

input:

a.ndim

output:

1

input:

b.ndim

output:

2

Array ‘a’ is a one-dimensional array and array b is a two-dimensional array.

Now, find the shape of the array ‘a’ and ‘b’:

input:

a.shape

output:

(3,)

input:

b.shape

output:

(2, 3)

Array ‘a’ is a one-dimensional array. So, it has only one value in shape. But array ‘b’ is a two-dimensional array. So, it’s shape is 2 x 3. That means it has 2 rows and 3 columns.

Find the length of the arrays:

input:

len(a)

output:

3

input:

len(b)

output:

2

Array ‘a’ has length 3 because it has 3 elements in it. Array ‘b’ is a two-dimensional array. So, the length of the array does not mean the number of elements in it. The length means the number of one-dimensional arrays in it or the number of rows in it. It has two rows. So, the length is 2.

Repeat

There are a few different ways to repeat the elements of an array. If you want to repeat the whole array,

input:

np.array([2,4,6]*4)

output:

array([2, 4, 6, 2, 4, 6, 2, 4, 6, 2, 4, 6])

Look, the array [2, 4, 6] was repeated 4 times.

Here is how to do the elementwise repetition,

input:

np.repeat([1,2,3], 3)

output:

array([1, 1, 1, 2, 2, 2, 3, 3, 3])

This time each element was repeated 3 times.

Let’s use this for a two-dimensional array,

input:

arr = np.array([[2, 4, 6]])
arr

output:

array([[2, 4, 6]])

Now, use repeat on it:

input:

np.repeat(arr,3,axis=0)

output:

array([[2, 4, 6],
       [2, 4, 6],
       [2, 4, 6]])

Here, we mentioned axis = 0. So, the repetition occurred in the axis-0 direction or rows direction.

input:

np.repeat(arr,3,axis=1)

output:

array([[2, 2, 2, 4, 4, 4, 6, 6, 6]])

Axis 1 indicates the direction of columns. So, repetition happens in the column’s direction.

Mathematics

In this section, I am going to show the mathematical operations. Most of the operations are self-explanatory. I will start with mathematical operations on one array.

input:

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

output:

array([1, 2, 3, 4])

input:

a+2

output:

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

It adds 2 to each element of the array.

input:

a-2

output:

array([-1,  0,  1,  2])

You can simply use similar operations this way such as:

input:

a/2

output:

array([0.5, 1\. , 1.5, 2\. ])

input:

a**2

output:

array([ 1,  4,  9, 16], dtype=int32)

Two asterisks mean exponents. Each element in ‘a’ is squared.

input:

np.sqrt(a)  #square root

output:

array([1\.        , 1.41421356, 1.73205081, 2\.        ])

We can also perform some trigonometric operations:

input:

np.cos(a)

Output:

array([ 0.54030231, -0.41614684, -0.9899925 , -0.65364362])

input:

np.sin(a)

output:

array([ 0.84147098,  0.90929743,  0.14112001, -0.7568025 ])

input:

np.tan(a)

output:

array([ 1.55740772, -2.18503986, -0.14254654,  1.15782128])

Now see how we can do some mathematical operation in two arrays or matrices. First, make one more array,

input:

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

output:

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

Just as a reminder, our array ‘a’ looked like this:

array([1, 2, 3, 4])

Now, we have two arrays, a and b. Let’s do the same mathematical operations. Again, it’s simple and self-explanatory,

input:

a + b

output:

array([ 4,  6,  8, 10])

In the same way, you can do these following operations:

a - b
a*b
a/b
a**b

Another widely used operation is,

input:

a.dot(b)

output:

50

What is a.dot(b)? It’s element-wise multiplication and then addition like this,

1*3 + 2*4 + 3*5 + 4*6

where array ‘a’ is [1,2,3,4] and array b is [3,4,5,6].

You can write the syntax a bit differently as well,

np.dot(a, b)

This works the same. The output will be 50.

We can use this dot process in multi-dimensional arrays. Let’s make two multi-dimensional arrays for that,

input:

c = np.array([[3, 5, 1], [6, 4, 9]])
c

output:

array([[3, 5, 1],
       [6, 4, 9]])

input:

d = np.array([[5,2], [7,9], [4, 3]])
d

output:

array([[5, 2],
       [7, 9],
       [4, 3]])

We are ready for a ‘dot’ operation on a multi-dimensional array,

input:

c.dot(d)

output:

array([[54, 54],
       [94, 75]])

When the inputs are 2D array, ‘dot’ function behaves like matrix multiplication.

That means you can only perform the ‘dot’ operation when the number of columns of the first array matches the number of rows in the second array.

If the first array is m x n, the second array should be n x p.

Matrix multiplication has another expression,

input:

np.matmul(c, d)

output:

array([[54, 54],
       [94, 75]])

‘np.matmul’ does not work in one-dimensional arrays

Remember, this multiplication rule does not apply in other operations such as addition, deduction or division. We need to have the arrays of same shape and size to add, subtract or divide one matrix by another.

Statistics

Numpy has basic statistical operations as well. Here are some examples.

Make a new array first.

input:

x = np.array([1,3,4,6,-3,-2])
x.sum()

output:

9

input:

x.max()

output:

6

input:

x.min()

output:

-3

input:

x.mean()

output:

1.5

input:

x.std()  #standard deviation

output:

3.2015621187164243

There are two other very useful functions which are not exactly statistical,

input:

x.argmin()

output:

4

input:

x.argmax()

output:

3

What ‘argmin()’ or ‘argmax()’?

‘argmin()’ gives you the index of the minimum element of the array and ‘argmax()’ returns the index of the maximum value of the array.

The minimum element of the array ‘x’ is -3 and the maximum element of array ‘x’ is 6. Now check if their index matches the result.

machine-learning data-science numpy python developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Machine Learning Developers in India

We supply you with world class machine learning experts / ML Developers with years of domain experience who can add more value to your business.

Data Science Projects | Data Science | Machine Learning | Python

Practice your skills in Data Science with Python, by learning and then trying all these hands-on, interactive projects, that I have posted for you.

Data Science Projects | Data Science | Machine Learning | Python

Practice your skills in Data Science with Python, by learning and then trying all these hands-on, interactive projects, that I have posted for you.

Data Science Projects | Data Science | Machine Learning | Python

Practice your skills in Data Science with Python, by learning and then trying all these hands-on, interactive projects, that I have posted for you.

Data Science Projects | Data Science | Machine Learning | Python

Practice your skills in Data Science with Python, by learning and then trying all these hands-on, interactive projects, that I have posted for you.