Jagroop Singh

Posted on

# Numpy Cheatsheet

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

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
``````

How to import :

``````import numpy as np
``````

Creating NumPy Array :

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

Change DataType of Array :

``````arr = np.array([1,2,3],dtype=float)
``````

Creating 2-Dimensional Array :

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

## Dummy Arrays Creation :

Creating Dummy array of zeroes(2x3 matrix) :

``````arr = np.zeros((2,3))
``````

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)
``````

Creating Dummy arrays of ones (3x4 matrix):

``````arr = np.ones((3,4))
``````

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

``````arr = np.eye(4)
``````

Creating matrix of (3x5 matrix) random number:

``````arr = np.random.rand(3,5)
``````

## 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.

``````np.add(a,b)
``````

Subtraction

``````np.subtract(a,b)
``````

Division :

``````np.divide(a,b)
``````

Multiply :

``````np.multiply(a,b)
``````

Exponential:

``````np.exp(a)
``````

Square root :

``````np.sqrt(a)
``````

Logarithm :

``````np.log(a)
``````

Dot Product :

``````a.dot(b)
``````

## Important In-built functions

Creating Copy of Array :

``````arr_two = arr_one.copy()
``````

Sorting of an Array :

``````sorted_arr = arr.sort()
``````

Transpose of an Array :

``````t = np.transpose(a)
``````

## Need Help ?

``````np.info(np.ndarray.dtype)
``````

That's all in this blog.Feel free to add more useful methods to this cheatsheet! 📝✨

Web

I use this :

`````` np.concatenate((a,b),axis=0)
``````

For Concatenate arrays

Jagroop Singh

Nice one !!
Thanks for sharing.

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.

Jagroop Singh

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