Array Shape Visualizer
Run the Array Shape Visualizer MicroSim Fullscreen
Edit in the p5.js Editor
About This MicroSim
The shape of a NumPy array says how many elements lie along each axis. This MicroSim starts with the flat array a = np.arange(n) and shows what b = a.reshape(...) produces for the shape you type.
- One size, such as
12, gives a row. Two sizes, such as3, 4, give a table with 3 rows and 4 columns. Three sizes, such as2, 2, 3, give a stack of 2 tables. - The panel reports
b.shape,b.ndim(the number of axes) andb.size(the number of elements). - Brackets under the flat array show how it is cut into runs. Each run fills the last axis of
b, so the numbers keep their order. - One element has a red frame in both views. The panel gives its index in
band the arithmetic behind it, for example \(7 = 1 \times 4 + 3\) forb[1, 3].
A reshape only works when the sizes multiply to a.size. One size may be -1, which tells NumPy to work that size out. When the request is impossible the MicroSim shows the message NumPy raises, for example ValueError: cannot reshape array of size 12 into shape (5,3), and draws the requested grid with its empty slots so you can see the mismatch.
Not included: the rotating 3-D view and the animated transition in the original specification. A stack of tables is drawn side by side, which is how NumPy prints a 3-D array.
How to Use
- Look at the default,
a.reshape(3, 4). Match each bracket under the flat array to a row of the table. - Type a new shape in the a.reshape( ) box, for example
4, 3,2, 6or2, 2, 3. Separate the sizes with commas. - Click any cell to follow that element. Read where it lands and check the arithmetic.
- Try
4, -1and-1, 1, then read how NumPy worked out the missing size. - Try
5, 3and5, -1and read the error message. Count the empty slots. - Drag Elements in a to 7 or 11 and find every shape that still works. The Examples menu jumps to cases from the chapter.
Iframe Embed Code
You can add this MicroSim to any web page by adding this to your HTML:
1 2 3 4 | |
Lesson Plan
Grade Level
High School (11-12) and College Freshman
Duration
10-15 minutes
Prerequisites
Creating arrays with np.arange, and multiplying whole numbers to find factor pairs.
Bloom's Taxonomy Level
Apply (L3)
Learning Objective
Students will be able to use reshape to rearrange an array into a given number of dimensions, predict the resulting shape, ndim and size, and determine whether a requested shape is possible.
Activities
- Predict (3 min): Students list every 2-D shape they think 12 numbers can take, then test each one in the reshape box.
- Follow an element (5 min): Students pick a position such as 10 and predict its index in shapes (3, 4), (4, 3) and (2, 2, 3) before clicking it to check.
- Break it (5 min): Students find three requests that fail, copy each error message, and explain in a sentence what went wrong. They then set the slider to 7 and explain why so few shapes work.
Assessment
Without the MicroSim, students state the result of np.arange(24).reshape(4, -1).shape, give the index of the value 17 in that array, and say whether np.arange(24).reshape(5, -1) works and why.
References
- NumPy documentation. numpy.reshape.
- NumPy documentation. NumPy: the absolute basics for beginners.
- Wikipedia. Row- and column-major order.