(License: CC BY-SA 4.0)
Prev - Object Oriented Programming, Next - Data input/output and cleaning
From Python for Data Analysis, 2nd Ed, chapter 4:
ndarray
objects and doing math on themVectors: $\vec{x} = [ 1, 2, 3 ]$
Can do bulk operations using math magic:
inner/dot product : $$ \vec{x} \cdot \vec{y} = \sum x_i y_i $$
outer product : $$ \vec{x} \times \vec{y} = [x_i y_j]_{ij} $$
$$ A=\left[ \begin{array}{ccc} a_{11} & \cdots & a_{1n} \newline \vdots & \ddots & \vdots \newline a_{m1} & \cdots & a_{mn} \newline \end{array} \right] $$
Uses:
Must have matching inner dimensions, results in a matrix: $$ A_{m\times n} \times B_{n\times o} = C_{m\times o} $$
Each element of output matrix is the result of one inner product: $$ c_{ij} = \sum_k a_{ik} b_{kj} $$
Rows of $A$ matched to columns of $B$ to create single elements of $C$: $$ \left[ \begin{array}{c} \bbox[5px,yellow,border:2px solid red]{\begin{array}{ccc} a_{11} & \cdots & a_{1n} \end{array}} \newline \begin{array}{ccc} \vdots & \ddots & \vdots \newline a_{m1} & \cdots & a_{mn} \newline \end{array} \end{array} \right] \times \left[ \begin{array}{cc} \bbox[5px,yellow,border:2px solid red]{\begin{array}{c} b_{11} \newline \vdots \newline b_{n1} \end{array}} & \begin{array}{cc} \cdots & b_{1o} \newline \ddots & \vdots \newline \cdots & b_{no} \newline \end{array} \end{array} \right] = \left[ \begin{array}{ccc} \bbox[5px,yellow,border:2px solid red]{c_{11}} & \cdots & c_{1o} \newline \vdots & \ddots & \vdots \newline c_{m1} & \cdots & c_{mo} \newline \end{array} \right] $$
Useful transforming rows of data, image operations, 3D rotations, machine learning, etc.
Loop through your data and calculate mean and standard deviation (or regression, min, max, etc.).
vector = [1,2,3]
sum = 0
for element in vector:
sum += element
mean = sum / len(vector)
Use vector operations to do it shorter and more efficiently. $$ \mu = \sum_{i=1..N} x_i / N $$
import numpy as np
vector = np.array([1,2,3])
mean = np.sum(vector) / len(vector)
Calculate standard deviation $$ \sigma = \sqrt{ \sum_{i=1..N} ( x_i - \mu )^2 / ( N - 1 ) } $$ where $N$ is the number of elements in $ \vec{x} $ and $ \mu $ is its mean.
Submit by following instructions on the page below.
np.std(vector, ddof=1)
in your notebookSeries
and DataFrame