How To Develop A learning Curve To Improve A Machine Learning Algorithm

How Learning Curve Works

Image for post

Image for post

Image for post

Develop A Learning Algorithm

%matplotlib inline
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
file = pd.ExcelFile('dataset.xlsx')
df = pd.read_excel(file, 'Xval', header=None)
df.head()

Image for post

y = pd.read_excel(file, 'yval', header=None)
y.head()

Image for post

m = len(df)def hypothesis(theta, X):
    return theta[0] + theta[1]*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[0] -= alpha*(sum(hx-y)/m)
        theta[1] -= (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[0], y[0], 1400, 0.001)
plt.figure()
plt.scatter(df, y)
plt.scatter(df, y_predict)

Image for post




Draw A Learning Curve

file = pd.ExcelFile('dataset.xlsx')
cross_val = pd.read_excel(file, 'X', header=None)
cross_val.head()

Image for post

cross_y = pd.read_excel(file, 'y', header=None)
cross_y.head()

Image for post

def grad_descent(theta, X, y, epoch, alpha):
    i = 0
    while i < epoch:
        hx = hypothesis(theta, X)
        theta[0] -= alpha*(sum(hx-y)/m)
        theta[1] -= (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]
    theta_list.append(grad_descent(theta, df[0][:i], y[0][:i], 1400, 0.001))
    #print(theta)
    j_tr.append(cost_calc(theta, df[0][:i], y[0][:i]))
theta_list

Image for post

Image for post

j_val = []
for i in theta_list:  
    j_val.append(cost_calc(i, cross_val[0], cross_y[0]))
j_val

Image for post

%matplotlib inline
import matplotlib.pyplot as plt
plt.figure()
plt.scatter(range(0, 21), j_tr)
plt.scatter(range(0, 21), j_val)

Image for post

Drawing Decision From Learning Curve

Image for post

Image for post

Fixing A Learning Algorithm

 

 

#machinelearning #learningcurve #datascience #artificialinteligence #dataAnalytics

Leave a Reply

Close Menu