Skip to content

Commit 5277b12

Browse files
committed
Scipy Numpy Lectures
Added more broadcast examples
1 parent d3682d7 commit 5277b12

2 files changed

+60
-16
lines changed

numpy/SciPy Numpy Lecture 3 - Broadcasting, Shape Manipulation and Sorting Data.ipynb

+60-16
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148
},
149149
{
150150
"cell_type": "code",
151-
"execution_count": 42,
151+
"execution_count": 46,
152152
"metadata": {
153153
"hideCode": false,
154154
"hidePrompt": false
@@ -163,7 +163,7 @@
163163
" [ 30., 30., 30.]])"
164164
]
165165
},
166-
"execution_count": 42,
166+
"execution_count": 46,
167167
"metadata": {},
168168
"output_type": "execute_result"
169169
}
@@ -176,7 +176,7 @@
176176
},
177177
{
178178
"cell_type": "code",
179-
"execution_count": 43,
179+
"execution_count": 47,
180180
"metadata": {
181181
"hideCode": false,
182182
"hidePrompt": false
@@ -188,7 +188,7 @@
188188
"array([ 1., 2., 3.])"
189189
]
190190
},
191-
"execution_count": 43,
191+
"execution_count": 47,
192192
"metadata": {},
193193
"output_type": "execute_result"
194194
}
@@ -200,7 +200,7 @@
200200
},
201201
{
202202
"cell_type": "code",
203-
"execution_count": 44,
203+
"execution_count": 48,
204204
"metadata": {
205205
"hideCode": false,
206206
"hidePrompt": false
@@ -215,7 +215,7 @@
215215
" [ 31., 32., 33.]])"
216216
]
217217
},
218-
"execution_count": 44,
218+
"execution_count": 48,
219219
"metadata": {},
220220
"output_type": "execute_result"
221221
}
@@ -236,16 +236,53 @@
236236
"A 2-D (two-dimensional) array multiplied by 1-D (one-dimensional) array. It got stretched in the column direction so as to match the elements of the 2D array columns.\n",
237237
"<img src=\"figures/broadcast_strectch2.gif\">\n",
238238
"\n",
239-
"### Can we do the above operation any faster?\n",
239+
"Would the same be possible for different shapes? Does broadcasting magically understands and fixes our assumptions?\n",
240240
"\n",
241-
"Like is one line?\n",
241+
"Let's take a look...\n",
242+
"\n"
243+
]
244+
},
245+
{
246+
"cell_type": "code",
247+
"execution_count": 53,
248+
"metadata": {},
249+
"outputs": [
250+
{
251+
"ename": "ValueError",
252+
"evalue": "operands could not be broadcast together with shapes (4,3) (4,) ",
253+
"output_type": "error",
254+
"traceback": [
255+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
256+
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
257+
"\u001b[0;32m<ipython-input-53-baf25e1d8c2c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m \u001b[0;36m0.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0.0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m10.0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10.0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10.0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m20.0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m20.0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m20.0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m30.0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m30.0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m30.0\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[1;32m 2\u001b[0m \u001b[0mi\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3.0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mg\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
258+
"\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (4,3) (4,) "
259+
]
260+
}
261+
],
262+
"source": [
263+
"g = np.array([[ 0.0, 0.0, 0.0], [10.0,10.0,10.0],[20.0,20.0,20.0],[30.0,30.0,30.0]])\n",
264+
"i = np.array([0.0, 1.0, 2.0, 3.0])\n",
265+
"g+i "
266+
]
267+
},
268+
{
269+
"cell_type": "markdown",
270+
"metadata": {},
271+
"source": [
272+
"### We had a mismatch...\n",
273+
"\n",
274+
"<img src=\"figures/broadcast_strectch3.gif\">\n",
275+
"\n",
276+
"Explanation: When the trainling dimensions of the arrays are different as you saw above, then broadcasting will fail making it impossible to align the values in the rows of the first array with the elements of the second array for an **element-by-element** addition or multiplication.\n",
242277
"\n",
243-
"Let's try np.tile and np.arange"
278+
"### Also, is there a way to do this in one line of code\n",
279+
"\n",
280+
"Tip: look up more into np.tile and np.arange"
244281
]
245282
},
246283
{
247284
"cell_type": "code",
248-
"execution_count": 10,
285+
"execution_count": 60,
249286
"metadata": {
250287
"hideCode": false,
251288
"hidePrompt": false
@@ -260,7 +297,7 @@
260297
" [30, 30, 30]])"
261298
]
262299
},
263-
"execution_count": 10,
300+
"execution_count": 60,
264301
"metadata": {},
265302
"output_type": "execute_result"
266303
}
@@ -273,7 +310,7 @@
273310
},
274311
{
275312
"cell_type": "code",
276-
"execution_count": 11,
313+
"execution_count": 57,
277314
"metadata": {
278315
"hideCode": false,
279316
"hidePrompt": false
@@ -285,7 +322,7 @@
285322
"array([0, 1, 2])"
286323
]
287324
},
288-
"execution_count": 11,
325+
"execution_count": 57,
289326
"metadata": {},
290327
"output_type": "execute_result"
291328
}
@@ -307,7 +344,7 @@
307344
},
308345
{
309346
"cell_type": "code",
310-
"execution_count": 13,
347+
"execution_count": 58,
311348
"metadata": {
312349
"hideCode": false,
313350
"hidePrompt": false
@@ -322,7 +359,7 @@
322359
" [30, 31, 32]])"
323360
]
324361
},
325-
"execution_count": 13,
362+
"execution_count": 58,
326363
"metadata": {},
327364
"output_type": "execute_result"
328365
}
@@ -340,7 +377,14 @@
340377
"source": [
341378
"##### So you see that broadcasting was applied magically...\n",
342379
"\n",
343-
"Ask yourself, why couldn't we add original `a` and `b` ?"
380+
"Ask yourself, why couldn't we add original `a` and `b` ?\n",
381+
"\n",
382+
"Note, original a was:\n",
383+
"```python\n",
384+
"array([[ 0, 10, 20, 30],\n",
385+
" [ 0, 10, 20, 30],\n",
386+
" [ 0, 10, 20, 30]])\n",
387+
"```"
344388
]
345389
},
346390
{

numpy/figures/broadcast_strectch3.gif

4.57 KB
Loading

0 commit comments

Comments
 (0)