# Vertical Line Test

## 1.1 Create two graphs, one that passes the vertical line test and one that does not.

```
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

```

```
ax = plt.subplot(111)

t = np.arange(0.0, 5.0, 0.01)
s = np.cos(2*np.pi*t)
line, = plt.plot(t, s, lw=2)

plt.title('Passes Vertical Line Test', color='w')

plt.ylim(-2,2)
plt.show()
```

```
circle = plt.Circle((5, 5), 3.0, color='b', fill=False, clip_on=False)

ax = plt.gca()
ax.cla()

ax.set_xlim((0, 10))
ax.set_ylim((0, 10))
plt.title('Does Not Pass Verticle Line Test', color='w')
ax.add_artist(circle)
plt.show()
```

## 1.2 Why are graphs that don't pass the vertical line test not considered "functions?"

They have multiple references for indicies. Which means that they are not linear in nature.

# Functions as Relations

## 2.1 Which of the following relations are functions? Why?

\begin{align}
\text{Relation 1: } \{(1, 2), (3, 2), (1, 3)\}
\\
\text{Relation 2: } \{(1, 3), (2, 3), (6, 7)\}
\\
\text{Relation 3: } \{(9, 4), (2, 1), (9, 6)\}
\\
\text{Relation 4: } \{(6, 2), (8, 3), (6, 4)\}
\\
\text{Relation 5: } \{(2, 6), (2, 7), (2, 4)\}
\end{align}

I believe relation 2 is the only function since it is the only one that passes the vertical line test

# Functions as a mapping between dimensions

## 3.1 for the following functions what is the dimensionality of the domain (input) and codomain (range/output)?

\begin{align}
m(𝑥_1,𝑥_2,𝑥_3)=(x_1+x_2, x_1+x_3, x_2+x_3)
\\
n(𝑥_1,𝑥_2,𝑥_3,𝑥_4)=(x_2^2 + x_3, x_2x_4)
\end{align}

For m the dimensionality of the domain is 3 and the dimensionality of the codomain is 3

For n the dimensionality of the domain is 4 and the codomain is 2

## 3.2 Do you think it's possible to create a function that maps from a lower dimensional space to a higher dimensional space? If so, provide an example.

It is possible as the transform to get from high dimensionality to low dimensionality can be reporoduced and inverted.

A system that has been overfitted can be mapped to higher dimensional space.

# Vector Transformations

## 4.1 Plug the corresponding unit vectors into each function. Use the output vectors to create a transformation matrix.

\begin{align}
p(\begin{bmatrix}x_1 \\ x_2 \end{bmatrix}) = \begin{bmatrix} x_1 + 3x_2 \\2 x_2 - x_1 \\  \end{bmatrix}
\\
\\
q(\begin{bmatrix}x_1 \\ x_2 \\ x_3\end{bmatrix}) = \begin{bmatrix} 4x_1 + x_2 + 2x_3 \\2 x_2 - x_1 + 3x_3 \\ 5x_1 - 2x_3 + x_2  \end{bmatrix}
\end{align}

```
# [x1, x2] = x1[1, 0] + x2[0, 1]
# f([1, 0]) = [1, -1] , f([0, 1]) = [3, 2]

t_p = np.array([
              [1, 3],
              [-1, 2]
              ])

```

```
# [
#  [x1],
#  [x2],
#  [x3]]= x1[1, 0 ,0] + x2[0, 1, 0] + x3[0, 0, 1]

#  (f([1, 0, 0]) = [4, -1, 5]), (f([0, 1, 0]) = [1, 2, 1]), (f([0, 0, 1]) = [2, 3, -2] )

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

## 4.2 Verify that your transformation matrices are correct by choosing an input matrix and calculating the result both via the traditional functions above and also via vector-matrix multiplication.

```
fun_mat = np.array([
                    [1],
                    [2]
                    ])
```

```
res_p = np.dot(t_p, fun_mat)
res_p
```

    array([[7],
           [3]])

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

```
res_q = np.dot(t_q, fun_mat_2)
res_q
```

    array([[12],
           [12],
           [ 1]])

# Eigenvalues and Eigenvectors

## 5.1 In your own words, give an explanation for the intuition behind eigenvalues and eigenvectors.

Eigenvectors are the point at which a system that is transformed has the same direction as it did before. Eigenvalues are the dimensions of the transformation on the eigenvector.

# The Curse of Dimensionality

## 6.1 What are some of the challenges of working with high dimensional spaces?

It requires a lot of data observations to avoid over fitting. Data spreads out the more dimensions are added

## 6.2 What is the rule of thumb for how many observations you should have compared to parameters in your model?

You should have at least 5 observations for each parameter

# Principal Component Analysis

## 7.1 Load the UCI Machine Learning Repository's [Iris Dataset](https://gist.githubusercontent.com/curran/a08a1080b88344b0c8a7/raw/d546eaee765268bf2f487608c537c05e22e4b221/iris.csv) and use PCA to isolate the dataset's first and second principal components and plot them on a graph.

```
df = pd.read_csv('https://gist.githubusercontent.com/curran/a08a1080b88344b0c8a7/raw/d546eaee765268bf2f487608c537c05e22e4b221/iris.csv')
df['species'].value_counts()
df.head()
```

      sepal_length
      sepal_width
      petal_length
      petal_width
      species

      0
      5.1
      3.5
      1.4
      0.2
      setosa

      1
      4.9
      3.0
      1.4
      0.2
      setosa

      2
      4.7
      3.2
      1.3
      0.2
      setosa

      3
      4.6
      3.1
      1.5
      0.2
      setosa

      4
      5.0
      3.6
      1.4
      0.2
      setosa

```
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
import math as math
from numpy import array
from numpy import mean
from numpy import std
from numpy import cov
from numpy.linalg import eig

