import numpy as npa = np.array(5)
print(a)
print(a.dtype)
print(a.shape)
print(a.__class__.__name__)5
int64
()
ndarray
a = np.array(3.0)
print(a)
print(a.dtype)3.0
float64
a = np.array([5, 8, 12])
print(a)
print(a.shape)[ 5 8 12]
(3,)
Representatie als een kolom-vector (een matrix met 1 kolom):
a.reshape(3, 1)array([[ 5],
[ 8],
[12]])Representatie als een rij-vector (een matrix met 1 rij):
a.reshape(1, 3)array([[ 5, 8, 12]])Transpositie terug naar kolom-vector:
a.reshape(1, 3).Tarray([[ 5],
[ 8],
[12]])b = np.array([[1.0, 2.0, 3.0], [7.0, 1.0, 2.0]])
print(f"b: {b}")
print(f"b shape: {b.shape}")b: [[1. 2. 3.]
[7. 1. 2.]]
b shape: (2, 3)
print(b.T)[[1. 7.]
[2. 1.]
[3. 2.]]
c = np.array([[[1.0, 2.0, 3.0], [7.0, 1.0, 2.0]], [[5.0, 1.0, 3.0], [2.0, 9.0, 6.0]]])
print(f"c: {c}")
print(f"c shape: {c.shape}")c: [[[1. 2. 3.]
[7. 1. 2.]]
[[5. 1. 3.]
[2. 9. 6.]]]
c shape: (2, 2, 3)
# Reshape the 3D array
c.reshape(3, 2, 2)array([[[1., 2.],
[3., 7.]],
[[1., 2.],
[5., 1.]],
[[3., 2.],
[9., 6.]]])# 4D tensor with random values
rng = np.random.default_rng(12345)
d = rng.random((5, 3, 2, 3))
print(d)[[[[0.22733602 0.31675834 0.79736546]
[0.67625467 0.39110955 0.33281393]]
[[0.59830875 0.18673419 0.67275604]
[0.94180287 0.24824571 0.94888115]]
[[0.66723745 0.09589794 0.44183967]
[0.88647992 0.6974535 0.32647286]]]
[[[0.73392816 0.22013496 0.08159457]
[0.1598956 0.34010018 0.46519315]]
[[0.26642103 0.8157764 0.19329439]
[0.12946908 0.09166475 0.59856801]]
[[0.8547419 0.60162124 0.93198836]
[0.72478136 0.86055132 0.9293378 ]]]
[[[0.54618601 0.93767296 0.49498794]
[0.27377318 0.45177871 0.66503892]]
[[0.33089093 0.90345401 0.25707418]
[0.33982834 0.2588534 0.35544648]]
[[0.00502233 0.62860454 0.28238271]
[0.06808769 0.61682898 0.17632632]]]
[[[0.30438839 0.44088681 0.15020234]
[0.21792886 0.47433312 0.47636886]]
[[0.25523235 0.29756527 0.27906712]
[0.26057921 0.48276159 0.21197904]]
[[0.4956306 0.24626133 0.83848265]
[0.18013059 0.86215629 0.17829944]]]
[[[0.75053133 0.6111204 0.20915503]
[0.75987242 0.24926057 0.08557173]]
[[0.61805672 0.53696833 0.63452671]
[0.17437411 0.24816449 0.68482298]]
[[0.08087165 0.8750736 0.42869438]
[0.6183942 0.3131055 0.17896286]]]]
print(f"a: {a}")
print(f"a+5: {a + 5}")a: [ 5 8 12]
a+5: [10 13 17]
print(f"a x 5: {a * 5}")a x 5: [25 40 60]
b = np.array([1, 2, 3])
print(f"a + b: {a + b}")a + b: [ 6 10 15]
Let op voor dimensies!
b = np.array([1, 2, 3, 4])
print(f"a + b: {a + b}")---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[16], line 2
1 b = np.array([1, 2, 3, 4])
----> 2 print(f"a + b: {a + b}")
ValueError: operands could not be broadcast together with shapes (3,) (4,) b = np.array([1, 2, 3])
print(f"a * b: {a * b}")a * b: [ 5 16 36]
A = np.array([[1, 2], [3, 4], [5, 6]])
print(f"A: {A}")
print(f"A + 5: {A + 5}")A: [[1 2]
[3 4]
[5 6]]
A + 5: [[ 6 7]
[ 8 9]
[10 11]]
print(f"A x 5: {A * 5}")A x 5: [[ 5 10]
[15 20]
[25 30]]
B = np.array([[5, 3], [3, 1], [9, 0]])
print(f"A: {A}")
print(f"B: {B}")
print(f"A + B: {A + B}")A: [[1 2]
[3 4]
[5 6]]
B: [[5 3]
[3 1]
[9 0]]
A + B: [[ 6 5]
[ 6 5]
[14 6]]
print(f"A * B: {A * B}")A * B: [[ 5 6]
[ 9 4]
[45 0]]
print(f"A: {A}")
print(f"b: {b}")A: [[1 2]
[3 4]
[5 6]]
b: [1 2 3]
print(A + b)---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[23], line 1
----> 1 print(A + b)
ValueError: operands could not be broadcast together with shapes (3,2) (3,) print(A.T + b)[[2 5 8]
[3 6 9]]
print(A.T * b)[[ 1 6 15]
[ 2 8 18]]
# Subtract two 4D tensors
d = rng.random((5, 3, 2, 3))
g = rng.random((5, 3, 2, 3))
print(f"(d - g).shape: {(d - g).shape}")(d - g).shape: (5, 3, 2, 3)
h = g.reshape((3, 3, 2, 5))
j = g.reshape((3, 3 * 2 * 5))print((d - h).shape)---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[28], line 1
----> 1 print((d - h).shape)
ValueError: operands could not be broadcast together with shapes (5,3,2,3) (3,3,2,5) print((d - j).shape)---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[29], line 1
----> 1 print((d - j).shape)
ValueError: operands could not be broadcast together with shapes (5,3,2,3) (3,30) a = np.array([7, 3, 2])
b = np.array([2, 8, 1])
print(f"a: {a}")
print(f"b: {b}")
print(f"a'b: {np.dot(a, b)}")a: [7 3 2]
b: [2 8 1]
a'b: 40
print(f"a'b: {7 * 2 + 3 * 8 + 2 * 1}")a'b: 40
a = np.array([4.6, 1.1, 5.3, 8.2, 10.1])
b = np.array([6.1, 1.5, 3.5, 7.9, 2.8])
print(f"a: {a}")
print(f"b: {b}")
print(f"a'b: {np.dot(a, b)}")a: [ 4.6 1.1 5.3 8.2 10.1]
b: [6.1 1.5 3.5 7.9 2.8]
a'b: 141.32
a = np.array([6, 1, 2, 7])
b = np.array([2, 2, 1])
print(f"a: {a}")
print(f"b: {b}")
print(f"a'b: {np.dot(a, b)}")a: [6 1 2 7]
b: [2 2 1]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[33], line 5
3 print(f"a: {a}")
4 print(f"b: {b}")
----> 5 print(f"a'b: {np.dot(a, b)}")
ValueError: shapes (4,) and (3,) not aligned: 4 (dim 0) != 3 (dim 0)A = np.array([[3, 3, 2, 7], [1, 4, 7, 5], [9, 2, 8, 3]])
b = np.array([4, 2, 8, 1])
print(f"A: {A}")
print(f"b: {b}")
print(f"Ab: {np.dot(A, b)}")A: [[3 3 2 7]
[1 4 7 5]
[9 2 8 3]]
b: [4 2 8 1]
Ab: [ 41 73 107]
# Mind the order!
print(f"bA: {np.dot(b, A)}")---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[35], line 2
1 # Mind the order!
----> 2 print(f"bA: {np.dot(b, A)}")
ValueError: shapes (4,) and (3,4) not aligned: 4 (dim 0) != 3 (dim 0)A = np.array([[6.1, 3.7], [1.0, 4.3], [5.1, 3.3], [3.9, 3.7], [7.9, 8.2]])
b = np.array([3.0, 2.2])
print(f"A: {A}")
print(f"b: {b}")
print(f"Ab: {np.dot(A, b)}")A: [[6.1 3.7]
[1. 4.3]
[5.1 3.3]
[3.9 3.7]
[7.9 8.2]]
b: [3. 2.2]
Ab: [26.44 12.46 22.56 19.84 41.74]
A = np.array([[3, 3, 2, 7], [1, 4, 7, 5], [9, 2, 8, 3]])
B = np.array(
[
[6, 2],
[1, 7],
[2, 2],
[7, 5],
]
)
print(f"A: {A}")
print(f"B: {B}")
print(f"AB: {np.dot(A, B)}")A: [[3 3 2 7]
[1 4 7 5]
[9 2 8 3]]
B: [[6 2]
[1 7]
[2 2]
[7 5]]
AB: [[74 66]
[59 69]
[93 63]]
# Mind the order!
print(f"BA: {np.dot(B, A)}")---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[38], line 2
1 # Mind the order!
----> 2 print(f"BA: {np.dot(B, A)}")
ValueError: shapes (4,2) and (3,4) not aligned: 2 (dim 1) != 3 (dim 0)Bereken het gemiddelde van de reeks 0 tot en met 113 aan de hand van een dot product.
n = 114
a = np.arange(n)
print(f"a: {a[:10]} ...")
w = np.ones((n,))
w /= n
print(f"w: {w[:10]} ...")
print(f"a'w: {np.dot(a, w)}")a: [0 1 2 3 4 5 6 7 8 9] ...
w: [0.00877193 0.00877193 0.00877193 0.00877193 0.00877193 0.00877193
0.00877193 0.00877193 0.00877193 0.00877193] ...
a'w: 56.5
a = np.array([5, 3, 2, 9, 2])
print(f"||a||: {np.sqrt(np.dot(a, a))}")||a||: 11.090536506409418
# Built-in L2 norm function
print(f"||a||: {np.linalg.norm(a)}")||a||: 11.090536506409418
Hoe groot is de cosine similarity tussen:
a = np.array([4.6, 1.1, 5.3, 8.2, 10.1])
b = np.array([6.1, 1.5, 3.5, 7.9, 2.8])
a_norm = np.linalg.norm(a)
b_norm = np.linalg.norm(b)
print(f"cos(theta): {np.dot(a, b) / (a_norm * b_norm)}")cos(theta): 0.8633164353920106
np.diag([3.3, 6.0, 7.1, 5.4])array([[3.3, 0. , 0. , 0. ],
[0. , 6. , 0. , 0. ],
[0. , 0. , 7.1, 0. ],
[0. , 0. , 0. , 5.4]])Maak een idenditeitsmatrix:
np.identity(67)array([[1., 0., 0., ..., 0., 0., 0.],
[0., 1., 0., ..., 0., 0., 0.],
[0., 0., 1., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 1., 0., 0.],
[0., 0., 0., ..., 0., 1., 0.],
[0., 0., 0., ..., 0., 0., 1.]], shape=(67, 67))Transformeer de volgende matrix in (a) een upper triangular matrix en (b) een lower triangular matrix:
X = np.array([[3, 3, 2, 7], [1, 4, 7, 5], [8, 5, 5, 2], [9, 2, 8, 3]])
np.triu(X)array([[3, 3, 2, 7],
[0, 4, 7, 5],
[0, 0, 5, 2],
[0, 0, 0, 3]])np.tril(X)array([[3, 0, 0, 0],
[1, 4, 0, 0],
[8, 5, 5, 0],
[9, 2, 8, 3]])X = np.array(
[
[0.70710678, -0.70710678, 0.0, 0.0],
[0.70710678, 0.70710678, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
]
)
# a) Check orthogonality
XTX = X.T @ X
ortho = np.all(np.round(XTX) == np.identity(X.shape[0]))
print(f"Orthogonality: {ortho}")
# b) Check rotation matrix
rot = np.linalg.det(X).round() == 1.0
print(f"Rotation matrix: {ortho and rot}")Orthogonality: True
Rotation matrix: True
Systeem van lineaire vergelijkingen¶
Gegeven het volgende stelsel:
wat is de oplossing voor , en ?
Matrix notatie
y = np.array([14, 18, 12])
X = np.array([[2, 1, 3], [1, 4, 2], [3, 2, 1]])# Determinant of X
det = np.linalg.det(X)
print(f"det(X) = {det.round()}")
# Inverse of X
inv = np.linalg.inv(X)
print(f"X-1 = {inv}")det(X) = -25.0
X-1 = [[ 0. -0.2 0.4 ]
[-0.2 0.28 0.04]
[ 0.4 0.04 -0.28]]
Oplossing
b = np.dot(inv, y)
print(f"b: {b}")
# Verification
print(f"y: {y}")
print(f"Xb: {np.dot(X, b)}")b: [1.2 2.72 2.96]
y: [14 18 12]
Xb: [14. 18. 12.]