Пример #1
0
def main():
    # Get input data
    (countries, features, values) = a1.load_unicef_data()

    targets = values[:, 1]
    x = values[:, 10]
    mue = 100
    subplot = 211
    for mue in [100, 10000]:
        plt.figure(1)
        plt.subplot(subplot)
        PlotMatrixRMSErorr, PlotMatrixRMSErorrTest, x_train, t_train, ourTrainValidation, x_test, t_test, wml = calculateToPlot(
            x, targets, mue)
        print PlotMatrixRMSErorr, PlotMatrixRMSErorrTest
        t1 = np.arange(min(x_test[:, 0]), max(x_test[:, 0]), 0.1)
        plt.plot(t1, calCurve(t1, wml, mue), color="Green")
        plt.plot(x_train, t_train, 'ro', color="Blue")
        #plt.plot(x_train, ourTrainValidation, 'k', color="Red")
        plt.plot(x_test, t_test, 'ro', color="Yellow")
        plt.legend(['Learned polynomial', 'Train point', 'Test Point'],
                   bbox_to_anchor=(1.00, 0.73),
                   loc=1,
                   borderaxespad=0)

        subplot = subplot + 1
        #PlotMatrixRMSErorr, PlotMatrixRMSErorrTest = calculateToPlot(x, targets)
        # Set EW
        # Ew = t_train - (0.5*np.square(PhiTrain))*w
    plt.show()
Пример #2
0
def main():
    # Get input data
    (countries, features, values) = a1.load_unicef_data()

    targets = values[:, 1]

    x = values[:, 7:]
    x = a1.normalize_data(x)
    x = x[0:100, :]
    targets = targets[0:100, :]
    # normalize data
    #
    minAvgRMS = 999999999
    landas = [0, 0.01, 0.1, 1, 10, 100, 1000, 10000]
    avgErrorForEachL = []
    for landa in landas:
        start = 0
        step = 10
        avgErrorRMS = 0
        for i in range(0, 10):
            x_train, t_train, x_validation, t_validation = setXes(
                i * 10, x, targets)
            PlotMatrixRMSErorr = crossValidationCal(x_train, t_train, landa,
                                                    x_validation, t_validation)
            avgErrorRMS += PlotMatrixRMSErorr[0, 0]
        avgErrorRMS = avgErrorRMS / 10
        print avgErrorRMS, landa
        avgErrorForEachL.append(avgErrorRMS)
    plt.semilogx(landas, avgErrorForEachL)

    plt.show()
def main():
    # Get input data
    (countries, features, values) = a1.load_unicef_data()

    targets = values[:, 1]
    #x = values[:, 7:]

    # normalize data
    # x = a1.normalize_data(x)

    # set param value
    PlotMatrixRMSErorr = []
    PlotMatrixRMSErorrTest = []
    for i in range(7, 15):
        x = values[:, i]
        rmsErrorTrain, rmsErorrTest = calculateToPlotPolynomial3(x, targets)
        PlotMatrixRMSErorr.append(rmsErrorTrain[0, 0])
        PlotMatrixRMSErorrTest.append(rmsErorrTest[0, 0])

    # Set EW
    # Ew = t_train - (0.5*np.square(PhiTrain))*w
    plotMatrixDegree = np.matrix([[1], [2], [3], [4], [5], [6], [7], [8]])
    labels = features[7:15]
    plt.xticks([1, 2, 3, 4, 5, 6, 7, 8], labels, rotation='vertical')
    plt.figure(1)
    plt.subplot(111)
    plt.tight_layout()
    #plt.plot(plotMatrixDegree, PlotMatrixRMSErorr)
    plt.bar(plotMatrixDegree - 0.2, PlotMatrixRMSErorr, width=0.1, color="red")
    plt.ylabel('RMS')
    plt.legend(['Training error', 'Test error'])
    plt.title('Feature 8 to 16')
    #plt.xlabel('Polynomial degree')

    plt.subplot(111)
    # plt.plot(plotMatrixDegree, PlotMatrixRMSErorr)
    plt.bar(plotMatrixDegree, PlotMatrixRMSErorrTest, width=0.1, color="Blue")
    plt.ylabel('RMS')
    plt.legend(['Training error', 'Test error'])
    plt.title('Feature 8 to 16')
    #plt.xlabel('Polynomial degree')

    plt.figure(2)
    for i in range(0, 3):
        plt.subplot(311 + i)
        x_train, t_train, ourTrainValidation, x_test, t_test, wml = getFitPolynomial3(
            values[:, 10 + i], targets)

        t1 = np.arange(min(x_test[:, 0]), max(x_test[:, 0]), 0.1)
        plt.plot(t1, calCurve(t1, wml), color="Green")
        plt.plot(x_train, t_train, 'ro', color="Blue")
        #plt.plot(x_train, ourTrainValidation, 'k', color="Red")
        plt.plot(x_test, t_test, 'ro', color="Yellow")
        plt.legend(['Learned polynomial', 'Train point', 'Test Point'],
                   bbox_to_anchor=(1.00, 0.73),
                   loc=1,
                   borderaxespad=0)

    plt.show()
