Merge pull request #23 from ibah/master

adding a simpler solution to 'sum over last two axes' exercise'
This commit is contained in:
Nicolas P. Rougier 2016-09-27 15:28:04 +02:00 committed by GitHub
commit 48025c7521
2 changed files with 10 additions and 0 deletions

View File

@ -1436,6 +1436,11 @@
"outputs": [],
"source": [
"A = np.random.randint(0,10,(3,4,3,4))\n",
"# solution by passing a tuple of axes\n",
"sum = A.sum(axis=(-2,-1))\n",
"print(sum)\n",
"# solution by flattening the last two dimensions into one\n",
"# (useful for functions that don't accept tuples for axis argument)\n",
"sum = A.reshape(A.shape[:-2] + (-1,)).sum(axis=-1)\n",
"print(sum)"
]

View File

@ -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)
```