```python
%matplotlib inline
import numpy as np
from matplotlib import pyplot as plt
import pandas as pd
import os
import data_helper as dh
```


```python
def computeCost(X, y, theta,m):
    costs = np.power(np.matmul(X,theta) - y,2)  
    return np.sum(costs) / (2*m)


def gradient(X, y, theta,alpha, iterations):
    m = len(X)
    cost = np.zeros(iterations)
    for i in range(0, iterations):
        h = np.matmul(X, theta) - y
        sm = np.sum(np.multiply(h, X), axis=0)
        z = np.multiply(sm, alpha*1/m)
        theta = (theta.T - z).T
        cost[i] = computeCost(X, y, theta, m)
    return [cost, theta]  
             
def plotData(x, y,h, xlabel, ylabel, xlim, ylim):
    plt.subplots()
    plt.plot(X, h)
    plt.scatter(x, y, c='red')
    plt.xlabel(xlabel)
    plt.ylabel(ylabel) 
    plt.xlim(xlim)
    plt.ylim(ylim)
    plt.grid(color='gray', linestyle='--', linewidth=.6, axis='both', which='both', alpha=.4)
    plt.show()
```


```python
data = np.genfromtxt('../data/food-cart.csv', delimiter=',')
m = len(data)
X = np.hstack([np.ones([m, 1]), data[:,0].reshape(m,1)])
y = data[:,1].reshape(m,1)
iterations = 1500;
alpha = 0.01;
initial_theta = np.array([[0],[0]])

[costs, thetas] = gradient(X, y, initial_theta, alpha, iterations)
thetas
```




    array([[-3.63029144],
           [ 1.16636235]])






```python
predictions = np.matmul(X, thetas)
plotData(X[:, 1], y, predictions, 'City Population (10K)',
         'Profits ($10K)', [4, 24], [-5, 25])
```

\begin{align}
(a+b)^2 = a^2+2ab+b^2
\end{align}
\begin{equation*}
\left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right)
\end{equation*}

\begin{equation*}
minimize \theta_{0},\theta_{1} \frac{1}{2m} \sum \limits _{i=0} ^{m} \left(h{_\theta}\left(x^\left(i\right)\right) - y^\left(i\right) \right)^2
\end{equation*}

6bc11e6f778093c3e421dcaaf16f093ae1c03443
ipynb
Jupyter Notebook
notebooks/03-food-cart-lr.ipynb
vickykatoch/py-ml-scratchpad
e4de515b657971f7889d73e09a1308f91856c09f
MIT
notebooks/03-food-cart-lr.ipynb
vickykatoch/py-ml-scratchpad
e4de515b657971f7889d73e09a1308f91856c09f
MIT
notebooks/03-food-cart-lr.ipynb
vickykatoch/py-ml-scratchpad
e4de515b657971f7889d73e09a1308f91856c09f
MIT
Qwen/Qwen-72B
1. YES
2. YES