added random walk example
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Introduction\n",
|
||||
"\n",
|
||||
"Our emphasis throughout this series of lectures \n",
|
||||
@@ -57,6 +58,7 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Software and needed installations\n",
|
||||
"\n",
|
||||
"We will make extensive use of Python as programming language and its\n",
|
||||
@@ -91,6 +93,7 @@
|
||||
"\n",
|
||||
"etc etc. \n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Python installers\n",
|
||||
"\n",
|
||||
"If you don't want to perform these operations separately and venture\n",
|
||||
@@ -114,6 +117,7 @@
|
||||
"license.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Installing R, C++, cython or Julia\n",
|
||||
"\n",
|
||||
"You will also find it convenient to utilize R. Although we will mainly\n",
|
||||
@@ -132,6 +136,7 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Installing R, C++, cython, Numba etc\n",
|
||||
"\n",
|
||||
"\n",
|
||||
@@ -170,6 +175,7 @@
|
||||
"[doconce](https://github.com/hplgit/doconce) you can convert a standard ascii text file into various HTML \n",
|
||||
"formats, ipython notebooks, latex files, pdf files etc with minimal edits.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Simple linear regression model using **scikit-learn**\n",
|
||||
"\n",
|
||||
"We start with perhaps our simplest possible example, using **scikit-learn** to perform linear regression analysis on a data set produced by us. \n",
|
||||
@@ -582,6 +588,8 @@
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Similarly, using **R**, we can perform similar studies. The following **R** code illustrates this.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Non-Linear Least squares in R"
|
||||
]
|
||||
},
|
||||
@@ -1573,6 +1581,111 @@
|
||||
"plt.grid(True)\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Random walk model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"from sklearn.preprocessing import PolynomialFeatures\n",
|
||||
"from sklearn.linear_model import LinearRegression\n",
|
||||
"\n",
|
||||
"steps=250\n",
|
||||
"\n",
|
||||
"distance=0\n",
|
||||
"x=0\n",
|
||||
"distance_list=[]\n",
|
||||
"steps_list=[]\n",
|
||||
"while x<steps:\n",
|
||||
" distance+=np.random.randint(-1,2)\n",
|
||||
" distance_list.append(distance)\n",
|
||||
" x+=1\n",
|
||||
" steps_list.append(x)\n",
|
||||
"plt.plot(steps_list,distance_list, color='green', label=\"Random Walk Data\")\n",
|
||||
"\n",
|
||||
"steps_list=np.asarray(steps_list)\n",
|
||||
"distance_list=np.asarray(distance_list)\n",
|
||||
"\n",
|
||||
"X=steps_list[:,np.newaxis]\n",
|
||||
"\n",
|
||||
"#Polynomial fits\n",
|
||||
"\n",
|
||||
"#Degree 2\n",
|
||||
"poly_features=PolynomialFeatures(degree=2, include_bias=False)\n",
|
||||
"X_poly=poly_features.fit_transform(X)\n",
|
||||
"\n",
|
||||
"lin_reg=LinearRegression()\n",
|
||||
"poly_fit=lin_reg.fit(X_poly,distance_list)\n",
|
||||
"b=lin_reg.coef_\n",
|
||||
"c=lin_reg.intercept_\n",
|
||||
"print (\"2nd degree coefficients:\")\n",
|
||||
"print (\"zero power: \",c)\n",
|
||||
"print (\"first power: \", b[0])\n",
|
||||
"print (\"second power: \",b[1])\n",
|
||||
"\n",
|
||||
"z = np.arange(0, steps, .01)\n",
|
||||
"z_mod=b[1]*z**2+b[0]*z+c\n",
|
||||
"\n",
|
||||
"fit_mod=b[1]*X**2+b[0]*X+c\n",
|
||||
"plt.plot(z, z_mod, color='r', label=\"2nd Degree Fit\")\n",
|
||||
"plt.title(\"Polynomial Regression\")\n",
|
||||
"\n",
|
||||
"plt.xlabel(\"Steps\")\n",
|
||||
"plt.ylabel(\"Distance\")\n",
|
||||
"\n",
|
||||
"#Degree 10\n",
|
||||
"poly_features10=PolynomialFeatures(degree=10, include_bias=False)\n",
|
||||
"X_poly10=poly_features10.fit_transform(X)\n",
|
||||
"\n",
|
||||
"poly_fit10=lin_reg.fit(X_poly10,distance_list)\n",
|
||||
"\n",
|
||||
"y_plot=poly_fit10.predict(X_poly10)\n",
|
||||
"plt.plot(X, y_plot, color='black', label=\"10th Degree Fit\")\n",
|
||||
"\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"#Decision Tree Regression\n",
|
||||
"from sklearn.tree import DecisionTreeRegressor\n",
|
||||
"regr_1=DecisionTreeRegressor(max_depth=2)\n",
|
||||
"regr_2=DecisionTreeRegressor(max_depth=5)\n",
|
||||
"regr_3=DecisionTreeRegressor(max_depth=7)\n",
|
||||
"regr_1.fit(X, distance_list)\n",
|
||||
"regr_2.fit(X, distance_list)\n",
|
||||
"regr_3.fit(X, distance_list)\n",
|
||||
"\n",
|
||||
"X_test = np.arange(0.0, steps, 0.01)[:, np.newaxis]\n",
|
||||
"y_1 = regr_1.predict(X_test)\n",
|
||||
"y_2 = regr_2.predict(X_test)\n",
|
||||
"y_3=regr_3.predict(X_test)\n",
|
||||
"\n",
|
||||
"# Plot the results\n",
|
||||
"plt.figure()\n",
|
||||
"plt.scatter(X, distance_list, s=2.5, c=\"black\", label=\"data\")\n",
|
||||
"plt.plot(X_test, y_1, color=\"red\",\n",
|
||||
" label=\"max_depth=2\", linewidth=2)\n",
|
||||
"plt.plot(X_test, y_2, color=\"green\", label=\"max_depth=5\", linewidth=2)\n",
|
||||
"plt.plot(X_test, y_3, color=\"m\", label=\"max_depth=7\", linewidth=2)\n",
|
||||
"\n",
|
||||
"plt.xlabel(\"Data\")\n",
|
||||
"plt.ylabel(\"Darget\")\n",
|
||||
"plt.title(\"Decision Tree Regression\")\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {},
|
||||
|
||||
Reference in New Issue
Block a user