Skip to content

Commit 5b73b47

Browse files
committed
Create starting point for numpy multiply, dot exercise. Clarify sum function calls
1 parent 37f16c9 commit 5b73b47

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

content/numpy.rst

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,32 @@ Exercises 2
191191

192192
.. challenge:: Exercises: Numpy-2
193193

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?
197212

198213

199214
.. solution:: Solutions: Numpy-2
200215

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.
202220
- **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.
203221

204222

0 commit comments

Comments
 (0)