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
+22-4Lines changed: 22 additions & 4 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.
0 commit comments