Permalink
Browse files

Update examples

  • Loading branch information...
rossant committed Aug 22, 2015
1 parent 9e0c3dd commit 632cd181e27454a8263cfd7ec897f6ca147f0119
View
@@ -31,7 +31,7 @@
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": "!wget https://github.com/ipython-books/minibook-2nd-data/blob/master/facebook.zip?raw=true -O facebook.zip"
"source": "!wget https://raw.githubusercontent.com/ipython-books/minibook-2nd-data/master/facebook.zip"
},
{
"cell_type": "code",
@@ -372,7 +372,7 @@
"output_type": "execute_result"
}
],
"source": "from IPython.html.widgets import interact\n@interact(x=(0, 10))\ndef square(x):\n return(\"The square of %d is %d.\" % (x, x**2))"
"source": "from ipywidgets import interact # IPython.html.widgets before IPython 4.0\n@interact(x=(0, 10))\ndef square(x):\n return(\"The square of %d is %d.\" % (x, x**2))"
},
{
"cell_type": "markdown",
@@ -34,7 +34,7 @@
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": "!curl -O https://github.com/ipython-books/minibook-2nd-data/blob/master/nyc_taxi.zip?raw=true\n!unzip nyc_taxi.zip"
"source": "!wget https://raw.githubusercontent.com/ipython-books/minibook-2nd-data/master/nyc_taxi.zip\n!unzip nyc_taxi.zip"
},
{
"cell_type": "code",
@@ -84,7 +84,7 @@
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": "from IPython.html.widgets import interact"
"source": "from ipywidgets import interact"
},
{
"cell_type": "code",
@@ -297,28 +297,56 @@
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": "bin = .0001\nbins_lon = np.arange(-74.0214, -73.9524, bin)\nbins_lat = np.arange(+40.6978, +40.7982, bin)"
"source": "def lat_lon_to_pixels(lat, lon):\n lat_rad = lat * np.pi / 180.0\n lat_rad = np.log(np.tan((lat_rad + np.pi / 2.0) / 2.0))\n x = 100 * (lon + 180.0) / 360.0\n y = 100 * (lat_rad - np.pi) / (2.0 * np.pi)\n return (x, y)"
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": "grid, _, _ = np.histogram2d(points[:, 0], points[:, 1],\n weights=weights,\n bins=(bins_lon, bins_lat))"
"source": "lon, lat = points.T\nx, y = lat_lon_to_pixels(lat, lon)"
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": "density = 1. / (1. + np.exp(-.5 * grid))"
"source": "lon_min, lat_min = -74.0214, 40.6978\nlon_max, lat_max = -73.9524, 40.7982"
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": "plt.imshow(density.T,\n origin='lower',\n interpolation='bicubic'\n )\nplt.axis('off')"
"source": "x_min, y_min = lat_lon_to_pixels(lat_min, lon_min)\nx_max, y_max = lat_lon_to_pixels(lat_max, lon_max)"
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": "bin = .00003\nbins_x = np.arange(x_min, x_max, bin)\nbins_y = np.arange(y_min, y_max, bin)"
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": "grid, _, _ = np.histogram2d(y, x, weights=weights,\n bins=(bins_y, bins_x))"
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": "density = 1. / (1. + np.exp(-.5 * grid))"
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": "plt.imshow(density,\n origin='lower',\n interpolation='bicubic'\n )\nplt.axis('off')"
},
{
"cell_type": "markdown",
View
@@ -95,7 +95,7 @@
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": "from IPython.html.widgets import interact"
"source": "from ipywidgets import interact"
},
{
"cell_type": "code",
View
@@ -29,74 +29,67 @@
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": "def walk(x):\n n = len(x)\n dx = 1. / n\n for i in range(n - 1):\n x_new = x[i] + dx * step()\n if x_new > 5e-3:\n x[i + 1] = 0.\n else:\n x[i + 1] = x_new"
"source": "def walk(n):\n x = np.zeros(n)\n dx = 1. / n\n for i in range(n - 1):\n x_new = x[i] + dx * step()\n if x_new > 5e-3:\n x[i + 1] = 0.\n else:\n x[i + 1] = x_new\n return x"
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": "n = 100000\nx = np.zeros(n)"
"source": "n = 100000\nx = walk(n)"
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": "walk(x)"
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": "plt.plot(x)"
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": "10 loops, best of 3: 47.9 ms per loop"
"text/plain": "10 loops, best of 3: 57.6 ms per loop"
},
"execution_count": 7,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": "%%timeit x = np.zeros(n)\nwalk(x)"
"source": "%%timeit\nwalk(n)"
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": "@jit(nopython=True)\ndef step_numba():\n return 1. if random.random() > .5 else -1."
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": "@jit(nopython=True)\ndef walk_numba(x):\n n = len(x)\n dx = 1. / n\n for i in range(n - 1):\n x_new = x[i] + dx * step_numba()\n if x_new > 5e-3:\n x[i + 1] = 0.\n else:\n x[i + 1] = x_new"
"source": "@jit(nopython=True)\ndef walk_numba(n):\n x = np.zeros(n)\n dx = 1. / n\n for i in range(n - 1):\n x_new = x[i] + dx * step_numba()\n if x_new > 5e-3:\n x[i + 1] = 0.\n else:\n x[i + 1] = x_new\n return x"
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": "The slowest run took 33.25 times longer than the fastest. This could mean that an intermediate result is being cached\n1000 loops, best of 3: 1.59 ms per loop"
"text/plain": "The slowest run took 81.94 times longer than the fastest. This could mean that an intermediate result is being cached\n1000 loops, best of 3: 1.89 ms per loop"
},
"execution_count": 10,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": "%%timeit x = np.zeros(n)\nwalk_numba(x)"
"source": "%%timeit\nwalk_numba(n)"
},
{
"cell_type": "markdown",
@@ -105,14 +98,14 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": "1 loops, best of 3: 689 ms per loop"
},
"execution_count": 11,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
@@ -121,21 +114,21 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": "@vectorize\ndef kernel(x):\n return np.cos(2*x**2 + 3*x + 4*np.exp(x**3))"
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": "-0.98639139715432589"
},
"execution_count": 13,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
@@ -144,14 +137,14 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": "1 loops, best of 3: 324 ms per loop"
},
"execution_count": 14,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
@@ -160,14 +153,14 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": "10 loops, best of 3: 122 ms per loop"
},
"execution_count": 15,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
@@ -176,14 +169,14 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": "4"
},
"execution_count": 16,
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
View
@@ -52,7 +52,7 @@
"outputs": [
{
"data": {
"text/plain": "100 loops, best of 3: 2.84 ms per loop"
"text/plain": "100 loops, best of 3: 4 ms per loop"
},
"execution_count": 4,
"metadata": {},
@@ -71,7 +71,9 @@
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"metadata": {
"scrolled": true
},
"outputs": [],
"source": "%%cython\ndef primes_cython_1(n):\n primes = [False, False] + [True] * (n - 2)\n i = 2\n while i < n:\n # We do not deal with composite numbers.\n if not primes[i]:\n i += 1\n continue\n k = i * i\n # We mark multiples of i as composite numbers.\n while k < n:\n primes[k] = False\n k += i\n i += 1\n # We return all numbers marked with True.\n return [i for i in range(2, n) if primes[i]]"
},
@@ -98,7 +100,7 @@
"outputs": [
{
"data": {
"text/plain": "1000 loops, best of 3: 1.75 ms per loop"
"text/plain": "100 loops, best of 3: 1.99 ms per loop"
},
"execution_count": 8,
"metadata": {},
@@ -121,7 +123,7 @@
"outputs": [
{
"data": {
"text/plain": "1000 loops, best of 3: 217 \u00b5s per loop"
"text/plain": "1000 loops, best of 3: 266 \u00b5s per loop"
},
"execution_count": 10,
"metadata": {},
@@ -10,7 +10,7 @@
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": "import numpy as np\nfrom IPython.parallel import Client"
"source": "import numpy as np\nfrom ipyparallel import Client # IPython.parallel before IPython 4.0"
},
{
"cell_type": "code",
View
@@ -42,7 +42,7 @@
"output_type": "execute_result"
}
],
"source": "%%writefile -a cpp/cpp_kernel.py\n\n\"\"\"C++ wrapper kernel.\"\"\"\nfrom IPython.kernel.zmq.kernelbase import Kernel\n\nclass CppKernel(Kernel):\n\n # Kernel information.\n implementation = 'C++'\n implementation_version = '1.0'\n language = 'c++'\n language_version = '1.0'\n language_info = {'name': 'c++',\n 'mimetype': 'text/plain'}\n banner = \"C++ kernel\"\n\n def do_execute(self, code, silent,\n store_history=True,\n user_expressions=None,\n allow_stdin=False):\n \"\"\"This function is called when a code cell is executed.\"\"\"\n\n if not silent:\n # We run the C++ code and get the output.\n output = exec_cpp(code)\n\n # We send back the result to the frontend.\n stream_content = {'name': 'stdout', 'text': output}\n self.send_response(self.iopub_socket, 'stream', stream_content)\n\n return {'status': 'ok',\n # The base class increments the execution count\n 'execution_count': self.execution_count,\n 'payload': [],\n 'user_expressions': {},\n }\n\nif __name__ == '__main__':\n from IPython.kernel.zmq.kernelapp import IPKernelApp\n IPKernelApp.launch_instance(kernel_class=CppKernel)"
"source": "%%writefile -a cpp/cpp_kernel.py\n\n\"\"\"C++ wrapper kernel.\"\"\"\nfrom ipykernel.kernelbase import Kernel\n\nclass CppKernel(Kernel):\n\n # Kernel information.\n implementation = 'C++'\n implementation_version = '1.0'\n language = 'c++'\n language_version = '1.0'\n language_info = {'name': 'c++',\n 'mimetype': 'text/plain'}\n banner = \"C++ kernel\"\n\n def do_execute(self, code, silent,\n store_history=True,\n user_expressions=None,\n allow_stdin=False):\n \"\"\"This function is called when a code cell is executed.\"\"\"\n\n if not silent:\n # We run the C++ code and get the output.\n output = exec_cpp(code)\n\n # We send back the result to the frontend.\n stream_content = {'name': 'stdout', 'text': output}\n self.send_response(self.iopub_socket, 'stream', stream_content)\n\n return {'status': 'ok',\n # The base class increments the execution count\n 'execution_count': self.execution_count,\n 'payload': [],\n 'user_expressions': {},\n }\n\nif __name__ == '__main__':\n from ipykernel.kernelapp import IPKernelApp\n IPKernelApp.launch_instance(kernel_class=CppKernel)"
},
{
"cell_type": "code",
@@ -64,7 +64,16 @@
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": "[InstallKernelSpec] Installed kernelspec cpp in /Users/cyrille/Library/Jupyter/kernels/cpp"
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": "!jupyter kernelspec install --replace --user cpp"
},
{
@@ -74,7 +83,7 @@
"outputs": [
{
"data": {
"text/plain": "Available kernels:\n python3\n cpp"
"text/plain": "Available kernels:\n cpp\n python3"
},
"execution_count": 6,
"metadata": {},

0 comments on commit 632cd18

Please sign in to comment.