# Z-Score

A z-score measures how many standard deviations above or below the mean a data point is.

> 
\begin{align}
z = \frac{data\ point - mean}{standard\ deviation} \\
\end{align}


## Important facts

- A positive z-score says the data point is above average.
- A negative z-score says the data point is below average.
- A z-score close to 0 says the data point is close to average.

__Source:__ [Khan Academy](https://www.khanacademy.org/math/statistics-probability/modeling-distributions-of-data/z-scores/a/z-scores-review)

## Code

### Packages


```python
import numpy as np
```

### Function


```python
def z_score(sample, data_point):
    # data point needs to come from the sample
    assert data_point in sample
    avg = np.mean(sample)
    sd = np.std(sample)
    z_score = (data_point - avg)/sd
    return z_score
```

### Input


```python
sample = np.random.randn(30)
sample
```




    array([-0.59877513, -0.51223475, -1.40174708, -1.86207935, -0.10035927,
            0.12655593,  0.32697515, -0.35919666, -1.865143  , -0.27180892,
            0.42933215, -0.44281586, -0.30340738,  1.17923063, -0.09077022,
           -0.60174546,  0.34032583, -0.58059233, -1.90742451, -1.00759512,
            0.73630683, -0.13672296,  1.54279119, -0.4798372 ,  1.41396989,
            0.87904765, -0.9446896 ,  1.02256431,  0.77302047, -0.15333515])




```python
# grab the first data point
first_data_point = sample[0]
first_data_point
```




    -0.59877512996891891



### Output


```python
z_score(sample, first_data_point)
```




    -0.4795330537549915



1ee1a6b981dc3488418501c541f274f070425f19
ipynb
Jupyter Notebook
notebooks/statistics/z_score.ipynb
kvn219/notebooks
8d218c48e28d1cebdd39152b6496335a587e96eb
CC-BY-4.0
notebooks/statistics/z_score.ipynb
kvn219/notebooks
8d218c48e28d1cebdd39152b6496335a587e96eb
CC-BY-4.0
notebooks/statistics/z_score.ipynb
kvn219/notebooks
8d218c48e28d1cebdd39152b6496335a587e96eb
CC-BY-4.0
Qwen/Qwen-72B
1. YES
2. YES