def evalExp(data, coeffs): x,y = data b, loga = coeffs a = 2**loga # y = a * 2*(b**x) y_est = a * pylab.exp2(b*x) r2 = rSquare(y, y_est) return y_est, r2
def generatePlots(datasets): """ Generate plots to compare the different fit functions for the given data sets. Parameters: datasets - a list of data sets stored as (x, y) tuples, where x and y are each a list of data points (x or y values) """ xdata1, ydata1 = datasets[0] m = 2 n = 6 funcs = [ polyFit(xdata1, ydata1, 1), polyFit(xdata1, ydata1, 2), polyFit(xdata1, ydata1, 4), expFit(xdata1, ydata1) ] pylab.figure() pylab.title("Comparison of fit functions to space cow data sets") # Generate the polynomial plots for i in range(0, m*n - 3, 3): p = funcs[i/3] for j, data in enumerate(datasets): x = data[0] y = data[1] pylab.subplot(m, n, i+j+1) ey = [ f(p, xval) for xval in x ] pylab.plot(x, y, 'bo', x, ey, 'r-') pylab.title("R2 = %s" % round(rSquare(y, ey), 6)) pylab.xlabel("Time") pylab.ylabel("# of cows") # Generate the exponential plots i = 9 for j, data in enumerate(datasets): x = data[0] y = data[1] pylab.subplot(m, n, i+j+1) ## TWICE-FIXED CODE b, ln_a = funcs[3] #WRONG: expFit(x, y) a = pylab.exp2(ln_a) ey = [a * math.pow(2, b * xval) for xval in x] pylab.plot(x, y, 'bo', x, ey, 'r-') pylab.title("R2 = %s" % round(rSquare(y, ey), 6)) pylab.xlabel("Time") pylab.ylabel("# of cows") pylab.subplots_adjust(wspace = 0.75, hspace = 0.35) pylab.show()
def createPlots(data, prtData): """ This function saves 4 pylab plots, modeling: 1. Linear 2. Quadratic 3. Quartic 4. Exponential """ degree = [1, 2, 4] iterList = [1, 2, 3] x = data[0] y = data[1] #use for 3 calls to polyFit() for i in iterList: #generate unique figure figure = i + ((prtData - 1) * 4) #index for degree list index = i - 1 coef = polyFit(x, y, degree[index]) #reset pylab plotter pylab.figure(figure) estimate = pylab.polyval(coef, x) ##print rSquare(y, estimate) #plots pylab.scatter(x, y) pylab.plot(x, estimate) pylab.savefig(str(figure), format=None) #single call to expFit() pylab.figure(4 * prtData) b, a = expFit(x, y) a = pylab.exp2(a) #exponential function estimate = (a * (2**(b * x))) ##print rSquare(y, estimate) pylab.scatter(x, y) pylab.plot(x, estimate) pylab.savefig(str(4 * prtData), format=None)
def createPlots(data, prtData): """ This function saves 4 pylab plots, modeling: 1. Linear 2. Quadratic 3. Quartic 4. Exponential """ degree = [1, 2, 4] iterList = [1,2,3] x = data[0] y = data[1] #use for 3 calls to polyFit() for i in iterList: #generate unique figure figure = i + ((prtData-1) * 4) #index for degree list index = i-1 coef = polyFit(x, y, degree[index]) #reset pylab plotter pylab.figure(figure) estimate = pylab.polyval(coef, x) ##print rSquare(y, estimate) #plots pylab.scatter(x, y) pylab.plot(x, estimate) pylab.savefig(str(figure), format=None) #single call to expFit() pylab.figure(4 * prtData) b, a = expFit(x,y) a = pylab.exp2(a) #exponential function estimate = ( a * ( 2 ** (b * x) ) ) ##print rSquare(y, estimate) pylab.scatter(x, y) pylab.plot(x, estimate) pylab.savefig(str(4 * prtData), format=None)