sns.pairplot(df)
```

```
x = np.array(df[['sepal_length',	'sepal_width',	'petal_length',	'petal_width']])
y = np.array(df[['species']])
x
```

    array([[5.1, 3.5, 1.4, 0.2],
           [4.9, 3. , 1.4, 0.2],
           [4.7, 3.2, 1.3, 0.2],
           [4.6, 3.1, 1.5, 0.2],
           [5. , 3.6, 1.4, 0.2],
           [5.4, 3.9, 1.7, 0.4],
           [4.6, 3.4, 1.4, 0.3],
           [5. , 3.4, 1.5, 0.2],
           [4.4, 2.9, 1.4, 0.2],
           [4.9, 3.1, 1.5, 0.1],
           [5.4, 3.7, 1.5, 0.2],
           [4.8, 3.4, 1.6, 0.2],
           [4.8, 3. , 1.4, 0.1],
           [4.3, 3. , 1.1, 0.1],
           [5.8, 4. , 1.2, 0.2],
           [5.7, 4.4, 1.5, 0.4],
           [5.4, 3.9, 1.3, 0.4],
           [5.1, 3.5, 1.4, 0.3],
           [5.7, 3.8, 1.7, 0.3],
           [5.1, 3.8, 1.5, 0.3],
           [5.4, 3.4, 1.7, 0.2],
           [5.1, 3.7, 1.5, 0.4],
           [4.6, 3.6, 1. , 0.2],
           [5.1, 3.3, 1.7, 0.5],
           [4.8, 3.4, 1.9, 0.2],
           [5. , 3. , 1.6, 0.2],
           [5. , 3.4, 1.6, 0.4],
           [5.2, 3.5, 1.5, 0.2],
           [5.2, 3.4, 1.4, 0.2],
           [4.7, 3.2, 1.6, 0.2],
           [4.8, 3.1, 1.6, 0.2],
           [5.4, 3.4, 1.5, 0.4],
           [5.2, 4.1, 1.5, 0.1],
           [5.5, 4.2, 1.4, 0.2],
           [4.9, 3.1, 1.5, 0.1],
           [5. , 3.2, 1.2, 0.2],
           [5.5, 3.5, 1.3, 0.2],
           [4.9, 3.1, 1.5, 0.1],
           [4.4, 3. , 1.3, 0.2],
           [5.1, 3.4, 1.5, 0.2],
           [5. , 3.5, 1.3, 0.3],
           [4.5, 2.3, 1.3, 0.3],
           [4.4, 3.2, 1.3, 0.2],
           [5. , 3.5, 1.6, 0.6],
           [5.1, 3.8, 1.9, 0.4],
           [4.8, 3. , 1.4, 0.3],
           [5.1, 3.8, 1.6, 0.2],
           [4.6, 3.2, 1.4, 0.2],
           [5.3, 3.7, 1.5, 0.2],
           [5. , 3.3, 1.4, 0.2],
           [7. , 3.2, 4.7, 1.4],
           [6.4, 3.2, 4.5, 1.5],
           [6.9, 3.1, 4.9, 1.5],
           [5.5, 2.3, 4. , 1.3],
           [6.5, 2.8, 4.6, 1.5],
           [5.7, 2.8, 4.5, 1.3],
           [6.3, 3.3, 4.7, 1.6],
           [4.9, 2.4, 3.3, 1. ],
           [6.6, 2.9, 4.6, 1.3],
           [5.2, 2.7, 3.9, 1.4],
           [5. , 2. , 3.5, 1. ],
           [5.9, 3. , 4.2, 1.5],
           [6. , 2.2, 4. , 1. ],
           [6.1, 2.9, 4.7, 1.4],
           [5.6, 2.9, 3.6, 1.3],
           [6.7, 3.1, 4.4, 1.4],
           [5.6, 3. , 4.5, 1.5],
           [5.8, 2.7, 4.1, 1. ],
           [6.2, 2.2, 4.5, 1.5],
           [5.6, 2.5, 3.9, 1.1],
           [5.9, 3.2, 4.8, 1.8],
           [6.1, 2.8, 4. , 1.3],
           [6.3, 2.5, 4.9, 1.5],
           [6.1, 2.8, 4.7, 1.2],
           [6.4, 2.9, 4.3, 1.3],
           [6.6, 3. , 4.4, 1.4],
           [6.8, 2.8, 4.8, 1.4],
           [6.7, 3. , 5. , 1.7],
           [6. , 2.9, 4.5, 1.5],
           [5.7, 2.6, 3.5, 1. ],
           [5.5, 2.4, 3.8, 1.1],
           [5.5, 2.4, 3.7, 1. ],
           [5.8, 2.7, 3.9, 1.2],
           [6. , 2.7, 5.1, 1.6],
           [5.4, 3. , 4.5, 1.5],
           [6. , 3.4, 4.5, 1.6],
           [6.7, 3.1, 4.7, 1.5],
           [6.3, 2.3, 4.4, 1.3],
           [5.6, 3. , 4.1, 1.3],
           [5.5, 2.5, 4. , 1.3],
           [5.5, 2.6, 4.4, 1.2],
           [6.1, 3. , 4.6, 1.4],
           [5.8, 2.6, 4. , 1.2],
           [5. , 2.3, 3.3, 1. ],
           [5.6, 2.7, 4.2, 1.3],
           [5.7, 3. , 4.2, 1.2],
           [5.7, 2.9, 4.2, 1.3],
           [6.2, 2.9, 4.3, 1.3],
           [5.1, 2.5, 3. , 1.1],
           [5.7, 2.8, 4.1, 1.3],
           [6.3, 3.3, 6. , 2.5],
           [5.8, 2.7, 5.1, 1.9],
           [7.1, 3. , 5.9, 2.1],
           [6.3, 2.9, 5.6, 1.8],
           [6.5, 3. , 5.8, 2.2],
           [7.6, 3. , 6.6, 2.1],
           [4.9, 2.5, 4.5, 1.7],
           [7.3, 2.9, 6.3, 1.8],
           [6.7, 2.5, 5.8, 1.8],
           [7.2, 3.6, 6.1, 2.5],
           [6.5, 3.2, 5.1, 2. ],
           [6.4, 2.7, 5.3, 1.9],
           [6.8, 3. , 5.5, 2.1],
           [5.7, 2.5, 5. , 2. ],
           [5.8, 2.8, 5.1, 2.4],
           [6.4, 3.2, 5.3, 2.3],
           [6.5, 3. , 5.5, 1.8],
           [7.7, 3.8, 6.7, 2.2],
           [7.7, 2.6, 6.9, 2.3],
           [6. , 2.2, 5. , 1.5],
           [6.9, 3.2, 5.7, 2.3],
           [5.6, 2.8, 4.9, 2. ],
           [7.7, 2.8, 6.7, 2. ],
           [6.3, 2.7, 4.9, 1.8],
           [6.7, 3.3, 5.7, 2.1],
           [7.2, 3.2, 6. , 1.8],
           [6.2, 2.8, 4.8, 1.8],
           [6.1, 3. , 4.9, 1.8],
           [6.4, 2.8, 5.6, 2.1],
           [7.2, 3. , 5.8, 1.6],
           [7.4, 2.8, 6.1, 1.9],
           [7.9, 3.8, 6.4, 2. ],
           [6.4, 2.8, 5.6, 2.2],
           [6.3, 2.8, 5.1, 1.5],
           [6.1, 2.6, 5.6, 1.4],
           [7.7, 3. , 6.1, 2.3],
           [6.3, 3.4, 5.6, 2.4],
           [6.4, 3.1, 5.5, 1.8],
           [6. , 3. , 4.8, 1.8],
           [6.9, 3.1, 5.4, 2.1],
           [6.7, 3.1, 5.6, 2.4],
           [6.9, 3.1, 5.1, 2.3],
           [5.8, 2.7, 5.1, 1.9],
           [6.8, 3.2, 5.9, 2.3],
           [6.7, 3.3, 5.7, 2.5],
           [6.7, 3. , 5.2, 2.3],
           [6.3, 2.5, 5. , 1.9],
           [6.5, 3. , 5.2, 2. ],
           [6.2, 3.4, 5.4, 2.3],
           [5.9, 3. , 5.1, 1.8]])

```
means = mean(x.T, axis=1)
means
```

    array([5.84333333, 3.054     , 3.75866667, 1.19866667])

```
centered_data = x - means
centered_data
```

    array([[-7.43333333e-01,  4.46000000e-01, -2.35866667e+00,
            -9.98666667e-01],
           [-9.43333333e-01, -5.40000000e-02, -2.35866667e+00,
            -9.98666667e-01],
           [-1.14333333e+00,  1.46000000e-01, -2.45866667e+00,
            -9.98666667e-01],
           [-1.24333333e+00,  4.60000000e-02, -2.25866667e+00,
            -9.98666667e-01],
           [-8.43333333e-01,  5.46000000e-01, -2.35866667e+00,
            -9.98666667e-01],
           [-4.43333333e-01,  8.46000000e-01, -2.05866667e+00,
            -7.98666667e-01],
           [-1.24333333e+00,  3.46000000e-01, -2.35866667e+00,
            -8.98666667e-01],
           [-8.43333333e-01,  3.46000000e-01, -2.25866667e+00,
            -9.98666667e-01],
           [-1.44333333e+00, -1.54000000e-01, -2.35866667e+00,
            -9.98666667e-01],
           [-9.43333333e-01,  4.60000000e-02, -2.25866667e+00,
            -1.09866667e+00],
           [-4.43333333e-01,  6.46000000e-01, -2.25866667e+00,
            -9.98666667e-01],
           [-1.04333333e+00,  3.46000000e-01, -2.15866667e+00,
            -9.98666667e-01],
           [-1.04333333e+00, -5.40000000e-02, -2.35866667e+00,
            -1.09866667e+00],
           [-1.54333333e+00, -5.40000000e-02, -2.65866667e+00,
            -1.09866667e+00],
           [-4.33333333e-02,  9.46000000e-01, -2.55866667e+00,
            -9.98666667e-01],
           [-1.43333333e-01,  1.34600000e+00, -2.25866667e+00,
            -7.98666667e-01],
           [-4.43333333e-01,  8.46000000e-01, -2.45866667e+00,
            -7.98666667e-01],
           [-7.43333333e-01,  4.46000000e-01, -2.35866667e+00,
            -8.98666667e-01],
           [-1.43333333e-01,  7.46000000e-01, -2.05866667e+00,
            -8.98666667e-01],
           [-7.43333333e-01,  7.46000000e-01, -2.25866667e+00,
            -8.98666667e-01],
           [-4.43333333e-01,  3.46000000e-01, -2.05866667e+00,
            -9.98666667e-01],
           [-7.43333333e-01,  6.46000000e-01, -2.25866667e+00,
            -7.98666667e-01],
           [-1.24333333e+00,  5.46000000e-01, -2.75866667e+00,
            -9.98666667e-01],
           [-7.43333333e-01,  2.46000000e-01, -2.05866667e+00,
            -6.98666667e-01],
           [-1.04333333e+00,  3.46000000e-01, -1.85866667e+00,
            -9.98666667e-01],
           [-8.43333333e-01, -5.40000000e-02, -2.15866667e+00,
            -9.98666667e-01],
           [-8.43333333e-01,  3.46000000e-01, -2.15866667e+00,
            -7.98666667e-01],
           [-6.43333333e-01,  4.46000000e-01, -2.25866667e+00,
            -9.98666667e-01],
           [-6.43333333e-01,  3.46000000e-01, -2.35866667e+00,
            -9.98666667e-01],
           [-1.14333333e+00,  1.46000000e-01, -2.15866667e+00,
            -9.98666667e-01],
           [-1.04333333e+00,  4.60000000e-02, -2.15866667e+00,
            -9.98666667e-01],
           [-4.43333333e-01,  3.46000000e-01, -2.25866667e+00,
            -7.98666667e-01],
           [-6.43333333e-01,  1.04600000e+00, -2.25866667e+00,
            -1.09866667e+00],
           [-3.43333333e-01,  1.14600000e+00, -2.35866667e+00,
            -9.98666667e-01],
           [-9.43333333e-01,  4.60000000e-02, -2.25866667e+00,
            -1.09866667e+00],
           [-8.43333333e-01,  1.46000000e-01, -2.55866667e+00,
            -9.98666667e-01],
           [-3.43333333e-01,  4.46000000e-01, -2.45866667e+00,
            -9.98666667e-01],
           [-9.43333333e-01,  4.60000000e-02, -2.25866667e+00,
            -1.09866667e+00],
           [-1.44333333e+00, -5.40000000e-02, -2.45866667e+00,
            -9.98666667e-01],
           [-7.43333333e-01,  3.46000000e-01, -2.25866667e+00,
            -9.98666667e-01],
           [-8.43333333e-01,  4.46000000e-01, -2.45866667e+00,
            -8.98666667e-01],
           [-1.34333333e+00, -7.54000000e-01, -2.45866667e+00,
            -8.98666667e-01],
           [-1.44333333e+00,  1.46000000e-01, -2.45866667e+00,
            -9.98666667e-01],
           [-8.43333333e-01,  4.46000000e-01, -2.15866667e+00,
            -5.98666667e-01],
           [-7.43333333e-01,  7.46000000e-01, -1.85866667e+00,
            -7.98666667e-01],
           [-1.04333333e+00, -5.40000000e-02, -2.35866667e+00,
            -8.98666667e-01],
           [-7.43333333e-01,  7.46000000e-01, -2.15866667e+00,
            -9.98666667e-01],
           [-1.24333333e+00,  1.46000000e-01, -2.35866667e+00,
            -9.98666667e-01],
           [-5.43333333e-01,  6.46000000e-01, -2.25866667e+00,
            -9.98666667e-01],
           [-8.43333333e-01,  2.46000000e-01, -2.35866667e+00,
            -9.98666667e-01],
           [ 1.15666667e+00,  1.46000000e-01,  9.41333333e-01,
             2.01333333e-01],
           [ 5.56666667e-01,  1.46000000e-01,  7.41333333e-01,
             3.01333333e-01],
           [ 1.05666667e+00,  4.60000000e-02,  1.14133333e+00,
             3.01333333e-01],
           [-3.43333333e-01, -7.54000000e-01,  2.41333333e-01,
             1.01333333e-01],
           [ 6.56666667e-01, -2.54000000e-01,  8.41333333e-01,
             3.01333333e-01],
           [-1.43333333e-01, -2.54000000e-01,  7.41333333e-01,
             1.01333333e-01],
           [ 4.56666667e-01,  2.46000000e-01,  9.41333333e-01,
             4.01333333e-01],
           [-9.43333333e-01, -6.54000000e-01, -4.58666667e-01,
            -1.98666667e-01],
           [ 7.56666667e-01, -1.54000000e-01,  8.41333333e-01,
             1.01333333e-01],
           [-6.43333333e-01, -3.54000000e-01,  1.41333333e-01,
             2.01333333e-01],
           [-8.43333333e-01, -1.05400000e+00, -2.58666667e-01,
            -1.98666667e-01],
           [ 5.66666667e-02, -5.40000000e-02,  4.41333333e-01,
             3.01333333e-01],
           [ 1.56666667e-01, -8.54000000e-01,  2.41333333e-01,
            -1.98666667e-01],
           [ 2.56666667e-01, -1.54000000e-01,  9.41333333e-01,
             2.01333333e-01],
           [-2.43333333e-01, -1.54000000e-01, -1.58666667e-01,
             1.01333333e-01],
           [ 8.56666667e-01,  4.60000000e-02,  6.41333333e-01,
             2.01333333e-01],
           [-2.43333333e-01, -5.40000000e-02,  7.41333333e-01,
             3.01333333e-01],
           [-4.33333333e-02, -3.54000000e-01,  3.41333333e-01,
            -1.98666667e-01],
           [ 3.56666667e-01, -8.54000000e-01,  7.41333333e-01,
             3.01333333e-01],
           [-2.43333333e-01, -5.54000000e-01,  1.41333333e-01,
            -9.86666667e-02],
           [ 5.66666667e-02,  1.46000000e-01,  1.04133333e+00,
             6.01333333e-01],
           [ 2.56666667e-01, -2.54000000e-01,  2.41333333e-01,
             1.01333333e-01],
           [ 4.56666667e-01, -5.54000000e-01,  1.14133333e+00,
             3.01333333e-01],
           [ 2.56666667e-01, -2.54000000e-01,  9.41333333e-01,
             1.33333333e-03],
           [ 5.56666667e-01, -1.54000000e-01,  5.41333333e-01,
             1.01333333e-01],
           [ 7.56666667e-01, -5.40000000e-02,  6.41333333e-01,
             2.01333333e-01],
           [ 9.56666667e-01, -2.54000000e-01,  1.04133333e+00,
             2.01333333e-01],
           [ 8.56666667e-01, -5.40000000e-02,  1.24133333e+00,
             5.01333333e-01],
           [ 1.56666667e-01, -1.54000000e-01,  7.41333333e-01,
             3.01333333e-01],
           [-1.43333333e-01, -4.54000000e-01, -2.58666667e-01,
            -1.98666667e-01],
           [-3.43333333e-01, -6.54000000e-01,  4.13333333e-02,
            -9.86666667e-02],
           [-3.43333333e-01, -6.54000000e-01, -5.86666667e-02,
            -1.98666667e-01],
           [-4.33333333e-02, -3.54000000e-01,  1.41333333e-01,
             1.33333333e-03],
           [ 1.56666667e-01, -3.54000000e-01,  1.34133333e+00,
             4.01333333e-01],
           [-4.43333333e-01, -5.40000000e-02,  7.41333333e-01,
             3.01333333e-01],
           [ 1.56666667e-01,  3.46000000e-01,  7.41333333e-01,
             4.01333333e-01],
           [ 8.56666667e-01,  4.60000000e-02,  9.41333333e-01,
             3.01333333e-01],
           [ 4.56666667e-01, -7.54000000e-01,  6.41333333e-01,
             1.01333333e-01],
           [-2.43333333e-01, -5.40000000e-02,  3.41333333e-01,
             1.01333333e-01],
           [-3.43333333e-01, -5.54000000e-01,  2.41333333e-01,
             1.01333333e-01],
           [-3.43333333e-01, -4.54000000e-01,  6.41333333e-01,
             1.33333333e-03],
           [ 2.56666667e-01, -5.40000000e-02,  8.41333333e-01,
             2.01333333e-01],
           [-4.33333333e-02, -4.54000000e-01,  2.41333333e-01,
             1.33333333e-03],
           [-8.43333333e-01, -7.54000000e-01, -4.58666667e-01,
            -1.98666667e-01],
           [-2.43333333e-01, -3.54000000e-01,  4.41333333e-01,
             1.01333333e-01],
           [-1.43333333e-01, -5.40000000e-02,  4.41333333e-01,
             1.33333333e-03],
           [-1.43333333e-01, -1.54000000e-01,  4.41333333e-01,
             1.01333333e-01],
           [ 3.56666667e-01, -1.54000000e-01,  5.41333333e-01,
             1.01333333e-01],
           [-7.43333333e-01, -5.54000000e-01, -7.58666667e-01,
            -9.86666667e-02],
           [-1.43333333e-01, -2.54000000e-01,  3.41333333e-01,
             1.01333333e-01],
           [ 4.56666667e-01,  2.46000000e-01,  2.24133333e+00,
             1.30133333e+00],
           [-4.33333333e-02, -3.54000000e-01,  1.34133333e+00,
             7.01333333e-01],
           [ 1.25666667e+00, -5.40000000e-02,  2.14133333e+00,
             9.01333333e-01],
           [ 4.56666667e-01, -1.54000000e-01,  1.84133333e+00,
             6.01333333e-01],
           [ 6.56666667e-01, -5.40000000e-02,  2.04133333e+00,
             1.00133333e+00],
           [ 1.75666667e+00, -5.40000000e-02,  2.84133333e+00,
             9.01333333e-01],
           [-9.43333333e-01, -5.54000000e-01,  7.41333333e-01,
             5.01333333e-01],
           [ 1.45666667e+00, -1.54000000e-01,  2.54133333e+00,
             6.01333333e-01],
           [ 8.56666667e-01, -5.54000000e-01,  2.04133333e+00,
             6.01333333e-01],
           [ 1.35666667e+00,  5.46000000e-01,  2.34133333e+00,
             1.30133333e+00],
           [ 6.56666667e-01,  1.46000000e-01,  1.34133333e+00,
             8.01333333e-01],
           [ 5.56666667e-01, -3.54000000e-01,  1.54133333e+00,
             7.01333333e-01],
           [ 9.56666667e-01, -5.40000000e-02,  1.74133333e+00,
             9.01333333e-01],
           [-1.43333333e-01, -5.54000000e-01,  1.24133333e+00,
             8.01333333e-01],
           [-4.33333333e-02, -2.54000000e-01,  1.34133333e+00,
             1.20133333e+00],
           [ 5.56666667e-01,  1.46000000e-01,  1.54133333e+00,
             1.10133333e+00],
           [ 6.56666667e-01, -5.40000000e-02,  1.74133333e+00,
             6.01333333e-01],
           [ 1.85666667e+00,  7.46000000e-01,  2.94133333e+00,
             1.00133333e+00],
           [ 1.85666667e+00, -4.54000000e-01,  3.14133333e+00,
             1.10133333e+00],
           [ 1.56666667e-01, -8.54000000e-01,  1.24133333e+00,
             3.01333333e-01],
           [ 1.05666667e+00,  1.46000000e-01,  1.94133333e+00,
             1.10133333e+00],
           [-2.43333333e-01, -2.54000000e-01,  1.14133333e+00,
             8.01333333e-01],
           [ 1.85666667e+00, -2.54000000e-01,  2.94133333e+00,
             8.01333333e-01],
           [ 4.56666667e-01, -3.54000000e-01,  1.14133333e+00,
             6.01333333e-01],
           [ 8.56666667e-01,  2.46000000e-01,  1.94133333e+00,
             9.01333333e-01],
           [ 1.35666667e+00,  1.46000000e-01,  2.24133333e+00,
             6.01333333e-01],
           [ 3.56666667e-01, -2.54000000e-01,  1.04133333e+00,
             6.01333333e-01],
           [ 2.56666667e-01, -5.40000000e-02,  1.14133333e+00,
             6.01333333e-01],
           [ 5.56666667e-01, -2.54000000e-01,  1.84133333e+00,
             9.01333333e-01],
           [ 1.35666667e+00, -5.40000000e-02,  2.04133333e+00,
             4.01333333e-01],
           [ 1.55666667e+00, -2.54000000e-01,  2.34133333e+00,
             7.01333333e-01],
           [ 2.05666667e+00,  7.46000000e-01,  2.64133333e+00,
             8.01333333e-01],
           [ 5.56666667e-01, -2.54000000e-01,  1.84133333e+00,
             1.00133333e+00],
           [ 4.56666667e-01, -2.54000000e-01,  1.34133333e+00,
             3.01333333e-01],
           [ 2.56666667e-01, -4.54000000e-01,  1.84133333e+00,
             2.01333333e-01],
           [ 1.85666667e+00, -5.40000000e-02,  2.34133333e+00,
             1.10133333e+00],
           [ 4.56666667e-01,  3.46000000e-01,  1.84133333e+00,
             1.20133333e+00],
           [ 5.56666667e-01,  4.60000000e-02,  1.74133333e+00,
             6.01333333e-01],
           [ 1.56666667e-01, -5.40000000e-02,  1.04133333e+00,
             6.01333333e-01],
           [ 1.05666667e+00,  4.60000000e-02,  1.64133333e+00,
             9.01333333e-01],
           [ 8.56666667e-01,  4.60000000e-02,  1.84133333e+00,
             1.20133333e+00],
           [ 1.05666667e+00,  4.60000000e-02,  1.34133333e+00,
             1.10133333e+00],
           [-4.33333333e-02, -3.54000000e-01,  1.34133333e+00,
             7.01333333e-01],
           [ 9.56666667e-01,  1.46000000e-01,  2.14133333e+00,
             1.10133333e+00],
           [ 8.56666667e-01,  2.46000000e-01,  1.94133333e+00,
             1.30133333e+00],
           [ 8.56666667e-01, -5.40000000e-02,  1.44133333e+00,
             1.10133333e+00],
           [ 4.56666667e-01, -5.54000000e-01,  1.24133333e+00,
             7.01333333e-01],
           [ 6.56666667e-01, -5.40000000e-02,  1.44133333e+00,
             8.01333333e-01],
           [ 3.56666667e-01,  3.46000000e-01,  1.64133333e+00,
             1.10133333e+00],
           [ 5.66666667e-02, -5.40000000e-02,  1.34133333e+00,
             6.01333333e-01]])

```
std_devs = std(x.T, axis=1)
std_devs
```

    array([0.82530129, 0.43214658, 1.75852918, 0.76061262])

```
standardized_data = centered_data / std_devs
standardized_data
```

    array([[-9.00681170e-01,  1.03205722e+00, -1.34127240e+00,
            -1.31297673e+00],
           [-1.14301691e+00, -1.24957601e-01, -1.34127240e+00,
            -1.31297673e+00],
           [-1.38535265e+00,  3.37848329e-01, -1.39813811e+00,
            -1.31297673e+00],
           [-1.50652052e+00,  1.06445364e-01, -1.28440670e+00,
            -1.31297673e+00],
           [-1.02184904e+00,  1.26346019e+00, -1.34127240e+00,
            -1.31297673e+00],
           [-5.37177559e-01,  1.95766909e+00, -1.17067529e+00,
            -1.05003079e+00],
           [-1.50652052e+00,  8.00654259e-01, -1.34127240e+00,
            -1.18150376e+00],
           [-1.02184904e+00,  8.00654259e-01, -1.28440670e+00,
            -1.31297673e+00],
           [-1.74885626e+00, -3.56360566e-01, -1.34127240e+00,
            -1.31297673e+00],
           [-1.14301691e+00,  1.06445364e-01, -1.28440670e+00,
            -1.44444970e+00],
           [-5.37177559e-01,  1.49486315e+00, -1.28440670e+00,
            -1.31297673e+00],
           [-1.26418478e+00,  8.00654259e-01, -1.22754100e+00,
            -1.31297673e+00],
           [-1.26418478e+00, -1.24957601e-01, -1.34127240e+00,
            -1.44444970e+00],
           [-1.87002413e+00, -1.24957601e-01, -1.51186952e+00,
            -1.44444970e+00],
           [-5.25060772e-02,  2.18907205e+00, -1.45500381e+00,
            -1.31297673e+00],
           [-1.73673948e-01,  3.11468391e+00, -1.28440670e+00,
            -1.05003079e+00],
           [-5.37177559e-01,  1.95766909e+00, -1.39813811e+00,
            -1.05003079e+00],
           [-9.00681170e-01,  1.03205722e+00, -1.34127240e+00,
            -1.18150376e+00],
           [-1.73673948e-01,  1.72626612e+00, -1.17067529e+00,
            -1.18150376e+00],
           [-9.00681170e-01,  1.72626612e+00, -1.28440670e+00,
            -1.18150376e+00],
           [-5.37177559e-01,  8.00654259e-01, -1.17067529e+00,
            -1.31297673e+00],
           [-9.00681170e-01,  1.49486315e+00, -1.28440670e+00,
            -1.05003079e+00],
           [-1.50652052e+00,  1.26346019e+00, -1.56873522e+00,
            -1.31297673e+00],
           [-9.00681170e-01,  5.69251294e-01, -1.17067529e+00,
            -9.18557817e-01],
           [-1.26418478e+00,  8.00654259e-01, -1.05694388e+00,
            -1.31297673e+00],
           [-1.02184904e+00, -1.24957601e-01, -1.22754100e+00,
            -1.31297673e+00],
           [-1.02184904e+00,  8.00654259e-01, -1.22754100e+00,
            -1.05003079e+00],
           [-7.79513300e-01,  1.03205722e+00, -1.28440670e+00,
            -1.31297673e+00],
           [-7.79513300e-01,  8.00654259e-01, -1.34127240e+00,
            -1.31297673e+00],
           [-1.38535265e+00,  3.37848329e-01, -1.22754100e+00,
            -1.31297673e+00],
           [-1.26418478e+00,  1.06445364e-01, -1.22754100e+00,
            -1.31297673e+00],
           [-5.37177559e-01,  8.00654259e-01, -1.28440670e+00,
            -1.05003079e+00],
           [-7.79513300e-01,  2.42047502e+00, -1.28440670e+00,
            -1.44444970e+00],
           [-4.16009689e-01,  2.65187798e+00, -1.34127240e+00,
            -1.31297673e+00],
           [-1.14301691e+00,  1.06445364e-01, -1.28440670e+00,
            -1.44444970e+00],
           [-1.02184904e+00,  3.37848329e-01, -1.45500381e+00,
            -1.31297673e+00],
           [-4.16009689e-01,  1.03205722e+00, -1.39813811e+00,
            -1.31297673e+00],
           [-1.14301691e+00,  1.06445364e-01, -1.28440670e+00,
            -1.44444970e+00],
           [-1.74885626e+00, -1.24957601e-01, -1.39813811e+00,
            -1.31297673e+00],
           [-9.00681170e-01,  8.00654259e-01, -1.28440670e+00,
            -1.31297673e+00],
           [-1.02184904e+00,  1.03205722e+00, -1.39813811e+00,
            -1.18150376e+00],
           [-1.62768839e+00, -1.74477836e+00, -1.39813811e+00,
            -1.18150376e+00],
           [-1.74885626e+00,  3.37848329e-01, -1.39813811e+00,
            -1.31297673e+00],
           [-1.02184904e+00,  1.03205722e+00, -1.22754100e+00,
            -7.87084847e-01],
           [-9.00681170e-01,  1.72626612e+00, -1.05694388e+00,
            -1.05003079e+00],
           [-1.26418478e+00, -1.24957601e-01, -1.34127240e+00,
            -1.18150376e+00],
           [-9.00681170e-01,  1.72626612e+00, -1.22754100e+00,
            -1.31297673e+00],
           [-1.50652052e+00,  3.37848329e-01, -1.34127240e+00,
            -1.31297673e+00],
           [-6.58345429e-01,  1.49486315e+00, -1.28440670e+00,
            -1.31297673e+00],
           [-1.02184904e+00,  5.69251294e-01, -1.34127240e+00,
            -1.31297673e+00],
           [ 1.40150837e+00,  3.37848329e-01,  5.35295827e-01,
             2.64698913e-01],
           [ 6.74501145e-01,  3.37848329e-01,  4.21564419e-01,
             3.96171883e-01],
           [ 1.28034050e+00,  1.06445364e-01,  6.49027235e-01,
             3.96171883e-01],
           [-4.16009689e-01, -1.74477836e+00,  1.37235899e-01,
             1.33225943e-01],
           [ 7.95669016e-01, -5.87763531e-01,  4.78430123e-01,
             3.96171883e-01],
           [-1.73673948e-01, -5.87763531e-01,  4.21564419e-01,
             1.33225943e-01],
           [ 5.53333275e-01,  5.69251294e-01,  5.35295827e-01,
             5.27644853e-01],
           [-1.14301691e+00, -1.51337539e+00, -2.60824029e-01,
            -2.61192967e-01],
           [ 9.16836886e-01, -3.56360566e-01,  4.78430123e-01,
             1.33225943e-01],
           [-7.79513300e-01, -8.19166497e-01,  8.03701950e-02,
             2.64698913e-01],
           [-1.02184904e+00, -2.43898725e+00, -1.47092621e-01,
            -2.61192967e-01],
           [ 6.86617933e-02, -1.24957601e-01,  2.50967307e-01,
             3.96171883e-01],
           [ 1.89829664e-01, -1.97618132e+00,  1.37235899e-01,
            -2.61192967e-01],
           [ 3.10997534e-01, -3.56360566e-01,  5.35295827e-01,
             2.64698913e-01],
           [-2.94841818e-01, -3.56360566e-01, -9.02269170e-02,
             1.33225943e-01],
           [ 1.03800476e+00,  1.06445364e-01,  3.64698715e-01,
             2.64698913e-01],
           [-2.94841818e-01, -1.24957601e-01,  4.21564419e-01,
             3.96171883e-01],
           [-5.25060772e-02, -8.19166497e-01,  1.94101603e-01,
            -2.61192967e-01],
           [ 4.32165405e-01, -1.97618132e+00,  4.21564419e-01,
             3.96171883e-01],
           [-2.94841818e-01, -1.28197243e+00,  8.03701950e-02,
            -1.29719997e-01],
           [ 6.86617933e-02,  3.37848329e-01,  5.92161531e-01,
             7.90590793e-01],
           [ 3.10997534e-01, -5.87763531e-01,  1.37235899e-01,
             1.33225943e-01],
           [ 5.53333275e-01, -1.28197243e+00,  6.49027235e-01,
             3.96171883e-01],
           [ 3.10997534e-01, -5.87763531e-01,  5.35295827e-01,
             1.75297293e-03],
           [ 6.74501145e-01, -3.56360566e-01,  3.07833011e-01,
             1.33225943e-01],
           [ 9.16836886e-01, -1.24957601e-01,  3.64698715e-01,
             2.64698913e-01],
           [ 1.15917263e+00, -5.87763531e-01,  5.92161531e-01,
             2.64698913e-01],
           [ 1.03800476e+00, -1.24957601e-01,  7.05892939e-01,
             6.59117823e-01],
           [ 1.89829664e-01, -3.56360566e-01,  4.21564419e-01,
             3.96171883e-01],
           [-1.73673948e-01, -1.05056946e+00, -1.47092621e-01,
            -2.61192967e-01],
           [-4.16009689e-01, -1.51337539e+00,  2.35044910e-02,
            -1.29719997e-01],
           [-4.16009689e-01, -1.51337539e+00, -3.33612130e-02,
            -2.61192967e-01],
           [-5.25060772e-02, -8.19166497e-01,  8.03701950e-02,
             1.75297293e-03],
           [ 1.89829664e-01, -8.19166497e-01,  7.62758643e-01,
             5.27644853e-01],
           [-5.37177559e-01, -1.24957601e-01,  4.21564419e-01,
             3.96171883e-01],
           [ 1.89829664e-01,  8.00654259e-01,  4.21564419e-01,
             5.27644853e-01],
           [ 1.03800476e+00,  1.06445364e-01,  5.35295827e-01,
             3.96171883e-01],
           [ 5.53333275e-01, -1.74477836e+00,  3.64698715e-01,
             1.33225943e-01],
           [-2.94841818e-01, -1.24957601e-01,  1.94101603e-01,
             1.33225943e-01],
           [-4.16009689e-01, -1.28197243e+00,  1.37235899e-01,
             1.33225943e-01],
           [-4.16009689e-01, -1.05056946e+00,  3.64698715e-01,
             1.75297293e-03],
           [ 3.10997534e-01, -1.24957601e-01,  4.78430123e-01,
             2.64698913e-01],
           [-5.25060772e-02, -1.05056946e+00,  1.37235899e-01,
             1.75297293e-03],
           [-1.02184904e+00, -1.74477836e+00, -2.60824029e-01,
            -2.61192967e-01],
           [-2.94841818e-01, -8.19166497e-01,  2.50967307e-01,
             1.33225943e-01],
           [-1.73673948e-01, -1.24957601e-01,  2.50967307e-01,
             1.75297293e-03],
           [-1.73673948e-01, -3.56360566e-01,  2.50967307e-01,
             1.33225943e-01],
           [ 4.32165405e-01, -3.56360566e-01,  3.07833011e-01,
             1.33225943e-01],
           [-9.00681170e-01, -1.28197243e+00, -4.31421141e-01,
            -1.29719997e-01],
           [-1.73673948e-01, -5.87763531e-01,  1.94101603e-01,
             1.33225943e-01],
           [ 5.53333275e-01,  5.69251294e-01,  1.27454998e+00,
             1.71090158e+00],
           [-5.25060772e-02, -8.19166497e-01,  7.62758643e-01,
             9.22063763e-01],
           [ 1.52267624e+00, -1.24957601e-01,  1.21768427e+00,
             1.18500970e+00],
           [ 5.53333275e-01, -3.56360566e-01,  1.04708716e+00,
             7.90590793e-01],
           [ 7.95669016e-01, -1.24957601e-01,  1.16081857e+00,
             1.31648267e+00],
           [ 2.12851559e+00, -1.24957601e-01,  1.61574420e+00,
             1.18500970e+00],
           [-1.14301691e+00, -1.28197243e+00,  4.21564419e-01,
             6.59117823e-01],
           [ 1.76501198e+00, -3.56360566e-01,  1.44514709e+00,
             7.90590793e-01],
           [ 1.03800476e+00, -1.28197243e+00,  1.16081857e+00,
             7.90590793e-01],
           [ 1.64384411e+00,  1.26346019e+00,  1.33141568e+00,
             1.71090158e+00],
           [ 7.95669016e-01,  3.37848329e-01,  7.62758643e-01,
             1.05353673e+00],
           [ 6.74501145e-01, -8.19166497e-01,  8.76490051e-01,
             9.22063763e-01],
           [ 1.15917263e+00, -1.24957601e-01,  9.90221459e-01,
             1.18500970e+00],
           [-1.73673948e-01, -1.28197243e+00,  7.05892939e-01,
             1.05353673e+00],
           [-5.25060772e-02, -5.87763531e-01,  7.62758643e-01,
             1.57942861e+00],
           [ 6.74501145e-01,  3.37848329e-01,  8.76490051e-01,
             1.44795564e+00],
           [ 7.95669016e-01, -1.24957601e-01,  9.90221459e-01,
             7.90590793e-01],
           [ 2.24968346e+00,  1.72626612e+00,  1.67260991e+00,
             1.31648267e+00],
           [ 2.24968346e+00, -1.05056946e+00,  1.78634131e+00,
             1.44795564e+00],
           [ 1.89829664e-01, -1.97618132e+00,  7.05892939e-01,
             3.96171883e-01],
           [ 1.28034050e+00,  3.37848329e-01,  1.10395287e+00,
             1.44795564e+00],
           [-2.94841818e-01, -5.87763531e-01,  6.49027235e-01,
             1.05353673e+00],
           [ 2.24968346e+00, -5.87763531e-01,  1.67260991e+00,
             1.05353673e+00],
           [ 5.53333275e-01, -8.19166497e-01,  6.49027235e-01,
             7.90590793e-01],
           [ 1.03800476e+00,  5.69251294e-01,  1.10395287e+00,
             1.18500970e+00],
           [ 1.64384411e+00,  3.37848329e-01,  1.27454998e+00,
             7.90590793e-01],
           [ 4.32165405e-01, -5.87763531e-01,  5.92161531e-01,
             7.90590793e-01],
           [ 3.10997534e-01, -1.24957601e-01,  6.49027235e-01,
             7.90590793e-01],
           [ 6.74501145e-01, -5.87763531e-01,  1.04708716e+00,
             1.18500970e+00],
           [ 1.64384411e+00, -1.24957601e-01,  1.16081857e+00,
             5.27644853e-01],
           [ 1.88617985e+00, -5.87763531e-01,  1.33141568e+00,
             9.22063763e-01],
           [ 2.49201920e+00,  1.72626612e+00,  1.50201279e+00,
             1.05353673e+00],
           [ 6.74501145e-01, -5.87763531e-01,  1.04708716e+00,
             1.31648267e+00],
           [ 5.53333275e-01, -5.87763531e-01,  7.62758643e-01,
             3.96171883e-01],
           [ 3.10997534e-01, -1.05056946e+00,  1.04708716e+00,
             2.64698913e-01],
           [ 2.24968346e+00, -1.24957601e-01,  1.33141568e+00,
             1.44795564e+00],
           [ 5.53333275e-01,  8.00654259e-01,  1.04708716e+00,
             1.57942861e+00],
           [ 6.74501145e-01,  1.06445364e-01,  9.90221459e-01,
             7.90590793e-01],
           [ 1.89829664e-01, -1.24957601e-01,  5.92161531e-01,
             7.90590793e-01],
           [ 1.28034050e+00,  1.06445364e-01,  9.33355755e-01,
             1.18500970e+00],
           [ 1.03800476e+00,  1.06445364e-01,  1.04708716e+00,
             1.57942861e+00],
           [ 1.28034050e+00,  1.06445364e-01,  7.62758643e-01,
             1.44795564e+00],
           [-5.25060772e-02, -8.19166497e-01,  7.62758643e-01,
             9.22063763e-01],
           [ 1.15917263e+00,  3.37848329e-01,  1.21768427e+00,
             1.44795564e+00],
           [ 1.03800476e+00,  5.69251294e-01,  1.10395287e+00,
             1.71090158e+00],
           [ 1.03800476e+00, -1.24957601e-01,  8.19624347e-01,
             1.44795564e+00],
           [ 5.53333275e-01, -1.28197243e+00,  7.05892939e-01,
             9.22063763e-01],
           [ 7.95669016e-01, -1.24957601e-01,  8.19624347e-01,
             1.05353673e+00],
           [ 4.32165405e-01,  8.00654259e-01,  9.33355755e-01,
             1.44795564e+00],
           [ 6.86617933e-02, -1.24957601e-01,  7.62758643e-01,
             7.90590793e-01]])

```
covariance_matrix = cov(standardized_data.T)
covariance_matrix
```

    array([[ 1.00671141, -0.11010327,  0.87760486,  0.82344326],
           [-0.11010327,  1.00671141, -0.42333835, -0.358937  ],
           [ 0.87760486, -0.42333835,  1.00671141,  0.96921855],
           [ 0.82344326, -0.358937  ,  0.96921855,  1.00671141]])

```
values, vectors = eig(covariance_matrix)
print("\n Eigenvectors: \n", vectors)
print("\n Eigenvalues: \n", values)
```

     Eigenvectors:
     [[ 0.52237162 -0.37231836 -0.72101681  0.26199559]
     [-0.26335492 -0.92555649  0.24203288 -0.12413481]
     [ 0.58125401 -0.02109478  0.14089226 -0.80115427]
     [ 0.56561105 -0.06541577  0.6338014   0.52354627]]

     Eigenvalues:
     [2.93035378 0.92740362 0.14834223 0.02074601]

```
P = vectors.T.dot(centered_data.T)
P
```

    array([[-2.44159388e+00, -2.41439075e+00, -2.62966145e+00,
            -2.53931232e+00, -2.52016653e+00, -2.10272595e+00,
            -2.61988309e+00, -2.40937015e+00, -2.64924106e+00,
            -2.43916194e+00, -2.27942798e+00, -2.45571907e+00,
            -2.52318901e+00, -2.95875103e+00, -2.32386200e+00,
            -2.19394272e+00, -2.33522755e+00, -2.38503277e+00,
            -1.97624008e+00, -2.40591385e+00, -2.08417070e+00,
            -2.32301725e+00, -2.96161678e+00, -2.04486338e+00,
            -2.28134287e+00, -2.24590278e+00, -2.23812254e+00,
            -2.33123132e+00, -2.36302123e+00, -2.45528525e+00,
            -2.37671260e+00, -2.08729929e+00, -2.54580537e+00,
            -2.41699367e+00, -2.43916194e+00, -2.53107537e+00,
            -2.29077063e+00, -2.43916194e+00, -2.73370196e+00,
            -2.35713299e+00, -2.49539534e+00, -2.44055525e+00,
            -2.78637294e+00, -2.15133582e+00, -2.11685114e+00,
            -2.41006680e+00, -2.40434955e+00, -2.62377322e+00,
            -2.33166514e+00, -2.44116006e+00,  1.22679015e+00,
             8.53677484e-01,  1.37370039e+00,  2.16813236e-01,
             1.06938201e+00,  4.80237105e-01,  9.47916736e-01,
            -6.99506346e-01,  9.82161473e-01, -4.68045116e-02,
            -4.25676417e-01,  4.70786455e-01,  3.34651223e-01,
             8.35662168e-01, -1.21464153e-01,  9.22037956e-01,
             4.88451170e-01,  1.56624842e-01,  1.01255807e+00,
             4.51318047e-02,  9.36551190e-01,  3.98558751e-01,
             1.21829036e+00,  7.48875450e-01,  7.03310947e-01,
             8.96136285e-01,  1.28578319e+00,  1.46680917e+00,
             7.23735310e-01, -2.18029232e-01, -3.88952664e-02,
            -1.53581772e-01,  1.53496251e-01,  1.18171980e+00,
             3.83976846e-01,  6.48618957e-01,  1.15297526e+00,
             8.67212135e-01,  1.42827358e-01,  1.64142253e-01,
             3.13747259e-01,  7.51201276e-01,  2.37957143e-01,
            -6.20933693e-01,  2.79959233e-01,  1.96628816e-01,
             2.79525412e-01,  5.98836623e-01, -7.39182611e-01,
             2.47735503e-01,  2.21259689e+00,  1.24692879e+00,
             2.42513084e+00,  1.68950952e+00,  2.11014357e+00,
             3.09319445e+00,  3.67590704e-01,  2.61875894e+00,
             2.12005093e+00,  2.66185027e+00,  1.53747257e+00,
             1.67660257e+00,  2.03591775e+00,  1.24579832e+00,
             1.50339883e+00,  1.77116953e+00,  1.70952295e+00,
             3.04943419e+00,  3.53827199e+00,  1.19871075e+00,
             2.26485694e+00,  1.05642928e+00,  3.19966689e+00,
             1.33530270e+00,  2.02092491e+00,  2.31313910e+00,
             1.19860464e+00,  1.15182190e+00,  1.93776549e+00,
             2.13643708e+00,  2.63764190e+00,  2.86641010e+00,
             1.99432659e+00,  1.25553469e+00,  1.43779725e+00,
             2.96792682e+00,  1.89719869e+00,  1.63095030e+00,
             1.04145934e+00,  2.00369402e+00,  2.18515381e+00,
             1.94244003e+00,  1.24692879e+00,  2.32887058e+00,
             2.24716933e+00,  1.92242660e+00,  1.50266019e+00,
             1.64826896e+00,  1.67214962e+00,  1.16359837e+00],
           [-2.09574513e-02,  5.16284468e-01,  4.07746320e-01,
             5.33314850e-01, -7.62812643e-02, -5.22287145e-01,
             2.51215803e-01,  1.06720557e-01,  7.94999300e-01,
             4.28160918e-01, -3.19873737e-01,  1.79074752e-01,
             5.60057882e-01,  7.52545496e-01, -7.40139597e-01,
            -1.09254195e+00, -5.13849234e-01, -2.74990282e-02,
            -5.34885427e-01, -3.07275454e-01, -4.64257439e-02,
            -2.21261382e-01,  8.10839917e-02,  1.38200684e-01,
             1.72746319e-01,  4.74833677e-01,  9.15279253e-02,
            -6.02987653e-02,  3.43663618e-02,  4.01417887e-01,
             4.56741700e-01, -5.52899424e-02, -6.09091085e-01,
            -8.17774343e-01,  4.28160918e-01,  2.98160289e-01,
            -1.67775319e-01,  4.28160918e-01,  7.04553128e-01,
             6.94887205e-02,  1.18422858e-02,  1.30866926e+00,
             5.19441829e-01, -1.41108779e-02, -3.22254942e-01,
             5.46974728e-01, -3.02843355e-01,  4.42868679e-01,
            -2.82641900e-01,  2.01385684e-01, -5.98807080e-01,
            -3.77738683e-01, -4.79780126e-01,  8.13979231e-01,
            -4.68573998e-02,  2.66189922e-01, -4.43823029e-01,
             9.79205674e-01, -1.63561732e-01,  5.51020043e-01,
             1.30797748e+00, -1.39769834e-04,  7.40000429e-01,
             1.39463954e-02,  2.29851409e-01, -3.88227488e-01,
             1.05227306e-01,  3.49576377e-01,  6.22281483e-01,
             6.06828727e-01, -2.17532665e-01,  1.27809966e-01,
             2.98944788e-01,  1.19585199e-01, -8.27696260e-02,
            -2.58440003e-01, -1.56230287e-01, -3.27953436e-01,
             4.88556102e-02,  4.92020729e-01,  7.38725690e-01,
             7.47376745e-01,  3.40712179e-01,  2.14768466e-01,
             1.79690979e-01, -4.20464214e-01, -4.01097498e-01,
             5.07686629e-01,  1.26748371e-01,  6.28867932e-01,
             5.34415949e-01, -7.64997763e-02,  4.31158350e-01,
             1.03452949e+00,  4.02305841e-01,  9.39486336e-02,
             1.79962706e-01, -8.30595329e-03,  8.11973208e-01,
             2.74627833e-01, -5.30120431e-01,  2.69607408e-01,
            -5.22032388e-01, -1.05668884e-01, -3.03073469e-01,
            -7.22957913e-01,  8.15545254e-01, -4.92753591e-01,
             1.11407413e-01, -1.14498338e+00, -4.60335270e-01,
             4.19974346e-02, -4.01898968e-01,  4.87518444e-01,
             1.44343874e-01, -4.46947120e-01, -2.70578728e-01,
            -1.50928600e+00, -4.09378739e-01,  6.86197768e-01,
            -6.41544213e-01,  2.49192810e-01, -5.70646352e-01,
             9.42087586e-02, -6.46553035e-01, -7.26860270e-01,
             4.09944232e-02, -1.08994517e-01, -6.99698017e-02,
            -5.24446862e-01, -4.39752400e-01, -1.56433809e+00,
            -7.65113786e-02,  1.70588845e-02,  2.72628044e-01,
            -7.62725515e-01, -6.07696593e-01, -3.25902542e-01,
            -6.96532030e-02, -5.29576976e-01, -4.78956990e-01,
            -5.36331697e-01,  2.69607408e-01, -6.08531332e-01,
            -6.72719343e-01, -3.71421853e-01,  2.70669003e-01,
            -2.77333449e-01, -5.59704224e-01, -3.87497997e-02],
           [-3.21371717e-01, -2.98184794e-01, -1.19664083e-01,
            -4.35872377e-02, -2.25066749e-01, -2.71835651e-01,
             7.83135399e-02, -2.59384098e-01,  3.81203228e-02,
            -3.23272421e-01, -4.75180959e-01, -1.01091511e-01,
            -2.89463253e-01,  2.87774736e-02, -7.33245497e-01,
            -3.95302707e-01, -3.28192554e-01, -2.57991577e-01,
            -5.75724122e-01, -1.71292488e-01, -5.19612370e-01,
            -1.32115635e-01,  6.98307166e-03, -1.37370194e-01,
            -5.88238330e-02, -3.42108023e-01, -1.18534592e-01,
            -3.79384172e-01, -4.17676686e-01, -7.73964050e-02,
            -1.73701374e-01, -4.21030541e-01, -2.97544586e-01,
            -4.40355427e-01, -3.23272421e-01, -3.50058351e-01,
            -6.23867667e-01, -3.23272421e-01,  4.82343847e-02,
            -3.31485779e-01, -1.99979122e-01, -1.29910170e-01,
             9.66409601e-02,  3.24289767e-02, -5.15554441e-02,
            -1.62702973e-01, -2.20583402e-01, -3.34731758e-02,
            -4.03079278e-01, -2.97676612e-01, -5.38407381e-01,
            -7.05956065e-02, -3.98950395e-01,  1.63283522e-01,
            -2.25421212e-01,  2.10542728e-01,  1.17267954e-01,
             3.31331893e-01, -4.00079886e-01,  5.25692630e-01,
             1.90595513e-01,  1.99238545e-01, -4.11568591e-01,
             3.78978844e-02,  1.80044664e-01, -3.88573303e-01,
             4.57811265e-01, -1.32259565e-01, -1.68425122e-01,
            -1.26108970e-03,  5.22320897e-01, -1.48310125e-01,
            -1.11560036e-01, -1.13065684e-01, -2.98144202e-01,
            -3.40674910e-01, -4.76927944e-01, -1.38100815e-01,
             1.45201254e-01, -1.68896527e-01,  3.25480776e-02,
            -4.49212886e-02, -3.36777357e-02,  2.44710174e-01,
             6.02014627e-01,  3.29597833e-01, -2.82925485e-01,
            -3.57173022e-01,  2.74694081e-01,  2.11690098e-01,
             2.28870149e-01,  4.80119463e-02, -4.37917976e-02,
             2.35026925e-01,  2.16173444e-01,  1.53301486e-01,
             1.92478338e-01, -1.53940840e-01,  2.32444282e-01,
             1.54185825e-01,  8.70849153e-01,  5.79053957e-01,
            -4.61839444e-02,  2.74018117e-01,  4.35717056e-01,
            -3.08067768e-01,  9.68266874e-01, -3.48374112e-01,
            -8.30233062e-02,  3.08633114e-01,  2.58738769e-01,
             1.74622323e-01,  1.13764195e-01,  6.52039977e-01,
             9.20157946e-01,  5.49159323e-01,  1.39928817e-01,
            -1.09073781e-01, -3.07954642e-01,  4.62243692e-02,
             2.45007822e-01,  7.82662295e-01, -4.77866939e-01,
             1.26986960e-01,  2.86654191e-01, -2.45930245e-01,
             2.09202703e-01,  3.43800185e-01,  3.67853569e-01,
            -4.49275553e-01, -4.09477391e-01, -4.22305101e-01,
             4.31233709e-01, -1.07717214e-02,  9.20910539e-02,
            -3.23855297e-01,  7.75315397e-01,  2.36233785e-01,
             4.01812640e-01,  5.17765759e-02,  4.14298810e-01,
             1.36269179e-01,  5.79053957e-01,  3.45287954e-01,
             5.40174752e-01,  2.70358479e-01,  1.56049751e-01,
             2.24421420e-01,  7.55858486e-01,  5.16181999e-01],
           [ 1.11669348e+00,  1.12636177e+00,  1.12925112e+00,
             9.55234185e-01,  1.07808044e+00,  1.01000121e+00,
             1.05046380e+00,  1.02279198e+00,  1.00777746e+00,
             9.81478234e-01,  1.09034977e+00,  8.90277432e-01,
             1.04780758e+00,  1.15715607e+00,  1.39825384e+00,
             1.18676333e+00,  1.33046291e+00,  1.16904811e+00,
             1.04865874e+00,  1.05169224e+00,  9.67359358e-01,
             1.11646035e+00,  1.29374391e+00,  1.05823804e+00,
             6.49931152e-01,  9.92330474e-01,  1.04738580e+00,
             1.06277761e+00,  1.15530652e+00,  8.88904836e-01,
             9.27517875e-01,  1.23229947e+00,  9.35942100e-01,
             1.13459735e+00,  9.81478234e-01,  1.28796522e+00,
             1.30160714e+00,  9.81478234e-01,  1.07547940e+00,
             1.04899154e+00,  1.22296398e+00,  1.24092796e+00,
             1.05065244e+00,  1.13968158e+00,  7.83585158e-01,
             1.15251684e+00,  9.19222184e-01,  1.02293613e+00,
             1.06415021e+00,  1.11532088e+00, -3.63828023e-01,
            -3.08439894e-01, -4.85490327e-01, -1.36646713e-01,
            -3.12701838e-01, -5.46892135e-01, -4.54929160e-01,
             9.74865609e-02, -4.03625015e-01, -1.32429259e-01,
             1.31091898e-02, -1.74264445e-01, -1.50299320e-01,
            -5.62383608e-01,  1.35533667e-01, -1.89666937e-01,
            -4.93209401e-01, -3.44881269e-01, -2.36704201e-01,
            -1.59867944e-01, -5.22720087e-01, -4.15167657e-02,
            -5.68206793e-01, -6.54679381e-01, -2.15677851e-01,
            -2.03453015e-01, -4.46688643e-01, -5.00882136e-01,
            -3.75997686e-01,  1.22025215e-01, -9.35385943e-02,
            -6.57777946e-02, -7.99411610e-02, -7.79508658e-01,
            -5.45608519e-01, -3.85710463e-01, -3.77658591e-01,
            -2.47511951e-01, -2.77456948e-01, -1.61473675e-01,
            -5.46703491e-01, -4.94681662e-01, -1.47643107e-01,
             1.36099601e-01, -3.20331932e-01, -3.83727443e-01,
            -3.18959335e-01, -2.68076969e-01,  4.30173105e-01,
            -2.26430427e-01, -1.02523807e+00, -6.74843894e-01,
            -9.07704235e-01, -1.02160482e+00, -9.32431533e-01,
            -1.33751443e+00, -5.09829653e-01, -1.32041723e+00,
            -1.02738352e+00, -9.06797907e-01, -5.01159761e-01,
            -6.77877395e-01, -6.65841203e-01, -5.43746436e-01,
            -4.25484239e-01, -5.30526292e-01, -9.01503761e-01,
            -1.43838352e+00, -1.39729797e+00, -6.89680453e-01,
            -7.19990206e-01, -5.27071011e-01, -1.41895796e+00,
            -4.35969874e-01, -8.89512059e-01, -1.14351095e+00,
            -3.94467486e-01, -5.25609434e-01, -8.25927903e-01,
            -1.06316238e+00, -1.06921870e+00, -1.25034738e+00,
            -7.73573276e-01, -7.65678090e-01, -1.24618201e+00,
            -8.06028482e-01, -7.69544466e-01, -9.40116800e-01,
            -4.71693566e-01, -5.71939699e-01, -6.27505788e-01,
            -2.26884164e-01, -6.74843894e-01, -9.06420619e-01,
            -6.80093550e-01, -3.46985227e-01, -4.38903711e-01,
            -5.56448226e-01, -6.87867798e-01, -7.38239405e-01]])

```
from numpy import array
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
scaler = StandardScaler()
Z = scaler.fit_transform(x)
print("\n Standardized Data: \n", Z)
pca = PCA(2)
pca.fit(Z)
print("\n Eigenvectors: \n", pca.components_)
print("\n Eigenvalues: \n",pca.explained_variance_)
B = pca.transform(Z)
print("\n Projected Data: \n", B)
```

     Standardized Data:
     [[-9.00681170e-01  1.03205722e+00 -1.34127240e+00 -1.31297673e+00]
     [-1.14301691e+00 -1.24957601e-01 -1.34127240e+00 -1.31297673e+00]
     [-1.38535265e+00  3.37848329e-01 -1.39813811e+00 -1.31297673e+00]
     [-1.50652052e+00  1.06445364e-01 -1.28440670e+00 -1.31297673e+00]
     [-1.02184904e+00  1.26346019e+00 -1.34127240e+00 -1.31297673e+00]
     [-5.37177559e-01  1.95766909e+00 -1.17067529e+00 -1.05003079e+00]
     [-1.50652052e+00  8.00654259e-01 -1.34127240e+00 -1.18150376e+00]
     [-1.02184904e+00  8.00654259e-01 -1.28440670e+00 -1.31297673e+00]
     [-1.74885626e+00 -3.56360566e-01 -1.34127240e+00 -1.31297673e+00]
     [-1.14301691e+00  1.06445364e-01 -1.28440670e+00 -1.44444970e+00]
     [-5.37177559e-01  1.49486315e+00 -1.28440670e+00 -1.31297673e+00]
     [-1.26418478e+00  8.00654259e-01 -1.22754100e+00 -1.31297673e+00]
     [-1.26418478e+00 -1.24957601e-01 -1.34127240e+00 -1.44444970e+00]
     [-1.87002413e+00 -1.24957601e-01 -1.51186952e+00 -1.44444970e+00]
     [-5.25060772e-02  2.18907205e+00 -1.45500381e+00 -1.31297673e+00]
     [-1.73673948e-01  3.11468391e+00 -1.28440670e+00 -1.05003079e+00]
     [-5.37177559e-01  1.95766909e+00 -1.39813811e+00 -1.05003079e+00]
     [-9.00681170e-01  1.03205722e+00 -1.34127240e+00 -1.18150376e+00]
     [-1.73673948e-01  1.72626612e+00 -1.17067529e+00 -1.18150376e+00]
     [-9.00681170e-01  1.72626612e+00 -1.28440670e+00 -1.18150376e+00]
     [-5.37177559e-01  8.00654259e-01 -1.17067529e+00 -1.31297673e+00]
     [-9.00681170e-01  1.49486315e+00 -1.28440670e+00 -1.05003079e+00]
     [-1.50652052e+00  1.26346019e+00 -1.56873522e+00 -1.31297673e+00]
     [-9.00681170e-01  5.69251294e-01 -1.17067529e+00 -9.18557817e-01]
     [-1.26418478e+00  8.00654259e-01 -1.05694388e+00 -1.31297673e+00]
     [-1.02184904e+00 -1.24957601e-01 -1.22754100e+00 -1.31297673e+00]
     [-1.02184904e+00  8.00654259e-01 -1.22754100e+00 -1.05003079e+00]
     [-7.79513300e-01  1.03205722e+00 -1.28440670e+00 -1.31297673e+00]
     [-7.79513300e-01  8.00654259e-01 -1.34127240e+00 -1.31297673e+00]
     [-1.38535265e+00  3.37848329e-01 -1.22754100e+00 -1.31297673e+00]
     [-1.26418478e+00  1.06445364e-01 -1.22754100e+00 -1.31297673e+00]
     [-5.37177559e-01  8.00654259e-01 -1.28440670e+00 -1.05003079e+00]
     [-7.79513300e-01  2.42047502e+00 -1.28440670e+00 -1.44444970e+00]
     [-4.16009689e-01  2.65187798e+00 -1.34127240e+00 -1.31297673e+00]
     [-1.14301691e+00  1.06445364e-01 -1.28440670e+00 -1.44444970e+00]
     [-1.02184904e+00  3.37848329e-01 -1.45500381e+00 -1.31297673e+00]
     [-4.16009689e-01  1.03205722e+00 -1.39813811e+00 -1.31297673e+00]
     [-1.14301691e+00  1.06445364e-01 -1.28440670e+00 -1.44444970e+00]
     [-1.74885626e+00 -1.24957601e-01 -1.39813811e+00 -1.31297673e+00]
     [-9.00681170e-01  8.00654259e-01 -1.28440670e+00 -1.31297673e+00]
     [-1.02184904e+00  1.03205722e+00 -1.39813811e+00 -1.18150376e+00]
     [-1.62768839e+00 -1.74477836e+00 -1.39813811e+00 -1.18150376e+00]
     [-1.74885626e+00  3.37848329e-01 -1.39813811e+00 -1.31297673e+00]
     [-1.02184904e+00  1.03205722e+00 -1.22754100e+00 -7.87084847e-01]
     [-9.00681170e-01  1.72626612e+00 -1.05694388e+00 -1.05003079e+00]
     [-1.26418478e+00 -1.24957601e-01 -1.34127240e+00 -1.18150376e+00]
     [-9.00681170e-01  1.72626612e+00 -1.22754100e+00 -1.31297673e+00]
     [-1.50652052e+00  3.37848329e-01 -1.34127240e+00 -1.31297673e+00]
     [-6.58345429e-01  1.49486315e+00 -1.28440670e+00 -1.31297673e+00]
     [-1.02184904e+00  5.69251294e-01 -1.34127240e+00 -1.31297673e+00]
     [ 1.40150837e+00  3.37848329e-01  5.35295827e-01  2.64698913e-01]
     [ 6.74501145e-01  3.37848329e-01  4.21564419e-01  3.96171883e-01]
     [ 1.28034050e+00  1.06445364e-01  6.49027235e-01  3.96171883e-01]
     [-4.16009689e-01 -1.74477836e+00  1.37235899e-01  1.33225943e-01]
     [ 7.95669016e-01 -5.87763531e-01  4.78430123e-01  3.96171883e-01]
     [-1.73673948e-01 -5.87763531e-01  4.21564419e-01  1.33225943e-01]
     [ 5.53333275e-01  5.69251294e-01  5.35295827e-01  5.27644853e-01]
     [-1.14301691e+00 -1.51337539e+00 -2.60824029e-01 -2.61192967e-01]
     [ 9.16836886e-01 -3.56360566e-01  4.78430123e-01  1.33225943e-01]
     [-7.79513300e-01 -8.19166497e-01  8.03701950e-02  2.64698913e-01]
     [-1.02184904e+00 -2.43898725e+00 -1.47092621e-01 -2.61192967e-01]
     [ 6.86617933e-02 -1.24957601e-01  2.50967307e-01  3.96171883e-01]
     [ 1.89829664e-01 -1.97618132e+00  1.37235899e-01 -2.61192967e-01]
     [ 3.10997534e-01 -3.56360566e-01  5.35295827e-01  2.64698913e-01]
     [-2.94841818e-01 -3.56360566e-01 -9.02269170e-02  1.33225943e-01]
     [ 1.03800476e+00  1.06445364e-01  3.64698715e-01  2.64698913e-01]
     [-2.94841818e-01 -1.24957601e-01  4.21564419e-01  3.96171883e-01]
     [-5.25060772e-02 -8.19166497e-01  1.94101603e-01 -2.61192967e-01]
     [ 4.32165405e-01 -1.97618132e+00  4.21564419e-01  3.96171883e-01]
     [-2.94841818e-01 -1.28197243e+00  8.03701950e-02 -1.29719997e-01]
     [ 6.86617933e-02  3.37848329e-01  5.92161531e-01  7.90590793e-01]
     [ 3.10997534e-01 -5.87763531e-01  1.37235899e-01  1.33225943e-01]
     [ 5.53333275e-01 -1.28197243e+00  6.49027235e-01  3.96171883e-01]
     [ 3.10997534e-01 -5.87763531e-01  5.35295827e-01  1.75297293e-03]
     [ 6.74501145e-01 -3.56360566e-01  3.07833011e-01  1.33225943e-01]
     [ 9.16836886e-01 -1.24957601e-01  3.64698715e-01  2.64698913e-01]
     [ 1.15917263e+00 -5.87763531e-01  5.92161531e-01  2.64698913e-01]
     [ 1.03800476e+00 -1.24957601e-01  7.05892939e-01  6.59117823e-01]
     [ 1.89829664e-01 -3.56360566e-01  4.21564419e-01  3.96171883e-01]
     [-1.73673948e-01 -1.05056946e+00 -1.47092621e-01 -2.61192967e-01]
     [-4.16009689e-01 -1.51337539e+00  2.35044910e-02 -1.29719997e-01]
     [-4.16009689e-01 -1.51337539e+00 -3.33612130e-02 -2.61192967e-01]
     [-5.25060772e-02 -8.19166497e-01  8.03701950e-02  1.75297293e-03]
     [ 1.89829664e-01 -8.19166497e-01  7.62758643e-01  5.27644853e-01]
     [-5.37177559e-01 -1.24957601e-01  4.21564419e-01  3.96171883e-01]
     [ 1.89829664e-01  8.00654259e-01  4.21564419e-01  5.27644853e-01]
     [ 1.03800476e+00  1.06445364e-01  5.35295827e-01  3.96171883e-01]
     [ 5.53333275e-01 -1.74477836e+00  3.64698715e-01  1.33225943e-01]
     [-2.94841818e-01 -1.24957601e-01  1.94101603e-01  1.33225943e-01]
     [-4.16009689e-01 -1.28197243e+00  1.37235899e-01  1.33225943e-01]
     [-4.16009689e-01 -1.05056946e+00  3.64698715e-01  1.75297293e-03]
     [ 3.10997534e-01 -1.24957601e-01  4.78430123e-01  2.64698913e-01]
     [-5.25060772e-02 -1.05056946e+00  1.37235899e-01  1.75297293e-03]
     [-1.02184904e+00 -1.74477836e+00 -2.60824029e-01 -2.61192967e-01]
     [-2.94841818e-01 -8.19166497e-01  2.50967307e-01  1.33225943e-01]
     [-1.73673948e-01 -1.24957601e-01  2.50967307e-01  1.75297293e-03]
     [-1.73673948e-01 -3.56360566e-01  2.50967307e-01  1.33225943e-01]
     [ 4.32165405e-01 -3.56360566e-01  3.07833011e-01  1.33225943e-01]
     [-9.00681170e-01 -1.28197243e+00 -4.31421141e-01 -1.29719997e-01]
     [-1.73673948e-01 -5.87763531e-01  1.94101603e-01  1.33225943e-01]
     [ 5.53333275e-01  5.69251294e-01  1.27454998e+00  1.71090158e+00]
     [-5.25060772e-02 -8.19166497e-01  7.62758643e-01  9.22063763e-01]
     [ 1.52267624e+00 -1.24957601e-01  1.21768427e+00  1.18500970e+00]
     [ 5.53333275e-01 -3.56360566e-01  1.04708716e+00  7.90590793e-01]
     [ 7.95669016e-01 -1.24957601e-01  1.16081857e+00  1.31648267e+00]
     [ 2.12851559e+00 -1.24957601e-01  1.61574420e+00  1.18500970e+00]
     [-1.14301691e+00 -1.28197243e+00  4.21564419e-01  6.59117823e-01]
     [ 1.76501198e+00 -3.56360566e-01  1.44514709e+00  7.90590793e-01]
     [ 1.03800476e+00 -1.28197243e+00  1.16081857e+00  7.90590793e-01]
     [ 1.64384411e+00  1.26346019e+00  1.33141568e+00  1.71090158e+00]
     [ 7.95669016e-01  3.37848329e-01  7.62758643e-01  1.05353673e+00]
     [ 6.74501145e-01 -8.19166497e-01  8.76490051e-01  9.22063763e-01]
     [ 1.15917263e+00 -1.24957601e-01  9.90221459e-01  1.18500970e+00]
     [-1.73673948e-01 -1.28197243e+00  7.05892939e-01  1.05353673e+00]
     [-5.25060772e-02 -5.87763531e-01  7.62758643e-01  1.57942861e+00]
     [ 6.74501145e-01  3.37848329e-01  8.76490051e-01  1.44795564e+00]
     [ 7.95669016e-01 -1.24957601e-01  9.90221459e-01  7.90590793e-01]
     [ 2.24968346e+00  1.72626612e+00  1.67260991e+00  1.31648267e+00]
     [ 2.24968346e+00 -1.05056946e+00  1.78634131e+00  1.44795564e+00]
     [ 1.89829664e-01 -1.97618132e+00  7.05892939e-01  3.96171883e-01]
     [ 1.28034050e+00  3.37848329e-01  1.10395287e+00  1.44795564e+00]
     [-2.94841818e-01 -5.87763531e-01  6.49027235e-01  1.05353673e+00]
     [ 2.24968346e+00 -5.87763531e-01  1.67260991e+00  1.05353673e+00]
     [ 5.53333275e-01 -8.19166497e-01  6.49027235e-01  7.90590793e-01]
     [ 1.03800476e+00  5.69251294e-01  1.10395287e+00  1.18500970e+00]
     [ 1.64384411e+00  3.37848329e-01  1.27454998e+00  7.90590793e-01]
     [ 4.32165405e-01 -5.87763531e-01  5.92161531e-01  7.90590793e-01]
     [ 3.10997534e-01 -1.24957601e-01  6.49027235e-01  7.90590793e-01]
     [ 6.74501145e-01 -5.87763531e-01  1.04708716e+00  1.18500970e+00]
     [ 1.64384411e+00 -1.24957601e-01  1.16081857e+00  5.27644853e-01]
     [ 1.88617985e+00 -5.87763531e-01  1.33141568e+00  9.22063763e-01]
     [ 2.49201920e+00  1.72626612e+00  1.50201279e+00  1.05353673e+00]
     [ 6.74501145e-01 -5.87763531e-01  1.04708716e+00  1.31648267e+00]
     [ 5.53333275e-01 -5.87763531e-01  7.62758643e-01  3.96171883e-01]
     [ 3.10997534e-01 -1.05056946e+00  1.04708716e+00  2.64698913e-01]
     [ 2.24968346e+00 -1.24957601e-01  1.33141568e+00  1.44795564e+00]
     [ 5.53333275e-01  8.00654259e-01  1.04708716e+00  1.57942861e+00]
     [ 6.74501145e-01  1.06445364e-01  9.90221459e-01  7.90590793e-01]
     [ 1.89829664e-01 -1.24957601e-01  5.92161531e-01  7.90590793e-01]
     [ 1.28034050e+00  1.06445364e-01  9.33355755e-01  1.18500970e+00]
     [ 1.03800476e+00  1.06445364e-01  1.04708716e+00  1.57942861e+00]
     [ 1.28034050e+00  1.06445364e-01  7.62758643e-01  1.44795564e+00]
     [-5.25060772e-02 -8.19166497e-01  7.62758643e-01  9.22063763e-01]
     [ 1.15917263e+00  3.37848329e-01  1.21768427e+00  1.44795564e+00]
     [ 1.03800476e+00  5.69251294e-01  1.10395287e+00  1.71090158e+00]
     [ 1.03800476e+00 -1.24957601e-01  8.19624347e-01  1.44795564e+00]
     [ 5.53333275e-01 -1.28197243e+00  7.05892939e-01  9.22063763e-01]
     [ 7.95669016e-01 -1.24957601e-01  8.19624347e-01  1.05353673e+00]
     [ 4.32165405e-01  8.00654259e-01  9.33355755e-01  1.44795564e+00]
     [ 6.86617933e-02 -1.24957601e-01  7.62758643e-01  7.90590793e-01]]

     Eigenvectors:
     [[ 0.52237162 -0.26335492  0.58125401  0.56561105]
     [ 0.37231836  0.92555649  0.02109478  0.06541577]]

     Eigenvalues:
     [2.93035378 0.92740362]

     Projected Data:
     [[-2.26454173e+00  5.05703903e-01]
     [-2.08642550e+00 -6.55404729e-01]
     [-2.36795045e+00 -3.18477311e-01]
     [-2.30419716e+00 -5.75367713e-01]
     [-2.38877749e+00  6.74767397e-01]
     [-2.07053681e+00  1.51854856e+00]
     [-2.44571134e+00  7.45626750e-02]
     [-2.23384186e+00  2.47613932e-01]
     [-2.34195768e+00 -1.09514636e+00]
     [-2.18867576e+00 -4.48629048e-01]
     [-2.16348656e+00  1.07059558e+00]
     [-2.32737775e+00  1.58587455e-01]
     [-2.22408272e+00 -7.09118158e-01]
     [-2.63971626e+00 -9.38281982e-01]
     [-2.19229151e+00  1.88997851e+00]
     [-2.25146521e+00  2.72237108e+00]
     [-2.20275048e+00  1.51375028e+00]
     [-2.19017916e+00  5.14304308e-01]
     [-1.89407429e+00  1.43111071e+00]
     [-2.33994907e+00  1.15803343e+00]
     [-1.91455639e+00  4.30465163e-01]
     [-2.20464540e+00  9.52457317e-01]
     [-2.77416979e+00  4.89517027e-01]
     [-1.82041156e+00  1.06750793e-01]
     [-2.22821750e+00  1.62186163e-01]
     [-1.95702401e+00 -6.07892567e-01]
     [-2.05206331e+00  2.66014312e-01]
     [-2.16819365e+00  5.52016495e-01]
     [-2.14030596e+00  3.36640409e-01]
     [-2.26879019e+00 -3.14878603e-01]
     [-2.14455443e+00 -4.83942097e-01]
     [-1.83193810e+00  4.45266836e-01]
     [-2.60820287e+00  1.82847519e+00]
     [-2.43795086e+00  2.18539162e+00]
     [-2.18867576e+00 -4.48629048e-01]
     [-2.21111990e+00 -1.84337811e-01]
     [-2.04441652e+00  6.84956426e-01]
     [-2.18867576e+00 -4.48629048e-01]
     [-2.43595220e+00 -8.82169415e-01]
     [-2.17054720e+00  2.92726955e-01]
     [-2.28652724e+00  4.67991716e-01]
     [-1.87170722e+00 -2.32769161e+00]
     [-2.55783442e+00 -4.53816380e-01]
     [-1.96427929e+00  4.97391640e-01]
     [-2.13337283e+00  1.17143211e+00]
     [-2.07535759e+00 -6.91917347e-01]
     [-2.38125822e+00  1.15063259e+00]
     [-2.39819169e+00 -3.62390765e-01]
     [-2.22678121e+00  1.02548255e+00]
     [-2.20595417e+00  3.22378453e-02]
     [ 1.10399365e+00  8.63112446e-01]
     [ 7.32481440e-01  5.98635573e-01]
     [ 1.24210951e+00  6.14822450e-01]
     [ 3.97307283e-01 -1.75816895e+00]
     [ 1.07259395e+00 -2.11757903e-01]
     [ 3.84458146e-01 -5.91062469e-01]
     [ 7.48715076e-01  7.78698611e-01]
     [-4.97863388e-01 -1.84886877e+00]
     [ 9.26222368e-01  3.03308268e-02]
     [ 4.96802558e-03 -1.02940111e+00]
     [-1.24697461e-01 -2.65806268e+00]
     [ 4.38730118e-01 -5.88812850e-02]
     [ 5.51633981e-01 -1.77258156e+00]
     [ 7.17165066e-01 -1.85434315e-01]
     [-3.72583830e-02 -4.32795099e-01]
     [ 8.75890536e-01  5.09998151e-01]
     [ 3.48006402e-01 -1.90621647e-01]
     [ 1.53392545e-01 -7.90725456e-01]
     [ 1.21530321e+00 -1.63335564e+00]
     [ 1.56941176e-01 -1.30310327e+00]
     [ 7.38256104e-01  4.02470382e-01]
     [ 4.72369682e-01 -4.16608222e-01]
     [ 1.22798821e+00 -9.40914793e-01]
     [ 6.29381045e-01 -4.16811643e-01]
     [ 7.00472799e-01 -6.34939277e-02]
     [ 8.73536987e-01  2.50708611e-01]
     [ 1.25422219e+00 -8.26200998e-02]
     [ 1.35823985e+00  3.28820266e-01]
     [ 6.62126138e-01 -2.24346071e-01]
     [-4.72815133e-02 -1.05721241e+00]
     [ 1.21534209e-01 -1.56359238e+00]
     [ 1.41182261e-02 -1.57339235e+00]
     [ 2.36010837e-01 -7.75923784e-01]
     [ 1.05669143e+00 -6.36901284e-01]
     [ 2.21417088e-01 -2.80847693e-01]
     [ 4.31783161e-01  8.55136920e-01]
     [ 1.04941336e+00  5.22197265e-01]
     [ 1.03587821e+00 -1.39246648e+00]
     [ 6.70675999e-02 -2.12620735e-01]
     [ 2.75425066e-01 -1.32981591e+00]
     [ 2.72335066e-01 -1.11944152e+00]
     [ 6.23170540e-01  2.75426333e-02]
     [ 3.30005364e-01 -9.88900732e-01]
     [-3.73627623e-01 -2.01793227e+00]
     [ 2.82944343e-01 -8.53950717e-01]
     [ 8.90531103e-02 -1.74908548e-01]
     [ 2.24356783e-01 -3.80484659e-01]
     [ 5.73883486e-01 -1.53719974e-01]
     [-4.57012873e-01 -1.53946451e+00]
     [ 2.52244473e-01 -5.95860746e-01]
     [ 1.84767259e+00  8.71696662e-01]
     [ 1.15318981e+00 -7.01326114e-01]
     [ 2.20634950e+00  5.54470105e-01]
     [ 1.43868540e+00 -5.00105223e-02]
     [ 1.86789070e+00  2.91192802e-01]
     [ 2.75419671e+00  7.88432206e-01]
     [ 3.58374475e-01 -1.56009458e+00]
     [ 2.30300590e+00  4.09516695e-01]
     [ 2.00173530e+00 -7.23865359e-01]
     [ 2.26755460e+00  1.92144299e+00]
     [ 1.36590943e+00  6.93948040e-01]
     [ 1.59906459e+00 -4.28248836e-01]
     [ 1.88425185e+00  4.14332758e-01]
     [ 1.25308651e+00 -1.16739134e+00]
     [ 1.46406152e+00 -4.44147569e-01]
     [ 1.59180930e+00  6.77035372e-01]
     [ 1.47128019e+00  2.53192472e-01]
     [ 2.43737848e+00  2.55675734e+00]
     [ 3.30914118e+00 -2.36132010e-03]
     [ 1.25398099e+00 -1.71758384e+00]
     [ 2.04049626e+00  9.07398765e-01]
     [ 9.73915114e-01 -5.71174376e-01]
     [ 2.89806444e+00  3.97791359e-01]
     [ 1.32919369e+00 -4.86760542e-01]
     [ 1.70424071e+00  1.01414842e+00]
     [ 1.95772766e+00  1.00333452e+00]
     [ 1.17190451e+00 -3.18896617e-01]
     [ 1.01978105e+00  6.55429631e-02]
     [ 1.78600886e+00 -1.93272800e-01]
     [ 1.86477791e+00  5.55381532e-01]
     [ 2.43549739e+00  2.46654468e-01]
     [ 2.31608241e+00  2.62618387e+00]
     [ 1.86037143e+00 -1.84672394e-01]
     [ 1.11127173e+00 -2.95986102e-01]
     [ 1.19746916e+00 -8.17167742e-01]
     [ 2.80094940e+00  8.44748194e-01]
     [ 1.58015525e+00  1.07247450e+00]
     [ 1.34704442e+00  4.22255966e-01]
     [ 9.23432978e-01  1.92303705e-02]
     [ 1.85355198e+00  6.72422729e-01]
     [ 2.01615720e+00  6.10397038e-01]
     [ 1.90311686e+00  6.86024832e-01]
     [ 1.15318981e+00 -7.01326114e-01]
     [ 2.04330844e+00  8.64684880e-01]
     [ 2.00169097e+00  1.04855005e+00]
     [ 1.87052207e+00  3.82821838e-01]
     [ 1.55849189e+00 -9.05313601e-01]
     [ 1.52084506e+00  2.66794575e-01]
     [ 1.37639119e+00  1.01636193e+00]
     [ 9.59298576e-01 -2.22839447e-02]]

```
transpose_B = B.T
```

```
x = transpose_B[0]
y = transpose_B[1]

