NumPy (**Num**erical **Py**thon) is a Python library used for scientific computing. It can also be used as an efficient multi-dimensional container for data.

**How to use NumPy**

To use any library in Python as in most programming languages, we must first import it. To import NumPy you should have installed Python 3.6 and above. To import “**numpy**” as any other Python modules we use the keyword “**import**” as follows:

`import numpy as np`

We make a reference to the library as “**np**” instead of “**numpy**.” so that we don’t have to use the word “**numpy**” everywhere in our code, i.e., creating an array: **np.array([1, 2, 3])**.

In this tutorial, we will be using this method, although we could just import it as ** import numpy** and the example for this would be

**numpy.array([1,2,3])**when creating an array.

If you already know Python and you have used Python lists, the comparison and differences between the two will make you understand NumPy easily and better.

**NumPy vs. Lists**

NumPy and Lists are similar to each other in the sense that they can both store data, be indexed, and be iterated. However, NumPy:

- uses less memory,
- is faster, and more
- convenient than Lists.

Also, we cannot perform calculations (add, subtract, multiply, divide and exponentiation) on Python Lists but we can on NumPy Arrays.

**Examples**

In the following examples, **In** means INPUT and **Out** is the OUTPUT after running the code snippet.

**Example to show the difference between Lists and NumPy:**

1 2 3 4 5 6 7 |
In [3]: python_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] In [4]: print((python_list) * 2) Out[4]: TypeError: unsupported operand type for *:'list' and 'int' In [5]: numpy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) In [6]: print((numpy_array) * 2) Out[6]: [2 4 6 8 10 12 14 16 18] |

**Another example:**

1 2 3 4 5 |
In [7]: print(python_list + python_list) Out[7]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [8]: print(numpy_array + numpy_array) Out[8]: [2 4 6 8 10 12 14 16 18] |

**NumPy Operations with Examples**

**1. Finding the size of an array: the size tool returns how many values are in the array.**

1 2 3 4 |
In [9]: numpy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) In [10]: print(numpy_array.size) Out[10]: 9 |

**2. How to get the shape of an array: the shape tool returns the dimensions of the array in the format (rows, columns). Note: an array can be referred to as a matrix.**

1 2 3 4 |
In [11]: numpy_matrix = np.array([(1, 2, 3, 4), (5, 6, 7, 8)]) In [12]: print(numpy_matrix.shape) Out[12]: (2, 4) |

**3. Reshaping an array: the reshape tool allows us to change the original dimension of an array. In this example, we changed the array dimensions from (2, 4) to (4, 2).**

1 2 3 4 5 6 7 8 |
In [13]: numpy_matrix = np.array([(1, 2, 3, 4), (5, 6, 7, 8)]) In [14]: numpy_matrix = numpy_matrix.reshape(4, 2) In [15]: print(numpy_matrix) Out[15]: [[1, 2] [3, 4] [5, 6] [7, 8]] |

**4. Getting a specific value from an array: returns the value at a specific position called on. It follows the format [rows, columns] with indexes starting at 0.**

1 2 3 4 |
In [16]: numpy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) In [17]: print(numpy_array[0, 2]) Out[17]: 3 |

**5. Extracting multiple values from an array: [0, 0:4] returns values from the first row and values from columns with indexes 0 to 3.**

1 2 3 4 |
In [18]: numpy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) In [19]: print(numpy_array[0, 0:4]) Out[19]: [1 2 3 4] |

**6. Reversing the order of an array: “ ::-1 ” in the rows position reverses the order of the rows whereas “ ::-1 ” in the columns position reverses the order of values in the columns.**

1 2 3 4 |
In [20]: numpy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) In [21]: print(numpy_array[ , ::-1]) Out[21]: [9 8 7 6 5 4 3 2 1] |

**7. Getting the max, min and sum of an array.**

