# simple basis functions fns = [lambda j, x: x, lambda j, x: x**2, lambda j, x: x**3, lambda j, x: x**4] if __name__ == '__main__': np.random.seed(7) # 100 data point N = 100 M = len(fns) s = 10 # the data to learn x = s * np.random.rand(N) w = np.random.rand() y = np.sin(x) + 0.1 * np.random.randn(N) # plot the raw data (sin + noise) pl.plot(x, y, '.r') # predict with regression x0 = np.linspace(min(x), max(x), 500) m = Regression(0.5, N, M, fns) m.fit(x, y) y0 = m.predict(x0) pl.plot(x0, y0) pl.show()
# Now set the values in the array: ys[ln] = y xs[ln] = x ln += 1 return ys, xs if __name__ == '__main__': ys, xs = read_data() N = 4000 M = 8 # from sex to shell height # simple basis functions fns = [lambda j, x: x[j]] * M # train model = Regression(5, N, M, fns) model.fit(xs[0:N], ys[0:N]) # predict predict_ys = model.predict(xs[N:]) # plot the result x0 = range(0, len(predict_ys)) pl.plot(x0, ys[N:], '-r') pl.plot(x0, predict_ys, '-') pl.show()
xs[ln] = x ln += 1 return ys, xs if __name__ == '__main__': ys, xs = read_data() N = 4000 M = 8 # from sex to shell height # simple basis functions fns = [lambda j, x : x[j]] * M # train model = Regression(5, N, M, fns) model.fit(xs[0:N], ys[0:N]) # predict predict_ys = model.predict(xs[N:]) # plot the result x0 = range(0, len(predict_ys)) pl.plot(x0, ys[N:], '-r') pl.plot(x0, predict_ys, '-') pl.show()
# simple basis functions fns = [lambda j, x : x, lambda j, x : x**2, lambda j, x: x**3, lambda j, x: x**4] if __name__ == '__main__': np.random.seed(7) # 100 data point N = 100 M = len(fns) s = 10 # the data to learn x = s * np.random.rand(N) w = np.random.rand() y = np.sin(x) + 0.1 * np.random.randn(N) # plot the raw data (sin + noise) pl.plot(x,y,'.r') # predict with regression x0 = np.linspace(min(x),max(x),500) m = Regression(0.5, N, M, fns) m.fit(x, y) y0 = m.predict(x0) pl.plot(x0, y0) pl.show()