```

```
data = {"x": x, "y": y}

df = pd.DataFrame(data)

plt.scatter(df['x'], df['y'])
plt.title("Data After PCA")
plt.xlabel('Species?')
plt.show()
```

# Stretch Goal

## 1) Do NOT work on the stretch goal until you feel like you have a firm grasp of eigenvectors, eigenvalues, and PCA. Prioritize self-study over the stretch goal if you are not comfortable with those topics yet.

## 2) Explore further the intuition behind eigenvalues and eigenvectors by creating your very own eigenfaces:

You don't necessarily have to use this resource, but this will get you started:
[Eigenface Tutorial](https://sandipanweb.wordpress.com/2018/01/06/eigenfaces-and-a-simple-face-detector-with-pca-svd-in-python/)


```python
from sympy import *
from sympy import init_printing
init_printing()
import numpy as np
from numpy import *
from scipy import special, optimize
from scipy.special import *
from fractions import Fraction
```

```python
p=-Fraction(3,2)
b=Fraction(7,100)
sigma=Fraction(25,100)
vsig=-Fraction(4,100)
r=Fraction(2,100)
c=3
bta=Fraction(3,100)
```

```python
F1=(b-r)/((1-p)*sigma**2)
F2=1/((1-p)*sigma**2)
JI=lambda y:((1+y*vsig)**(p-1)-1)*c*vsig
x=optimize.brentq(lambda z: z-F1-F2*JI(z),0,1)
A=(bta/(1-p)-p*(x*b+r*(1-x))/(1-p)+(1/2)*sigma**2*x**2*p-(1/(1-p))*c*((1+vsig*x)**p-1-x*p*vsig))**(p-1)
LOC=(x,A,A**(1/(p-1)))
LOC
```

```python

