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/numpy.rst
+41-11Lines changed: 41 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -191,14 +191,32 @@ Exercises 2
191
191
192
192
.. challenge:: Exercises: Numpy-2
193
193
194
-
- **Matrix multiplication** What is the difference between :data:`numpy.multiply` and :func:`numpy.dot` ? Try it.
195
-
- **Axis** What is the difference between :func:`np.sum(axis=1) <numpy.sum>` vs
196
-
:func:`np.sum(axis=0) <numpy.sum>` on a two-dimensional array? What if you leave out the axis parameter?
194
+
Create the following arrays:
195
+
196
+
.. code-block:: python
197
+
198
+
# 1-dimensional arrays
199
+
x = np.array([1, 10, 100])
200
+
#TODO: similar to `x`, create another 1-dimensional array with shape (3,)
201
+
# y = np.array(...)
202
+
203
+
# 2-dimensional arrays
204
+
a = np.array([[1, 2], [3, 4]])
205
+
#TODO: similar to `a`, create another 2-dimensional array with shape (2, 2)
206
+
# b = np.array(...)
207
+
208
+
- **Matrix multiplication** What is the difference between :data:`numpy.multiply` and :func:`numpy.dot` ? Try calling these functions
209
+
with either ``x, y`` (1D arrays) or ``a, b`` (2D arrays) as input and observe the behaviour.
210
+
- **Axis** What is the difference between :func:`np.sum(a, axis=1) <numpy.sum>` vs
211
+
:func:`np.sum(a, axis=0) <numpy.sum>` on a two-dimensional array? What if you leave out the axis parameter?
197
212
198
213
199
214
.. solution:: Solutions: Numpy-2
200
215
201
-
- **Matrix multiplication** ``np.multiply`` does elementwise multiplication on two arrays, while ``np.dot`` enables matrix multiplication.
216
+
- **Matrix multiplication** ``np.multiply`` does elementwise multiplication on two arrays. The function ``np.dot`` enables:
217
+
- *dot product* and returns a scalar, when both input arrays are 1 dimensional
218
+
- *matrix multiplication* and returns back a 2-dimensional array, when both the input arrays are 2 dimensional
219
+
However, ``a @ b`` is preferred over ``np.dot(a, b)`` to express matrix multiplication.
202
220
- **Axis** ``axis=1`` does the operation (here: ``np.sum``) over each row, while axis=0 does it over each column. If axis is left out, the sum of the full array is given.
203
221
204
222
@@ -248,18 +266,30 @@ Exercises 3
248
266
::
249
267
250
268
a = np.eye(4)
251
-
b = a[:,0]
269
+
b = a[:,0]
252
270
b[0] = 5
253
271
254
-
- **View vs copy** Try out above code. How does ``a`` look like before ``b`` has changed and after? How could it be avoided?
272
+
Try out the above code.
273
+
274
+
- How does ``a`` look like before ``b`` has changed and after?
275
+
- How could it be avoided?
255
276
256
277
.. solution:: Solution: Numpy-3
257
278
258
-
- **View vs copy** The change in ``b`` has also changed the array ``a``!
259
-
This is because ``b`` is merely a view of a part of array ``a``. Both
260
-
variables point to the same memory. Hence, if one is changed, the other
261
-
one also changes. If you need to keep the original array as is, use
262
-
``np.copy(a)``.
279
+
**View vs copy**: The change in ``b`` has also changed the array ``a``!
280
+
This is because ``b`` is merely a *view* or a *shallow copy* of a part of array ``a``. Both
281
+
variables point to the same memory. Hence, if one is changed, the other
282
+
one also changes.
283
+
284
+
In this example, if you need to keep the original array as is, use
285
+
:func:`np.copy(a) <numpy.copy>` or ``np.copy(a[:, 0])``
286
+
to create a new *deep copy* of the whole or a slice of array respectively,
287
+
before updating ``b``.
288
+
289
+
.. seealso::
290
+
291
+
NumPy's documentation on :numpy:ref:`quickstart.copies-and-views`
0 commit comments