Skip to content

Commit e519e60

Browse files
committed
MAINT: Access linalg from the np namespace.
1 parent bdf94bf commit e519e60

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

content/tutorial-svd.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,6 @@ It is possible to use methods from linear algebra to approximate an existing set
142142

143143
+++
144144

145-
To proceed, import the linear algebra submodule from NumPy:
146-
147-
```{code-cell}
148-
from numpy import linalg
149-
```
150-
151145
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:
152146

153147
$$U \Sigma V^T = A$$
@@ -183,7 +177,7 @@ plt.show()
183177
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:
184178

185179
```{code-cell}
186-
U, s, Vt = linalg.svd(img_gray)
180+
U, s, Vt = np.linalg.svd(img_gray)
187181
```
188182

189183
**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
221215
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
222216

223217
```{code-cell}
224-
linalg.norm(img_gray - U @ Sigma @ Vt)
218+
np.linalg.norm(img_gray - U @ Sigma @ Vt)
225219
```
226220

227221
(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
295289
Now we are ready to apply the SVD:
296290

297291
```{code-cell}
298-
U, s, Vt = linalg.svd(img_array_transposed)
292+
U, s, Vt = np.linalg.svd(img_array_transposed)
299293
```
300294

301295
Finally, to obtain the full approximated image, we need to reassemble these matrices into the approximation. Now, note that

0 commit comments

Comments
 (0)