```

```python

```

```python
(0.2967292770068063,7322.720440666773,0.028453193557581948)
```

    (0.2967292770068063, 7322.720440666773, 0.028453193557581948)

```python

```


# Computing mass functions, halo biases and concentrations
This notebook illustrates how to compute mass functions, halo biases and concentration-mass relations with CCL, as well as how to translate between different mass definitions.

```python
import numpy as np
import pylab as plt
import pyccl as ccl
%matplotlib inline
```

## Preliminaries
Generate a cosmology object and a few mass/redshift arrays

```python
# Cosmology
cosmo = ccl.Cosmology(Omega_c=0.27, Omega_b=0.045,
                      h=0.67, A_s=2.1e-9, n_s=0.96)

# Array of masses
m_arr = np.geomspace(1E10,1E15,128)

# Array of redshifts
z_arr = np.linspace(0.,1.,16)
```

## Mass definitions
CCL admits 3 different classes of definitions:
- Spherical overdensity (SO). The mass is defined as that enclosed by a radius within which the mean density is a factor $\Delta$ larger than the matter or critical density ($x$ is either $M$ or ${\rm crit}$:

\begin{equation}
   M_{\Delta,x} = \frac{4\pi}{3}\Delta\rho_x R_{\Delta,x}^3
\end{equation},
- Virial spherical overdensity. The same as SO for the specific choice $\Delta=\Delta_{\rm vir}(z)$ and $x={\rm critical}$, where $\Delta_{\rm vir}$ is the virial overdensity, which CCL computes from Brian & Norman 1998.
- Friends-of-friends masses (fof).

If you can attach a concentration-mass relation to a given SO mass definition, CCL is then able to translate masses according to that definition into any other SO definition assuming an NFW profile. This is only an approximation, and it's actually better to make sure you use consistent mass definitions throughout, but this functionality is provided for convenience.

These mass definition objects can then be passed around to all halo-model functions to make sure masses are treated consistently.

```python
# Delta=200 (matter).
# This one has an associated concentration-mass relation,
# so we can convert to other SO mass definitions
hmd_200m = ccl.halos.MassDef200m()

