Skip to content

Commit 996f958

Browse files
committed
ch02 code ready
1 parent 2435591 commit 996f958

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+5760
-1
lines changed

.convert_notebook_to_script.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
'# In[',
21+
'# <hr>',
22+
'from IPython.display import Image',
23+
'get_ipython()',
24+
'# <br>')
25+
26+
clean_content = []
27+
imports = []
28+
existing_imports = set()
29+
with open(path, 'r') as f:
30+
next(f)
31+
next(f)
32+
for line in f:
33+
line = line.rstrip(' ')
34+
if line.startswith(skip_lines_startwith):
35+
continue
36+
if line.startswith('import ') or (
37+
'from ' in line and 'import ' in line):
38+
if 'from __future__ import print_function' in line:
39+
if line != imports[0]:
40+
imports.insert(0, line)
41+
else:
42+
if line.strip() not in existing_imports:
43+
imports.append(line)
44+
existing_imports.add(line.strip())
45+
else:
46+
clean_content.append(line)
47+
48+
clean_content = ['# coding: utf-8\n\n\n'] + imports + clean_content
49+
50+
with open(path, 'w') as f:
51+
for line in clean_content:
52+
f.write(line)
53+
54+
55+
if __name__ == '__main__':
56+
57+
parser = argparse.ArgumentParser(
58+
description='Convert Jupyter notebook to Python script.',
59+
formatter_class=argparse.RawTextHelpFormatter)
60+
61+
parser.add_argument('-i', '--input',
62+
required=True,
63+
help='Path to the Jupyter Notebook file')
64+
65+
parser.add_argument('-o', '--output',
66+
required=True,
67+
help='Path to the Python script file')
68+
69+
parser.add_argument('-v', '--version',
70+
action='version',
71+
version='v. 0.1')
72+
73+
args = parser.parse_args()
74+
75+
convert(input_path=args.input,
76+
output_path=os.path.splitext(args.output)[0])
77+
78+
cleanup(args.output)

ch01/ch01.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@
564564
"name": "python",
565565
"nbconvert_exporter": "python",
566566
"pygments_lexer": "ipython3",
567-
"version": "3.7.1"
567+
"version": "3.7.3"
568568
}
569569
},
570570
"nbformat": 4,

ch02/README.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Python Machine Learning - Code Examples
2+
3+
4+
## Chapter 2: Training Simple Machine Learning Algorithms for Classification
5+
6+
### Chapter Outline
7+
8+
- Artificial neurons – a brief glimpse into the early history of machine learning
9+
- The formal definition of an artificial neuron
10+
- The perceptron learning rule
11+
- Implementing a perceptron learning algorithm in Python
12+
- An object-oriented perceptron API
13+
- Training a perceptron model on the Iris dataset
14+
- Adaptive linear neurons and the convergence of learning
15+
- Minimizing cost functions with gradient descent
16+
- Implementing an Adaptive Linear Neuron in Python
17+
- Improving gradient descent through feature scaling
18+
- Large scale machine learning and stochastic gradient descent
19+
- Summary
20+
21+
### A note on using the code examples
22+
23+
The recommended way to interact with the code examples in this book is via Jupyter Notebook (the `.ipynb` files). Using Jupyter Notebook, you will be able to execute the code step by step and have all the resulting outputs (including plots and images) all in one convenient document.
24+
25+
![](images/jupyter-example-1.png)
26+
27+
28+
29+
Setting up Jupyter Notebook is really easy: if you are using the Anaconda Python distribution, all you need to install jupyter notebook is to execute the following command in your terminal:
30+
31+
conda install jupyter notebook
32+
33+
Then you can launch jupyter notebook by executing
34+
35+
jupyter notebook
36+
37+
A window will open up in your browser, which you can then use to navigate to the target directory that contains the `.ipynb` file you wish to open.
38+
39+
**More installation and setup instructions can be found in the [README.md file of Chapter 1](../ch01/README.md)**.
40+
41+
**(Even if you decide not to install Jupyter Notebook, note that you can also view the notebook files on GitHub by simply clicking on them: [`ch02.ipynb`](ch02.ipynb))**
42+
43+
In addition to the code examples, I added a table of contents to each Jupyter notebook as well as section headers that are consistent with the content of the book. Also, I included the original images and figures in hope that these make it easier to navigate and work with the code interactively as you are reading the book.
44+
45+
![](images/jupyter-example-2.png)
46+
47+
48+
When I was creating these notebooks, I was hoping to make your reading (and coding) experience as convenient as possible! However, if you don't wish to use Jupyter Notebooks, I also converted these notebooks to regular Python script files (`.py` files) that can be viewed and edited in any plaintext editor.

ch02/ch02.ipynb

+1,409
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)