Download Notebook (.ipynb)
R_010 — NumPy Basics (Reproducible)
Purpose: reproduce key NumPy operations from the note.
Outputs: small arrays + sanity prints (shape/dtype) + simple plot.
import numpy as np
import matplotlib.pyplot as plt
print("numpy:", np.__version__)numpy: 2.2.6
a = np.array([1, 2, 3])
b = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64)
print("a:", a, "shape=", a.shape, "dtype=", a.dtype)
print("b:\n", b, "\nshape=", b.shape, "dtype=", b.dtype)
zeros = np.zeros((2, 3))
ones = np.ones((2, 3))
ar = np.arange(0, 10, 2)
print("zeros:\n", zeros)
print("ones:\n", ones)
print("arange:", ar)a: [1 2 3] shape= (3,) dtype= int64 b: [[1. 2. 3.] [4. 5. 6.]] shape= (2, 3) dtype= float64 zeros: [[0. 0. 0.] [0. 0. 0.]] ones: [[1. 1. 1.] [1. 1. 1.]] arange: [0 2 4 6 8]
x = np.arange(1, 13).reshape(3, 4)
print("x:\n", x)
print("x[0, :]:", x[0, :])
print("x[:, 2]:", x[:, 2])
print("x[1:, 1:3]:\n", x[1:, 1:3])x: [[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]] x[0, :]: [1 2 3 4] x[:, 2]: [ 3 7 11] x[1:, 1:3]: [[ 6 7] [10 11]]
y = np.arange(1, 7)
y2 = y.reshape(2, 3)
yt = y2.T
print("y:", y)
print("y2:\n", y2)
print("y2.T:\n", yt)y: [1 2 3 4 5 6] y2: [[1 2 3] [4 5 6]] y2.T: [[1 4] [2 5] [3 6]]
m = np.arange(1, 13).reshape(3, 4)
v = np.array([1, 10, 100, 1000])
out = m + v
print("m:\n", m)
print("v:", v)
print("m + v:\n", out)m: [[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]] v: [ 1 10 100 1000] m + v: [[ 2 12 103 1004] [ 6 16 107 1008] [ 10 20 111 1012]]
t = np.linspace(0, 2*np.pi, 200)
s = np.sin(t)
plt.figure()
plt.plot(t, s)
plt.title("NumPy + Matplotlib sanity")
plt.xlabel("t")
plt.ylabel("sin(t)")
plt.show()