You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book.html
+7-1Lines changed: 7 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -150,7 +150,9 @@ <h2><a class="toc-backref" href="#contents">About the author</a></h2>
150
150
resources and tutorials (Matplotlib, numpy, OpenGL) and he's teaching Python,
151
151
numpy and scientific visualization at the University of Bordeaux and in various
152
152
conferences and schools worldwide (SciPy, EuroScipy, etc). He's also the author
153
-
of the popular article <aclass="reference external" href="http://dx.doi.org/10.1371/journal.pcbi.1003833">Ten Simple Rules for Better Figures</a>.</p>
153
+
of the popular article <aclass="reference external" href="http://dx.doi.org/10.1371/journal.pcbi.1003833">Ten Simple Rules for Better Figures</a> and a popular
<spanclass="name">steps</span><spanclass="operator">=</span><spanclass="name">random</span><spanclass="operator">.</span><spanclass="name">sample</span><spanclass="punctuation">([</span><spanclass="literal number integer">1</span><spanclass="punctuation">,</span><spanclass="operator">-</span><spanclass="literal number integer">1</span><spanclass="punctuation">]</span><spanclass="operator">*</span><spanclass="name">n</span><spanclass="punctuation">,</span><spanclass="name">n</span><spanclass="punctuation">)</span>
<spanclass="name">walk</span><spanclass="operator">=</span><spanclass="name">random_walk_faster</span><spanclass="punctuation">(</span><spanclass="literal number integer">1000</span><spanclass="punctuation">)</span>
314
318
</pre>
315
319
<p>In fact, we've just <em>vectorized</em> our function. Instead of looping for picking
316
320
sequential steps and add them to the current position, we fist generate all the
<spanclass="keyword">def</span><spanclass="name function">random_walk_fastest</span><spanclass="punctuation">(</span><spanclass="name">n</span><spanclass="operator">=</span><spanclass="literal number integer">1000</span><spanclass="punctuation">):</span>
330
334
<spanclass="name">steps</span><spanclass="operator">=</span><spanclass="literal number integer">2</span><spanclass="operator">*</span><spanclass="name">np</span><spanclass="operator">.</span><spanclass="name">random</span><spanclass="operator">.</span><spanclass="name">randint</span><spanclass="punctuation">(</span><spanclass="literal number integer">0</span><spanclass="punctuation">,</span><spanclass="literal number integer">2</span><spanclass="punctuation">,</span><spanclass="name">size</span><spanclass="operator">=</span><spanclass="literal number integer">1000</span><spanclass="punctuation">)</span><spanclass="operator">-</span><spanclass="literal number integer">1</span>
<spanclass="name">walk</span><spanclass="operator">=</span><spanclass="name">random_walk_fastest</span><spanclass="punctuation">(</span><spanclass="literal number integer">1000</span><spanclass="punctuation">)</span>
332
338
</pre>
333
339
<p>Not too difficult, but we gained a factor 500x using numpy:</p>
0 commit comments