# Numpy – Beginner notes

Getting introduced with Numpy.

## Prerequisite

• Understanding basic python
• comfort with python virtual envs, interpreter, and pip.

## Intro

NumPy is the fundamental package for scientific computing in Python. It is a Python library that provides a multidimensional array object, various derived objects (such as masked arrays and matrices), and an assortment of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation and much more.

https://numpy.org/doc/stable/

NumPy (Numerical Python) is an open source Python library that’s used in almost every field of science and engineering. It’s the universal standard for working with numerical data in Python.

The NumPy API is used extensively in Pandas, SciPy, Matplotlib, scikit-learn, scikit-image and most other data science and scientific Python packages.

NumPy gives you an enormous range of fast and efficient ways of creating arrays and manipulating numerical data inside them. While a Python list can contain different data types within a single list, all of the elements in a NumPy array should be homogeneous. The mathematical operations that are meant to be performed on arrays would be extremely inefficient if the arrays weren’t homogeneous.

NumPy arrays are faster and more compact than Python lists. An array consumes less memory and is convenient to use. NumPy uses much less memory to store data and it provides a mechanism of specifying the data types. This allows the code to be optimized even further.

https://numpy.org/doc/stable/user/absolute_beginners.html

## Install

``````pip install numpy
python3``````
```>>> import numpy as np
>>> a = np.arange(6)
>>> a2 = a[np.newaxis, :]
>>> a2.shape
(1, 6)
```

## Features/Cheatsheet

*Everything below is in interpreter

### Basic usage

```>>> arr = np.array([ [0, 1, 2], [3, 4, 5], [6, 7, 8] ])
>>> arr.size
9
>>> arr.size  # elements in array
9
>>> arr.ndim  # dimensions of array
2
>>> arr.shape  # (x, y) x = rows in array and y = columns in array
(3, 3)
>>> arr.reshape(9)  # changes number of rows/columns
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
>>> arr
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> # indexing and slicing looks same as python lists
...
>>> arr.reshape(9)[2:3]
array()
>>> # See this for generating random numbers: https://numpy.org/doc/stable/user/absolute_beginners.html#generating-random-numbers
...
>>> np.unique(arr)  # returns non repeating values
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
>>> np.flip(arr)  # reverse the array
array([[8, 7, 6],
[5, 4, 3],
[2, 1, 0]])
>>> rev_arr = np.flip(arr)
>>> np.unique(rev_arr, return_index=True)  # get indeces of unique values
(array([0, 1, 2, 3, 4, 5, 6, 7, 8]), array([8, 7, 6, 5, 4, 3, 2, 1, 0]))
```

### Basic Statistics

```>>> arr.max()
8
>>> arr.min()
0
>>> arr.sum()
36
>>> stat_arr = np.array([ [4, 0, 5], [1, 3, 6], [11, 23, 55], [4.5, 5.5, 8.1] ] )
>>> stat_arr
array([[ 4. ,  0. ,  5. ],
[ 1. ,  3. ,  6. ],
[11. , 23. , 55. ],
[ 4.5,  5.5,  8.1]])
>>> np.mean(stat_arr)
10.508333333333333
>>> np.median(stat_arr)
5.25
>>> np.std(arr)
2.581
```

## Next steps

1. Importing, exporting csv: https://numpy.org/doc/stable/user/absolute_beginners.html#importing-and-exporting-a-csv
2. Plotting array with matplotlib: https://numpy.org/doc/stable/user/absolute_beginners.html#plotting-arrays-with-matplotlib

## Stack overflow doubts

1. Numpy Standard Deviation ( Population vs Sample ): https://stackoverflow.com/questions/34050491/standard-deviation-in-numpy