chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
Sebastian Raschka, 2015
|
||||
|
||||
Python Machine Learning - Code Examples
|
||||
|
||||
## Bonus Material
|
||||
|
||||
A collection of additional notebooks and code examples to clarify and explain concepts based on reader feedback.
|
||||
|
||||
|
||||
- A Basic Pipeline and Grid Search Setup [[GitHub ipynb](./svm_iris_pipeline_and_gridsearch.ipynb)] [[nbviewer](http://nbviewer.ipython.org/github/rasbt/python-machine-learning-book/blob/master/code/bonus/svm_iris_pipeline_and_gridsearch.ipynb)]
|
||||
|
||||
- An Extended Nested Cross-Validation Example [[GitHub ipynb](./nested_cross_validation.ipynb)] [[nbviewer](http://nbviewer.ipython.org/github/rasbt/python-machine-learning-book/blob/master/code/bonus/nested_cross_validation.ipynb)]
|
||||
|
||||
- A Simple(r) Barebones Flask Webapp Template [[view directory](./flask_webapp_ex01)][[download as zip-file](https://github.com/rasbt/python-machine-learning-book/raw/master/code/bonus/flask_webapp_ex01/flask_webapp_ex01.zip)]
|
||||
|
||||
- Reading handwritten digits from MNIST into NumPy arrays [[GitHub ipynb](./reading_mnist.ipynb)] [[nbviewer](http://nbviewer.ipython.org/github/rasbt/python-machine-learning-book/blob/master/code/bonus/reading_mnist.ipynb)]
|
||||
|
||||
- Scikit-learn Model Persistence using JSON [[GitHub ipynb](./scikit-model-to-json.ipynb)] [[nbviewer](http://nbviewer.ipython.org/github/rasbt/python-machine-learning-book/blob/master/code/bonus/scikit-model-to-json.ipynb)]
|
||||
|
||||
- Multinomial logistic regression / softmax regression [[GitHub ipynb](./softmax-regression.ipynb)] [[nbviewer](http://nbviewer.ipython.org/github/rasbt/python-machine-learning-book/blob/master/code/bonus/softmax-regression.ipynb)]
|
||||
@@ -0,0 +1,16 @@
|
||||
Sebastian Raschka, 2015
|
||||
|
||||
Python Machine Learning - Code Examples (Bonus Material)
|
||||
|
||||
|
||||
# A Simple(r) Barebones Flask Webapp Template
|
||||
|
||||
A simple Flask app that calculates the sum of two numbers entered in the respective input fields.
|
||||
|
||||
You can run the app locally by executing `python app.py` within this directory.
|
||||
|
||||
<hr>
|
||||
|
||||

|
||||
|
||||
Click [here](https://github.com/rasbt/python-machine-learning-book/raw/master/code/bonus/flask_webapp_ex01/flask_webapp_ex01.zip) to download this example as zip-file.
|
||||
@@ -0,0 +1,31 @@
|
||||
from flask import Flask, render_template, request
|
||||
from wtforms import Form, DecimalField, validators
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
class EntryForm(Form):
|
||||
x_entry = DecimalField('x:',
|
||||
places=10,
|
||||
validators=[validators.NumberRange(-1e10, 1e10)])
|
||||
y_entry = DecimalField('y:',
|
||||
places=10,
|
||||
validators=[validators.NumberRange(-1e10, 1e10)])
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
form = EntryForm(request.form)
|
||||
return render_template('entry.html', form=form, z='')
|
||||
|
||||
@app.route('/results', methods=['POST'])
|
||||
def results():
|
||||
form = EntryForm(request.form)
|
||||
z = ''
|
||||
if request.method == 'POST' and form.validate():
|
||||
x = request.form['x_entry']
|
||||
y = request.form['y_entry']
|
||||
z = float(x) + float(y)
|
||||
return render_template('entry.html', form=form, z=z)
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 6.1 KiB |
@@ -0,0 +1,7 @@
|
||||
body{
|
||||
width:600px;
|
||||
}
|
||||
|
||||
#button{
|
||||
padding-top: 20px;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
{% macro render_field(field) %}
|
||||
<dt>{{ field.label }}
|
||||
<dd>{{ field(**kwargs)|safe }}
|
||||
{% if field.errors %}
|
||||
<ul class=errors>
|
||||
{% for error in field.errors %}
|
||||
<li>{{ error }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
</dd>
|
||||
{% endmacro %}
|
||||
@@ -0,0 +1,26 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Webapp Ex 1</title>
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
{% from "_formhelpers.html" import render_field %}
|
||||
|
||||
<form method=post action="/results">
|
||||
<dl>
|
||||
{{ render_field(form.x_entry, cols='1', rows='1') }}
|
||||
{{ render_field(form.y_entry, cols='1', rows='1') }}
|
||||
</dl>
|
||||
<div>
|
||||
<input type=submit value='Submit' name='submit_btn'>
|
||||
</div>
|
||||
|
||||
<dl>
|
||||
x + y = {{ z }}
|
||||
</dl>
|
||||
</form>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
BIN
Binary file not shown.
|
After Width: | Height: | Size: 59 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 76 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 106 KiB |
File diff suppressed because one or more lines are too long
@@ -0,0 +1,311 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"[Sebastian Raschka](http://sebastianraschka.com), 2015\n",
|
||||
"\n",
|
||||
"https://github.com/rasbt/python-machine-learning-book"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Python Machine Learning - Code Examples"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Bonus Material - An Extended Nested Cross-Validation Example"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"For an explanation of nested cross-validation, please see:\n",
|
||||
" \n",
|
||||
"- Chapter 6, section \"Algorithm-selection-with-nested-cross-validation\" (open the code example via [nbviewer](http://nbviewer.ipython.org/github/rasbt/python-machine-learning-book/blob/master/code/ch06/ch06.ipynb#Algorithm-selection-with-nested-cross-validation))\n",
|
||||
"- FAQ, section: [How do I evaluate a model?](https://github.com/rasbt/python-machine-learning-book/blob/master/faq/evaluate-a-model.md)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<br>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Note that the optional watermark extension is a small IPython notebook plugin that I developed to make the code reproducible. You can just skip the following line(s)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Sebastian Raschka \n",
|
||||
"Last updated: 11/30/2015 \n",
|
||||
"\n",
|
||||
"CPython 3.5.0\n",
|
||||
"IPython 4.0.0\n",
|
||||
"\n",
|
||||
"numpy 1.10.1\n",
|
||||
"pandas 0.17.1\n",
|
||||
"matplotlib 1.5.0\n",
|
||||
"scikit-learn 0.17\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%load_ext watermark\n",
|
||||
"%watermark -a 'Sebastian Raschka' -u -d -v -p numpy,pandas,matplotlib,scikit-learn"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Dataset and Estimator Setup"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from sklearn.grid_search import GridSearchCV\n",
|
||||
"from sklearn.pipeline import Pipeline\n",
|
||||
"from sklearn.preprocessing import StandardScaler\n",
|
||||
"from sklearn.svm import SVC\n",
|
||||
"from sklearn.datasets import load_iris\n",
|
||||
"from sklearn.cross_validation import train_test_split\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# load and split data\n",
|
||||
"iris = load_iris()\n",
|
||||
"X, y = iris.data, iris.target\n",
|
||||
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42)\n",
|
||||
"\n",
|
||||
"# pipeline setup\n",
|
||||
"cls = SVC(C=10.0, kernel='rbf', gamma=0.1, decision_function_shape='ovr')\n",
|
||||
"kernel_svm = Pipeline([('std', StandardScaler()), \n",
|
||||
" ('svc', cls)])\n",
|
||||
"\n",
|
||||
"# gridsearch setup\n",
|
||||
"param_grid = [\n",
|
||||
" {'svc__C': [1, 10, 100, 1000], \n",
|
||||
" 'svc__gamma': [0.001, 0.0001], \n",
|
||||
" 'svc__kernel': ['rbf']},\n",
|
||||
" ]\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# setup multiple GridSearchCV objects, 1 for each algorithm\n",
|
||||
"\n",
|
||||
"gs_svm = GridSearchCV(estimator=kernel_svm, \n",
|
||||
" param_grid=param_grid, \n",
|
||||
" scoring='accuracy', \n",
|
||||
" n_jobs=-1, \n",
|
||||
" cv=5, \n",
|
||||
" verbose=0, \n",
|
||||
" refit=True,\n",
|
||||
" pre_dispatch='2*n_jobs')\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## A. Nested Crossvalidation - Quick Version"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Here, the `cross_val_function` runs the 5 outer loops, and the the `GridSearch` object (`gs`) peforms the hyperparameter optimization during the 5 inner loops."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"Average Accuracy 0.95 +/- 0.06\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import numpy as np \n",
|
||||
"\n",
|
||||
"from sklearn.cross_validation import cross_val_score\n",
|
||||
"scores = cross_val_score(gs_svm, X_train, y_train, scoring='accuracy', cv=5)\n",
|
||||
"print('\\nAverage Accuracy %.2f +/- %.2f' % (np.mean(scores), np.std(scores)))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## B. Nested Crossvalidation - Manual Approach Printing the Model Parameters"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from sklearn.cross_validation import StratifiedKFold\n",
|
||||
"from sklearn.metrics import accuracy_score\n",
|
||||
"import numpy as np\n",
|
||||
"\n",
|
||||
"params = []\n",
|
||||
"scores = []\n",
|
||||
"\n",
|
||||
"skfold = StratifiedKFold(y=y_train, n_folds=5, shuffle=False, random_state=1)\n",
|
||||
"for train_idx, test_idx in skfold:\n",
|
||||
" gs_svm.fit(X_train[train_idx], y_train[train_idx])\n",
|
||||
" y_pred = gs_svm.predict(X_train[test_idx])\n",
|
||||
" acc = accuracy_score(y_true=y_train[test_idx], y_pred=y_pred)\n",
|
||||
" params.append(gs_svm.best_params_)\n",
|
||||
" scores.append(acc)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"SVM models:\n",
|
||||
"1. Acc: 0.96 Params: {'svc__C': 100, 'svc__gamma': 0.001, 'svc__kernel': 'rbf'}\n",
|
||||
"2. Acc: 1.00 Params: {'svc__C': 100, 'svc__gamma': 0.001, 'svc__kernel': 'rbf'}\n",
|
||||
"3. Acc: 0.83 Params: {'svc__C': 1000, 'svc__gamma': 0.001, 'svc__kernel': 'rbf'}\n",
|
||||
"4. Acc: 1.00 Params: {'svc__C': 100, 'svc__gamma': 0.001, 'svc__kernel': 'rbf'}\n",
|
||||
"5. Acc: 0.96 Params: {'svc__C': 100, 'svc__gamma': 0.001, 'svc__kernel': 'rbf'}\n",
|
||||
"\n",
|
||||
"Average Accuracy 0.95 +/- 0.06\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print('SVM models:')\n",
|
||||
"for idx, m in enumerate(zip(params, scores)):\n",
|
||||
" print('%s. Acc: %.2f Params: %s' % (idx+1, m[1], m[0]))\n",
|
||||
"print('\\nAverage Accuracy %.2f +/- %.2f' % (np.mean(scores), np.std(scores)))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Regular K-fold CV to Optimize the Model on the Complete Training Set"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Repeat the nested cross-validation for different algorithms. Then, pick the \"best\" algorithm (not the best model!). Next, use the complete training set to tune the best algorithm via grid search:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Best parameters {'svc__C': 100, 'svc__gamma': 0.001, 'svc__kernel': 'rbf'}\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"gs_svm.fit(X_train, y_train)\n",
|
||||
"print('Best parameters %s' % gs_svm.best_params_)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Training accuracy: 0.97\n",
|
||||
"Test accuracy: 0.97\n",
|
||||
"Parameters: {'svc__C': 100, 'svc__gamma': 0.001, 'svc__kernel': 'rbf'}\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"train_acc = accuracy_score(y_true=y_train, y_pred=gs_svm.predict(X_train))\n",
|
||||
"test_acc = accuracy_score(y_true=y_test, y_pred=gs_svm.predict(X_test))\n",
|
||||
"print('Training accuracy: %.2f' % train_acc)\n",
|
||||
"print('Test accuracy: %.2f' % test_acc)\n",
|
||||
"print('Parameters: %s' % gs_svm.best_params_)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"classes_":[
|
||||
0,
|
||||
1,
|
||||
2
|
||||
],
|
||||
"coef_":[
|
||||
[
|
||||
0.42625236403173844,
|
||||
-8.557501546363858
|
||||
],
|
||||
[
|
||||
1.5644231337040186,
|
||||
-1.6783659020502222
|
||||
],
|
||||
[
|
||||
-1.990675497337773,
|
||||
10.235867448186507
|
||||
]
|
||||
],
|
||||
"intercept_":[
|
||||
27.533384852155145,
|
||||
4.18509910962595,
|
||||
-31.71848396177913
|
||||
],
|
||||
"n_iter_":[
|
||||
27
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{"dual": false, "max_iter": 100, "warm_start": false, "verbose": 0, "C": 100.0, "class_weight": null, "random_state": 1, "fit_intercept": true, "multi_class": "multinomial", "intercept_scaling": 1, "penalty": "l2", "solver": "newton-cg", "n_jobs": 1, "tol": 0.0001}
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,355 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"[Sebastian Raschka](http://sebastianraschka.com), 2015\n",
|
||||
"\n",
|
||||
"https://github.com/rasbt/python-machine-learning-book"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Python Machine Learning - Code Examples"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Bonus Material - A Basic Pipeline and Grid Search Setup"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Note that the optional watermark extension is a small IPython notebook plugin that I developed to make the code reproducible. You can just skip the following line(s)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Sebastian Raschka \n",
|
||||
"Last updated: 01/20/2016 \n",
|
||||
"\n",
|
||||
"CPython 3.5.1\n",
|
||||
"IPython 4.0.1\n",
|
||||
"\n",
|
||||
"numpy 1.10.1\n",
|
||||
"pandas 0.17.1\n",
|
||||
"matplotlib 1.5.0\n",
|
||||
"scikit-learn 0.17\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%load_ext watermark\n",
|
||||
"%watermark -a 'Sebastian Raschka' -u -d -v -p numpy,pandas,matplotlib,scikit-learn"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Fitting 5 folds for each of 8 candidates, totalling 40 fits\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[Parallel(n_jobs=-1)]: Done 40 out of 40 | elapsed: 0.2s finished\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"GridSearchCV(cv=5, error_score='raise',\n",
|
||||
" estimator=Pipeline(steps=[('std', StandardScaler(copy=True, with_mean=True, with_std=True)), ('svc', SVC(C=10.0, cache_size=200, class_weight=None, coef0=0.0,\n",
|
||||
" decision_function_shape='ovr', degree=3, gamma=0.1, kernel='rbf',\n",
|
||||
" max_iter=-1, probability=False, random_state=None, shrinking=True,\n",
|
||||
" tol=0.001, verbose=False))]),\n",
|
||||
" fit_params={}, iid=True, n_jobs=-1,\n",
|
||||
" param_grid=[{'svc__kernel': ['rbf'], 'svc__C': [1, 10, 100, 1000], 'svc__gamma': [0.001, 0.0001]}],\n",
|
||||
" pre_dispatch='2*n_jobs', refit=True, scoring='accuracy', verbose=1)"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from sklearn.grid_search import GridSearchCV\n",
|
||||
"from sklearn.pipeline import Pipeline\n",
|
||||
"from sklearn.preprocessing import StandardScaler\n",
|
||||
"from sklearn.svm import SVC\n",
|
||||
"from sklearn.datasets import load_iris\n",
|
||||
"from sklearn.cross_validation import train_test_split\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# load and split data\n",
|
||||
"iris = load_iris()\n",
|
||||
"X, y = iris.data, iris.target\n",
|
||||
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42)\n",
|
||||
"\n",
|
||||
"# pipeline setup\n",
|
||||
"cls = SVC(C=10.0, \n",
|
||||
" kernel='rbf', \n",
|
||||
" gamma=0.1, \n",
|
||||
" decision_function_shape='ovr')\n",
|
||||
"\n",
|
||||
"kernel_svm = Pipeline([('std', StandardScaler()), \n",
|
||||
" ('svc', cls)])\n",
|
||||
"\n",
|
||||
"# gridsearch setup\n",
|
||||
"param_grid = [\n",
|
||||
" {'svc__C': [1, 10, 100, 1000], \n",
|
||||
" 'svc__gamma': [0.001, 0.0001], \n",
|
||||
" 'svc__kernel': ['rbf']},\n",
|
||||
" ]\n",
|
||||
"\n",
|
||||
"gs = GridSearchCV(estimator=kernel_svm, \n",
|
||||
" param_grid=param_grid, \n",
|
||||
" scoring='accuracy', \n",
|
||||
" n_jobs=-1, \n",
|
||||
" cv=5, \n",
|
||||
" verbose=1, \n",
|
||||
" refit=True,\n",
|
||||
" pre_dispatch='2*n_jobs')\n",
|
||||
"\n",
|
||||
"# run gridearch\n",
|
||||
"gs.fit(X_train, y_train)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Best GS Score 0.96\n",
|
||||
"best GS Params {'svc__kernel': 'rbf', 'svc__C': 100, 'svc__gamma': 0.001}\n",
|
||||
"\n",
|
||||
"Train Accuracy: 0.97\n",
|
||||
"\n",
|
||||
"Test Accuracy: 0.97\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print('Best GS Score %.2f' % gs.best_score_)\n",
|
||||
"print('best GS Params %s' % gs.best_params_)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# prediction on the training set\n",
|
||||
"y_pred = gs.predict(X_train)\n",
|
||||
"train_acc = (y_train == y_pred).sum()/len(y_train)\n",
|
||||
"print('\\nTrain Accuracy: %.2f' % (train_acc))\n",
|
||||
"\n",
|
||||
"# evaluation on the test set\n",
|
||||
"y_pred = gs.predict(X_test)\n",
|
||||
"test_acc = (y_test == y_pred).sum()/len(y_test)\n",
|
||||
"print('\\nTest Accuracy: %.2f' % (test_acc))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"source": [
|
||||
"### A Note about `GridSearchCV`'s `best_score_` attribute"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Please note that `gs.best_score_` is the average k-fold cross-validation score. I.e., if we have a `GridSearchCV` object with 5-fold cross-validation (like the one above), the `best_score_` attribute returns the average score over the 5-folds of the best model. To illustrate this with an example:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([ 0.6, 0.4, 0.6, 0.2, 0.6])"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from sklearn.cross_validation import StratifiedKFold, cross_val_score\n",
|
||||
"from sklearn.linear_model import LogisticRegression\n",
|
||||
"import numpy as np\n",
|
||||
"\n",
|
||||
"np.random.seed(0)\n",
|
||||
"np.set_printoptions(precision=6)\n",
|
||||
"y = [np.random.randint(3) for i in range(25)]\n",
|
||||
"X = (y + np.random.randn(25)).reshape(-1, 1)\n",
|
||||
"\n",
|
||||
"cv5_idx = list(StratifiedKFold(y, n_folds=5, shuffle=False, random_state=0))\n",
|
||||
"cross_val_score(LogisticRegression(random_state=123), X, y, cv=cv5_idx)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"By executing the code above, we created a simple data set of random integers that shall represent our class labels. Next, we fed the indices of 5 cross-validation folds (`cv3_idx`) to the `cross_val_score` scorer, which returned 5 accuracy scores -- these are the 5 accuracy values for the 5 test folds. \n",
|
||||
"\n",
|
||||
"Next, let us use the `GridSearchCV` object and feed it the same 5 cross-validation sets (via the pre-generated `cv3_idx` indices):"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Fitting 5 folds for each of 1 candidates, totalling 5 fits\n",
|
||||
"[CV] ................................................................\n",
|
||||
"[CV] ....................................... , score=0.600000 - 0.0s\n",
|
||||
"[CV] ................................................................\n",
|
||||
"[CV] ....................................... , score=0.400000 - 0.0s\n",
|
||||
"[CV] ................................................................\n",
|
||||
"[CV] ....................................... , score=0.600000 - 0.0s\n",
|
||||
"[CV] ................................................................\n",
|
||||
"[CV] ....................................... , score=0.200000 - 0.0s\n",
|
||||
"[CV] ................................................................\n",
|
||||
"[CV] ....................................... , score=0.600000 - 0.0s\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[Parallel(n_jobs=1)]: Done 5 out of 5 | elapsed: 0.0s finished\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from sklearn.grid_search import GridSearchCV\n",
|
||||
"gs = GridSearchCV(LogisticRegression(), {}, cv=cv5_idx, verbose=3).fit(X, y) "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"As we can see, the scores for the 5 folds are exactly the same as the ones from `cross_val_score` earlier. \n",
|
||||
"Now, the best_score_ attribute of the `GridSearchCV` object, which becomes available after `fit`ting, returns the average accuracy score of the best model:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"0.47999999999999998"
|
||||
]
|
||||
},
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"gs.best_score_"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"As we can see, the result above is consistent with the average score computed the `cross_val_score`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"0.47999999999999998"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"cross_val_score(LogisticRegression(), X, y, cv=cv5_idx).mean()"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.1"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
Reference in New Issue
Block a user