adding a simpler solution to 'sum over last two axes' exercise'
This commit is contained in:
@@ -693,6 +693,11 @@ print(np.unique(I))
|
||||
|
||||
```python
|
||||
A = np.random.randint(0,10,(3,4,3,4))
|
||||
# solution by passing a tuple of axes
|
||||
sum = A.sum(axis=(-2,-1))
|
||||
print(sum)
|
||||
# solution by flattening the last two dimensions into one
|
||||
# (useful for functions that don't accept tuples for axis argument)
|
||||
sum = A.reshape(A.shape[:-2] + (-1,)).sum(axis=-1)
|
||||
print(sum)
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user