# Delta=200 (critical).
# This one has an associated concentration-mass relation,
# so we can convert to other SO mass definitions
hmd_200c = ccl.halos.MassDef200c()

# You can also change the c(M) relation as follows:
hmd_200c_b = ccl.halos.MassDef200c(c_m='Bhattacharya13')

# Delta=500 (matter).
# This one does not have a c(M) relation.
hmd_500m = ccl.halos.MassDef(500, 'matter')

# Virial overdensity
hmd_vir = ccl.halos.MassDef('vir', 'critical')

# FoF mass definition
hmd_fof = ccl.halos.MassDef('fof', 'matter')
```

Note that associating concentration-mass relations with mass definitions is only necessary if you'll want to translate between different mass definitions. Otherwise, you can use any concentration-mass relation you want for a given mass definition as we show further down (even if that c(M) relation is not the one you used to initialize the corresponding mass definition object).

## Mass functions
Mass functions are computed through classes that inherit from the `MassFunc` class. CCL supports a wide variety of mass function parametrizations, but more can be created following the instructions in the documentation.

All mass functions have a mass definition attached to them. Some mass functions support a range of mass definitions, and you can select which one you want when instantiating the class. All mass functions have default mass definitions, which are used if `None` is passed (which is the case below).

```python
hmfs = []
# Press & Schechter mass function
hmfs.append(ccl.halos.MassFuncPress74(cosmo))

