For the task B I have a question about the solution:
def with_indices(p, q):
rows, cols = np.indices((p.shape[0], q.shape[0]))
distances = np.sqrt(np.sum((p[rows.ravel(), :] - q[cols.ravel(), :])**2, axis=1))
return distances.reshape((p.shape[0], q.shape[0]))
Is it necessary to use slicing for p[rows.ravel(), :]? Could we not just use p[rows.ravel()]?
You get True, they are the same, you can drop the ":" as you want the rows, if you would want the column then you would need something like p[:,np.array([0,1])].
Exercise 1, Task B
Hi!
For the task B I have a question about the solution:
def with_indices(p, q):
rows, cols = np.indices((p.shape[0], q.shape[0]))
distances = np.sqrt(np.sum((p[rows.ravel(), :] - q[cols.ravel(), :])**2, axis=1))
return distances.reshape((p.shape[0], q.shape[0]))
Is it necessary to use slicing for p[rows.ravel(), :]? Could we not just use p[rows.ravel()]?
Thank you!
If you try:
You get
True
, they are the same, you can drop the ":" as you want the rows, if you would want the column then you would need something likep[:,np.array([0,1])]
.1
Add comment