|
1 | 1 | {
|
2 | 2 | "metadata": {
|
3 | 3 | "name": "",
|
4 |
| - "signature": "sha256:90ccb5909624353434d7af8f1073a89ba8f8f43775a430cf536d07701670eb19" |
| 4 | + "signature": "sha256:291c02e0eda1008690ba35d47865fd743d899737c6c702e384430e08988052a0" |
5 | 5 | },
|
6 | 6 | "nbformat": 3,
|
7 | 7 | "nbformat_minor": 0,
|
8 | 8 | "worksheets": [
|
9 | 9 | {
|
10 | 10 | "cells": [
|
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "collapsed": false, |
| 14 | + "input": [ |
| 15 | + "# Let printing work the same in Python 2 and 3\n", |
| 16 | + "from __future__ import print_function" |
| 17 | + ], |
| 18 | + "language": "python", |
| 19 | + "metadata": {}, |
| 20 | + "outputs": [] |
| 21 | + }, |
11 | 22 | {
|
12 | 23 | "cell_type": "markdown",
|
13 | 24 | "metadata": {},
|
|
39 | 50 | "collapsed": false,
|
40 | 51 | "input": [
|
41 | 52 | "a = np.array([1, 2, 3])\n",
|
42 |
| - "print a.shape\n", |
43 |
| - "print a.size\n", |
44 |
| - "print a.ndim" |
| 53 | + "print(a.shape)\n", |
| 54 | + "print(a.size)\n", |
| 55 | + "print(a.ndim)" |
45 | 56 | ],
|
46 | 57 | "language": "python",
|
47 | 58 | "metadata": {},
|
|
52 | 63 | "collapsed": false,
|
53 | 64 | "input": [
|
54 | 65 | "x = np.arange(100)\n",
|
55 |
| - "print x.shape\n", |
56 |
| - "print x.size\n", |
57 |
| - "print x.ndim" |
| 66 | + "print(x.shape)\n", |
| 67 | + "print(x.size)\n", |
| 68 | + "print(x.ndim)" |
58 | 69 | ],
|
59 | 70 | "language": "python",
|
60 | 71 | "metadata": {},
|
|
65 | 76 | "collapsed": false,
|
66 | 77 | "input": [
|
67 | 78 | "y = np.random.rand(5, 80)\n",
|
68 |
| - "print y.shape\n", |
69 |
| - "print y.size\n", |
70 |
| - "print y.ndim" |
| 79 | + "print(y.shape)\n", |
| 80 | + "print(y.size)\n", |
| 81 | + "print(y.ndim)" |
71 | 82 | ],
|
72 | 83 | "language": "python",
|
73 | 84 | "metadata": {},
|
|
86 | 97 | "collapsed": false,
|
87 | 98 | "input": [
|
88 | 99 | "x.shape = (20, 5)\n",
|
89 |
| - "print x" |
| 100 | + "print(x)" |
90 | 101 | ],
|
91 | 102 | "language": "python",
|
92 | 103 | "metadata": {},
|
|
104 | 115 | "collapsed": false,
|
105 | 116 | "input": [
|
106 | 117 | "y.shape = (4, 20, -1)\n",
|
107 |
| - "print y.shape" |
| 118 | + "print(y.shape)" |
108 | 119 | ],
|
109 | 120 | "language": "python",
|
110 | 121 | "metadata": {},
|
|
122 | 133 | "collapsed": false,
|
123 | 134 | "input": [
|
124 | 135 | "# Scalar Indexing\n",
|
125 |
| - "print x[2]" |
| 136 | + "print(x[2])" |
126 | 137 | ],
|
127 | 138 | "language": "python",
|
128 | 139 | "metadata": {},
|
|
133 | 144 | "collapsed": false,
|
134 | 145 | "input": [
|
135 | 146 | "# Slicing\n",
|
136 |
| - "print x[2:5]" |
| 147 | + "print(x[2:5])" |
137 | 148 | ],
|
138 | 149 | "language": "python",
|
139 | 150 | "metadata": {},
|
|
144 | 155 | "collapsed": false,
|
145 | 156 | "input": [
|
146 | 157 | "# Advanced slicing\n",
|
147 |
| - "print \"First 5 rows\\n\", x[:5]\n", |
148 |
| - "print \"Row 18 to the end\\n\", x[18:]\n", |
149 |
| - "print \"Last 5 rows\\n\", x[-5:]\n", |
150 |
| - "print \"Reverse the rows\\n\", x[::-1]" |
| 158 | + "print(\"First 5 rows\\n\", x[:5])\n", |
| 159 | + "print(\"Row 18 to the end\\n\", x[18:])\n", |
| 160 | + "print(\"Last 5 rows\\n\", x[-5:])\n", |
| 161 | + "print(\"Reverse the rows\\n\", x[::-1])" |
151 | 162 | ],
|
152 | 163 | "language": "python",
|
153 | 164 | "metadata": {},
|
|
158 | 169 | "collapsed": false,
|
159 | 170 | "input": [
|
160 | 171 | "# Boolean Indexing\n",
|
161 |
| - "print x[(x % 2) == 0]" |
| 172 | + "print(x[(x % 2) == 0])" |
162 | 173 | ],
|
163 | 174 | "language": "python",
|
164 | 175 | "metadata": {},
|
|
169 | 180 | "collapsed": false,
|
170 | 181 | "input": [
|
171 | 182 | "# Fancy Indexing -- Note the use of a list, not tuple!\n",
|
172 |
| - "print x[[1, 3, 8, 9, 2]]" |
| 183 | + "print(x[[1, 3, 8, 9, 2]])" |
173 | 184 | ],
|
174 | 185 | "language": "python",
|
175 | 186 | "metadata": {},
|
|
187 | 198 | "cell_type": "code",
|
188 | 199 | "collapsed": true,
|
189 | 200 | "input": [
|
190 |
| - "print \"Shape of X:\", x.shape\n", |
191 |
| - "print \"Shape of Y:\", y.shape" |
| 201 | + "print(\"Shape of X:\", x.shape)\n", |
| 202 | + "print(\"Shape of Y:\", y.shape)" |
192 | 203 | ],
|
193 | 204 | "language": "python",
|
194 | 205 | "metadata": {},
|
|
206 | 217 | "collapsed": false,
|
207 | 218 | "input": [
|
208 | 219 | "a = x + y\n",
|
209 |
| - "print a.shape\n", |
| 220 | + "print(a.shape)\n", |
210 | 221 | "b = x[np.newaxis, :, :] + y\n",
|
211 |
| - "print b.shape\n", |
| 222 | + "print(b.shape)\n", |
212 | 223 | "c = np.tile(x, (4, 1, 1)) + y\n",
|
213 |
| - "print c.shape\n", |
214 |
| - "print \"Are a and b identical?\", np.all(a == b)\n", |
215 |
| - "print \"Are a and c identical?\", np.all(a == c)" |
| 224 | + "print(c.shape)\n", |
| 225 | + "print(\"Are a and b identical?\", np.all(a == b))\n", |
| 226 | + "print(\"Are a and c identical?\", np.all(a == c))" |
216 | 227 | ],
|
217 | 228 | "language": "python",
|
218 | 229 | "metadata": {},
|
|
231 | 242 | "input": [
|
232 | 243 | "x = np.arange(-5, 5, 0.1)\n",
|
233 | 244 | "y = np.arange(-8, 8, 0.25)\n",
|
234 |
| - "print x.shape, y.shape\n", |
| 245 | + "print(x.shape, y.shape)\n", |
235 | 246 | "z = x[np.newaxis, :] * y[:, np.newaxis]\n",
|
236 |
| - "print z.shape" |
| 247 | + "print(z.shape)" |
237 | 248 | ],
|
238 | 249 | "language": "python",
|
239 | 250 | "metadata": {},
|
|
245 | 256 | "input": [
|
246 | 257 | "# More concisely\n",
|
247 | 258 | "y, x = np.ogrid[-8:8:0.25, -5:5:0.1]\n",
|
248 |
| - "print x.shape, y.shape\n", |
| 259 | + "print(x.shape, y.shape)\n", |
249 | 260 | "z = x * y\n",
|
250 |
| - "print z.shape" |
| 261 | + "print(z.shape)" |
251 | 262 | ],
|
252 | 263 | "language": "python",
|
253 | 264 | "metadata": {},
|
|
0 commit comments