# Sheth & Tormen mass function
hmfs.append(ccl.halos.MassFuncSheth99(cosmo))

# Tinker 2008 mass function
hmfs.append(ccl.halos.MassFuncTinker08(cosmo))

# Tinker 2010 mass function
hmfs.append(ccl.halos.MassFuncTinker10(cosmo))

# Bocquet 2016 mass function
hmfs.append(ccl.halos.MassFuncBocquet16(cosmo))

# Let's plot all of them at z=0
plt.figure()
for mf in hmfs:
    nm = mf.get_mass_function(cosmo, m_arr, 1.)
    plt.plot(m_arr,
             m_arr * nm, label=mf.name)
plt.xscale('log')
plt.ylim([1E9,8.5E9])
plt.legend()
plt.xlabel(r'$M/M_\odot$', fontsize=14)
plt.ylabel(r'$M\,\frac{dn}{d\log_{10}M}\,[M_\odot\,{\rm Mpc}^{-3}]$',
           fontsize=14);
```

Let's explore the time evolution of the mass function

```python
# Look at time evolution
from matplotlib.pyplot import cm

hmf_200m = ccl.halos.MassFuncTinker08(cosmo, mass_def=hmd_200m)
plt.figure()
plt.title(r'$0<z<1$',fontsize=14)
for z in z_arr:
    nm = hmf_200m.get_mass_function(cosmo, m_arr, 1./(1+z))
    plt.plot(m_arr,
             m_arr * nm, c=cm.autumn(z))
