Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
rasbt committed Sep 10, 2017
2 parents 5121ea8 + a5fe037 commit 43c9ecf
Show file tree
Hide file tree
Showing 39 changed files with 11,426 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Simply click on the `open dir` links next to the chapter headlines to navigate t
3. A Tour of Machine Learning Classifiers Using Scikit-Learn [[open dir](./code/ch03)] [[ipynb](./code/ch03/ch03.ipynb)]
4. Building Good Training Sets – Data Pre-Processing [[open dir](./code/ch04)] [[ipynb](./code/ch04/ch04.ipynb)]
5. Compressing Data via Dimensionality Reduction [[open dir](./code/ch05)] [[ipynb](./code/ch05/ch05.ipynb)]
6. Learning Best Practices for Model Evaluation and Hyperparameter Optimization [[open dir](./code/ch06)]
6. Learning Best Practices for Model Evaluation and Hyperparameter Optimization [[open dir](./code/ch06)] [[ipynb](./code/ch06/ch06.ipynb)]
7. Combining Different Models for Ensemble Learning [[open dir](./code/ch07)] [[ipynb](./code/ch07/ch07.ipynb)]
8. Applying Machine Learning to Sentiment Analysis [[open dir](./code/ch08)] [[ipynb](./code/ch08/ch08.ipynb)]
9. Embedding a Machine Learning Model into a Web Application [[open dir](./code/ch09)] [[ipynb](./code/ch09/ch09.ipynb)]
Expand Down
64 changes: 64 additions & 0 deletions code/.convert_notebook_to_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Simple helper script to convert
# a Jupyter notebook to Python
#
# Sebastian Raschka, 2017


import argparse
import os
import subprocess


def convert(input_path, output_path):
subprocess.call(['jupyter', 'nbconvert', '--to', 'script',
input_path, '--output', output_path])


def cleanup(path):

skip_lines_startwith = ('Image(filename=',
'get_ipython()',
'# <br>',
'from __future__ import print_function')

clean_content = []
with open(path, 'r') as f:
for line in f:
if line.startswith(skip_lines_startwith):
if line.startswith('from __future__ import print_function'):
clean_content.insert(0,
'from __future__ import '
'print_function\n\n\n')
continue
else:
clean_content.append(line)

with open(path, 'w') as f:
for line in clean_content:
f.write(line)


if __name__ == '__main__':

parser = argparse.ArgumentParser(
description='Convert Jupyter notebook to Python script.',
formatter_class=argparse.RawTextHelpFormatter)

parser.add_argument('-i', '--input',
required=True,
help='Path to the Jupyter Notebook file')

parser.add_argument('-o', '--output',
required=True,
help='Path to the Python script file')

parser.add_argument('-v', '--version',
action='version',
version='v. 0.1')

args = parser.parse_args()

convert(input_path=args.input,
output_path=os.path.splitext(args.output)[0])