1 2 3 4 5 6 7 8 9 |
In [22]: numpy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) In [23]: print(numpy_array.max()) Out[23]: 9 In [24]: print(numpy_array.min()) Out[24]: 1 In [25]: print(numpy_array.sum()) Out[25]: 45 |

**8. Finding the mean, median, variance, and standard deviation of an array.**

1 2 3 4 5 6 7 8 9 10 11 12 |
In [26]: numpy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) In [27]: print(numpy_array.mean()) Out[27]: 5 In [28]: print(numpy_array.median()) Out[28]: 5 In [29]: print(numpy_array.var()) Out[29]: 6.666666666666667 In [30]: print(numpy_array.std()) Out[30]: 2.581988897471611 |

**9. Doing an operation on a row and column: “ axis = 0 ” performs an operation on each y-axis within the array. “ axis = 1 ” performs an operation on each x-axis within the array. “ axis = None ” performs an operation on all values in the array, it is default.**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
In [31]: print(numpy_matrix) Out[31]: [[1, 2] [3, 4] [5, 6] [7, 8]] In [32]: print(numpy_matrix.sum(axis = 0)) Out[32]: [16 20] In [33]: print(numpy_matrix.sum(axis = 1)) Out[33]: [3 7 11 15] In [34]: print(numpy_matrix.sum(axis = None)) Out[34]: 36 |

**10. Joining arrays together to create one array: the concatenate tool joins arrays together. The final array can be reshaped by performing operations on specific axes.**

1 2 3 4 5 6 7 8 9 10 11 |
In [35]: array_1 = np.array([1,2,3]) In [36]: array_2 = np.array([4,5,6]) In [37]: array_3 = np.array([7,8,9]) In [38]: print(np.concatenate((array_1, array_2, array_3))) Out[38]: [1 2 3 4 5 6 7 8 9] In [39]: array_4 = np.array([[1,2,3],[0,0,0]]) In [40]: array_5 = np.array([[0,0,0],[7,8,9]]) In [41]: print(np.concatenate((array_1, array_2), axis = 1)) Out[41]: [[1 2 3 0 0 0] [0 0 0 7 8 9]] |

**11. Printing an array filled with zeros and ones: to clarify the zeros and ones tools returns an array filled with zeros or ones. The default type is float therefore changing dtype to reflect an integer will print integer values.**

1 2 3 4 5 6 7 8 9 10 11 |
In [42]: print(np.zeros((1,2))) Out[42]: [[ 0. 0.]] In [43]: print(np.zeros((1,2), dtype = np.int)) Out[43]: [[0 0]] In [44]: print(np.ones((1,2))) Out[44]: [[ 1. 1.]] In [45]: print(np.ones((1,2), dtype = np.int)) Out[45]: [[1 1]] |

**12. Printing identity matrices: in the first example, the identity tool returns a square matrix with the dimensions 3 by 3. In the second example, the eye tool prints an 8 by 7 matrix with the first upper diagonal shifted one over denoted with k = 1.**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
In [46]: print(np.identity(3)) Out[46]: [[ 1. 0. 0.] [ 0. 1. 0.] [ 0. 0. 1.]] In [47]: print(np.eye(8, 7, k = 1)) Out[47]: [[ 0. 1. 0. 0. 0. 0. 0.] [ 0. 0. 1. 0. 0. 0. 0.] [ 0. 0. 0. 1. 0. 0. 0.] [ 0. 0. 0. 0. 1. 0. 0.] [ 0. 0. 0. 0. 0. 1. 0.] [ 0. 0. 0. 0. 0. 0. 1.] [ 0. 0. 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0. 0. 0.]] |

**13. Multiplying matrices: the dot tool returns the dot products of two arrays. The cross tool returns the cross product of two arrays.**

1 2 3 4 5 6 7 |
In [48]: array_6 = np.array([ 1, 2 ]) In [49]: array_7 = np.array([ 3, 4 ]) In [50]: print(np.dot(array_6, array_7)) Out[50]: 11 In [51]: print(np.cross(array_6, array_7)) Out[51]: -2 |