Skip to content

Commit 43c9ecf

Browse files

39 files changed

+11426
-9
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Simply click on the `open dir` links next to the chapter headlines to navigate t
3737
3. A Tour of Machine Learning Classifiers Using Scikit-Learn [[open dir](./code/ch03)] [[ipynb](./code/ch03/ch03.ipynb)]
3838
4. Building Good Training Sets – Data Pre-Processing [[open dir](./code/ch04)] [[ipynb](./code/ch04/ch04.ipynb)]
3939
5. Compressing Data via Dimensionality Reduction [[open dir](./code/ch05)] [[ipynb](./code/ch05/ch05.ipynb)]
40-
6. Learning Best Practices for Model Evaluation and Hyperparameter Optimization [[open dir](./code/ch06)]
40+
6. Learning Best Practices for Model Evaluation and Hyperparameter Optimization [[open dir](./code/ch06)] [[ipynb](./code/ch06/ch06.ipynb)]
4141
7. Combining Different Models for Ensemble Learning [[open dir](./code/ch07)] [[ipynb](./code/ch07/ch07.ipynb)]
4242
8. Applying Machine Learning to Sentiment Analysis [[open dir](./code/ch08)] [[ipynb](./code/ch08/ch08.ipynb)]
4343
9. Embedding a Machine Learning Model into a Web Application [[open dir](./code/ch09)] [[ipynb](./code/ch09/ch09.ipynb)]

Diff for: code/.convert_notebook_to_script.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Simple helper script to convert
2+
# a Jupyter notebook to Python
3+
#
4+
# Sebastian Raschka, 2017
5+
6+
7+
import argparse
8+
import os
9+
import subprocess
10+
11+
12+
def convert(input_path, output_path):
13+
subprocess.call(['jupyter', 'nbconvert', '--to', 'script',
14+
input_path, '--output', output_path])
15+
16+
17+
def cleanup(path):
18+
19+
skip_lines_startwith = ('Image(filename=',
20+
'get_ipython()',
21+
'# <br>',
22+
'from __future__ import print_function')
23+
24+
clean_content = []
25+
with open(path, 'r') as f:
26+
for line in f:
27+
if line.startswith(skip_lines_startwith):
28+
if line.startswith('from __future__ import print_function'):
29+
clean_content.insert(0,
30+
'from __future__ import '
31+
'print_function\n\n\n')
32+
continue
33+
else:
34+
clean_content.append(line)
35+
36+
with open(path, 'w') as f:
37+
for line in clean_content:
38+
f.write(line)
39+
40+
41+
if __name__ == '__main__':
42+
43+
parser = argparse.ArgumentParser(
44+
description='Convert Jupyter notebook to Python script.',
45+
formatter_class=argparse.RawTextHelpFormatter)
46+
47+
parser.add_argument('-i', '--input',
48+
required=True,
49+
help='Path to the Jupyter Notebook file')
50+
51+
parser.add_argument('-o', '--output',
52+
required=True,
53+
help='Path to the Python script file')
54+
55+
parser.add_argument('-v', '--version',
56+
action='version',
57+
version='v. 0.1')
58+
59+
args = parser.parse_args()
60+
61+
convert(input_path=args.input,
62+
output_path=os.path.splitext(args.output)[0])
63+
64+
cleanup(args.output)

Diff for: code/ch02/ch02.ipynb

