For the random part, one is expected to run a command like
rng = np.random.default_rng(12)
before anything, to call the generator rng
.
Code | Result |
---|---|
x = np.zeros(9) |
|
x = np.ones(9) |
|
x = np.full(9, 0.5) |
|
x = np.array([0, 0, 1, 0, 0, 0, 0, 0, 0]) |
|
x = np.arange(9) |
|
x = rng.random(9) |
Code | Result |
---|---|
T = np.zeros((3, 5, 9)) |
|
T = np.ones((3, 5, 9)) |
|
T = np.arange(3 * 5 * 9).reshape(3, 5, 9) |
|
T = rng.random((3, rows, cols)) |
We start here with
M = np.zeros((3, 4))
M[2, 2] = 1
Code | Result |
---|---|
M = M.reshape(4, 3) |
|
M = M.reshape(12, 1) |
|
M = M.reshape(1, 12) |
|
M = M.reshape(6, 2) |
|
M = M.reshape(2, 6) |
Start from a zero matrix:
M = np.zeros((5, 9))
Code | Result |
---|---|
M[...] = 1 |
|
M[:, ::2] = 1 |
|
M[::2, :] = 1 |
|
M[1, 1] = 1 |
|
M[:, 0] = 1 |
|
M[0, :] = 1 |
|
M[2:, 2:] = 1 |
|
M[:-2, :-2] = 1 |
|
M[2:4, 2:4] = 1 |
|
M[::2, ::2] = 1 |
|
M[3::2, 3::2] = 1 |
- This work is deeply inspired and adapted from the great work by Nicolas Rougier: https://github.com/rougier/numpy-tutorial