Skip to content

Commit 8ccb5d4

Browse files
committed
ch09 up
1 parent d615d65 commit 8ccb5d4

39 files changed

Lines changed: 1575 additions & 10 deletions

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ If you are interested in keeping in touch, I have quite a lively twitter stream
3232

3333
Excerpts from the [Foreword](./docs/foreword_ro.pdf) and [Preface](./docs/preface_sr.pdf).
3434

35-
1. Machine Learning - Giving Computers the Ability to Learn from Data [[./code/ch01](./code/ch01)] [[ipynb](./code/ch01/ch01.ipynb)] [[nbviewer](http://nbviewer.ipython.org/github/rasbt/python-machine-learning-book/blob/master/code/ch01/ch01.ipynb)]
36-
2. Training Machine Learning Algorithms for Classification [[./code/ch02](./code/ch02)] [[ipynb](./code/ch02/ch02.ipynb)] [[nbviewer](http://nbviewer.ipython.org/github/rasbt/python-machine-learning-book/blob/master/code/ch02/ch02.ipynb)]
37-
3. A Tour of Machine Learning Classifiers Using Scikit-Learn [[./code/ch03](./code/ch03)] [[ipynb](./code/ch03/ch03.ipynb)] [[nbviewer](http://nbviewer.ipython.org/github/rasbt/python-machine-learning-book/blob/master/code/ch03/ch03.ipynb)]
38-
4. Building Good Training Sets – Data Pre-Processing [[./code/ch04](./code/ch04)] [[ipynb](./code/ch04/ch04.ipynb)] [[nbviewer](http://nbviewer.ipython.org/github/rasbt/python-machine-learning-book/blob/master/code/ch04/ch04.ipynb)]
39-
5. Compressing Data via Dimensionality Reduction [[./code/ch05](./code/ch05)] [[ipynb](./code/ch05/ch05.ipynb)] [[nbviewer](http://nbviewer.ipython.org/github/rasbt/python-machine-learning-book/blob/master/code/ch05/ch05.ipynb)]
40-
6. Learning Best Practices for Model Evaluation and Hyperparameter Optimization [[./code/ch06](./code/ch06)] [[ipynb](./code/ch06/ch06.ipynb)] [[nbviewer](http://nbviewer.ipython.org/github/rasbt/python-machine-learning-book/blob/master/code/ch06/ch06.ipynb)]
41-
7. Combining Different Models for Ensemble Learning [[./code/ch07](./code/ch07)] [[ipynb](./code/ch07/ch07.ipynb)] [[nbviewer](http://nbviewer.ipython.org/github/rasbt/python-machine-learning-book/blob/master/code/ch07/ch07.ipynb)]
42-
8. Applying Machine Learning to Sentiment Analysis
43-
9. Embedding a Machine Learning Model into a Web Application
35+
1. Machine Learning - Giving Computers the Ability to Learn from Data [[dir](./code/ch01)] [[ipynb](./code/ch01/ch01.ipynb)] [[nbviewer](http://nbviewer.ipython.org/github/rasbt/python-machine-learning-book/blob/master/code/ch01/ch01.ipynb)]
36+
2. Training Machine Learning Algorithms for Classification [[dir](./code/ch02)] [[ipynb](./code/ch02/ch02.ipynb)] [[nbviewer](http://nbviewer.ipython.org/github/rasbt/python-machine-learning-book/blob/master/code/ch02/ch02.ipynb)]
37+
3. A Tour of Machine Learning Classifiers Using Scikit-Learn [[dir](./code/ch03)] [[ipynb](./code/ch03/ch03.ipynb)] [[nbviewer](http://nbviewer.ipython.org/github/rasbt/python-machine-learning-book/blob/master/code/ch03/ch03.ipynb)]
38+
4. Building Good Training Sets – Data Pre-Processing [[dir](./code/ch04)] [[ipynb](./code/ch04/ch04.ipynb)] [[nbviewer](http://nbviewer.ipython.org/github/rasbt/python-machine-learning-book/blob/master/code/ch04/ch04.ipynb)]
39+
5. Compressing Data via Dimensionality Reduction [[dir](./code/ch05)] [[ipynb](./code/ch05/ch05.ipynb)] [[nbviewer](http://nbviewer.ipython.org/github/rasbt/python-machine-learning-book/blob/master/code/ch05/ch05.ipynb)]
40+
6. Learning Best Practices for Model Evaluation and Hyperparameter Optimization [[dir](./code/ch06)] [[ipynb](./code/ch06/ch06.ipynb)] [[nbviewer](http://nbviewer.ipython.org/github/rasbt/python-machine-learning-book/blob/master/code/ch06/ch06.ipynb)]
41+
7. Combining Different Models for Ensemble Learning [[dir](./code/ch07)] [[ipynb](./code/ch07/ch07.ipynb)] [[nbviewer](http://nbviewer.ipython.org/github/rasbt/python-machine-learning-book/blob/master/code/ch07/ch07.ipynb)]
42+
8. Applying Machine Learning to Sentiment Analysis [[dir](./code/ch08)] [[ipynb](./code/ch08/ch08.ipynb)] [[nbviewer](http://nbviewer.ipython.org/github/rasbt/python-machine-learning-book/blob/master/code/ch08/ch08.ipynb)]
43+
9. Embedding a Machine Learning Model into a Web Application [[dir](./code/ch09)] [[ipynb](./code/ch09/ch09.ipynb)] [[nbviewer](http://nbviewer.ipython.org/github/rasbt/python-machine-learning-book/blob/master/code/ch09/ch09.ipynb)]
4444
10. Predicting Continuous Target Variables with Regression Analysis
4545
11. Working with Unlabeled Data – Clustering Analysis
4646
12. Training Artificial Neural Networks for Image Recognition

code/ch09/1st_flask_app_1/app.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from flask import Flask, render_template
2+
3+
app = Flask(__name__)
4+
5+
@app.route('/')
6+
def index():
7+
return render_template('first_app.html')
8+
9+
if __name__ == '__main__':
10+
app.run(debug=True)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>First app</title>
5+
</head>
6+
<body>
7+
8+
<div>
9+
Hi, this is my first Flask web app!
10+
</div>
11+
12+
</body>
13+
</html>

code/ch09/1st_flask_app_2/app.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from flask import Flask, render_template, request
2+
from wtforms import Form, TextAreaField, validators
3+
4+
app = Flask(__name__)
5+
6+
class HelloForm(Form):
7+
sayhello = TextAreaField('',[validators.DataRequired()])
8+
9+
@app.route('/')
10+
def index():
11+
form = HelloForm(request.form)
12+
return render_template('first_app.html', form=form)
13+
14+
@app.route('/hello', methods=['POST'])
15+
def hello():
16+
form = HelloForm(request.form)
17+
if request.method == 'POST' and form.validate():
18+
name = request.form['sayhello']
19+
return render_template('hello.html', name=name)
20+
return render_template('first_app.html', form=form)
21+
22+
if __name__ == '__main__':
23+
app.run(debug=True)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
font-size: 2em;
3+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{% macro render_field(field) %}
2+
<dt>{{ field.label }}
3+
<dd>{{ field(**kwargs)|safe }}
4+
{% if field.errors %}
5+
<ul class=errors>
6+
{% for error in field.errors %}
7+
<li>{{ error }}</li>
8+
{% endfor %}
9+
</ul>
10+
{% endif %}
11+
</dd>
12+
{% endmacro %}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>First app</title>
5+
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
6+
</head>
7+
<body>
8+
9+
{% from "_formhelpers.html" import render_field %}
10+
11+
<div>What's your name?</div>
12+
<form method=post action="/hello">
13+
14+
<dl>
15+
{{ render_field(form.sayhello) }}
16+
</dl>
17+
18+
<input type=submit value='Say Hello' name='submit_btn'>
19+
20+
</form>
21+
22+
</body>
23+
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>First app</title>
5+
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
6+
</head>
7+
<body>
8+
9+
<div>Hello {{ name }}</div>
10+
11+
12+
</body>
13+
</html>

code/ch09/README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,26 @@ Sebastian Raschka, 2015
33
# Python Machine Learning
44
# Chapter 9 Code Examples
55

6-
## Embedding a Machine Learning Model into a Web Application
6+
## Embedding a Machine Learning Model into a Web Application
7+
8+
9+
The code for the Flask web applications can be found in the following directories:
10+
11+
- `1st_flask_app_1/`: A simple Flask web app
12+
- `1st_flask_app_2/`: `1st_flask_app_1` extended with flexible form validation and rendering
13+
- `movieclassifier/`: The movie classifier embedded in a web application
14+
- `movieclassifier_with_update/`: same as `movieclassifier` but with update from sqlite database upon start
15+
16+
17+
To run the web applications locally, `cd` into the respective directory (as listed above) and execute the main-application script, for example,
18+
19+
cd ./1st_flask_app_1
20+
python3 app.py
21+
22+
Now, you should see something like
23+
24+
* Running on http://127.0.0.1:5000/
25+
* Restarting with reloader
26+
27+
in your terminal.
28+
Next, open a web browsert and enter the address displayed in your terminal (typically http://127.0.0.1:5000/) to view the web application.

0 commit comments

Comments
 (0)