Esempio n. 1
0
#       File Name: linregPolyVsDegree.py
#       Description:
#           Linear Regression with Polynomial Basis of different degrees
#           based on code code by Romain Thibaux
#           (Lecture 2 from http://www.cs.berkeley.edu/~asimma/294-fall06/)

from utils import preprocessor_create
from utils import poly_data_make
from SupervisedModels.linearRegression import linreg_fit
from SupervisedModels.linearRegression import linreg_fit_bayes
from SupervisedModels.linearRegression import linreg_predict
import numpy as np
import pylab as pl

N = 21
xtrain, ytrain, xtest, _, ytest, _ = poly_data_make(sampling='thibaux', n=N)

degs = np.arange(1, 22)
Nm = len(degs)

# Plot error vs degree
mseTrain = np.zeros(Nm)
mseTest = np.zeros(Nm)
for m in xrange(len(degs)):
    deg = degs[m]
    pp = preprocessor_create(rescale_X=True, poly=deg, add_ones=True)
    model = linreg_fit(xtrain, ytrain, preproc=pp)
    ypredTrain = linreg_predict(model, xtrain)
    ypredTest = linreg_predict(model, xtest)
    mseTrain[m] = np.mean(np.square(ytrain - ypredTrain))
    mseTest[m] = np.mean(np.square(ytest - ypredTest))
Esempio n. 2
0
#       File Name: linregPolyVsDegree.py
#       Description:
#           Linear Regression with Polynomial Basis of different degrees
#           based on code code by Romain Thibaux
#           (Lecture 2 from http://www.cs.berkeley.edu/~asimma/294-fall06/)

from utils import preprocessor_create
from utils import poly_data_make
from SupervisedModels.linearRegression import linreg_fit
from SupervisedModels.linearRegression import linreg_fit_bayes
from SupervisedModels.linearRegression import linreg_predict
import numpy as np
import pylab as pl

N = 21
xtrain, ytrain, xtest, _, ytest, _ = poly_data_make(sampling='thibaux', n=N)

degs = np.arange(1, 22)
Nm = len(degs)

# Plot error vs degree
mseTrain = np.zeros(Nm)
mseTest = np.zeros(Nm)
for m in xrange(len(degs)):
    deg = degs[m]
    pp = preprocessor_create(rescale_X=True, poly=deg, add_ones=True)
    model = linreg_fit(xtrain, ytrain, preproc=pp)
    ypredTrain = linreg_predict(model, xtrain)
    ypredTest = linreg_predict(model, xtest)
    mseTrain[m] = np.mean(np.square(ytrain - ypredTrain))
    mseTest[m] = np.mean(np.square(ytest - ypredTest))
Esempio n. 3
0
def contoursSSEDemo():
  N = 21
  x,y,_,_,_,_ = poly_data_make(sampling='thibaux', n=N)
  X = np.concatenate((np.ones((N,1)), x.reshape(N,1)), axis=1)

  return X,y
Esempio n. 4
0
def contoursSSEDemo():
    N = 21
    x, y, _, _, _, _ = poly_data_make(sampling='thibaux', n=N)
    X = np.concatenate((np.ones((N, 1)), x.reshape(N, 1)), axis=1)

    return X, y
Esempio n. 5
0
#       Author:    Srinivas Vasudevan
#       E-mail:    [email protected]
#
#       File Name: linregResiduals.py
#       Description:
#         Linear regression on data with residuals.
#
#       Last Modified:
#           2015-11-28

import matplotlib.pyplot as pl
import numpy as np
from utils import poly_data_make

N = 21
x, y, _, _, _, _ = poly_data_make(sampling='thibaux', n=N)
X = np.concatenate((np.ones((N,1)), x.reshape(N,1)), axis=1)  
w = np.linalg.lstsq(X, y)[0]
y_estim = np.dot(X,w)

pl.plot(X[:,1], y, 'o')
pl.plot(X[:,1], y_estim, '-')
pl.savefig('linregResidualsNoBars.png')
pl.show()

for x0, y0, y_hat in zip(X[:,1], y, y_estim):
  pl.plot([x0, x0],[y0, y_hat],'k-')
pl.plot(X[:,1], y, 'o')
pl.plot(X[:,1], y_estim, '-')

pl.savefig('linregResidualsBars.png')
Esempio n. 6
0
#       Author:    Srinivas Vasudevan
#       E-mail:    [email protected]
#
#       File Name: linregResiduals.py
#       Description:
#         Linear regression on data with residuals.
#
#       Last Modified:
#           2015-11-28

import matplotlib.pyplot as pl
import numpy as np
from utils import poly_data_make

N = 21
x, y, _, _, _, _ = poly_data_make(sampling='thibaux', n=N)
X = np.concatenate((np.ones((N, 1)), x.reshape(N, 1)), axis=1)
w = np.linalg.lstsq(X, y)[0]
y_estim = np.dot(X, w)

pl.plot(X[:, 1], y, 'o')
pl.plot(X[:, 1], y_estim, '-')
pl.savefig('linregResidualsNoBars.png')
pl.show()

for x0, y0, y_hat in zip(X[:, 1], y, y_estim):
    pl.plot([x0, x0], [y0, y_hat], 'k-')
pl.plot(X[:, 1], y, 'o')
pl.plot(X[:, 1], y_estim, '-')

pl.savefig('linregResidualsBars.png')