def plotUnivariate(X, y, d, reg_lambda): model = PolynomialRegression(degree=d, reg_lambda=reg_lambda) model.fit(X, y) # output predictions xpoints = np.linspace(np.max(X), np.min(X), 100).reshape(-1, 1) ypoints = model.predict(xpoints) # plot curve plt.plot(X, y, 'rx') plt.title('PolyRegression with d = ' + str(d) + ', lambda = ' + str(reg_lambda)) plt.plot(xpoints, ypoints, 'b-') plt.xlabel('X') plt.ylabel('Y')
reg_lambdas = list(range(0, 50, 8)) reg_lambdas.extend([100, 1000, 10000]) # load the data, accidentaly override a python builtin keyword filePath = "data/polydata.dat" file = open(filePath, 'r') allData = np.loadtxt(file, delimiter=',') # adopt a horrible naming convention X = allData[:, [0]] y = allData[:, [1]] # fit with different regression parameters and store results xAxisData, yAxisData = [], [] for reg_lambda in reg_lambdas: model = PolynomialRegression(degree=d, reg_lambda=reg_lambda) model.fit(X, y) # output predictions xpoints = np.linspace(np.max(X), np.min(X), 100).reshape(-1, 1) ypoints = model.predict(xpoints) xAxisData.append(xpoints) yAxisData.append(ypoints) # overplot fit results colors = cm.viridis(np.linspace(0, 1, len(yAxisData))) fig, ax = plt.subplots() # points ax.plot(X, y, 'rx') for x, y, color, label in zip(xAxisData, yAxisData, colors, reg_lambdas):
if __name__ == "__main__": ''' Main function to test polynomial regression ''' # load the data filePath = "data/polydata.dat" file = open(filePath,'r') allData = np.loadtxt(file, delimiter=',') X = allData[:, 0] y = allData[:, 1] # regression with degree = d d = 8 model = PolynomialRegression(degree = d, regLambda = 0) model.fit(X, y) # output predictions xpoints = np.linspace(np.max(X), np.min(X), 100).T ypoints = model.predict(xpoints) # plot curve plt.figure() plt.plot(X, y, 'rx') plt.title('PolyRegression with d = '+str(d)) plt.hold(True) plt.plot(xpoints, ypoints, 'b-') plt.xlabel('X') plt.ylabel('Y') plt.show()