## Develop A Learning Algorithm

```%matplotlib inline
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
file = pd.ExcelFile('dataset.xlsx')
```y = pd.read_excel(file, 'yval', header=None)
```m = len(df)def hypothesis(theta, X):
return theta + theta*Xdef cost_calc(theta, X, y):
return (1/2*m) * np.sum((hypothesis(theta, X) - y)**2)```
```def gradient_descent(theta, X, y, epoch, alpha):
cost = []
theta_hist = []
i = 0
while i < epoch:
hx = hypothesis(theta, X)
theta -= alpha*(sum(hx-y)/m)
theta -= (alpha * np.sum((hx - y) * X))/m
cost.append(cost_calc(theta, X, y))
i += 1
return theta, cost```
```def predict(theta, X, y, epoch, alpha):
theta, cost = gradient_descent(theta, X, y, epoch, alpha)
return hypothesis(theta, X), cost, theta```
```theta = [0,0]
y_predict, cost, theta = predict(theta, df, y, 1400, 0.001)```
```plt.figure()
plt.scatter(df, y)
plt.scatter(df, y_predict)```

## Draw A Learning Curve

```file = pd.ExcelFile('dataset.xlsx')
```cross_y = pd.read_excel(file, 'y', header=None)
```def grad_descent(theta, X, y, epoch, alpha):
i = 0
while i < epoch:
hx = hypothesis(theta, X)
theta -= alpha*(sum(hx-y)/m)
theta -= (alpha * np.sum((hx - y) * X))/m
i += 1
return theta```
```j_tr = []
theta_list = []
#theta = [0,0]
for i in range(0, len(df)):
theta = [0,0]
#print(theta)
j_tr.append(cost_calc(theta, df[:i], y[:i]))
theta_list```
```j_val = []
for i in theta_list:
j_val.append(cost_calc(i, cross_val, cross_y))
j_val```
```%matplotlib inline
import matplotlib.pyplot as plt
plt.figure()
plt.scatter(range(0, 21), j_tr)
plt.scatter(range(0, 21), j_val)```

## Fixing A Learning Algorithm

#machinelearning #learningcurve #datascience #artificialinteligence #dataAnalytics