# Vanilla Gradient Descent

This notebook applies the most basic gradient descent to predict `Happiness.Score` from `Economy..GDP.per.Capita.`. The dataset used is the World Happiness Report


```python
import numpy as np
import pandas as pd

import matplotlib.pyplot as plt
import seaborn as sns

sns.set_style("darkgrid")
%matplotlib inline
```

# Import Data


```python
df = pd.read_csv('data/2017.csv')
```


```python
economy = df['Economy..GDP.per.Capita.'].values
happiness = df['Happiness.Score'].values
```


```python
plt.scatter(economy, happiness)
```

# Gradient Descent

**Vanilla Gradient Descent** is used.

```python
update = learning_rate * gradient_of_parameters
parameters = parameters - update
```

\begin{align}
y' &=& mx + c \\
error &=& \frac{1}{N} \sum_{}(y - y') ^ 2 \\
\frac{\partial error}{\partial m} &=& -\frac{2}{N} * (y - y') * x \\
\frac{\partial error}{\partial c} &=& -\frac{2}{N} * (y - y') \\
\end{align}


```python
learning_rate = 0.0001
epochs = 200

n = len(happiness)

m = 0
c = 0

# a list of mean squared errors through the epochs
errors = []

for epoch in range(epochs):
    
    error = 0
    
    for x, y in zip(economy, happiness):
        
        # mean squared error
        error += (y - (m * x + c)) ** 2
        
        # gradient of parameters
        m_delta = -n * 0.5 * (y - (m * x + c)) * x
        c_delta = -n * 0.5 * (y - (m * x + c))
        
        m_update = learning_rate * m_delta
        c_update = learning_rate * c_delta
        
        m -= m_update
        c -= c_update
        
    errors.append(error)
        
        
plt.plot(errors)
```

As expected, the mean squared error reduces with epochs. After a few epochs, it is near-constant.

# Plotting the line of best fit


```python
limit = int(round(np.max(happiness)))


plt.plot([m * x + c for x in range(3)])
plt.scatter(economy, happiness)
```

# Result

A line of best fit is obtained that describes the correlation between `Happiness.Score` and `Economy..GDP.per.Capita.`.

a5aa2f9b9f9eb9fe078524e0f949554d7f7233bc
ipynb
Jupyter Notebook
GradientDescent.ipynb
prajwaldp/simple-gradient-descent
b88bfc4f1af98396f89baa91bd316eb918e6c7b7
MIT
GradientDescent.ipynb
prajwaldp/simple-gradient-descent
b88bfc4f1af98396f89baa91bd316eb918e6c7b7
MIT
GradientDescent.ipynb
prajwaldp/simple-gradient-descent
b88bfc4f1af98396f89baa91bd316eb918e6c7b7
MIT
Qwen/Qwen-72B
1. YES
2. YES