Numpy is a package for scientific computing in Python. It is blazing fast due to its implementation in C.

It is often used together with pandas, matplotlib and Jupyter notebooks. Often these packages are referred to as the datascience stack.

Table of Contents

## Installation

You can install numpy via pip

`pip install numpy`

## Basic Usage

In the datascience world numpy is often imported like this:

`import numpy as np`

The “as” keyword defines a so called alias. Now you can use structures from numpy by referencing them with “np” instaed of the whole name.

Think “abbreviation”.

### n-dimensional array

The most important data structure is ndarray, which is short for n-dimensional array.

You can convert a list to an numpy array with the array-method

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

You can also convert an array back to a list with

`my_new_list = my_array.tolist()`

You can retrieve the dimensionality of an array with the ndim property:

`my_array.ndim`

and get the number of data points with the shape property

`my_array.shape`

## Vector arithmetic

### Addition / Subtraction

```
a = np.array([1, 2, 3, 4])
b = np.array([4, 3, 2, 1])
a + b
array([5, 5, 5, 5])
a - b
array([-3, -1, 1, 3])
```

### Scalar Multiplication

```
a = np.array([1, 2, 3, 4])
a * 3
array([3, 6, 9, 12])
```

To see why it is charming to use numpy’s array for this operation You have to consider the alternative:

```
c = [1,2,3,4]
d = [x * 3 for x in c]
```

### Dot Product

```
a = np.array([1,2,3,4])
b = np.array([4,3,2,1])
a.dot(b)
20 # 1*3 + 2*3 + 3*2 + 4*1
```

Learn more about numpy: