forked from rasbt/python-machine-learning-book-3rd-edition
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
59 changed files
with
5,760 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.