added random walk example
This commit is contained in:
@@ -3,7 +3,7 @@ AUTHOR: Morten Hjorth-Jensen {copyright, 1999-present|CC BY-NC} at Department of
|
||||
DATE: today
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Introduction =====
|
||||
|
||||
Our emphasis throughout this series of lectures
|
||||
@@ -44,7 +44,7 @@ get started with programming.
|
||||
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Software and needed installations =====
|
||||
|
||||
We will make extensive use of Python as programming language and its
|
||||
@@ -79,7 +79,7 @@ o sudo apt-get install python3 (or python for pyhton2.7)
|
||||
|
||||
etc etc.
|
||||
|
||||
!split
|
||||
|
||||
===== Python installers =====
|
||||
|
||||
If you don't want to perform these operations separately and venture
|
||||
@@ -103,7 +103,7 @@ analysis environment, available for free and under a commercial
|
||||
license.
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Installing R, C++, cython or Julia =====
|
||||
|
||||
You will also find it convenient to utilize R. Although we will mainly
|
||||
@@ -122,7 +122,7 @@ To install _R_ with Jupyter notebook
|
||||
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Installing R, C++, cython, Numba etc =====
|
||||
|
||||
|
||||
@@ -153,7 +153,7 @@ Finally, if you wish to use the light mark-up language
|
||||
"doconce":"https://github.com/hplgit/doconce" you can convert a standard ascii text file into various HTML
|
||||
formats, ipython notebooks, latex files, pdf files etc with minimal edits.
|
||||
|
||||
!split
|
||||
|
||||
===== Simple linear regression model using _scikit-learn_ =====
|
||||
|
||||
We start with perhaps our simplest possible example, using _scikit-learn_ to perform linear regression analysis on a data set produced by us.
|
||||
@@ -430,7 +430,8 @@ print (error(y))
|
||||
!ec
|
||||
|
||||
Similarly, using _R_, we can perform similar studies. The following _R_ code illustrates this.
|
||||
!split
|
||||
|
||||
|
||||
===== Non-Linear Least squares in R =====
|
||||
!bblock
|
||||
!bc r
|
||||
@@ -473,7 +474,7 @@ display(data_pandas)
|
||||
!ec
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Examples =====
|
||||
|
||||
We present here several examples, with pertinent Python codes that we
|
||||
@@ -1026,3 +1027,96 @@ plt.show()
|
||||
|
||||
|
||||
|
||||
|
||||
=== Random walk model ===
|
||||
!bc pycod
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.preprocessing import PolynomialFeatures
|
||||
from sklearn.linear_model import LinearRegression
|
||||
|
||||
steps=250
|
||||
|
||||
distance=0
|
||||
x=0
|
||||
distance_list=[]
|
||||
steps_list=[]
|
||||
while x<steps:
|
||||
distance+=np.random.randint(-1,2)
|
||||
distance_list.append(distance)
|
||||
x+=1
|
||||
steps_list.append(x)
|
||||
plt.plot(steps_list,distance_list, color='green', label="Random Walk Data")
|
||||
|
||||
steps_list=np.asarray(steps_list)
|
||||
distance_list=np.asarray(distance_list)
|
||||
|
||||
X=steps_list[:,np.newaxis]
|
||||
|
||||
#Polynomial fits
|
||||
|
||||
#Degree 2
|
||||
poly_features=PolynomialFeatures(degree=2, include_bias=False)
|
||||
X_poly=poly_features.fit_transform(X)
|
||||
|
||||
lin_reg=LinearRegression()
|
||||
poly_fit=lin_reg.fit(X_poly,distance_list)
|
||||
b=lin_reg.coef_
|
||||
c=lin_reg.intercept_
|
||||
print ("2nd degree coefficients:")
|
||||
print ("zero power: ",c)
|
||||
print ("first power: ", b[0])
|
||||
print ("second power: ",b[1])
|
||||
|
||||
z = np.arange(0, steps, .01)
|
||||
z_mod=b[1]*z**2+b[0]*z+c
|
||||
|
||||
fit_mod=b[1]*X**2+b[0]*X+c
|
||||
plt.plot(z, z_mod, color='r', label="2nd Degree Fit")
|
||||
plt.title("Polynomial Regression")
|
||||
|
||||
plt.xlabel("Steps")
|
||||
plt.ylabel("Distance")
|
||||
|
||||
#Degree 10
|
||||
poly_features10=PolynomialFeatures(degree=10, include_bias=False)
|
||||
X_poly10=poly_features10.fit_transform(X)
|
||||
|
||||
poly_fit10=lin_reg.fit(X_poly10,distance_list)
|
||||
|
||||
y_plot=poly_fit10.predict(X_poly10)
|
||||
plt.plot(X, y_plot, color='black', label="10th Degree Fit")
|
||||
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
|
||||
#Decision Tree Regression
|
||||
from sklearn.tree import DecisionTreeRegressor
|
||||
regr_1=DecisionTreeRegressor(max_depth=2)
|
||||
regr_2=DecisionTreeRegressor(max_depth=5)
|
||||
regr_3=DecisionTreeRegressor(max_depth=7)
|
||||
regr_1.fit(X, distance_list)
|
||||
regr_2.fit(X, distance_list)
|
||||
regr_3.fit(X, distance_list)
|
||||
|
||||
X_test = np.arange(0.0, steps, 0.01)[:, np.newaxis]
|
||||
y_1 = regr_1.predict(X_test)
|
||||
y_2 = regr_2.predict(X_test)
|
||||
y_3=regr_3.predict(X_test)
|
||||
|
||||
# Plot the results
|
||||
plt.figure()
|
||||
plt.scatter(X, distance_list, s=2.5, c="black", label="data")
|
||||
plt.plot(X_test, y_1, color="red",
|
||||
label="max_depth=2", linewidth=2)
|
||||
plt.plot(X_test, y_2, color="green", label="max_depth=5", linewidth=2)
|
||||
plt.plot(X_test, y_3, color="m", label="max_depth=7", linewidth=2)
|
||||
|
||||
plt.xlabel("Data")
|
||||
plt.ylabel("Darget")
|
||||
plt.title("Decision Tree Regression")
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
!ec
|
||||
|
||||
Reference in New Issue
Block a user