You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/tutorial-svd.md
+3-9Lines changed: 3 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -142,12 +142,6 @@ It is possible to use methods from linear algebra to approximate an existing set
142
142
143
143
+++
144
144
145
-
To proceed, import the linear algebra submodule from NumPy:
146
-
147
-
```{code-cell}
148
-
from numpy import linalg
149
-
```
150
-
151
145
In order to extract information from a given matrix, we can use the SVD to obtain 3 arrays which can be multiplied to obtain the original matrix. From the theory of linear algebra, given a matrix $A$, the following product can be computed:
152
146
153
147
$$U \Sigma V^T = A$$
@@ -183,7 +177,7 @@ plt.show()
183
177
Now, applying the [linalg.svd](https://numpy.org/devdocs/reference/generated/numpy.linalg.svd.html#numpy.linalg.svd) function to this matrix, we obtain the following decomposition:
184
178
185
179
```{code-cell}
186
-
U, s, Vt = linalg.svd(img_gray)
180
+
U, s, Vt = np.linalg.svd(img_gray)
187
181
```
188
182
189
183
**Note** If you are using your own image, this command might take a while to run, depending on the size of your image and your hardware. Don't worry, this is normal! The SVD can be a pretty intensive computation.
@@ -221,7 +215,7 @@ Now, we want to check if the reconstructed `U @ Sigma @ Vt` is close to the orig
221
215
The [linalg](https://numpy.org/devdocs/reference/routines.linalg.html#module-numpy.linalg) module includes a `norm` function, which computes the norm of a vector or matrix represented in a NumPy array. For example, from the SVD explanation above, we would expect the norm of the difference between `img_gray` and the reconstructed SVD product to be small. As expected, you should see something like
222
216
223
217
```{code-cell}
224
-
linalg.norm(img_gray - U @ Sigma @ Vt)
218
+
np.linalg.norm(img_gray - U @ Sigma @ Vt)
225
219
```
226
220
227
221
(The actual result of this operation might be different depending on your architecture and linear algebra setup. Regardless, you should see a small number.)
@@ -295,7 +289,7 @@ img_array_transposed.shape
295
289
Now we are ready to apply the SVD:
296
290
297
291
```{code-cell}
298
-
U, s, Vt = linalg.svd(img_array_transposed)
292
+
U, s, Vt = np.linalg.svd(img_array_transposed)
299
293
```
300
294
301
295
Finally, to obtain the full approximated image, we need to reassemble these matrices into the approximation. Now, note that
0 commit comments