Пример #4
0
def PolynomialRegression(bias):
    (countries, features, values) = a1.load_unicef_data()
    targets = values[:, 1]
    x = values[:, 7:]
    x = a1.normalize_data(x)
    N_TRAIN = 100
    ALL = 195
    x_train = x[0:N_TRAIN, :]
    x_test = x[N_TRAIN:, :]
    t_train = targets[0:N_TRAIN]
    t_test = targets[N_TRAIN:]
    train_error = {}
    test_error = {}
    for degrees in range(1, 7):
        (w, t_err) = a1.linear_regression(x_train, t_train, 'polynomial', 0,
                                          degrees, 0, 1, N_TRAIN, bias)
        (t_est, te_err) = a1.evaluate_regression('polynomial', x_test, w,
                                                 t_test, degrees,
                                                 ALL - N_TRAIN, bias)
        print('degree = ', degrees)
        print(t_err)
        train_error[degrees] = np.sqrt(np.sum(t_err) / 100)
        print('sum=', np.sum(t_est, axis=0))
        print('train_error = ', train_error[degrees])
        test_error[degrees] = np.sqrt(np.sum(te_err) / 95)

    for i in range(1, 7):
        print(train_error[i])
    # for i in range (1,7):
    #     print(test_error[i])
    print(type(train_error))
    plt.rcParams.update({'font.size': 15})

    plt.plot([1, 2, 3, 4, 5, 6], [
        train_error[1], train_error[2], train_error[3], train_error[4],
        train_error[5], train_error[6]
    ])
    plt.plot([1, 2, 3, 4, 5, 6], [
        test_error[1], test_error[2], test_error[3], test_error[4],
        test_error[5], test_error[6]
    ])
    plt.ylabel('RMS')
    plt.legend(['Training error', 'Testing error'])
    plt.title('Fit with polynomials, no regularization, bias:' + bias)
    plt.xlabel('Polynomial degree')
    plt.show()
def main():
    # Get input data
    (countries, features, values) = a1.load_unicef_data()

    targets = values[:, 1]
    x = values[:, 7:]

    # normalize data
    # x = a1.normalize_data(x)

    # set param value
    PlotMatrixRMSErorr, PlotMatrixRMSErorrTest = calculateToPlot(x, targets)
    # Set EW
    # Ew = t_train - (0.5*np.square(PhiTrain))*w
    plotMatrixDegree = np.matrix([[1], [2], [3], [4], [5], [6]])
    plt.figure(1)
    plt.subplot(211)
    plt.plot(plotMatrixDegree, PlotMatrixRMSErorr)
    plt.ylabel('RMS')
    plt.legend(['Test error', 'Training error'])
    plt.title('Fit with polynomials, no regularization')
    plt.xlabel('Polynomial degree')
    plt.subplot(211)
    plt.plot(plotMatrixDegree, PlotMatrixRMSErorrTest)
    plt.ylabel('RMS')
    plt.legend(['Test error', 'Training error'])
    plt.title('Fit with polynomials, no regularization')
    plt.xlabel('Polynomial degree')

    x = a1.normalize_data(x)
    PlotMatrixRMSErorr, PlotMatrixRMSErorrTest = calculateToPlot(x, targets)

    plt.subplot(212)
    plt.plot(plotMatrixDegree, PlotMatrixRMSErorr)
    plt.ylabel('RMS')
    plt.legend(['Test error', 'Training error'])
    plt.title('Fit with polynomials, no regularization and normalize')
    plt.xlabel('Polynomial degree')
    plt.subplot(212)
    plt.plot(plotMatrixDegree, PlotMatrixRMSErorrTest)
    plt.ylabel('RMS')
    plt.legend(['Test error', 'Training error'])
    plt.xlabel('Polynomial degree')

    plt.show()
def PolynomialRegression(bias,featureNum):
    (countries, features, values) = a1.load_unicef_data()
    targets = values[:,1]
    x = values[:,7:]
    # x = a1.normalize_data(x)
    N_TRAIN = 100
    ALL = 195
    x_train = x[0:N_TRAIN,:]
    x_test = x[N_TRAIN:,:]
    t_train = targets[0:N_TRAIN]
    t_test = targets[N_TRAIN:]
    train_error = {}
    test_error = {}
    x_trainFeature = x_train[:,featureNum]
    x_testFeature = x_test[:,featureNum]

    (w, t_err) = a1.linear_regression(x_trainFeature, t_train, 'polynomial', 0, 3,0 ,1 ,N_TRAIN,bias)
    (t_est, te_err) = a1.evaluate_regression('polynomial',x_testFeature, w, t_test, 3, ALL-N_TRAIN, bias)
    train_error =  np.sqrt(np.sum(t_err)/100)
    test_error = np.sqrt(np.sum(te_err)/95)

    fig, (ax1, ax2) = plt.subplots(2)
    fig.suptitle('Visulization of feature '+ str(featureNum+8) )
    NumOfPoints = 500
    x_ev1 = np.linspace(np.asscalar(min(x_trainFeature)), np.asscalar(max(x_trainFeature)), num=NumOfPoints)
    x_ev1 = np.array(x_ev1).reshape(NumOfPoints,1)
    phi1 = a1.design_matrix('polynomial', bias, x_ev1, NumOfPoints, 3, 0, 0 )
    y1 = phi1.dot(w)

    x_ev2 = np.linspace(np.asscalar(min(min(x_trainFeature),min(x_testFeature))), np.asscalar(max(max(x_trainFeature) ,max(x_testFeature) )), num=NumOfPoints)
    x_ev2 = np.array(x_ev2).reshape(NumOfPoints,1)
    phi2 = a1.design_matrix('polynomial', bias, x_ev2, NumOfPoints, 3, 0, 0 )
    y2 = phi2.dot(w)

    ax1.plot(x_ev1,y1,'r.-')
    ax1.plot(x_trainFeature,t_train,'bo', color='b')
    ax1.plot(x_testFeature,t_test,'bo',color='g')
    ax2.plot(x_ev2,y2,'r.-')
    ax2.plot(x_trainFeature,t_train,'bo', color='b')
    ax2.plot(x_testFeature,t_test,'bo',color='g')


    
    plt.show()
