Skip to content

Commit

Permalink
ch02 code ready
Browse files Browse the repository at this point in the history
  • Loading branch information
rasbt committed Jun 10, 2019
1 parent 2435591 commit 996f958
Show file tree
Hide file tree
Showing 59 changed files with 5,760 additions and 1 deletion.
78 changes: 78 additions & 0 deletions .convert_notebook_to_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# 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=',
'# In[',
'# <hr>',
'from IPython.display import Image',
'get_ipython()',
'# <br>')

clean_content = []
imports = []
existing_imports = set()
with open(path, 'r') as f:
next(f)
next(f)
for line in f:
line = line.rstrip(' ')
if line.startswith(skip_lines_startwith):
continue
if line.startswith('import ') or (
'from ' in line and 'import ' in line):
if 'from __future__ import print_function' in line:
if line != imports[0]:
imports.insert(0, line)
else:
if line.strip() not in existing_imports:
imports.append(line)
existing_imports.add(line.strip())
else:
clean_content.append(line)

clean_content = ['# coding: utf-8\n\n\n'] + imports + clean_content

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)
2 changes: 1 addition & 1 deletion ch01/ch01.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
"version": "3.7.3"
}
},
"nbformat": 4,
Expand Down
48 changes: 48 additions & 0 deletions ch02/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Python Machine Learning - Code Examples


## Chapter 2: Training Simple Machine Learning Algorithms for Classification

### Chapter Outline

- Artificial neurons – a brief glimpse into the early history of machine learning
- The formal definition of an artificial neuron
- The perceptron learning rule
- Implementing a perceptron learning algorithm in Python
- An object-oriented perceptron API
- Training a perceptron model on the Iris dataset
- Adaptive linear neurons and the convergence of learning
- Minimizing cost functions with gradient descent
- Implementing an Adaptive Linear Neuron in Python
- Improving gradient descent through feature scaling
- Large scale machine learning and stochastic gradient descent
- Summary

### A note on using the code examples

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.

![](images/jupyter-example-1.png)



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:

conda install jupyter notebook

Then you can launch jupyter notebook by executing

jupyter notebook

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.

**More installation and setup instructions can be found in the [README.md file of Chapter 1](../ch01/README.md)**.

**(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))**

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.

![](images/jupyter-example-2.png)


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.
1,409 changes: 1,409 additions & 0 deletions ch02/ch02.ipynb

Large diffs are not rendered by default.

Loading

0 comments on commit 996f958

Please sign in to comment.