-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c281a98
Showing
10 changed files
with
2,058 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
load("/Users/lasiadhi/Dropbox-2/Dropbox/Statistical_Learning/Chapter2/ESL.mixture.rda") | ||
ESL.mixture | ||
load("/Users/lasiadhi/Dropbox-2/Dropbox/Statistical_Learning/Chapter2/mixture.rda") |
6 changes: 6 additions & 0 deletions
6
Chapter2/.ipynb_checkpoints/Fig_2_1_linear_reg-checkpoint.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"cells": [], | ||
"metadata": {}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
141 changes: 141 additions & 0 deletions
141
Chapter2/.ipynb_checkpoints/Fig_2_2_KNN-checkpoint.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [ | ||
{ | ||
"ename": "AttributeError", | ||
"evalue": "'DataFrame' object has no attribute 'ravel'", | ||
"output_type": "error", | ||
"traceback": [ | ||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | ||
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", | ||
"\u001b[0;32m<ipython-input-8-90b4bbfc116b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 33\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 34\u001b[0m \u001b[0mknn\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mneighbors\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mKNeighborsClassifier\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn_neighbors\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 35\u001b[0;31m \u001b[0mknn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mravel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 36\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 37\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", | ||
"\u001b[0;32m//anaconda/lib/python3.5/site-packages/pandas/core/generic.py\u001b[0m in \u001b[0;36m__getattr__\u001b[0;34m(self, name)\u001b[0m\n\u001b[1;32m 2742\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mname\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_info_axis\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2743\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2744\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mobject\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__getattribute__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2745\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2746\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__setattr__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | ||
"\u001b[0;31mAttributeError\u001b[0m: 'DataFrame' object has no attribute 'ravel'" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import rpy2.robjects as robjects\n", | ||
"import pandas.rpy.common as com\n", | ||
"import pandas as pd\n", | ||
"from rpy2.robjects import pandas2ri\n", | ||
"%matplotlib notebook\n", | ||
"import matplotlib.pyplot as plt # for scatter plot\n", | ||
"from ggplot import *\n", | ||
"from sklearn import neighbors\n", | ||
"pandas2ri.activate()\n", | ||
"\n", | ||
"## load .RData and converts to pd.DataFrame\n", | ||
"robj = robjects.r.load(\"/Users/lasiadhi/Dropbox-2/Dropbox/Statistical_Learning/Chapter2/ESL.mixture.rda\")\n", | ||
"#myRData = com.load_data(robj)\n", | ||
"\n", | ||
"for sets in robj:\n", | ||
" myRData = com.load_data(sets)\n", | ||
"\n", | ||
"# load each table to seperate dataframes\n", | ||
"marginal= pd.DataFrame(myRData['marginal'])\n", | ||
"means = pd.DataFrame(myRData['means'])\n", | ||
"px1 = pd.DataFrame(myRData['px1'])\n", | ||
"px2 = pd.DataFrame(myRData['px2'])\n", | ||
"xnew = pd.DataFrame(myRData['xnew'])\n", | ||
"x = pd.DataFrame(myRData['x'])\n", | ||
"x.columns = ['x1', 'x2']\n", | ||
"x.index = x.index - 1\n", | ||
"\n", | ||
"y = pd.DataFrame(myRData['y'])\n", | ||
"y.columns = ['y']\n", | ||
"\n", | ||
"mydata = x.join(y)\n", | ||
"\n", | ||
"#KNN\n", | ||
"knn = neighbors.KNeighborsClassifier(n_neighbors=5)\n", | ||
"knn.fit(x, y) \n", | ||
"\n", | ||
"# Plot the decision boundary. For that, we will asign a color to each\n", | ||
"# point in the mesh [x_min, m_max]x[y_min, y_max].\n", | ||
"h = 0.05 #step size in the mesh\n", | ||
"x_min, x_max = x['x1'].min() - .5, x['x1'].max() + .5\n", | ||
"y_min, y_max = x['x2'].min() - .5, x['x2'].max() + .5\n", | ||
"xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))\n", | ||
"Z = knn.predict(np.c_[xx.ravel(), yy.ravel()])\n", | ||
"\n", | ||
"# Put the result into a color plot\n", | ||
"Z = Z.reshape(xx.shape)\n", | ||
"plt.figure(1, figsize=(4, 3))\n", | ||
"plt.set_cmap(plt.cm.Paired)\n", | ||
"plt.pcolormesh(xx, yy, Z)\n", | ||
"\n", | ||
"# Plot also the training points\n", | ||
"plt.figure(figsize=(9,11)) # initialize figure\n", | ||
"use_colours = {0: \"blue\", 1: \"orange\"}\n", | ||
"plt.scatter(mydata['x1'],mydata['x2'], c=[use_colours[i] for i in y['y']], s=40)\n", | ||
"plt.xlabel(\"X1\")\n", | ||
"plt.ylabel(\"X2\")\n", | ||
"plt.title(\"Fig. 2.1: A classification example in 2D (linear regression)\")\n", | ||
"#pl.scatter(x['x1'], x['x2'],c=y )\n", | ||
"#pl.xlabel('Sepal length')\n", | ||
"#pl.ylabel('Sepal width')\n", | ||
"\n", | ||
"plt.xlim(xx.min(), xx.max())\n", | ||
"plt.ylim(yy.min(), yy.max())\n", | ||
"plt.xticks(())\n", | ||
"plt.yticks(())\n", | ||
"\n", | ||
"plt.show()\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"-2.5208196765959814" | ||
] | ||
}, | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"x['x1'].min()" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.5.2" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"cells": [], | ||
"metadata": {}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
Binary file not shown.
Oops, something went wrong.