From 32e73ea94270ff02d55300ff571d6123c4b59a28 Mon Sep 17 00:00:00 2001 From: mhjensen Date: Fri, 4 May 2018 12:16:36 -0400 Subject: [PATCH] adding material to intro chapter --- doc/src/How2ReadData/How2ReadData.do.txt | 109 ++++++++++++++++++++++- 1 file changed, 108 insertions(+), 1 deletion(-) diff --git a/doc/src/How2ReadData/How2ReadData.do.txt b/doc/src/How2ReadData/How2ReadData.do.txt index 6412f66f0..078f797d4 100644 --- a/doc/src/How2ReadData/How2ReadData.do.txt +++ b/doc/src/How2ReadData/How2ReadData.do.txt @@ -281,11 +281,118 @@ line = np.linspace(-3,3,1000,endpoint=False).reshape(-1,1) reg = DecisionTreeRegressor(min_samples_split=3).fit(x,y) plt.plot(line, reg.predict(line), label="decision tree") regline = LinearRegression().fit(x,y) -plt.plot(line, regline.predict(line), label= "Linear Rgression") +plt.plot(line, regline.predict(line), label= "Linear Regression") plt.show() !ec !eblock + +!split +===== Simple regression model ===== +Add info about the equations +!bc pycod +# Importing various packages +from random import random, seed +import numpy as np +import matplotlib.pyplot as plt + +x = 2*np.random.rand(100,1) +y = 4+3*x+np.random.randn(100,1) + +xb = np.c_[np.ones((100,1)), x] +theta = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y) +xnew = np.array([[0],[2]]) +xbnew = np.c_[np.ones((2,1)), xnew] +ypredict = xbnew.dot(theta) + +plt.plot(xnew, ypredict, "r-") +plt.plot(x, y ,'ro') +plt.axis([0,2.0,0, 15.0]) +plt.xlabel(r'$x$') +plt.ylabel(r'$y$') +plt.title(r'Linear Regression') +plt.show() + +!ec + + + + + + +!split +===== Simple regression model, now using scikit=learn ===== +Add info about the equations +!bc pycod +# Importing various packages +from random import random, seed +import numpy as np +import matplotlib.pyplot as plt +from sklearn.linear_model import LinearRegression + +x = 2*np.random.rand(100,1) +y = 4+3*x+np.random.randn(100,1) +linreg = LinearRegression() +linreg.fit(x,y) +xnew = np.array([[0],[2]]) +ypredict = linreg.predict(xnew) + +plt.plot(xnew, ypredict, "r-") +plt.plot(x, y ,'ro') +plt.axis([0,2.0,0, 15.0]) +plt.xlabel(r'$x$') +plt.ylabel(r'$y$') +plt.title(r'Random numbers ') +plt.show() +!ec + + +!split +===== Simple regression model using gradient descent===== +Add info about the equations, play around with different learning rates +!bc pycod +# Importing various packages +from math import exp, sqrt +from random import random, seed +import numpy as np +import matplotlib.pyplot as plt + +x = 2*np.random.rand(100,1) +y = 4+3*x+np.random.randn(100,1) + +xb = np.c_[np.ones((100,1)), x] +theta_linreg = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y) +print(theta_linreg) +theta = np.random.randn(2,1) + +eta = 0.1 +Niterations = 1000 +m = 100 + +for iter in range(Niterations): + gradients = 2.0/m*xb.T.dot(xb.dot(theta)-y) + theta -= eta*gradients + +print(theta) +xnew = np.array([[0],[2]]) +xbnew = np.c_[np.ones((2,1)), xnew] +ypredict = xbnew.dot(theta) +ypredict2 = xbnew.dot(theta_linreg) +plt.plot(xnew, ypredict, "r-") +plt.plot(xnew, ypredict2, "b-") +plt.plot(x, y ,'ro') +plt.axis([0,2.0,0, 15.0]) +plt.xlabel(r'$x$') +plt.ylabel(r'$y$') +plt.title(r'Random numbers ') +plt.show() + + +!ec + + + + !split ===== Predator-Prey model from ecology =====