Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

import numpy as np

Numerieke primitieven

Scalairen

a=5a = 5
a = np.array(5)
print(a)
print(a.dtype)
print(a.shape)
print(a.__class__.__name__)
5
int64
()
ndarray
a=3.0a = 3.0
a = np.array(3.0)
print(a)
print(a.dtype)
3.0
float64

Vectoren

a=[5812]\pmb{a} = \begin{bmatrix} 5 \cr 8 \cr 12 \end{bmatrix}
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):

aT=[5812]\pmb{a}^T = \begin{bmatrix} 5 & 8 & 12 \end{bmatrix}
a.reshape(1, 3)
array([[ 5, 8, 12]])

Transpositie terug naar kolom-vector:

a.reshape(1, 3).T
array([[ 5], [ 8], [12]])

Matrices

B=[1.02.03.07.01.02.0]\pmb{B} = \begin{bmatrix} 1.0 & 2.0 & 3.0 \cr 7.0 & 1.0 & 2.0 \end{bmatrix}
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)
BT\pmb{B}^T
print(b.T)
[[1. 7.]
 [2. 1.]
 [3. 2.]]

Tensors

C=[[1.02.03.07.01.02.0],[5.01.03.02.09.06.0]]\pmb{C} = \begin{bmatrix} \begin{bmatrix} 1.0 & 2.0 & 3.0 \cr 7.0 & 1.0 & 2.0 \end{bmatrix}, \cr \begin{bmatrix} 5.0 & 1.0 & 3.0 \cr 2.0 & 9.0 & 6.0 \end{bmatrix} \end{bmatrix}
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]]]]

Element-gewijse operaties

a=[5812]\pmb{a} = \begin{bmatrix} 5 \cr 8 \cr 12 \end{bmatrix}
a+5\pmb{a} + 5
print(f"a: {a}")
print(f"a+5: {a + 5}")
a: [ 5  8 12]
a+5: [10 13 17]
a×5\pmb{a} \times 5
print(f"a x 5: {a * 5}")
a x 5: [25 40 60]
b=[123]\pmb{b} = \begin{bmatrix} 1 \cr 2 \cr 3 \end{bmatrix}
a+b\pmb{a} + \pmb{b}
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,) 
ab\pmb{a} \odot \pmb{b}
b = np.array([1, 2, 3])
print(f"a * b: {a * b}")
a * b: [ 5 16 36]
A=[123456]\pmb{A} = \begin{bmatrix} 1 & 2 \cr 3 & 4 \cr 5 & 6 \end{bmatrix}
A+5\pmb{A}+5
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]]
A×5\pmb{A} \times 5
print(f"A x 5: {A * 5}")
A x 5: [[ 5 10]
 [15 20]
 [25 30]]
B=[533190]\pmb{B} = \begin{bmatrix} 5 & 3 \cr 3 & 1 \cr 9 & 0 \end{bmatrix}
A+B\pmb{A}+\pmb{B}
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]]
AB\pmb{A} \odot \pmb{B}
print(f"A * B: {A * B}")
A * B: [[ 5  6]
 [ 9  4]
 [45  0]]
AT+b\pmb{A}^T + \pmb{b}
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]]
ATb\pmb{A}^T \odot \pmb{b}
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) 

Dot product

a=[732]Tb=[281]TaTb=\begin{aligned} \pmb{a} &= \begin{bmatrix}7 & 3 & 2\end{bmatrix}^T \cr \pmb{b} &= \begin{bmatrix}2 & 8 & 1\end{bmatrix}^T \cr \pmb{a}^T\pmb{b} &= \ldots \end{aligned}
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=[4.61.15.38.210.1]Tb=[6.11.53.57.92.8]TaTb=\begin{aligned} \pmb{a} &= \begin{bmatrix}4.6 & 1.1 & 5.3 & 8.2 & 10.1\end{bmatrix}^T \cr \pmb{b} &= \begin{bmatrix}6.1 & 1.5 & 3.5 & 7.9 & 2.8\end{bmatrix}^T \cr \pmb{a}^T\pmb{b} &= \ldots \end{aligned}
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=[6127]Tb=[221]TaTb=\begin{aligned} \pmb{a} &= \begin{bmatrix}6 & 1 & 2 & 7\end{bmatrix}^T \cr \pmb{b} &= \begin{bmatrix}2 & 2 & 1\end{bmatrix}^T \cr \pmb{a}^T\pmb{b} &= \ldots \end{aligned}
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=[332714759283]b=[4281]TAb=\begin{aligned} \pmb{A} &= \begin{bmatrix} 3 & 3 & 2 & 7 \cr 1 & 4 & 7 & 5 \cr 9 & 2 & 8 & 3 \end{bmatrix} \cr \pmb{b} &= \begin{bmatrix}4 & 2 & 8 & 1\end{bmatrix}^T \cr \pmb{A}\pmb{b} &= \ldots \end{aligned}
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=[6.13.71.04.35.13.33.93.77.98.2]b=[3.02.2]TAb=\begin{aligned} \pmb{A} &= \begin{bmatrix} 6.1 & 3.7 \cr 1.0 & 4.3 \cr 5.1 & 3.3 \cr 3.9 & 3.7 \cr 7.9 & 8.2 \cr \end{bmatrix} \cr \pmb{b} &= \begin{bmatrix}3.0 & 2.2\end{bmatrix}^T \cr \pmb{A}\pmb{b} &= \ldots \end{aligned}
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=[332714759283]B=[62172275]AB=\begin{aligned} \pmb{A} &= \begin{bmatrix} 3 & 3 & 2 & 7 \cr 1 & 4 & 7 & 5 \cr 9 & 2 & 8 & 3 \end{bmatrix} \cr \pmb{B} &= \begin{bmatrix} 6 & 2 \cr 1 & 7 \cr 2 & 2 \cr 7 & 5 \end{bmatrix} \cr \pmb{A}\pmb{B} &= \ldots \end{aligned}
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=[53292]Ta2=\begin{aligned} \pmb{a} &= \begin{bmatrix}5 & 3 & 2 & 9 & 2\end{bmatrix}^T \cr ||\pmb{a}||_2 &= \ldots \end{aligned}
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=[4.61.15.38.210.1]Tb=[6.11.53.57.92.8]T\begin{aligned} \pmb{a} &= \begin{bmatrix}4.6 & 1.1 & 5.3 & 8.2 & 10.1\end{bmatrix}^T \cr \pmb{b} &= \begin{bmatrix}6.1 & 1.5 & 3.5 & 7.9 & 2.8\end{bmatrix}^T \cr \end{aligned}
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

