Read arrays and explain them with plots
English translation
Create your own copy to run, edit, and record results.
- Name: ⟦Enter here⟧
- Student ID: ⟦Enter here⟧
- Run Code Cells: ▶ or
Shift+Enter, in order from the top. - Edit a Text Cell: double-click → write
your response→Shift+Enter. - Response cell: enter your own code.
- After changing values: rerun that cell and the relevant cells below it.
1. Array shapes
First check shape and element count using an array of 12 elements.
A. Baseline example · Read and run first
import numpy as np
import matplotlib.pyplot as plt
a = np.arange(1,13).reshape(3,4)
print(a, '\nshape:',a.shape,'ndim:',a.ndim,'size:',a.size)
print('second row:',a[1,:])
print('mask:',a>7,'selected:',a[a>7])
B. Hands-on activities
- Create a NumPy Array from and print its shape, dtype, mean, values at least 80, and their count.
- Arrange 1~20 into (4,5), then print the first row, last column, middle 2-row×3-column region, and even values separately.
Your prediction may describe a shape or an increasing/decreasing trend rather than an exact number. Use the example above to write code; answer explanation-only items in the Text Cell below.
# ✍ Write your own: add your code below this cell.
# You may add Code Cells if the code gets long.
C. Your record · Double-click this Text Cell to edit
- Conditions checked or changed: ⟦Enter your response⟧
- Prediction before execution: ⟦Enter your response⟧
- Value checked in the actual output/plot: ⟦Enter your response⟧
- Difference from prediction and explanation: ⟦Enter your response⟧
2. Operations and verification
Compute and compare A*B, which multiplies matching elements, and A@B, which sums row-column products. Then compute A's Inverse and check whether A@Inverse is the Identity.
A. Baseline example · Read and run first
A=np.array([[2.,1.],[1.,3.]])
B=np.array([[1.,0.],[2.,1.]])
print('elementwise:',A*B,'matrix:',A@B)
print('inverse:',np.linalg.inv(A))
assert np.allclose(A@np.linalg.inv(A),np.eye(2))
values=np.array([3.,7.,5.]);print('max:',values.max(),'index:',values.argmax())
B. Hands-on activities
- For and , compute the Element-wise Product and Matrix Multiplication and explain how element (0,1) is obtained.
- Compute A's Determinant and Inverse and use
np.allcloseto verify thatA@Inverseis the Identity. - Change
A[0,0]to 4 and repeat.
Your prediction may describe a shape or an increasing/decreasing trend rather than an exact number. Use the example above to write code; answer explanation-only items in the Text Cell below.
# ✍ Write your own: add your code below this cell.
# You may add Code Cells if the code gets long.
C. Your record · Double-click this Text Cell to edit
- Conditions checked or changed: ⟦Enter your response⟧
- Prediction before execution: ⟦Enter your response⟧
- Value checked in the actual output/plot: ⟦Enter your response⟧
- Difference from prediction and explanation: ⟦Enter your response⟧
3. Plot the results
Compute the maximum's position and mark it on the plot.
A. Baseline example · Read and run first
x=np.linspace(0,2*np.pi,100)
y=np.sin(x)
fig,ax=plt.subplots();ax.plot(x,y,label='sin(x)');i=y.argmax()
ax.annotate('sample maximum',(x[i],y[i]),xytext=(2,0.5),arrowprops={'arrowstyle':'->'})
ax.set(xlabel='x (rad)',ylabel='value',title='Sampled sine');ax.grid();ax.legend();plt.show()
B. Hands-on activities
- Create x with 100 samples from 0~2π and plot sin and cos together.
- Then use , , and to display sin, cos, Loss, and Accuracy in a 2×2 Figure.
- Label axes and add a Legend, locate the maximum Accuracy in code, and mark it with an annotation.
Your prediction may describe a shape or an increasing/decreasing trend rather than an exact number. Use the example above to write code; answer explanation-only items in the Text Cell below.
# ✍ Write your own: add your code below this cell.
# You may add Code Cells if the code gets long.
C. Your record · Double-click this Text Cell to edit
- Conditions checked or changed: ⟦Enter your response⟧
- Prediction before execution: ⟦Enter your response⟧
- Value checked in the actual output/plot: ⟦Enter your response⟧
- Difference from prediction and explanation: ⟦Enter your response⟧
Practice through repetition · Reason before running
Summarize the three conditions compared in the preceding activities in this table. You do not need to repeat experiments already completed. If comparisons are insufficient, supplement them by changing just one condition.
| Changed condition | Predicted shape/value/result | Actual result | Cause of any error |
|---|---|---|---|
| ⟦Enter your response 1⟧ | ⟦Enter your response⟧ | ⟦Enter your response⟧ | ⟦Enter your response⟧ |
| ⟦Enter your response 2⟧ | ⟦Enter your response⟧ | ⟦Enter your response⟧ | ⟦Enter your response⟧ |
| ⟦Enter your response 3⟧ | ⟦Enter your response⟧ | ⟦Enter your response⟧ | ⟦Enter your response⟧ |
# ✍ Code for repeated experiments
Optional exercise · Find the cause of an error and fix the code
reshape with an incompatible element count
After importing NumPy, add a Code Cell at the bottom of the notebook and run the following code.
debug_values = np.arange(12) for debug_shape in [(5, 3), (3, 4)]: print("requested shape:", debug_shape, "elements:", debug_values.size) try: print(debug_values.reshape(debug_shape)) except ValueError as error: print(type(error).__name__, str(error))In a Text Cell, tabulate the required element count and execution success for each requested shape. Also explain in one sentence the order in which values from 0 to 11 are arranged in the successful array.
Check before saving
- Check the response fields, outputs, and plots, then run all cells in order in a fresh runtime.
- Save to your Drive, then download using
.ipynb.
Include code, actual outputs and plots, and your own interpretation. Submit through KLAS; check the semester schedule and latest announcements for the deadline.
Filename: Name_StudentID_Week3.ipynb.