+152-1
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,157 @@
13511351
"source": [
13521352
"..."
13531353
]
1354+
},
1355+
{
1356+
"cell_type": "markdown",
1357+
"metadata": {},
1358+
"source": [
1359+
"--- \n",
1360+
"\n",
1361+
"Readers may ignore the following cell"
1362+
]
1363+
},
1364+
{
1365+
"cell_type": "code",
1366+
"execution_count": 9,
1367+
"metadata": {},
1368+
"outputs": [
1369+
{
1370+
"name": "stdout",
1371+
"output_type": "stream",
1372+
"text": [
1373+
"[NbConvertApp] WARNING | pattern 'input_path' matched no files\n",
1374+
"This application is used to convert notebook files (*.ipynb) to various other\n",
1375+
"formats.\n",
1376+
"\n",
1377+
"WARNING: THE COMMANDLINE INTERFACE MAY CHANGE IN FUTURE RELEASES.\n",
1378+
"\n",
1379+
"Options\n",
1380+
"-------\n",
1381+
"\n",
1382+
"Arguments that take values are actually convenience aliases to full\n",
1383+
"Configurables, whose aliases are listed on the help line. For more information\n",
1384+
"on full configurables, see '--help-all'.\n",
1385+
"\n",
1386+
"--debug\n",
1387+
" set log level to logging.DEBUG (maximize logging output)\n",
1388+
"--generate-config\n",
1389+
" generate default config file\n",
1390+
"-y\n",
1391+
" Answer yes to any questions instead of prompting.\n",
1392+
"--execute\n",
1393+
" Execute the notebook prior to export.\n",
1394+
"--allow-errors\n",
1395+
" Continue notebook execution even if one of the cells throws an error and include the error message in the cell output (the default behaviour is to abort conversion). This flag is only relevant if '--execute' was specified, too.\n",
1396+
"--stdin\n",
1397+
" read a single notebook file from stdin. Write the resulting notebook with default basename 'notebook.*'\n",
1398+
"--stdout\n",
1399+
" Write notebook output to stdout instead of files.\n",
1400+
"--inplace\n",
1401+
" Run nbconvert in place, overwriting the existing notebook (only \n",
1402+
" relevant when converting to notebook format)\n",
1403+
"--no-prompt\n",
1404+
" Exclude input and output prompts from converted document.\n",
1405+
"--log-level=<Enum> (Application.log_level)\n",
1406+
" Default: 30\n",
1407+
" Choices: (0, 10, 20, 30, 40, 50, 'DEBUG', 'INFO', 'WARN', 'ERROR', 'CRITICAL')\n",
1408+
" Set the log level by value or name.\n",
1409+
"--config=<Unicode> (JupyterApp.config_file)\n",
1410+
" Default: ''\n",
1411+
" Full path of a config file.\n",
1412+
"--to=<Unicode> (NbConvertApp.export_format)\n",
1413+
" Default: 'html'\n",
1414+
" The export format to be used, either one of the built-in formats, or a\n",
1415+
" dotted object name that represents the import path for an `Exporter` class\n",
1416+
"--template=<Unicode> (TemplateExporter.template_file)\n",
1417+
" Default: ''\n",
1418+
" Name of the template file to use\n",
1419+
"--writer=<DottedObjectName> (NbConvertApp.writer_class)\n",
1420+
" Default: 'FilesWriter'\n",
1421+
" Writer class used to write the results of the conversion\n",
1422+
"--post=<DottedOrNone> (NbConvertApp.postprocessor_class)\n",
1423+
" Default: ''\n",
1424+
" PostProcessor class used to write the results of the conversion\n",
1425+
"--output=<Unicode> (NbConvertApp.output_base)\n",
1426+
" Default: ''\n",
1427+
" overwrite base name use for output files. can only be used when converting\n",
1428+
" one notebook at a time.\n",
1429+
"--output-dir=<Unicode> (FilesWriter.build_directory)\n",
1430+
" Default: ''\n",
1431+
" Directory to write output(s) to. Defaults to output to the directory of each\n",
1432+
" notebook. To recover previous default behaviour (outputting to the current\n",
1433+
" working directory) use . as the flag value.\n",
1434+
"--reveal-prefix=<Unicode> (SlidesExporter.reveal_url_prefix)\n",
1435+
" Default: ''\n",
1436+
" The URL prefix for reveal.js. This can be a a relative URL for a local copy\n",
1437+
" of reveal.js, or point to a CDN.\n",
1438+
" For speaker notes to work, a local reveal.js prefix must be used.\n",
1439+
"--nbformat=<Enum> (NotebookExporter.nbformat_version)\n",
1440+
" Default: 4\n",
1441+
" Choices: [1, 2, 3, 4]\n",
1442+
" The nbformat version to write. Use this to downgrade notebooks.\n",
1443+
"\n",
1444+
"To see all available configurables, use `--help-all`\n",
1445+
"\n",
1446+
"Examples\n",
1447+
"--------\n",
1448+
"\n",
1449+
" The simplest way to use nbconvert is\n",
1450+
" \n",
1451+
" > jupyter nbconvert mynotebook.ipynb\n",
1452+
" \n",
1453+
" which will convert mynotebook.ipynb to the default format (probably HTML).\n",
1454+
" \n",
1455+
" You can specify the export format with `--to`.\n",
1456+
" Options include ['asciidoc', 'custom', 'html', 'latex', 'markdown', 'notebook', 'pdf', 'python', 'rst', 'script', 'slides']\n",
1457+
" \n",
1458+
" > jupyter nbconvert --to latex mynotebook.ipynb\n",
1459+
" \n",
1460+
" Both HTML and LaTeX support multiple output templates. LaTeX includes\n",
1461+
" 'base', 'article' and 'report'. HTML includes 'basic' and 'full'. You\n",
1462+
" can specify the flavor of the format used.\n",
1463+
" \n",
1464+
" > jupyter nbconvert --to html --template basic mynotebook.ipynb\n",
1465+
" \n",
1466+
" You can also pipe the output to stdout, rather than a file\n",
1467+
" \n",
1468+
" > jupyter nbconvert mynotebook.ipynb --stdout\n",
1469+
" \n",
1470+
" PDF is generated via latex\n",
1471+
" \n",
1472+
" > jupyter nbconvert mynotebook.ipynb --to pdf\n",
1473+
" \n",
1474+
" You can get (and serve) a Reveal.js-powered slideshow\n",
1475+
" \n",
1476+
" > jupyter nbconvert myslides.ipynb --to slides --post serve\n",
1477+
" \n",
1478+
" Multiple notebooks can be given at the command line in a couple of \n",
1479+
" different ways:\n",
1480+
" \n",
1481+
" > jupyter nbconvert notebook*.ipynb\n",
1482+
" > jupyter nbconvert notebook1.ipynb notebook2.ipynb\n",
1483+
" \n",
1484+
" or you can specify the notebooks list in a config file, containing::\n",
1485+
" \n",
1486+
" c.NbConvertApp.notebooks = [\"my_notebook.ipynb\"]\n",
1487+
" \n",
1488+
" > jupyter nbconvert --config mycfg.py\n",
1489+
"\n"
1490+
]
1491+
}
1492+
],
1493+
"source": [
1494+
"! python ../.convert_notebook_to_script.py --input ch02.ipynb --output ch02.py"
1495+
]
1496+
},
1497+
{
1498+
"cell_type": "code",
1499+
"execution_count": null,
1500+
"metadata": {
1501+
"collapsed": true
1502+
},
1503+
"outputs": [],
1504+
"source": []
13541505
}
13551506
],
13561507
"metadata": {
@@ -1370,7 +1521,7 @@
13701521
"name": "python",
13711522
"nbconvert_exporter": "python",
13721523
"pygments_lexer": "ipython3",
1373-
"version": "3.6.2"
1524+
"version": "3.6.1"
13741525
}
13751526
},
13761527
"nbformat": 4,

0 commit comments

Comments
 (0)