# Numpy Starter Functions

Numpy arrays are closer to hardware. Thus, their execution/operation times are significantly lesser compared to the usual pythonic lists. They consume less memory, and are far more flexible which makes them the preferred choice when working with huge data sets.

```
import numpy as np
import time
import sys
```

One dimensional array

```
a = np.array([1, 2, 3])
print(a)
```

Two dimensional array

```
b = np.array([[1, 2, 3], [4, 5, 6]])
print(b)
```

Memory difference can be observed between a numpy array and a python list

```
lis = [x for x in range(1000)]
print(sys.getsizeof(lis[0])*len(lis))
# Storage required by a python list - 12000
arr = np.arange(1000)
print(arr.size*arr.itemsize)
# Storage required by numpy array - 4000
```

Shape of a numpy array is described in terms of number of rows and columns

```
c = np.array([[1, 5, 7], [4, 8, 9]])
print(c.shape)
# Prints out a tuple (2, 3)
# 2 rows and 3 columns
```

Reshape an array - (rows x columns) -> (columns x rows)

```
d = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
d_modified = d.reshape(4, 2)
print(d_modified)
# [[1, 2], [3, 4], [5, 6], [7, 8]]
```

Slicing

```
print(d[0, 2])
# 0th row, 3rd element -> 3
print(d[0:, 2])
# All rows starting from 0 and their
# respective 3rd elements -> [3,7]
```

Linspace -> np.linspace(x, y, z)

Prints out z equally spaced numbers between x and y (both included)

```
e = np.linspace(1, 3, 5)
print(e)
# [1, 1.5, 2, 2.5, 3]
```

**Math Operations:**

```
f = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(np.sum(f))
print(np.sum(f, axis=0))
# 0 is columns and 1 is rows
print(np.mean(f))
print(np.median(f))
print(np.std(f))
# std -> Standard deviation
```

Numpy performs element wise operations when we do +, -, *, / on two numpy arrays. To concatenate two arrays we can use np.vstack() and np.hstack() and concatenate horizontally or vertically

np.arange(start, stop, steps) - Returns evenly spaced values within a given interval.

```
print(np.arange(1, 10, 2))
# [1, 3, 5, 7, 9]
# upper limit (10) isn't considered
g = np.arange(6)
# [0, 1, 2, 3, 4, 5]
```

Shuffle - Numbers in array are shuffled. If array is multi-dimensional, rows will be shuffled

```
h = np.arange(5)
# [0, 1, 2, 3, 4]
k = np.random.shuffle(h)
print(k)
# [1, 4, 3, 0, 2]
```