plt.xscale('log')
plt.ylim([5E8,7E9])
plt.xlabel(r'$M/M_\odot$',fontsize=14)
plt.ylabel(r'$M\,\frac{dn}{d\log_{10}M}\,[M_\odot\,{\rm Mpc}^{-3}]$',
           fontsize=14);
```

## Halo bias
Similar comments apply to the different halo bias parametrizations supported by CCL.

```python
hbfs = []
# Sheth & Tormen 1999
hbfs.append(ccl.halos.HaloBiasSheth99(cosmo))

# Sheth & Tormen 2001
hbfs.append(ccl.halos.HaloBiasSheth01(cosmo))

# Bhattacharya 2011
hbfs.append(ccl.halos.HaloBiasBhattacharya11(cosmo))

# Tinker 2010
hbfs.append(ccl.halos.HaloBiasTinker10(cosmo))

# Let's plot all of them at z=0
plt.figure()
for bf in hbfs:
    bm = bf.get_halo_bias(cosmo, m_arr, 1.)
    plt.plot(m_arr, bm, label=bf.name)
plt.xscale('log')
plt.legend()
plt.xlabel(r'$M/M_\odot$', fontsize=14)
plt.ylabel(r'$b_h(M)$', fontsize=14);
```

## Concentration-mass relation
Concentration-mass relations work in a similar way

```python
cmrs = []
# Diemer 2015
cmrs.append(ccl.halos.ConcentrationDiemer15())

# Bhattacharya 2013
cmrs.append(ccl.halos.ConcentrationBhattacharya13())

# Prada 2012
cmrs.append(ccl.halos.ConcentrationPrada12())

# Klypin 2011
cmrs.append(ccl.halos.ConcentrationKlypin11())

# Duffy 2008
cmrs.append(ccl.halos.ConcentrationDuffy08())

# Let's plot all of them at z=0
plt.figure()
for cmr in cmrs:
    cm = cmr.get_concentration(cosmo, m_arr, 1.)
    plt.plot(m_arr, cm, label=cmr.name)
plt.xscale('log')
plt.legend()
plt.xlabel(r'$M/M_\odot$', fontsize=14)
plt.ylabel(r'$c(M)$', fontsize=14);
```

## Convenience functions
It is possible to select mass functions, halo biases and concentration-mass relation from their name as follows

```python
nm = ccl.halos.mass_function_from_name('Tinker08')
bm = ccl.halos.halo_bias_from_name('Tinker10')
cm = ccl.halos.concentration_from_name('Duffy08')
print(nm)
print(bm)
print(cm)
```

    <class 'pyccl.halos.hmfunc.MassFuncTinker08'>
    <class 'pyccl.halos.hbias.HaloBiasTinker10'>
    <class 'pyccl.halos.concentration.ConcentrationDuffy08'>

## Mass conversion
The lines below show how to convert between different mass definitions (and the consequences of doing so). First, we generate mass function objects for $\Delta=200$ and $500$. Then, we compute the mass function using both parametrizations, but for masses defined using $\Delta=200$ (the $\Delta=500$ mass function will use the concentration-mass relation to translate masses from $\Delta=200$ to $\Delta=500$ automatically). As you can see, doing so incurrs a systematic error of 5-20%.

```python
# Let's define a mass function object for Delta = 500 (matter)
hmf_500m = ccl.halos.MassFuncTinker08(cosmo, mass_def=hmd_500m)

# Now let's compare the mass function parametrized for 200 (matter)
# with the mass function parametrized for 500 (matter) but
# translated to 200 (matter)
nm = hmf_200m.get_mass_function(cosmo, m_arr, 1.,
                                mdef_other = hmd_200m)
nm_trans = hmf_500m.get_mass_function(cosmo, m_arr, 1.,
                                      mdef_other = hmd_200m)
plt.figure()
plt.plot(m_arr,nm_trans/nm-1)
plt.xscale('log')
plt.xlabel(r'$M/M_\odot$',fontsize=14)
plt.ylabel('Error from mass translation$',
           fontsize=14);

```

```python

```


| |Pierre Proulx, ing, professeur|
|:---|:---|
|Département de génie chimique et de génie biotechnologique |** GCH200-Phénomènes d'échanges I **|

## Exemple 7-6.1

#### Solution

##### Bilan de masse
$\begin{equation*}
  \boxed{\rho v_1 S_1 - \rho v_2 S_2 = 0}
  \end{equation*}$ (1)

##### Bilan de quantité de mouvement linéaire dans la direction z

$\begin{equation*}
  \boxed{ ( v_1w_1 + p_1 S_1)-( v_2w_2 + p_2 S_2)  =
          \vec F_{fluide \rightarrow surface}}
  \end{equation*}$ (2)

mais la force que le fluide exerce sur la conduite peut être évalué en reconnaissant que la force exercée par le fluide sur la conduite dans la direction z est principalement la pression exercée par le fluide sur la surface annulaire juste après l'expansion. Donc $F =-p_1  (S_2-S_1)$ En substituant dans (2):

$\begin{equation*}
  \boxed{ ( v_1w + p_1 S_1)-( v_2w + p_2 S_2)  =
          -p_1  (S_2-S_1)}
  \end{equation*}$

Souvenons-nous que $w_1=w_2=w$ et que $\rho v_1S_1=\rho v_2 S_2$

$\begin{equation*}
  \boxed{ S_1( \rho v_1^2 + p_1) - S_2( \rho v_2^2 + p_2)  =
          -p_1  (S_2-S_1)}
  \end{equation*}$

ou

$\begin{equation*}
  \boxed{ S_1 \rho v_1^2  - S_2 \rho v_2^2 = -S_2(p_1-p_2 )}
  \end{equation*}$ (3)

## 4. Bilan d'énergie mécanique

$\begin{equation*}
  \boxed{  \bigg ( \frac {1}{2} v_1^2 +  \frac {p_1}{\rho_1} \bigg)-
           \bigg ( \frac {1}{2} v_2^2 +  \frac {p_2}{\rho_2} \bigg )=
          E_v}
  \end{equation*}$

```python
# Préparation de l'affichage et des outils de calcul symbolique
#
import sympy as sp
from IPython.display import *
sp.init_printing(use_latex=True)
%matplotlib inline
#
v_1,v_2,p_1,p_2,S_1,S_2,rho,E_v=sp.symbols('v_1,v_2,p_1,p_2,S_1,S_2,rho,E_v')
eq1=sp.Eq(v_1*S_1-v_2*S_2)
eq2=sp.Eq(v_1**2*S_1*rho-v_2**2*S_2*rho+S_2*(p_1-p_2))
eq3=sp.Eq(1/2*v_1**2+p_1/rho-1/2*v_2**2+p_2/rho -E_v)
solution=sp.solve((eq1,eq2,eq3),v_2,p_2,E_v)
display(solution[0][2].simplify())
```

```python

```


# Development of Angular correlation function routine

## Theory

### General Derivation

In this development, we basically follow the arguments of the Simon+2007 paper, and try to repdroduce its results to test consistency.

In general, the correlation function takes two data-sets, which we'll subscript with 1 and 2 throughout. We usually want the "auto-correlation", which means the two sets are equivalent.

Let $n_1(\vec{r})$ and $n_2(\vec{r})$ be two densities fields, as a function of the position $\vec{r}$ in comoving co-ordinates. To obtain the projected density at any angle $\vec{\theta}$, we integrate over all radii, but also filter each slice by a function $q(r)$, which takes into account selection effects (i.e. $q$ is the fraction of galaxies at $r$ that are actually *seen*). Thus:

$$ \hat{n}_i(\vec{\theta}) = \int_0^\infty dr\ q_i(r) n_i(r\vec{\theta},r), $$

and

$$ \int_0^\infty dr\ q_i(r) = 1, $$

Specifying a different filter function based on the relationship of density *contrast* to density, we have

$$ p_i(r) = \frac{q_i(r) \bar{n}_i(r)}{\int_0^\infty dr\  q_i(r) \bar{n}_i(r)}. $$

This weight specifies not the chance of detecting a given galaxy at radius $r$ (as does $q$), but rather the chance of a galaxy in the sample coming from $r$. That is, it is the radial distribution of galaxies. From here, the formula for the projected correlation simply falls out as

$$ w(\theta) = \int_0^\infty dr_1\ \int_0^\infty dr_2\ p_1(r)1 p_2(r_2) \langle \delta_1(r_1 \theta'_1,r_1) \delta_2(r_2\theta'_2,r_2)\rangle $$

where $\delta$ is the overdensity, and $\theta'_i$ connotes the actual position of the two co-ordinates, which are separated by $\theta$.

The last term here is almost exactly the same as the standard correlation function, and can be transformed to such if we assume that a) the field is isotropic and homogeneous and b) the correlation function doesn't change significantly between $r_1$ and $r_2$ (in the case of an auto-correlation, this is always perfectly true). With these, we get the following general form:

$$ w(\theta) \approx \int_0^\infty dr_1\int_0^\infty dr_2\ p_1(r_1) p_2(r_2) \xi(R,\bar{r}), $$

where

$$ R \equiv \sqrt{r_1^2 + r_2^2 - 2r_1r_2 \cos \theta}, $$

and

$$ \bar{r} = (r_1+r_2)/2. $$

### Limber's Approximation

Limber's approximation is this:

$$ w(\theta) = 2\int_0^\infty d\bar{r} p_1(\bar{r}) p_2(\bar{r}) \int_0^{\infty} d\Delta r \xi(R,\bar{r}), $$

where

$$ R \equiv \sqrt{\bar{r}^2 \theta^2 + \Delta r^2}, $$

and the factor of 2 arises because of the symmetry of the integrand around 0.

The main usefulness of this approxiation is that in the case of a power-law $\xi(r)$, the result is analytic.

However, this breaks down in at least two regimes:

1. when the distribution of galaxies is thin (approaching a delta function)
2. when $\theta$ is large.

One easy way to see this is to notice that the general result for a correlation function with power-law slope $-\gamma$ is a power-law with slope $1-\gamma$.

However, using the more general equation, with $p_i = \delta(r-r')$, we get the simple result that $w(\theta) = \xi(r'\theta,r')$, i.e. the slope is unchanged.

### Transformation of general equation for numerical use

Though the general equation is valid, it is not necessarily optimal for numerical evaluation, because we can't force $R$ to be logarithmic. However, we can transform the integral to the following:

$$ w(\theta) = \frac{1}{1+\cos \theta} \int_0^\infty d\bar{r} \int\limits_{\bar{r}\sqrt{2(1-\cos \theta)}}^{2\bar{r}} dR\ Q(\bar{r},\Delta) \xi(R,\bar{r}) \frac{R}{\Delta}, $$

where

$$ \Delta \equiv \frac{1}{\sqrt{2}} \sqrt{\frac{R^2 - 2\bar{r}^2(1-\cos \theta)}{1+\cos \theta}}.$$

and

$$ Q(\bar{r},\Delta) \equiv p_1(\bar{r}-\Delta)p_2(\bar{r}+\Delta)+p_1(\bar{r}+\Delta)p_2(\bar{r}-\Delta).$$

In the case of Limber's approximation, we also want to do a similar transformation. We have

$$ \frac{dR}{d\Delta r} = \frac{\Delta r}{2R}, $$

so we have

$$ w(\theta) = 2\int_0^\infty d\bar{r}\ p_1(\bar{r}) p_2(\bar{r}) \int_{\bar{r}\theta}^\infty dR\ \frac{2R}{\sqrt{R^2-\bar{r}^2\theta^2}} \xi(R,\bar{r}). $$

### Small-angle approximation

In-built into the Limber approximation is the small-angle approximation, in which $1+\cos \theta \approx 2$. This is accurate to $\sim 10\%$ for angles less than 40 degrees. For their comparison of the general form to the Limber approximation, Simon+2007 employ this approximation to gaurantee fairness, and we'll implement it as well to test. In that case, we have

\begin{align}
 \frac{1}{1+\cos \theta} &\approx 1/2 \\
 \Delta &\approx \frac{1}{2} \sqrt{R^2 - \bar{r}^2\theta^2}\\
 \bar{r}\sqrt{2(1-\cos\theta)} &\approx \bar{r}\theta.
\end{align}

## Implementation

Throughout the following there's a few things to keep in mind. I don't have the luxury of just passing a function which calculates $\xi(R,r)$. I'll have to base it on having array-style functions.

```python
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

from halomod.halo_exclusion import dblsimps
from scipy.integrate import simps
from mrpy.special import gamma as Gamma
```

```python
# LIMBER APPROXIMATION

