# Getting Started with NumPy

NumPy (Numerical Python) 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:
```In : python_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
In : print((python_list) * 2)
Out: TypeError: unsupported operand type for *:'list' and 'int'

In : numpy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
In : print((numpy_array) * 2)
Out: [2 4 6 8 10 12 14 16 18]```
###### Another example:
```In : print(python_list + python_list)
Out: [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In : print(numpy_array + numpy_array)
Out: [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.
```In : numpy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
In : print(numpy_array.size)

Out: 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.
```In : numpy_matrix = np.array([(1, 2, 3, 4), (5, 6, 7, 8)])
In : print(numpy_matrix.shape)

Out: (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).
```In : numpy_matrix = np.array([(1, 2, 3, 4), (5, 6, 7, 8)])
In : numpy_matrix = numpy_matrix.reshape(4, 2)
In : print(numpy_matrix)

Out: [[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.
```In : numpy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
In : print(numpy_array[0, 2])

Out: 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.
```In : numpy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
In : print(numpy_array[0, 0:4])

Out: [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.
```In : numpy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
In : print(numpy_array[ , ::-1])

Out: [9 8 7 6 5 4 3 2 1]```
###### 7. Getting the max, min and sum of an array.
```In : numpy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
In : print(numpy_array.max())
Out: 9

In : print(numpy_array.min())
Out: 1

In : print(numpy_array.sum())
Out: 45```
###### 8. Finding the mean, median, variance, and standard deviation of an array.
```In : numpy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
In : print(numpy_array.mean())
Out: 5

In : print(numpy_array.median())
Out: 5

In : print(numpy_array.var())
Out: 6.666666666666667

In : print(numpy_array.std())
Out: 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.
```In : print(numpy_matrix)
Out: [[1, 2]
[3, 4]
[5, 6]
[7, 8]]

In : print(numpy_matrix.sum(axis = 0))
Out: [16 20]

In : print(numpy_matrix.sum(axis = 1))
Out: [3 7 11 15]

In : print(numpy_matrix.sum(axis = None))
Out: 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.
```In : array_1 = np.array([1,2,3])
In : array_2 = np.array([4,5,6])
In : array_3 = np.array([7,8,9])
In : print(np.concatenate((array_1, array_2, array_3)))
Out: [1 2 3 4 5 6 7 8 9]

In : array_4 = np.array([[1,2,3],[0,0,0]])
In : array_5 = np.array([[0,0,0],[7,8,9]])
In : print(np.concatenate((array_1, array_2), axis = 1))
Out: [[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.
```In : print(np.zeros((1,2)))
Out: [[ 0. 0.]]

In : print(np.zeros((1,2), dtype = np.int))
Out: [[0 0]]

In : print(np.ones((1,2)))
Out: [[ 1. 1.]]

In : print(np.ones((1,2), dtype = np.int))
Out: [[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.
```In : print(np.identity(3))
Out: [[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]

In : print(np.eye(8, 7, k = 1))
Out: [[ 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.
```In : array_6 = np.array([ 1, 2 ])
In : array_7 = np.array([ 3, 4 ])
In : print(np.dot(array_6, array_7))
Out: 11

In : print(np.cross(array_6, array_7))
Out: -2```
CategoriesTags

### All You Need to Know About Code Editors for Web Development

What Is a Code Editor? We cannot start talking about writing codes, without talking about…

### PRG Pattern in PHP: What, Why, and How

Setting on an HTTP POST request as the last request sent to your server can…