Commit 2352f6cd authored by Steve Tjoa's avatar Steve Tjoa

removing cross validation

parent 61341eb3
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"cells": [
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy, scipy, matplotlib.pyplot as plt, sklearn, stanford_mir\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[← Back to Index](index.html)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Cross Validation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://en.wikipedia.org/wiki/Cross-validation_(statistics)\">K-fold cross validation</a> is a method for evaluating the correctness of a classifier.\n",
"\n",
"For example, with 10-fold cross validation:\n",
"\n",
"1. Divide the data set into 10 random partitions.\n",
"2. Choose one of the partitions as the test set. Train on the other nine partitions.\n",
"3. Repeat for the partitions.\n",
"\n",
"Why cross validation is good?\n",
"* In K-fold cross validation, evaluation on models can be done K times, but each time on a different partition of the data. \n",
"* It can be used to tune parameters and to choose the best model and/or features."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Setup Data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Load some features from ten kick drums and ten snare drums:\n",
"* training_features is a 2 dimensional vector with zero crossing rate and spectral centroids as features of drum samples."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"training_features, training_labels, scaler = stanford_mir.get_features()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[-0.56578947 0.2490469 ]\n",
" [-0.92105263 -0.63084112]\n",
" [-1. -1. ]\n",
" [-1. 0.32776395]\n",
" [-0.77631579 -0.55766302]\n",
" [-0.96052632 -0.69890631]\n",
" [-0.68421053 -0.6353643 ]\n",
" [-0.73684211 -0.10209073]\n",
" [-0.82894737 -0.38692759]\n",
" [-0.69736842 -0.5365142 ]\n",
" [ 0.53947368 0.89837486]\n",
" [ 0.63157895 0.55248334]\n",
" [ 1. 0.99378504]\n",
" [ 0.93421053 1. ]\n",
" [ 0.85526316 0.87391641]\n",
" [ 0.89473684 0.96797404]\n",
" [ 0.23684211 0.86119147]\n",
" [ 0.97368421 0.79526304]\n",
" [ 0.40789474 0.38793404]\n",
" [ 0.88157895 0.88600432]]\n",
"[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1. 1. 1. 1.\n",
" 1. 1.]\n"
]
}
],
"source": [
"print training_features\n",
"print training_labels"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Plot their features:"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.collections.PathCollection at 0x1c121d9b50>"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYYAAAD8CAYAAABzTgP2AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAGu9JREFUeJzt3X+MXeV95/H3J8Y2U1XN2HiS2AOO\njdbrhIqVnb2l2bXUJAQYJ39gl9LESFFMCrKSLV1po1ixRaWuaCOc+g92q7KbuIRA0gpIqONMRaKp\nwdBIu4H6WiYMBg0ezGaZGS92A5Nql6mxzXf/uGfIOcO9c+/MOffXzOclXd1znvOcc75+5vp+73nO\nc85RRGBmZjbtPe0OwMzMOosTg5mZZTgxmJlZhhODmZllODGYmVmGE4OZmWU4MZiZWYYTg5mZZTgx\nmJlZxiXtDmA+Vq1aFevWrWt3GGZmXeXYsWP/FBF99ep1ZWJYt24d5XK53WGYmXUVST9vpJ67kszM\nLMOJwczMMpwYzMwsw4nBzMwynBjMzCzD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1T6YL8/Y1mRbVX1szJLvDcDR5K2GQR2qDJqaT2wAfjHHLE0HJOkzcA3gRsj4kyqvOrfsoCYGo1r\ndWr2RuDFZHoIuCGJbwVwA9kj5qbGlcS2kcrJ3J+myprZXvUMAp9PRid9FPhl8qOneW3VjLPsnfAC\nfpdKRj0HvAYMJeVrgB+l6n0aeIlK5r8zVX4llf+8o8D3geUFxXUZ8ARwMnlfmZSXgPtS9dYB48B7\nZqx/BBim8iX318Cvtyou4N8n+/5Z8n5bM9urwZg+B5wHnk29NjWjrap9Vqh0Td2YTF+a/NtHk7a4\nMrXuncl6I8CnCvyc14vp8eTzP902g/X+li2K627gRLL/J4EPpdb9g6QNR4EvtDKuZP4/A/tmrNe0\n9qLy4+908jkeo3Iu6IvAF5PlAu5NYh4mNcqyWW3lK5/NzCxjsXclmZnZDE4MZmaW4cRgZmYZTgxm\nZpbhxGBmZhlODGZmluHEYGZmGU4MZmaW8f8B0tI9wTs74GUAAAAASUVORK5CYII=\n",
"text/plain": [
"<matplotlib.figure.Figure at 0x1c11f931d0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.scatter(training_features[:,0], training_features[:,1])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Load model and perform cross-validation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Initialize the K-Nearest Neighbor classifier:\n",
"* Note that K=3 here (3 neighbors), but K is so-called a hyperparameter, which can be tuned to select the best value (We will get to that below!)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"model = sklearn.neighbors.KNeighborsClassifier(n_neighbors=3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Perform 5-fold cross validation (cv=5):"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"acc = sklearn.model_selection.cross_val_score(model, training_features, training_labels, cv=5)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 1. 1. 1. 1. 1.]\n",
"1.0\n"
]
}
],
"source": [
"print acc\n",
"print acc.mean()"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"### Tuning parameter K"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since value of K has been arbitrarily chosen, we do not know whether it was the best choice (although here in this example, we have a perfect score anyway...). \n",
"Therefore, testing the result with several other values will help to choose the best parameter."
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[K=1] Accuracy=1.000\n",
"[K=2] Accuracy=1.000\n",
"[K=3] Accuracy=1.000\n",
"[K=4] Accuracy=1.000\n",
"[K=5] Accuracy=1.000\n"
]
}
],
"source": [
"K_choices = [1,2,3,4,5]\n",
"for k in K_choices:\n",
" model = sklearn.neighbors.KNeighborsClassifier(n_neighbors=k)\n",
" mean_score = sklearn.model_selection.cross_val_score(model, training_features, training_labels, cv=5).mean()\n",
" print \"[K=%d] Accuracy=%.3f\"%(k, mean_score)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Bonus : Try out other models and perform cross validations to find the best model!"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Logistic regression for binary classification\n",
"model = sklearn.linear_model.LogisticRegression()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[&larr; Back to Index](index.html)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.14"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
......@@ -11919,7 +11919,6 @@ div#notebook {
<div class="text_cell_render border-box-sizing rendered_html">
<ol>
<li><a href="knn.html">K-Nearest Neighbor Classification</a> (<a href="knn.ipynb">ipynb</a>)</li>
<li><a href="cross_validation.html">Cross Validation</a> (<a href="cross_validation.ipynb">ipynb</a>)</li>
<li><a href="knn_instrument_classification.html">Exercise: K-Nearest Neighbor Instrument Classification</a> (<a href="knn_instrument_classification.ipynb">ipynb</a>)</li>
<li><a href="kmeans.html">K-Means Clustering</a> (<a href="kmeans.ipynb">ipynb</a>)</li>
<li><a href="kmeans_instrument_classification.html">Exercise: Unsupervised Instrument Classification using K-Means</a> (<a href="kmeans_instrument_classification.ipynb">ipynb</a>)</li>
......
......@@ -121,7 +121,6 @@
"metadata": {},
"source": [
"1. [K-Nearest Neighbor Classification](knn.html) ([ipynb](knn.ipynb))\n",
"1. [Cross Validation](cross_validation.html) ([ipynb](cross_validation.ipynb))\n",
"1. [Exercise: K-Nearest Neighbor Instrument Classification](knn_instrument_classification.html) ([ipynb](knn_instrument_classification.ipynb))\n",
"1. [K-Means Clustering](kmeans.html) ([ipynb](kmeans.ipynb))\n",
"1. [Exercise: Unsupervised Instrument Classification using K-Means](kmeans_instrument_classification.html) ([ipynb](kmeans_instrument_classification.ipynb))\n",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment