**Python NumPy Cheat Sheet**

Whether you are a professional and have been working with Python for quite some time or you are a fresher and have just started using python, you must have heard of NumPy, a python library for numerical operations. It is extensively used but regardless of how popular it is, wouldn’t you agree that it’s practically not possible to know all the commands and operations by heart? Sometimes you just have to turn to internet to look up even the most basic things. Don’t worry, no judgments here, we all have done it.

**Download a Printable PDF of this Cheat Sheet**

We, at Intellipaat understand that this happens more often than not and that is exactly why we have come up with this NumPy cheat sheet for our learners, in case they need a quick reference for NumPy.

This cheat sheet has been designed assuming, one has basic python knowledge and will provide you with all the basics that you need to get started with NumPy in Python. Get extensive knowledge on Python via our online Python Tutorial.

**Watch this Python Numpy Tutorial Video for Beginners:**

**What is NumPy?**

It is a library consisting of multidimensional array objects and a collection of routines for processing those arrays. NumPy has put python lists out of the job as NumPy arrays are more efficient, convenient and makes it faster to read or write an item.

**Why use NumPy?**

Using NumPy, mathematical and logical operations on arrays can be performed. NumPy also provides high performance.Some of the key features that contribute in the popularity of NumPyare:

- It is a powerful N-dimensional array object
- It is a sophisticated broadcasting functions
- It is a tool for integrating C/C++ and Fortran code
- It is useful linear algebra, Fourier transform, and random number capabilities

**Import Convention**

Since NumPy is a Python Library, it has to be imported first before you start using NumPy. To import NumPy, type in the following command:

Import numpyImport numpy as np-

**ND array**

An NDarray in numpy is a space efficient multi-dimensional array which contains items of same type and size. It provides vectorized arithmetic operations.

**Creating array**

Just knowing what a NumPy array is not enough, we need to know how to create a Numpy array. You can create a NumPy array in the following ways:

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

b=np.array([(1,2,3,4),(7,8,9,10)],dtype=int) #creating a 2D array

**Initial Placeholders**

When you have the data you need to import to python, you can use NumPy to convert that data into NumPy arrays but sometimes when you don’t initially have any data or when you are starting from scratch and need an empty array you can use later then you can use numpy.zeros() function.

You can create empty arrays in following ways:

- np.zeros(3) – 1D array of length 3 all zeros
- array([0., 0., 0.])
- np.zeros((2,3))-2D array of all zeros

array([[0., 0., 0.],

[0., 0., 0.]]) - np.zeros((3,2,4))- 3D array of all zeros

array([[[0., 0., 0., 0.],

[0., 0., 0., 0.]],[[0., 0., 0., 0.],

[0., 0., 0., 0.]],[[0., 0., 0., 0.],

[0., 0., 0., 0.]]])

- np.full((3,4),2) – 3×4 array with all values 2
- np.random.rand(3,5) – 3×5 array of random floats between 0-1
- np.ones((3,4)) – 3×4 array with all values 1
- np.eye(4) – 4×4 array of 0 with 1 on diagonal

**Saving and Loading**

Next comes, how to save or load an array or a file in NumPy

- On disk:
- np.save(“new_array”,x)
- np.load(“new_array.npy”)

- Text/CSV files:
- np.loadtxt(‘New_file.txt’) – From a text file
- np.genfromtxt(‘New_file.csv’,delimiter=’,’) – From a CSV file
- np.savetxt(‘New_file.txt’,arr,delimiter=’ ‘) – Writes to a text file
- np.savetxt(‘New_file.csv’,arr,delimiter=’,’) – Writes to a CSV file

**Properties**

NumPy offers a few numbers of what we call ‘properties of NumPy array’ which can be used to check the nature of the array, that is, what kind of elements it contains or what is the size,etc.

**size –**Returns number of elements in array**shape –**Returns dimensions of array(rows, columns)**array.dtype –**Returns type of elements in array

**Array Mathematics**

Following are some mathematical operations you can perform using NumPy arrays:Note: While performing arithmetic operations in NumPy arrays, you should make sure that the items are of same shape.

**Arithmetic Operations:**

- Addition: np.add(a,b)
- Subtraction:np.subtract(a,b)
- Multiplication: np.multiply(a,b)
- Division: np.divide(a,b)
- Exponentiation: np.exp(a)
- Square Root: np.sqrt(b)

**Comparison**

- Element-wise: a==b
- Array-wise: np.array_equal(a,b)

**Operations**

There are some array shape manipulation operations present in NumPy. Following is the list of such operations with their respective commands and syntax:

**Copying:**

- np.copy(array) – Copies array to new memory array.
- view(dtype) – Creates view of array elements with typedtype

**Sorting:**

- array.sort() – Sorts array
- array.sort(axis=0) – Sorts specific axis of array
- array.reshape(2,3) – Reshapes array to 2 rows, 3 columns without changing data.

**Adding:**

- np.append(array,values) – Appends values to end of array
- np.insert(array,4,values) – Inserts values into array before index 4

**Removing:**

- np.delete(array,2,axis=0) – Deletes row on index 2 of array
- np.delete(array,3,axis=1) – Deletes column on index 3 of array

**Combining:**

- np.concatenate((array1,array2),axis=0) – Adds array2 as rows to the end of array1
- np.concatenate((array1,array2),axis=1) – Adds array2 as columns to end of array1

**Splitting:**

- np.split(array,3) – Splits array into 3sub-arrays

**Indexing:**

- a[0]=5 – Assigns array element on index 0 the value 5
- a[2,3]=1 – Assigns array element on index [2][3] the value 1

**Subsetting:**

- a[2]: Returns the element of index 2 in array a.
- a[3,5] – Returns the 2D array element on index [3][5]

**Slicing:**

- a[0:4] – Returns the elements at indices 0,1,2,3
- a[0:4,3] – Returns the elements on rows 0,1,2,3 at column 3
- a[:2] – Returns the elements at indices 0,1
- a[:,1] – Returns the elements at index 1 on all rows

**Functions**

Following is a set of functions in NumPy that operate on NumPy arrays:

**Array-wise Sum:**a.sum()**Array-wise min value: a.min()****Array row max value:**a.max(axis=0)**Mean:**a.mean()**Median:**a.median()

## Download a Printable PDF of this Cheat Sheet

We have covered all the basics of NumPy in this cheat sheet. If you want to start learning NumPy in depth then check out the Python Certification Training Course by Intellipaat. Not only will you get to learn and implement NumPy with a step by step guidance and support from us, but you will also get to learn some other important libraries in python such as SciPy, NumPy, MatPlotLib,Scikit-learn, Pandas, Lambda function and more. You will also get 24*7 technical support to help you with any and all of your queries, from the experts in the respective technologies here at intellipaat throughout the certification period. Also you will be provided with free Python interview questions asked by the experts during interviews.