def PolynomialRegression(bias):
    (countries, features, values) = a1.load_unicef_data()
    targets = values[:,1]
    x = values[:,7:]
    # x = a1.normalize_data(x)
    N_TRAIN = 100
    ALL = 195
    x_train = x[0:N_TRAIN,:]
    x_test = x[N_TRAIN:,:]
    t_train = targets[0:N_TRAIN]
    t_test = targets[N_TRAIN:]
    train_error = {}
    test_error = {}

    print(x_train)
    for featureNum in range(0,8):
        print('__________',featureNum,'___________________')
        print(x_train[:,featureNum])
    for featureNum in range(0,8):
        x_trainFeature = x_train[:,featureNum]
        x_testFeature = x_test[:,featureNum]
        (w, t_err) = a1.linear_regression(x_trainFeature, t_train, 'polynomial', 0, 3,0 ,1 ,N_TRAIN,bias)
        (t_est, te_err) = a1.evaluate_regression('polynomial',x_testFeature, w, t_test, 3, ALL-N_TRAIN, bias)
        print('featureNum = ',featureNum)
        print(t_err)
        train_error[featureNum] =   np.sqrt(np.sum(t_err)/100)
        print('sum=', np.sum(t_est,axis=0))
        print('train_error = ',  train_error[featureNum])
        test_error[featureNum] = np.sqrt(np.sum(te_err)/95)
        print('train_error = ',  test_error[featureNum])
        print('____________________________')
    x=[8,9,10,11,12,13,14,15]
    x1=[8.35,9.35,10.35,11.35,12.35,13.35,14.35,15.35]
    y_train = [train_error[0],train_error[1],train_error[2],train_error[3],train_error[4],train_error[5],train_error[6],train_error[7]]
    y_test =  [test_error[0],test_error[1],test_error[2],test_error[3],test_error[4],test_error[5],test_error[6],test_error[7]]
    width = 0.35
    fig, ax = plt.subplots()
    ax.bar(x,y_train,width)
    ax.bar(x1,y_test,0.35)
    plt.ylabel('RMS')
    plt.legend(['Training error','Testing error'])
    plt.title('Fit with polynomials, no regularization, bias:'+bias)
    plt.xlabel('Polynomial degree')
    plt.show()
#!/usr/bin/env python

import assignment1 as a1
import numpy as np
import matplotlib.pyplot as plt

(countries, features, values) = a1.load_unicef_data()
bias_arr = [True, False]
featureVal = [10, 11, 12]

for bias in bias_arr:
    weight = []
    train_err = []
    test_err = []

    for i in range(7, 15):
        targets = values[:, 1]
        x = values[:, i]

        N_TRAIN = 100
        x_train = x[0:N_TRAIN, :]
        x_test = x[N_TRAIN:, :]
        t_train = targets[0:N_TRAIN]
        t_test = targets[N_TRAIN:]

        w, t_err = a1.linear_regression(x_train, t_train, 'polynomial', bias,
                                        0, 3, 1)
        pred, te_err = a1.evaluate_regression(x_test, t_test, 'polynomial',
                                              bias, w, 3)
        weight.append(w)
        train_err.append(t_err)
Пример #9
0
#!/usr/bin/env python

import assignment1 as a1
import numpy as np
import matplotlib.pyplot as plt


(countries, features, values) = a1.load_unicef_data()

targets = values[:,1]
x = values[:,7:]
#x = a1.normalize_data(x)

N_TRAIN = 100;
# Select a single feature.
x_train = x[0:N_TRAIN,10]
t_train = targets[0:N_TRAIN]


# Plot a curve showing learned function.
# Use linspace to get a set of samples on which to evaluate
x_ev = np.linspace(np.asscalar(min(x_train)), np.asscalar(max(x_train)), num=500)

# TO DO:: Put your regression estimate here in place of x_ev.
# Evaluate regression on the linspace samples.
y_ev = np.random.random_sample(x_ev.shape)
y_ev = 100*np.sin(x_ev)


plt.plot(x_ev,y_ev,'r.-')
plt.plot(x_train,t_train,'bo')