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
<spanid="cb21-2"><ahref="#cb21-2" aria-hidden="true" tabindex="-1"></a><spanclass="bu">print</span>(patient1.intersection(patient2)) <spanclass="co"># use intersection method</span></span>
456
456
<spanid="cb21-3"><ahref="#cb21-3" aria-hidden="true" tabindex="-1"></a><spanclass="bu">print</span>(patient1 <spanclass="op">&</span> patient2) <spanclass="co"># use & operator</span></span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
1.68 ms ± 3.7 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)
346
+
1.7 ms ± 29.2 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)
347
347
348
348
np.array time for my_dot_0:
349
-
2.67 ms ± 39.9 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)</code></pre>
349
+
2.59 ms ± 23.7 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)</code></pre>
350
350
</div>
351
351
</div>
352
352
<p>In a surprise result, seemingly contrary to everything previously stated, the <code>list</code> manipulation came out faster than the manipulation using the <code>numpy.array</code>. The small caveat is that the <code>numpy.array</code> object is built for speed when used with NumPy’s built in functions. We can try the test again, but this time we use some built in functions from NumPy to clean up the function definition and improve our results, hopefully.</p>
@@ -363,10 +363,10 @@ <h2 class="anchored" data-anchor-id="whats-the-deal-with-numpy">What’s the dea
710 μs ± 14.4 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)
366
+
732 μs ± 32.3 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)
367
367
368
368
np.array time for my_dot_1:
369
-
9.7 μs ± 1.05 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)</code></pre>
369
+
9.62 μs ± 794 ns per loop (mean ± std. dev. of 7 runs, 10 loops each)</code></pre>
370
370
</div>
371
371
</div>
372
372
<p>That’s more like it! Notice that the <code>list</code> speed sees some improvement using the built in NumPy functions as well, but certainly not as much as the <code>numpy.array</code>. In general the NumPy functions can handle generic <code>list</code> types, but the full potential is unlocked with the <code>numpy.array</code> type. For this final speed test, we can use the the totally pre-built <code>numpy.dot</code> function and check out the speed.</p>
@@ -377,11 +377,11 @@ <h2 class="anchored" data-anchor-id="whats-the-deal-with-numpy">What’s the dea
822 μs ± 5.33 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)
380
+
842 μs ± 21 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)
381
381
382
382
np.array time for np.dot:
383
-
The slowest run took 8.27 times longer than the fastest. This could mean that an intermediate result is being cached.
384
-
12.8 μs ± 10.8 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)</code></pre>
383
+
The slowest run took 11.74 times longer than the fastest. This could mean that an intermediate result is being cached.
384
+
8.62 μs ± 9.56 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)</code></pre>
385
385
</div>
386
386
</div>
387
387
<p>Will you look at that! The totally pre-built function is even faster than our last test with the two different functions. Notice that the <code>list</code> test really struggles, comparitively, with this function. Together, all of these speed tests should highlight: 1. NumPy functions are very efficient. 2. NumPy functions are relatively easy to use and clean up your code. 3. NumPy functions work best with <code>numpy.array</code> objects. 4. If you can find a NumPy function to handle your <code>numpy.array</code> objects, you should use it rather than trying to build your own function.</p>
<pre><code>the location of the NaN is (np.int64(3), np.int64(5))
491
491
492
-
Requirement already satisfied: matplotlib in /opt/hostedtoolcache/Python/3.13.3/x64/lib/python3.13/site-packages (3.10.3)
493
-
Requirement already satisfied: contourpy>=1.0.1 in /opt/hostedtoolcache/Python/3.13.3/x64/lib/python3.13/site-packages (from matplotlib) (1.3.2)
494
-
Requirement already satisfied: cycler>=0.10 in /opt/hostedtoolcache/Python/3.13.3/x64/lib/python3.13/site-packages (from matplotlib) (0.12.1)
495
-
Requirement already satisfied: fonttools>=4.22.0 in /opt/hostedtoolcache/Python/3.13.3/x64/lib/python3.13/site-packages (from matplotlib) (4.58.2)
496
-
Requirement already satisfied: kiwisolver>=1.3.1 in /opt/hostedtoolcache/Python/3.13.3/x64/lib/python3.13/site-packages (from matplotlib) (1.4.8)
497
-
Requirement already satisfied: numpy>=1.23 in /opt/hostedtoolcache/Python/3.13.3/x64/lib/python3.13/site-packages (from matplotlib) (2.2.6)
498
-
Requirement already satisfied: packaging>=20.0 in /opt/hostedtoolcache/Python/3.13.3/x64/lib/python3.13/site-packages (from matplotlib) (25.0)
499
-
Requirement already satisfied: pillow>=8 in /opt/hostedtoolcache/Python/3.13.3/x64/lib/python3.13/site-packages (from matplotlib) (11.2.1)
500
-
Requirement already satisfied: pyparsing>=2.3.1 in /opt/hostedtoolcache/Python/3.13.3/x64/lib/python3.13/site-packages (from matplotlib) (3.2.3)
501
-
Requirement already satisfied: python-dateutil>=2.7 in /opt/hostedtoolcache/Python/3.13.3/x64/lib/python3.13/site-packages (from matplotlib) (2.9.0.post0)
502
-
Requirement already satisfied: six>=1.5 in /opt/hostedtoolcache/Python/3.13.3/x64/lib/python3.13/site-packages (from python-dateutil>=2.7->matplotlib) (1.17.0)
492
+
Requirement already satisfied: matplotlib in /opt/hostedtoolcache/Python/3.13.4/x64/lib/python3.13/site-packages (3.10.3)
493
+
Requirement already satisfied: contourpy>=1.0.1 in /opt/hostedtoolcache/Python/3.13.4/x64/lib/python3.13/site-packages (from matplotlib) (1.3.2)
494
+
Requirement already satisfied: cycler>=0.10 in /opt/hostedtoolcache/Python/3.13.4/x64/lib/python3.13/site-packages (from matplotlib) (0.12.1)
495
+
Requirement already satisfied: fonttools>=4.22.0 in /opt/hostedtoolcache/Python/3.13.4/x64/lib/python3.13/site-packages (from matplotlib) (4.58.2)
496
+
Requirement already satisfied: kiwisolver>=1.3.1 in /opt/hostedtoolcache/Python/3.13.4/x64/lib/python3.13/site-packages (from matplotlib) (1.4.8)
497
+
Requirement already satisfied: numpy>=1.23 in /opt/hostedtoolcache/Python/3.13.4/x64/lib/python3.13/site-packages (from matplotlib) (2.3.0)
498
+
Requirement already satisfied: packaging>=20.0 in /opt/hostedtoolcache/Python/3.13.4/x64/lib/python3.13/site-packages (from matplotlib) (25.0)
499
+
Requirement already satisfied: pillow>=8 in /opt/hostedtoolcache/Python/3.13.4/x64/lib/python3.13/site-packages (from matplotlib) (11.2.1)
500
+
Requirement already satisfied: pyparsing>=2.3.1 in /opt/hostedtoolcache/Python/3.13.4/x64/lib/python3.13/site-packages (from matplotlib) (3.2.3)
501
+
Requirement already satisfied: python-dateutil>=2.7 in /opt/hostedtoolcache/Python/3.13.4/x64/lib/python3.13/site-packages (from matplotlib) (2.9.0.post0)
502
+
Requirement already satisfied: six>=1.5 in /opt/hostedtoolcache/Python/3.13.4/x64/lib/python3.13/site-packages (from python-dateutil>=2.7->matplotlib) (1.17.0)
503
503
Note: you may need to restart the kernel to use updated packages.
504
504
row 2 mean = 0.02053209316073448 and variance = 1.0315188879373587
505
505
row 4 mean = -0.10027662190124971 and variance = 0.8912458143138923</code></pre>
@@ -545,20 +545,20 @@ <h1>In class exercise</h1>
545
545
<spanclass="ansi-green-fg"> 8</span><spanstyle="font-style:italic;color:rgb(95,135,135)"># wanted last step</span>
546
546
<spanclass="ansi-green-fg"> 9</span> t_arange = np.arange(<spanclass="ansi-green-fg">0.</span>, <spanclass="ansi-yellow-fg">'''</span><spanclass="ansi-yellow-fg">end of range value</span><spanclass="ansi-yellow-fg">'''</span>, <spanclass="ansi-green-fg">0.01</span>)
<spanclass="ansi-green-fg">29</span><spanstyle="font-style:italic" class="ansi-yellow-fg"> Return evenly spaced numbers over a specified interval.</span>
<spanclass="ansi-green-fg">--> </span><spanclass="ansi-green-fg">121</span> num = <spanclass="ansi-yellow-bg">operator</span><spanclass="ansi-yellow-bg">.</span><spanclass="ansi-yellow-bg">index</span><spanclass="ansi-yellow-bg">(</span><spanclass="ansi-yellow-bg">num</span><spanclass="ansi-yellow-bg">)</span>
558
-
<spanclass="ansi-green-fg">122</span><spanstyle="font-weight:bold;color:rgb(0,135,0)">if</span> num < <spanclass="ansi-green-fg">0</span>:
<spanclass="ansi-green-fg">124</span><spanclass="ansi-yellow-fg">"</span><spanclass="ansi-yellow-fg">Number of samples, </span><spanstyle="font-weight:bold;color:rgb(175,95,135)">%s</span><spanclass="ansi-yellow-fg">, must be non-negative.</span><spanclass="ansi-yellow-fg">"</span> % num
<spanclass="ansi-green-fg">30</span><spanstyle="font-style:italic" class="ansi-yellow-fg"> Return evenly spaced numbers over a specified interval.</span>
<spanclass="ansi-green-fg">--> </span><spanclass="ansi-green-fg">122</span> num = <spanclass="ansi-yellow-bg">operator</span><spanclass="ansi-yellow-bg">.</span><spanclass="ansi-yellow-bg">index</span><spanclass="ansi-yellow-bg">(</span><spanclass="ansi-yellow-bg">num</span><spanclass="ansi-yellow-bg">)</span>
558
+
<spanclass="ansi-green-fg">123</span><spanstyle="font-weight:bold;color:rgb(0,135,0)">if</span> num < <spanclass="ansi-green-fg">0</span>:
<spanclass="ansi-green-fg">125</span><spanclass="ansi-yellow-fg">f</span><spanclass="ansi-yellow-fg">"</span><spanclass="ansi-yellow-fg">Number of samples, </span><spanstyle="font-weight:bold;color:rgb(175,95,135)">{</span>num<spanstyle="font-weight:bold;color:rgb(175,95,135)">}</span><spanclass="ansi-yellow-fg">, must be non-negative.</span><spanclass="ansi-yellow-fg">"</span>
561
+
<spanclass="ansi-green-fg">126</span> )
562
562
563
563
<spanclass="ansi-red-fg">TypeError</span>: 'str' object cannot be interpreted as an integer</pre>
0 commit comments