- IPython Interactive Computing and Visualization Cookbook(Second Edition)
- Cyrille Rossant
- 625字
- 2021-07-02 16:23:32
Teaching programming in the Notebook with IPython Blocks
The Jupyter Notebook is not only a tool for scientific research and data analysis but also a great tool for teaching. In this recipe, we show a simple and fun Python library for teaching programming notions: IPython Blocks (available at http://ipythonblocks.org). This library allows you or your students to create grids of colorful blocks. You can change the color and size of individual blocks, and you can even animate your grids. There are many basic technical notions you can illustrate with this tool. The visual aspect of this tool makes the learning process more effective and engaging.
In this recipe, we will notably perform the following tasks:
- Illustrate matrix multiplication with an animation
- Display an image as a block grid
Getting ready
To install IPython Blocks, type pip install ipythonblocks
in a Terminal.
How to do it...
- First, we import some modules as follows:
>>> import time from IPython.display import clear_output from ipythonblocks import BlockGrid, colors
- Now, we create a block grid with five columns and five rows, and we fill each block with purple:
>>> grid = BlockGrid(width=5, height=5, fill=colors['Purple']) grid.show()
- We can access individual blocks with 2D indexing. This illustrates the indexing syntax in Python. We can also access an entire row or line with a
:
(colon). Each block is represented by an RGB color. The library comes with a handy dictionary of colors, assigning RGB tuples to standard color names as follows:>>> grid[0, 0] = colors['Lime'] grid[-1, 0] = colors['Lime'] grid[:, -1] = colors['Lime'] grid.show()
- Now, we are going to illustrate matrix multiplication. We will represent two (n, n) matrices,
A
(in cyan) andB
(lime), aligned with C = A B (yellow). To do this, we use a small trick: creating a big white grid of size (2n+1, 2n+1). The matricesA
,B
, andC
are just views on parts of the grid.>>> n = 5 grid = BlockGrid(width=2 * n + 1, height=2 * n + 1, fill=colors['White']) A = grid[n + 1:, :n] B = grid[:n, n + 1:] C = grid[n + 1:, n + 1:] A[:, :] = colors['Cyan'] B[:, :] = colors['Lime'] C[:, :] = colors['Yellow'] grid.show()
- Let's turn to matrix multiplication itself. We perform a loop over all rows and columns, and we highlight the corresponding rows and columns in
A
andB
that are multiplied together during the matrix product. We combine IPython'sclear_output()
method withgrid.show()
andtime.sleep()
(pause) to implement the animation as follows:>>> for i in range(n): for j in range(n): # We reset the matrix colors. A[:, :] = colors['Cyan'] B[:, :] = colors['Lime'] C[:, :] = colors['Yellow'] # We highlight the adequate rows # and columns in red. A[i, :] = colors['Red'] B[:, j] = colors['Red'] C[i, j] = colors['Red'] # We animate the grid in the loop. clear_output() grid.show() time.sleep(.25)
- Finally, we display an image with IPython Blocks. We download and import a PNG image with Matplotlib and we retrieve the data as follows:
>>> # We downsample the image by a factor of 4 for # performance reasons. img = plt.imread('https://github.com/ipython-books/' 'cookbook-2nd-data/blob/master/' 'beach.png?raw=true')[::4, ::4, :] >>> rgb = [img[..., i].ravel() for i in range(3)]
- Now, we create a
BlockGrid
instance with the appropriate number of rows and columns, and we set each block's color to the corresponding pixel's color in the image (multiplying by255
to convert from a floating-point number in [0, 1] into an 8-bit integer). We use a small block size, and we remove the lines between the blocks as follows:>>> height, width = img.shape[:2] grid = BlockGrid(width=width, height=height, block_size=2, lines_on=False) for block, r, g, b in zip(grid, *rgb): block.rgb = (r * 255, g * 255, b * 255) grid.show()
推薦閱讀
- Mastering Ninject for Dependency Injection
- Learning Spring Boot
- 新型數據庫系統:原理、架構與實踐
- 數據庫系統原理及應用教程(第4版)
- Spark核心技術與高級應用
- 3D計算機視覺:原理、算法及應用
- Remote Usability Testing
- Hands-On Mathematics for Deep Learning
- Oracle PL/SQL實例精解(原書第5版)
- 金融商業算法建模:基于Python和SAS
- 深入理解InfluxDB:時序數據庫詳解與實踐
- Visual FoxPro數據庫技術基礎
- 智能與數據重構世界
- Unity for Architectural Visualization
- 數據庫基礎與應用