DEV Community

Cover image for Numpy Cheatsheet
Jagroop Singh
Jagroop Singh

Posted on

Numpy Cheatsheet

Numpy :
Numpy is a python library which is used to perform wide variety of mathematical operations on array.

Numpy

The question now arises as to why we require NumPy when we have lists and can execute these operations directly in Python ?. πŸ€”
So, the answer is that NumPy intends to deliver an array object that is up to 50 times faster than typical Python lists or Python operations. ⚑️

Where it used ?
NumPy πŸš€ enables efficient numerical computing and array operations in :

  • Scientific computing
  • Data analysis
  • Machine learning
  • Signal processing
  • Image processing
  • Statistical analysis
  • Financial and economic modelling

Basics of NumPy:

How to install :

pip install numpy
Enter fullscreen mode Exit fullscreen mode

How to import :

import numpy as np 
Enter fullscreen mode Exit fullscreen mode

Creating NumPy Array :

arr = np.array([1, 2, 3, 4])
Enter fullscreen mode Exit fullscreen mode

Change DataType of Array :

arr = np.array([1,2,3],dtype=float)
Enter fullscreen mode Exit fullscreen mode

Creating 2-Dimensional Array :

arr = np.array([(1,2,3,4),(7,8,9,10)],dtype=int)
Enter fullscreen mode Exit fullscreen mode

Dummy Arrays Creation :

Creating Dummy array of zeroes(2x3 matrix) :

arr = np.zeros((2,3))
Enter fullscreen mode Exit fullscreen mode

Creating Dummy Array of Specific Number :
Here (3,4) refers to (rows x columns) with all values of 3.

arr = np.full((3,4),3)
Enter fullscreen mode Exit fullscreen mode

Creating Dummy arrays of ones (3x4 matrix):

arr = np.ones((3,4))
Enter fullscreen mode Exit fullscreen mode

Creating array of 0 with 1 on diagonal (4x4 matrix) :

arr = np.eye(4)
Enter fullscreen mode Exit fullscreen mode

Creating matrix of (3x5 matrix) random number:

arr = np.random.rand(3,5)
Enter fullscreen mode Exit fullscreen mode

Properties of Arrays :

  • arr.size - Returns number of elements in array.
  • arr.shape - Returns dimensions of array(rows, columns)
  • arr.dtype - Returns data type of elements in array
  • arr.ndim - Returns number of array dimension

Arithmetic Operations

Assume a and b are array or matrices.
Addition:

np.add(a,b)
Enter fullscreen mode Exit fullscreen mode

Subtraction

np.subtract(a,b)
Enter fullscreen mode Exit fullscreen mode

Division :

np.divide(a,b)
Enter fullscreen mode Exit fullscreen mode

Multiply :

np.multiply(a,b)
Enter fullscreen mode Exit fullscreen mode

Exponential:

np.exp(a)
Enter fullscreen mode Exit fullscreen mode

Square root :

np.sqrt(a)
Enter fullscreen mode Exit fullscreen mode

Logarithm :

np.log(a)
Enter fullscreen mode Exit fullscreen mode

Dot Product :

a.dot(b) 
Enter fullscreen mode Exit fullscreen mode

Important In-built functions

Creating Copy of Array :

arr_two = arr_one.copy()
Enter fullscreen mode Exit fullscreen mode

Sorting of an Array :

sorted_arr = arr.sort()
Enter fullscreen mode Exit fullscreen mode

Transpose of an Array :

t = np.transpose(a)
Enter fullscreen mode Exit fullscreen mode

Need Help ?

np.info(np.ndarray.dtype)
Enter fullscreen mode Exit fullscreen mode

That's all in this blog.Feel free to add more useful methods to this cheatsheet! πŸ“βœ¨

Top comments (4)

Collapse
 
works profile image
Web

I use this :

 np.concatenate((a,b),axis=0)
Enter fullscreen mode Exit fullscreen mode

For Concatenate arrays

Collapse
 
jagroop2001 profile image
Jagroop Singh

Nice one !!
Thanks for sharing.

Collapse
 
works profile image
Web

@jagroop2001 , Very insightful!

I kept up with you over your adventure, and I eagerly await your blogs each week. I hope you'll share it frequently.

Collapse
 
jagroop2001 profile image
Jagroop Singh

Thanks, @works!!
Thank you for your support. I'd attempt to maintain consistency and post twice a week.