cleanup(args.output)
153 changes: 152 additions & 1 deletion code/ch02/ch02.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,157 @@
"source": [
"..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"--- \n",
"\n",
"Readers may ignore the following cell"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[NbConvertApp] WARNING | pattern 'input_path' matched no files\n",
"This application is used to convert notebook files (*.ipynb) to various other\n",
"formats.\n",
"\n",
"WARNING: THE COMMANDLINE INTERFACE MAY CHANGE IN FUTURE RELEASES.\n",
"\n",
"Options\n",
"-------\n",
"\n",
"Arguments that take values are actually convenience aliases to full\n",
"Configurables, whose aliases are listed on the help line. For more information\n",
"on full configurables, see '--help-all'.\n",
"\n",
"--debug\n",
" set log level to logging.DEBUG (maximize logging output)\n",
"--generate-config\n",
" generate default config file\n",
"-y\n",
" Answer yes to any questions instead of prompting.\n",
"--execute\n",
" Execute the notebook prior to export.\n",
"--allow-errors\n",
" 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",
"--stdin\n",
" read a single notebook file from stdin. Write the resulting notebook with default basename 'notebook.*'\n",
"--stdout\n",
" Write notebook output to stdout instead of files.\n",
"--inplace\n",
" Run nbconvert in place, overwriting the existing notebook (only \n",
" relevant when converting to notebook format)\n",
"--no-prompt\n",
" Exclude input and output prompts from converted document.\n",
"--log-level=<Enum> (Application.log_level)\n",
" Default: 30\n",
" Choices: (0, 10, 20, 30, 40, 50, 'DEBUG', 'INFO', 'WARN', 'ERROR', 'CRITICAL')\n",
" Set the log level by value or name.\n",
"--config=<Unicode> (JupyterApp.config_file)\n",
" Default: ''\n",
" Full path of a config file.\n",
"--to=<Unicode> (NbConvertApp.export_format)\n",
" Default: 'html'\n",
" The export format to be used, either one of the built-in formats, or a\n",
" dotted object name that represents the import path for an `Exporter` class\n",
"--template=<Unicode> (TemplateExporter.template_file)\n",
" Default: ''\n",
" Name of the template file to use\n",
"--writer=<DottedObjectName> (NbConvertApp.writer_class)\n",
" Default: 'FilesWriter'\n",
" Writer class used to write the results of the conversion\n",
"--post=<DottedOrNone> (NbConvertApp.postprocessor_class)\n",
" Default: ''\n",
" PostProcessor class used to write the results of the conversion\n",
"--output=<Unicode> (NbConvertApp.output_base)\n",
" Default: ''\n",
" overwrite base name use for output files. can only be used when converting\n",
" one notebook at a time.\n",
"--output-dir=<Unicode> (FilesWriter.build_directory)\n",
" Default: ''\n",
" Directory to write output(s) to. Defaults to output to the directory of each\n",
" notebook. To recover previous default behaviour (outputting to the current\n",
" working directory) use . as the flag value.\n",
"--reveal-prefix=<Unicode> (SlidesExporter.reveal_url_prefix)\n",
" Default: ''\n",
" The URL prefix for reveal.js. This can be a a relative URL for a local copy\n",
" of reveal.js, or point to a CDN.\n",
" For speaker notes to work, a local reveal.js prefix must be used.\n",
"--nbformat=<Enum> (NotebookExporter.nbformat_version)\n",
" Default: 4\n",
" Choices: [1, 2, 3, 4]\n",
" The nbformat version to write. Use this to downgrade notebooks.\n",
"\n",
"To see all available configurables, use `--help-all`\n",
"\n",
"Examples\n",
"--------\n",
"\n",
" The simplest way to use nbconvert is\n",
" \n",
" > jupyter nbconvert mynotebook.ipynb\n",
" \n",
" which will convert mynotebook.ipynb to the default format (probably HTML).\n",
" \n",
" You can specify the export format with `--to`.\n",
" Options include ['asciidoc', 'custom', 'html', 'latex', 'markdown', 'notebook', 'pdf', 'python', 'rst', 'script', 'slides']\n",
" \n",
" > jupyter nbconvert --to latex mynotebook.ipynb\n",
" \n",
" Both HTML and LaTeX support multiple output templates. LaTeX includes\n",
" 'base', 'article' and 'report'. HTML includes 'basic' and 'full'. You\n",
" can specify the flavor of the format used.\n",
" \n",
" > jupyter nbconvert --to html --template basic mynotebook.ipynb\n",
" \n",
" You can also pipe the output to stdout, rather than a file\n",
" \n",
" > jupyter nbconvert mynotebook.ipynb --stdout\n",
" \n",
" PDF is generated via latex\n",
" \n",
" > jupyter nbconvert mynotebook.ipynb --to pdf\n",
" \n",
" You can get (and serve) a Reveal.js-powered slideshow\n",
" \n",
" > jupyter nbconvert myslides.ipynb --to slides --post serve\n",
" \n",
" Multiple notebooks can be given at the command line in a couple of \n",
" different ways:\n",
" \n",
" > jupyter nbconvert notebook*.ipynb\n",
" > jupyter nbconvert notebook1.ipynb notebook2.ipynb\n",
" \n",
" or you can specify the notebooks list in a config file, containing::\n",
" \n",
" c.NbConvertApp.notebooks = [\"my_notebook.ipynb\"]\n",
" \n",
" > jupyter nbconvert --config mycfg.py\n",
"\n"
]
}
],
"source": [
"! python ../.convert_notebook_to_script.py --input ch02.ipynb --output ch02.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -1370,7 +1521,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
"version": "3.6.1"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 43c9ecf

Please sign in to comment.