Skip to content

Commit 8cbaacc

Browse files
committed
Tweak Ex 31 in the notebook too
1 parent 4cb1d8c commit 8cbaacc

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

100 Numpy exercises.ipynb

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,8 @@
477477
"outputs": [],
478478
"source": [
479479
"Z = np.dot(np.ones((5,3)), np.ones((3,2)))\n",
480-
"print(Z)",
481-
"\n\n",
480+
"print(Z)\n",
481+
"\n",
482482
"# Alternative solution, in Python 3.5 and above\n",
483483
"Z = np.ones((5,3)) @ np.ones((3,2))"
484484
]
@@ -628,10 +628,16 @@
628628
"source": [
629629
"# Suicide mode on\n",
630630
"defaults = np.seterr(all=\"ignore\")\n",
631-
"Z = np.ones(1)/0\n",
631+
"Z = np.ones(1) / 0\n",
632632
"\n",
633633
"# Back to sanity\n",
634-
"_ = np.seterr(**defaults)"
634+
"_ = np.seterr(**defaults)\n",
635+
"\n",
636+
"An equivalent way, with a context manager:\n",
637+
"\n",
638+
"```python\n",
639+
"with np.errstate(divide='ignore'):\n",
640+
" Z = np.ones(1) / 0"
635641
]
636642
},
637643
{
@@ -1439,11 +1445,11 @@
14391445
"outputs": [],
14401446
"source": [
14411447
"A = np.random.randint(0,10,(3,4,3,4))\n",
1442-
"# solution by passing a tuple of axes (introduced in numpy 1.7.0)\n",
1443-
"sum = A.sum(axis=(-2,-1))\n",
1444-
"print(sum)\n",
1445-
"# solution by flattening the last two dimensions into one\n",
1446-
"# (useful for functions that don't accept tuples for axis argument)\n",
1448+
"# solution by passing a tuple of axes (introduced in numpy 1.7.0)\n",
1449+
"sum = A.sum(axis=(-2,-1))\n",
1450+
"print(sum)\n",
1451+
"# solution by flattening the last two dimensions into one\n",
1452+
"# (useful for functions that don't accept tuples for axis argument)\n",
14471453
"sum = A.reshape(A.shape[:-2] + (-1,)).sum(axis=-1)\n",
14481454
"print(sum)"
14491455
]
@@ -1471,7 +1477,7 @@
14711477
"D_counts = np.bincount(S)\n",
14721478
"D_means = D_sums / D_counts\n",
14731479
"print(D_means)\n",
1474-
"\n",
1480+
"\n",
14751481
"# Pandas solution as a reference due to more intuitive code\n",
14761482
"import pandas as pd\n",
14771483
"print(pd.Series(D).groupby(S).mean())"
@@ -2334,7 +2340,7 @@
23342340
"name": "python",
23352341
"nbconvert_exporter": "python",
23362342
"pygments_lexer": "ipython3",
2337-
"version": "3.5.2"
2343+
"version": "3.5.1"
23382344
}
23392345
},
23402346
"nbformat": 4,

0 commit comments

Comments
 (0)