Merge pull request #29 from ev-br/patch-1

mention `np.errstate` context manager in exercise 31.
This commit is contained in:
Nicolas P. Rougier 2016-10-30 06:59:41 +01:00 committed by GitHub
commit ac525959c9
2 changed files with 25 additions and 12 deletions

View File

@ -477,8 +477,8 @@
"outputs": [],
"source": [
"Z = np.dot(np.ones((5,3)), np.ones((3,2)))\n",
"print(Z)",
"\n\n",
"print(Z)\n",
"\n",
"# Alternative solution, in Python 3.5 and above\n",
"Z = np.ones((5,3)) @ np.ones((3,2))"
]
@ -628,10 +628,16 @@
"source": [
"# Suicide mode on\n",
"defaults = np.seterr(all=\"ignore\")\n",
"Z = np.ones(1)/0\n",
"Z = np.ones(1) / 0\n",
"\n",
"# Back to sanity\n",
"_ = np.seterr(**defaults)"
"_ = np.seterr(**defaults)\n",
"\n",
"An equivalent way, with a context manager:\n",
"\n",
"```python\n",
"with np.errstate(divide='ignore'):\n",
" Z = np.ones(1) / 0"
]
},
{
@ -1439,11 +1445,11 @@
"outputs": [],
"source": [
"A = np.random.randint(0,10,(3,4,3,4))\n",
"# solution by passing a tuple of axes (introduced in numpy 1.7.0)\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",
"# solution by passing a tuple of axes (introduced in numpy 1.7.0)\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)"
]
@ -1471,7 +1477,7 @@
"D_counts = np.bincount(S)\n",
"D_means = D_sums / D_counts\n",
"print(D_means)\n",
"\n",
"\n",
"# Pandas solution as a reference due to more intuitive code\n",
"import pandas as pd\n",
"print(pd.Series(D).groupby(S).mean())"
@ -2334,7 +2340,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
"version": "3.5.1"
}
},
"nbformat": 4,

View File

@ -282,12 +282,19 @@ print(np.intersect1d(Z1,Z2))
```python
# Suicide mode on
defaults = np.seterr(all="ignore")
Z = np.ones(1)/0
Z = np.ones(1) / 0
# Back to sanity
_ = np.seterr(**defaults)
```
An equivalent way, with a context manager:
```python
with np.errstate(divide='ignore'):
Z = np.ones(1) / 0
```
#### 32. Is the following expressions true? (★☆☆)