Example #1
0
def main():
    #read input data
    my_data = np.genfromtxt('regression_data.txt', dtype=float, delimiter=',')
     
    # create input and output numpy arrays     
    x = my_data[:,0]
    y = my_data[:,1]
     
    # create regression class object
    reg = Regression()
     
    # set learning rate
    reg.set_learning_rate(0.001)
     
    # set maximum iterations
    reg.set_max_iterations(20000)
     
    # set l1 and l2 penalty
    reg.set_l1_penalty(0.1)
    reg.set_l2_penalty(0.1)
     
    # set tolerance
    reg.set_tolerance(1e-5)
     
    # fit a polynomial regression model 
    theta, cost, it = reg.polynomial_regression(x, y, 6)
     
    print "Regression coefficients :" + str(theta)
    print "Minimum cost function: " + str(cost)
    print "Iterations taken: " + str(it)
     
    # predict values for new input
    z = np.linspace(-2, 2, 4/0.01)
    prediction = reg.predict(z)
     
    # plot
    fig = plt.figure()
    plt.plot(x,y,'.', label='Input data')
    plt.plot(z,prediction,'r-', label='Prediction')
    plt.legend(loc=4)
    fig.suptitle('Polynomial Regression Fit')
    plt.xlabel('x (input)')
    plt.ylabel('y (predicted)')
    plt.savefig('fit_values.eps')
    plt.show()
Example #2
0
def predict(xlabel,ylabel,x_val,x,y):
    meanx=np.mean(x)
    stddevx=np.std(x)
    meany=np.mean(y)
    stddevy=np.std(y)
    x=featureNormalize(x)
    y=featureNormalize(y)

    reg = Regression()
    reg.set_learning_rate(0.01)
    reg.set_max_iterations(10000)
    reg.set_l1_penalty(0.1)
    reg.set_l2_penalty(0.1)
    reg.set_tolerance(1e-5)
    theta, cost, it = reg.polynomial_regression(x, y, 5)

    z = np.linspace(-1.9, 2.1, 4/0.01)
    prediction = reg.predict(z)

    x=np.array(x)*stddevx+meanx
    z=np.array(z)*stddevx+meanx
    y=np.array(y)*stddevy+meany
    prediction=np.array(prediction)*stddevy+meany

    x_val=(x_val-meanx)/stddevx
    y_val=reg.predict([x_val])
    x_val=[x_val]
    x_val=np.array(x_val)*stddevx+meanx
    y_val=np.array(y_val)*stddevy+meany

    fig = plt.figure(figsize=(4,4))
    plt.plot(x,y,'.', label='Input data')
    plt.plot(z,prediction,'r-', label='Best fit curve')
    plt.plot(x_val,y_val,'gx',label='Predicted Data')
    plt.legend(loc=4)
    title=xlabel + " vs " + ylabel
    plt.title(title,size=10)
    plt.xticks(size=8)
    plt.yticks(size=8)
    plt.close('all')

    return([y_val,fig])
Example #3
0
def predict2(x_val,x,y):
    meanx=np.mean(x)
    stddevx=np.std(x)
    meany=np.mean(y)
    stddevy=np.std(y)
    x=featureNormalize(x)
    y=featureNormalize(y)

    reg = Regression()
    reg.set_learning_rate(0.001)
    reg.set_max_iterations(10000)
    reg.set_l1_penalty(0.1)
    reg.set_l2_penalty(0.1)
    reg.set_tolerance(1e-5)
    theta, cost, it = reg.polynomial_regression(x, y, 5)

    x_val=(x_val-meanx)/stddevx
    y_val=reg.predict([x_val])
    y_val=np.array(y_val)*stddevy+meany

    return(y_val)