Speciale matrices

Maak een diagonale matrix:

diag(3.3,6.0,7.1,5.4)diag(3.3, 6.0, 7.1, 5.4)
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:

I67\pmb{I}_{67}
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:

[3327147585529283]\begin{bmatrix} 3 & 3 & 2 & 7 \cr 1 & 4 & 7 & 5 \cr 8 & 5 & 5 & 2 \cr 9 & 2 & 8 & 3 \end{bmatrix}
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]])

Ga na of deze matrix (a) een orthogonale matrix is en (b) een rotatiematrix is. $$ \pmb{A} =

[0.707106780.707106780.00.00.707106780.707106780.00.00.00.01.00.00.00.00.01.0]\begin{bmatrix} 0.70710678 & -0.70710678 & 0.0 & 0.0 \cr 0.70710678 & 0.70710678 & 0.0 & 0.0 \cr 0.0 & 0.0 & 1.0 & 0.0 \cr 0.0 & 0.0 & 0.0 & 1.0 \end{bmatrix}

$$

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:

14=2β1+β2+3β318=β1+4β2+2β312=3β1+2β2+β3\begin{align} 14 &= 2\beta_1 + \beta_2 + 3\beta_3 \cr 18 &= \beta_1 + 4\beta_2 + 2\beta_3 \cr 12 &= 3\beta_1 + 2\beta_2 + \beta_3 \cr \end{align}

wat is de oplossing voor β1\beta_1, β2\beta_2 en β3\beta_3?

  1. Matrix notatie

y=[141812],X=[213142321],b=[β1β2β3]\mathbf{y} = \begin{bmatrix} 14 \\ 18 \\ 12 \end{bmatrix}, \quad \mathbf{X} = \begin{bmatrix} 2 & 1 & 3 \\ 1 & 4 & 2 \\ 3 & 2 & 1 \end{bmatrix}, \quad \mathbf{b} = \begin{bmatrix} \beta_1 \\ \beta_2 \\ \beta_3 \end{bmatrix}
y = np.array([14, 18, 12])
X = np.array([[2, 1, 3], [1, 4, 2], [3, 2, 1]])
  1. Inverse van X\pmb{X}

X1=1det(X)[(x22x33x23x32)(x12x33x13x32)(x12x23x13x22)(x21x33x23x31)(x11x33x13x31)(x11x23x13x21)(x21x32x22x31)(x11x32x12x31)(x11x22x12x21)]\mathbf{X}^{-1} = \frac{1}{det(\pmb{X})} \begin{bmatrix} (x_{22}x_{33} - x_{23}x_{32}) & -(x_{12}x_{33} - x_{13}x_{32}) & (x_{12}x_{23} - x_{13}x_{22}) \cr -(x_{21}x_{33} - x_{23}x_{31}) & (x_{11}x_{33} - x_{13}x_{31}) & -(x_{11}x_{23} - x_{13}x_{21}) \cr (x_{21}x_{32} - x_{22}x_{31}) & -(x_{11}x_{32} - x_{12}x_{31}) & (x_{11}x_{22} - x_{12}x_{21}) \end{bmatrix}
det(X)=2(4122)1(1123)+3(1243)=2(44)1(16)+3(212)=0+530=25\begin{align} \det(\mathbf{X}) &= 2(4 \cdot 1 - 2 \cdot 2) - 1(1 \cdot 1 - 2 \cdot 3) + 3(1 \cdot 2 - 4 \cdot 3) \cr &= 2(4 - 4) - 1(1 - 6) + 3(2 - 12) = 0 + 5 - 30 = -25 \end{align}
X1=125[05105711017]=[00.20.40.20.280.040.40.040.28]\begin{align} \mathbf{X}^{-1} &= \frac{1}{-25} \begin{bmatrix} 0 & 5 & -10 \\ 5 & -7 & -1 \\ -10 & -1 & 7 \end{bmatrix} \cr &= \begin{bmatrix} 0 & -0.2 & 0.4 \\ -0.2 & 0.28 & 0.04 \\ 0.4 & 0.04 & -0.28 \end{bmatrix} \end{align}
# 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]]
  1. Oplossing

b=X1y\pmb{b} = \pmb{X}^{-1}\pmb{y}
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.]