Skip to content

Commit e528eb9

Browse files
committed
update lecture to avoid literal box warning
1 parent 4c1eb44 commit e528eb9

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

lectures/numba.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,6 @@ When Numba cannot infer all type information, it will raise an error.
257257
For example, in the case below, Numba is unable to determine the type of function `mean` when compiling the function `bootstrap`
258258

259259
```{code-cell} ipython3
260-
:tags: [raises-exception, hide-output]
261-
262260
@njit
263261
def bootstrap(data, statistics, n):
264262
bootstrap_stat = np.empty(n)
@@ -273,14 +271,17 @@ def mean(data):
273271
274272
data = np.array([2.3, 3.1, 4.3, 5.9, 2.1, 3.8, 2.2])
275273
n_resamples = 10
276-
bootstrap(data, mean, n_resamples)
274+
275+
#Error
276+
try:
277+
bootstrap(data, mean, n_resamples)
278+
except Exception as e:
279+
print(e)
277280
```
278281

279282
But Numba recognizes JIT-compiled functions
280283

281284
```{code-cell} ipython3
282-
:tags: []
283-
284285
@njit
285286
def mean(data):
286287
return np.mean(data)
@@ -291,13 +292,13 @@ def mean(data):
291292
We can check the signature of the JIT-compiled function
292293

293294
```{code-cell} ipython3
294-
:tags: []
295-
296295
bootstrap.signatures
297296
```
298297

299298
It shows that the function `bootstrap` takes one `float64` floating point array, one function called `mean` and an `int64` integer.
300299

300+
Now let's see what happens when we change the inputs.
301+
301302
Running it again with a larger integer for `n` and a different set of data does not change the signature of the function.
302303

303304
```{code-cell} ipython3
@@ -308,27 +309,25 @@ bootstrap.signatures
308309

309310
As expected, the second run is much faster.
310311

311-
Let's try to change the data again and use integer arrays as data
312+
Let's try to change the data again and use an integer array as data
312313

313314
```{code-cell} ipython3
314-
:tags: []
315-
316315
data = np.array([1, 2, 3, 4, 5], dtype=np.int64)
317316
%time bootstrap(data, mean, 100)
318317
bootstrap.signatures
319318
```
320319

321-
Note that a second signature is added into the signature of `bootstrap` with `int64` array as the first argument.
320+
Note that a second signature with an `int64` array as the first argument is added into the signature of `bootstrap` function.
322321

323-
The runtime is the same as if we ran the function for the first time.
322+
The runtime is slower as if we ran the function for the first time.
324323

325-
It suggests that Numba needs to recompile as the type changes.
324+
It suggests that Numba recompiles this function as the type changes.
326325

327-
Overall, type inference is what helps Numba to achieve its performance, but it also limits what Numba supports.
326+
Overall, type inference helps Numba to achieve its performance, but it also limits what Numba supports as we have shown in the function example.
328327

329-
You can refer to the list of supported Python and Numpy features [here](https://numba.pydata.org/numba-doc/dev/reference/pysupported.html).
328+
In fact, this limitation means that Numba does not support everything in Python and scientific libraries we learned before.
330329

331-
+++
330+
You can refer to the list of supported Python and Numpy features [here](https://numba.pydata.org/numba-doc/dev/reference/pysupported.html).
332331

333332
## Compiling Classes
334333

0 commit comments

Comments
 (0)