def preprocessor_apply_to_train(preproc, X): """ Apply Preprocessor to training data and memorize parameters preproc is initially a struct with the following fields [default] standardize_X - if True, makes columns of X zero mean and unit var. [True] rescale_X - if True, scale columns of X to lie in [-1, +1] [False] kernel_fn - if not None, apply kernel fn to X default [None] The returned preproc object has several more fields added to it, which are used by preprocessor_apply_to_test """ # Set defaults try: preproc.standardize_X except AttributeError: preproc.standardize_X = True try: preproc.rescale_X except AttributeError: preproc.rescale_X = False try: preproc.kernel_fn except AttributeError: preproc.kernel_fn = None try: preproc.poly except AttributeError: preproc.poly = None try: preproc.add_ones except AttributeError: preproc.add_ones = None if preproc.standardize_X: X, preproc.Xmu = util.center_cols(X) X, preproc.Xstnd = util.mk_unit_variance(X) if preproc.rescale_X: try: preproc.Xscale except AttributeError: preproc.Xscale = [-1, 1] X = util.rescale_data(X, preproc.Xscale[0], preproc.Xscale[1]) if preproc.kernel_fn is not None: preproc.basis = X X = preproc.kernel_fn(X, preproc.basis) if preproc.poly is not None: assert preproc.poly > 0, 'polynomial degree must be greater than 0' X = util.degexpand(X, preproc.poly, False) if preproc.add_ones: X = util.add_ones(X) return preproc, X
def preprocessor_apply_to_test(preproc, X): """Transform the test data in the same way as the training data""" try: X = util.center_cols(X, preproc.Xmu) except AttributeError: pass try: X = util.mk_unit_variance(X, preproc.Xstnd) except AttributeError: pass try: X = util.rescale_data(X, preproc.Xscale[0], preproc.Xscale[1]) except AttributeError: pass try: if preproc.kernel_fn is not None: X = preproc.kernel_fn(X, preproc.basis) except AttributeError: pass try: if preproc.poly is not None: X = util.degexpand(X, preproc.poly, False) except AttributeError: pass try: if preproc.add_ones: X = util.add_ones(X) except AttributeError: pass return X
def contoursSSEDemo(): N = 21 x,y,_,_,_,_ = util.poly_data_make(sampling='thibaux', n=N) X = util.add_ones(x) return X,y
#!/usr/bin/env python # Fit linear and quadratic surfaces to data # Based on code by Romain Thibaux <*****@*****.**> import matplotlib.pyplot as pl import numpy as np import utils.util as util from mpl_toolkits.mplot3d import Axes3D data = util.load_mat('moteData/moteData.mat') X = data['X'] y = data['y'] X_pad = util.add_ones(X) for use_quad in (False, True): phi = X_pad if use_quad: phi = np.column_stack((X_pad, X**2)) fig = pl.figure() ax = Axes3D(fig) ax.set_zlim(15, 19) ax.scatter(X[:, 0], X[:, 1], y) xrange = np.linspace(min(X[:, 0]), max(X[:, 0]), 10) yrange = np.linspace(min(X[:, 1]), max(X[:, 1]), 10) xx, yy = np.meshgrid(xrange, yrange) flatxx = xx.reshape((100, 1))
import matplotlib.pyplot as pl import numpy as np import utils.util as util from mpl_toolkits.mplot3d import Axes3D from scipy.optimize import minimize np.random.seed(0) #Generating synthetic data: N = 21 wTrue = np.array([1.45, 0.92]) X = util.add_ones(np.random.uniform(-2, 2, N)) y = wTrue[0] * X[:, 0] + wTrue[1] * X[:, 1] + np.random.normal(0, .1, N) # #Uncomment if we wish to use data generated from poly_data_make # x, y,_ ,_ ,_ ,_ = util.poly_data_make(sampling='thibaux', n=N) # X = util.add_ones(x) #Plot SSE surface over parameter space. v = np.arange(-1, 3, .1) W0, W1 = np.meshgrid(v, v) SS = np.array([ sum((w0 * X[:, 0] + w1 * X[:, 1] - y)**2) for w0, w1 in zip(np.ravel(W0), np.ravel(W1)) ]) SS = SS.reshape(W0.shape) fig = pl.figure() ax = fig.add_subplot(111, projection='3d') surf = ax.plot_surface(W0, W1, SS) pl.savefig('figures/linregSurfSSEPy.pdf') pl.draw()
#!/usr/bin/env python # Fit linear and quadratic surfaces to data # Based on code by Romain Thibaux <*****@*****.**> import matplotlib.pyplot as pl import numpy as np import utils.util as util from mpl_toolkits.mplot3d import Axes3D data = util.load_mat('moteData/moteData.mat') X = data['X'] y = data['y'] X_pad = util.add_ones(X) for use_quad in (False, True): phi = X_pad if use_quad: phi = np.column_stack((X_pad, X**2)) fig = pl.figure() ax = Axes3D(fig) ax.set_zlim(15, 19) ax.scatter(X[:,0], X[:,1], y) xrange = np.linspace(min(X[:,0]), max(X[:,0]), 10) yrange = np.linspace(min(X[:,1]), max(X[:,1]), 10) xx, yy = np.meshgrid(xrange, yrange) flatxx = xx.reshape((100, 1))