def limber_no_r_dep(theta,xi,p1,p2=None,logx_min=-5,logx_max=5,xnum=100,
                   logu_min=-4,logu_max=2,unum=500):
    # Arrays
    u = np.logspace(logu_min,logu_max,unum)
    x = np.logspace(logx_min,logx_max,xnum)
    dlnx = np.log(x[1]/x[0])
    dlnu = np.log(u[1]/u[0])

    p1 = check_p(p1,x,dlnx)

    if p2 is None:
        p2 = p1
    else:
        p2 = check_p(p2,x,dlnx)

    R = np.sqrt(np.add.outer(np.outer(theta**2 ,x**2),u**2)).flatten()

    integrand = np.einsum("kij,i,j->kij", xi(R).reshape((len(theta),len(x), len(u))), p1(x)*p2(x)* x, u)

    return 2*dblsimps(integrand,dlnx,dlnu)

def limber_r_dep(theta,xi,p1,p2=None,logx_min=-5,logx_max=5,xnum=100,
                 logu_min=-4,logu_max=2,unum=500):
    # Arrays
    u = np.logspace(logu_min,logu_max,unum)
    x = np.logspace(logx_min,logx_max,xnum)
    dlnx = np.log(x[1]/x[0])
    dlnu = np.log(u[1]/u[0])

    p1 = check_p(p1,x,dlnx)

    if p2 is None:
        p2 = p1
    else:
        p2 = check_p(p2,x,dlnx)

    R = np.sqrt(np.add.outer(np.outer(theta**2 ,x**2),u**2))
    xi = xi(np.transpose(R,(0,2,1)),x) #theta, u , x
    print xi.shape
    print (p1(x)*p2(x)* x).shape
    xu_bit = np.outer(u,p1(x)*p2(x)* x)

    integrand =  xi*xu_bit
#    np.einsum("kij,i,j->kij", xi , )

    return 2*dblsimps(integrand,dlnx,dlnu)

def check_p(p,x,dlnx):
    integ = simps(p(x)*x,dx=dlnx)
    if not np.isclose(integ,1.0,rtol=0.01):
        print "WARNING: Filter function p(x) did not integrate to 1 (%s). Tentatively re-normalising."%integ
        return (lambda x: p(x)/integ)
    else:
        return p

def limber_analytic_pl(theta,gamma,r0,p1,logx_min=-5,logx_max=5.0,p2=None):
    x = np.logspace(logx_min,logx_max,100)
    dlnx = np.log(x[1]/x[0])

    p1 = check_p(p1,x,dlnx)

    if p2 is None:
        p2 = p1
    else:
        p2 = check_p(p2,x,dlnx)

    pl = 1-gamma

    integ = p1(x)*p2(x)*x**pl

    Aw = np.sqrt(np.pi)*r0**gamma * Gamma(gamma/2-0.5)/Gamma(gamma/2) * simps(integ*x,dx=dlnx)
    print pl, Aw
    return Aw*theta**pl
```

```python
theta = np.logspace(-1,0,50)/3437.75

def pform(xc,sigp):
    return (lambda x: x**2 * np.exp(-0.5*(x-xc)**2/sigp**2))

def pl_xi(gamma,r0):
    return (lambda r: (r/r0)**(-gamma))

def pl_xi_rdep(gamma,r0,rind):
    return (lambda r,x: x**rind * (r/r0)**(-gamma))

plt.plot(3437.75*theta,limber_analytic_pl(theta,1.77,5.4,pform(500,500*0.15),logx_min=2,logx_max=3),color="b")
plt.plot(3437.75*theta,limber_no_r_dep(theta,pl_xi(1.77,5.4),pform(500,500*0.15),logx_min=2,logx_max=3,logu_max=5),color="b")
plt.plot(3437.75*theta,limber_r_dep(theta,pl_xi_rdep(1.77,5.4,0.0),pform(500,500*0.15),logx_min=2,logx_max=3,logu_max=5),color="g")
plt.plot(3437.75*theta,limber_r_dep(theta,pl_xi_rdep(1.77,5.4,0.2),pform(500,500*0.15),logx_min=2,logx_max=3,logu_max=5),color="g")

plt.plot(3437.75*theta,limber_analytic_pl(theta,1.77,5.4,pform(500,500*0.37),logx_min=2,logx_max=3),color="r")
plt.plot(3437.75*theta,limber_no_r_dep(theta,pl_xi(1.77,5.4),pform(500,500*0.37),logx_min=2,logx_max=3,logu_max=5),color="r")

plt.xscale('log')
plt.yscale('log')
plt.xlabel("arcmin")
```

```python
def general_no_r_dep(theta,xi,p1,p2=None,logx_min=-5,logx_max=5,xnum=100,
                     rmin=0.01,rmax = 1e4,
                     f=0.001,Rnum=500,dR_min=1e-4):
    # Arrays
    x = np.logspace(logx_min,logx_max,xnum)
    dlnx = np.log(x[1]/x[0])

    p1 = check_p(p1,x,dlnx)
    if p2 is None:
        p2 = p1
    else:
        p2 = check_p(p2,x,dlnx)

    Rmin = np.outer(x,np.sqrt(2*(1-np.cos(theta))))   # (x*theta array)

    # Assuming a roughly power-law slope, get approximately 1-f of the integral
    # below (1-f)**(1./(1-slope)) times the minimum.
    slope = -np.log(xi(rmax)/xi(rmin))/np.log(rmax/rmin)
    a = f**(1./(1-slope))
    print slope, a

    # Problem witht he following is that Rmax may be greater than 2*x, which is impossible.
    #Rmax = Rmin * a
    R = np.logspace(0,np.log10(a),Rnum)
    dlnR = np.log(R[1])

    R = np.outer(R,(Rmin+dR_min)).reshape((Rnum,len(x),len(theta))) #(Rnum,x,theta) array.
    x2theta = np.outer(2*x**2,(1-np.cos(theta))) # (x,theta) array
    x2theta = np.clip(R**2 - x2theta,0,np.inf)

    Delta = np.transpose(np.sqrt(x2theta/(2*(1+np.cos(theta)))),(2,0,1)) #(theta,Rnum,x) array

    Q = p1(x-Delta)*p2(x+Delta) + p1(x+Delta)*p2(x-Delta) #(theta,Rnum,x array)
    integrand = Q*xi(R.flatten()).reshape((len(theta),Rnum,len(x))) * np.transpose(R**2,(2,0,1))/Delta * x

    #print integrand, dlnR, dlnx
    return dblsimps(integrand,dlnR, dlnx)
```

```python
theta = np.logspace(-2,-1,5)
a = general_no_r_dep(theta,pl_xi(1.77,5.4),pform(500,500*0.15),xnum=10,Rnum=8)
print a
```

    WARNING: Filter function p(x) did not integrate to 1 (228685537.537). Tentatively re-normalising.
    1.77 7872.34096884
    [  3.42481852e+08   4.08858649e+05   6.00569789e+04   1.05714466e+03
       5.54148752e-02]

```python
a
```

    array([  3.42481852e+08,   4.08858649e+05,   6.00569789e+04,
             1.05714466e+03,   5.54148752e-02])

```python
theta = np.logspace(-1,0,50)/3437.75

def pform(xc,sigp):
    return (lambda x: x**2 * np.exp(-0.5*(x-xc)**2/sigp**2))

def pl_xi(gamma,r0):
    return (lambda r: (r/r0)**(-gamma))

plt.plot(3437.75*theta,limber_analytic_pl(theta,1.77,5.4,pform(500,500*0.15),logx_min=2,logx_max=3),color="b")
plt.plot(3437.75*theta,limber_no_r_dep(theta,pl_xi(1.77,5.4),pform(500,500*0.15),logx_min=2,logx_max=3,logu_max=5),color="b")
plt.plot(3437.75*theta,general_no_r_dep(theta,pl_xi(1.77,5.4),pform(500,500*0.15)),color="b")

plt.plot(3437.75*theta,limber_analytic_pl(theta,1.77,5.4,pform(500,500*0.37),logx_min=2,logx_max=3),color="r")
plt.plot(3437.75*theta,limber_no_r_dep(theta,pl_xi(1.77,5.4),pform(500,500*0.37),logx_min=2,logx_max=3,logu_max=5),color="r")

plt.xscale('log')
plt.yscale('log')
plt.xlabel("arcmin")
```

```python
theta = np.logspace(-2,1,50)
plt.plot(theta*180.0/np.pi,2./np.sqrt(2*(1-np.cos(theta))))
plt.xscale('log')
plt.yscale('log')
```

```python

```


# Characterization of Systems in the Time Domain

*This Jupyter notebook is part of a [collection of notebooks](../index.ipynb) in the bachelors module Signals and Systems, Communications Engineering, Universität Rostock. Please direct questions and suggestions to [Sascha.Spors@uni-rostock.de](mailto:Sascha.Spors@uni-rostock.de).*

## Impulse Response

The response $y(t)$ of a linear time-invariant (LTI) system $\mathcal{H}$ to an arbitrary input signal $x(t)$ is derived in the following. The input signal can be represented as an integral when applying the [sifting-property of the Dirac impulse](../continuous_signals/standard_signals.ipynb#Dirac-Impulse)

\begin{equation}
x(t) = \int_{-\infty}^{\infty} x(\tau) \cdot \delta(t-\tau) \; d \tau
\end{equation}

Introducing above relation for the the input signal $x(t)$ into the output signal $y(t) = \mathcal{H} \{ x(t) \}$ of the system yields

\begin{equation}
y(t) = \mathcal{H} \left\{ \int_{-\infty}^{\infty} x(\tau) \cdot \delta(t-\tau) \; d \tau \right\}
\end{equation}

where $\mathcal{H} \{ \cdot \}$ denotes the system response operator. The integration and system response operator can be exchanged under the assumption that the system is linear

\begin{equation}
y(t) = \int_{-\infty}^{\infty}  x(\tau) \cdot \mathcal{H} \left\{ \delta(t-\tau) \right\} \; d \tau
\end{equation}

where $\mathcal{H} \{\cdot\}$ was only applied to the Dirac impulse, since $x(\tau)$ can be regarded as constant factor with respect to the time $t$. It becomes evident that the response of a system to a Dirac impulse plays an important role in the calculation of the output signal for arbitrary input signals.

The response of a system to a Dirac impulse as input signal is denoted as [*impulse response*](https://en.wikipedia.org/wiki/Impulse_response). It is defined as

\begin{equation}
h(t) = \mathcal{H} \left\{ \delta(t) \right\}
\end{equation}

If the system is time-invariant, the response to a shifted Dirac impulse is $\mathcal{H} \left\{ \delta(t-\tau) \right\} = h(t-\tau)$. Hence, for an LTI system we finally get

\begin{equation}
y(t) = \int_{-\infty}^{\infty}  x(\tau) \cdot h(t-\tau) \; d \tau
\end{equation}

Due to its relevance in the theory of LTI systems, this operation is explicitly termed as [*convolution*](https://en.wikipedia.org/wiki/Convolution). It is commonly abbreviated by $*$, hence for above integral we get $y(t) = x(t) * h(t)$. In some books the mathematically more precise nomenclature $y(t) = (x*h)(t)$ is used, since $*$ is the operator acting on the two signals $x$ and $h$ with regard to time $t$.

In can be concluded that the properties of an LTI system are entirely characterized by its impulse response. The response $y(t)$ of a system to an arbitrary input signal $x(t)$ is given by the convolution of the input signal $x(t)$ with its impulse response $h(t)$.

**Example**

The following example considers an LTI system whose relation between input $x(t)$ and output $y(t)$ is given by an ordinary differential equation (ODE) with constant coefficients

\begin{equation}
y(t) + \frac{d}{dt} y(t) = x(t)
\end{equation}

The system response is computed for the input signal $x(t) = e^{- 2 t} \cdot \epsilon(t)$ by

1. explicitly solving the ODE and by
2. computing the impulse response $h(t)$ and convolution with the input signal.

The solution should fulfill the initial conditions $y(t)\big\vert_{t = 0-} = 0$ and $\frac{d}{dt}y(t)\big\vert_{t = 0-} = 0$ due to causality.

First the ODE is defined in `SymPy`

```python
import sympy as sym
%matplotlib inline
sym.init_printing()

t = sym.symbols('t', real=True)
x = sym.Function('x')(t)
y = sym.Function('y')(t)

ode = sym.Eq(y + y.diff(t), x)
ode
```

The ODE is solved for the given input signal in order to calculate the output signal. The integration constant is calculated such that the solution fulfills the initial conditions

```python
solution = sym.dsolve(ode.subs(x, sym.exp(-2*t)*sym.Heaviside(t)))
integration_constants = sym.solve(
    (solution.rhs.limit(t, 0, '-'), solution.rhs.diff(t).limit(t, 0, '-')), 'C1')
y1 = solution.subs(integration_constants)
y1
```

Lets plot the output signal derived by explicit solution of the ODE

```python
sym.plot(y1.rhs, (t,-1,10), ylabel=r'$y(t)$');
```

The impulse response $h(t)$ is computed by solving the ODE for a Dirac impulse as input signal, $x(t) = \delta(t)$

```python
h = sym.Function('h')(t)
solution2 = sym.dsolve(ode.subs(x, sym.DiracDelta(t)).subs(y, h))
integration_constants = sym.solve((solution2.rhs.limit(
    t, 0, '-'), solution2.rhs.diff(t).limit(t, 0, '-')), 'C1')
h = solution2.subs(integration_constants)
h
```

Lets plot the impulse response $h(t)$ of the LTI system

```python
sym.plot(h.rhs, (t,-1,10), ylabel=r'$h(t)$');
```

As alternative to the explicit solution of the ODE, the system response is computed by evaluating the convolution $y(t) = x(t) * h(t)$. Since `SymPy` cannot handle the Heaviside function properly in integrands, the convolution integral is first simplified. Both the input signal $x(t)$ and the impulse response $h(t)$ are causal signals. Hence, the convolution integral degenerates to

\begin{equation}
y(t) = \int_{0}^{t} x(\tau) \cdot h(t - \tau) \; d\tau
\end{equation}

for $t \geq 0$. Note that $y(t) = 0$ for $t<0$.

```python
tau = sym.symbols('tau', real=True)

y2 = sym.integrate(sym.exp(-2*tau) * h.rhs.subs(t, t-tau), (tau, 0, t))
y2
```

Lets plot the output signal derived by evaluation of the convolution

```python
sym.plot(y2, (t,-1,10), ylabel=r'$y(t)$');
```

**Exercise**

* Compare the output signal derived by explicit solution of the ODE with the signal derived by convolution. Are both equal?
* Check if the impulse response $h(t)$ is a solution of the ODE by manual calculation. Hint $\frac{d}{dt} \epsilon(t) = \delta(t)$.
* Check the solution of the convolution integral by manual calculation including the Heaviside functions.

**Copyright**

This notebook is provided as [Open Educational Resource](https://en.wikipedia.org/wiki/Open_educational_resources). Feel free to use the notebook for your own purposes. The text is licensed under [Creative Commons Attribution 4.0](https://creativecommons.org/licenses/by/4.0/), the code of the IPython examples under the [MIT license](https://opensource.org/licenses/MIT). Please attribute the work as follows: *Sascha Spors, Continuous- and Discrete-Time Signals and Systems - Theory and Computational Examples*.