1
0
mirror of https://github.com/gsi-upm/sitc synced 2024-11-24 23:42:29 +00:00
sitc/ml3/2_4_1_Exercise.ipynb

666 lines
608 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](images/EscUpmPolit_p.gif \"UPM\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Course Notes for Learning Intelligent Systems"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-03-06 16:46:12 +00:00
"Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © Óscar Araque"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## [Introduction to Machine Learning III](2_4_0_Intro_NN.ipynb)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# MultiLayer Perceptron (MLP) Introduction"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Multilayer perceptrons, also called feedforward neural networks or deep feedforward networks, are the most basic deep learning models."
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2017-03-15T12:33:49.116461",
"start_time": "2017-03-15T12:33:49.111870"
}
},
"source": [
"<img src=\"images/multilayerperceptron_network.png\" alt=\"Drawing\" style=\"width: 400px;\"/>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this notebook we are going to try the spiral dataset with different algorthms. In particular, we are going to focus our attention on the MLP classifier.\n",
"\n",
"\n",
"Answer directly in your copy of the exercise and submit it as a moodle task."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load dataset"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2017-03-16T18:10:16.146770",
"start_time": "2017-03-16T18:10:15.825583"
},
"collapsed": true
},
"outputs": [],
"source": [
"# Show plots in the notebooks\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2017-03-16T18:10:16.200490",
"start_time": "2017-03-16T18:10:16.149330"
}
},
"outputs": [],
"source": [
"# Load the utilities\n",
"from spiral import *"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2017-03-16T18:10:16.662881",
"start_time": "2017-03-16T18:10:16.203138"
}
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"sns.set(color_codes=True)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2017-03-16T18:10:17.018804",
"start_time": "2017-03-16T18:10:16.665477"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of classes: 5\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAloAAAGrCAYAAAAYfTnLAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XdgFNXax/HvbMlm00ivdEINXXqV3hEBEUFQqSLSFCwI\niqiAFKUIKAJSRUAEG0URlV6kCIQSQiAhpPe2abvz/hFv7s0bvF4gYVOez1/snN3Nbw6TzbNnzpxR\nVFVVEUIIIYQQRU5j7QBCCCGEEGWVFFpCCCGEEMVECi0hhBBCiGIihZYQQgghRDGRQksIIYQQophI\noSWEEEIIUUx0D/PihQsXcu7cOcxmM+PGjaNBgwbMmDEDVVXx8PBg4cKF6PX6osoqhBBCCFGqKA+6\njtapU6dYv349n332GUlJSTz55JO0atWKxx9/nB49evDxxx/j4+PD0KFDizqzEEIIIUSp8MCFlqqq\nZGVlYWtri6qqtGrVCkdHR/bt24der+fChQusX7+e5cuXF3VmIYQQQohS4YHnaCmKgq2tLQA7d+7k\n8ccfx2Qy5Z8qdHNzIzY2tmhSCiGEEEKUQg89Gf7gwYPs2rWL2bNnF9iuqiqKojzs2wshhBBClFoP\nVWgdOXKENWvWsHbtWhwcHLCzsyM7OxuA6OhoPDw8/uvr5TaLQgghhCjLHviqw7S0NBYtWsSGDRtw\ndHQEo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szb968B317IYQoV3xqN+Db\nS0ex0Sg4VWmYv/361ct8t3YFiqLSfdhYGjZtbsWUQoj79cCFVkBAAJs3by60vVu3bg8VSAghyqNB\nI8Zw40Z7zGYzderUy9/
"text/plain": [
"<matplotlib.figure.Figure at 0x7f5f80361748>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# load and plot the spiral dataset\n",
"n_classes = 5\n",
"X, y = load_spiral_dataset(n_classes=n_classes)\n",
"\n",
"plt.figure(figsize=(10,7))\n",
"plot_dataset(X, y)\n",
"\n",
"print('Number of classes: {}'.format(n_classes))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2017-03-16T18:10:17.083617",
"start_time": "2017-03-16T18:10:17.021488"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1050, 2)\n",
"(450, 2)\n",
"(1050,)\n",
"(450,)\n"
]
}
],
"source": [
"from sklearn.metrics import classification_report\n",
"from sklearn.model_selection import train_test_split\n",
"\n",
"# split the dataset in train and test\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)\n",
"\n",
"# check the dimensions\n",
"print(X_train.shape)\n",
"print(X_test.shape)\n",
"print(y_train.shape)\n",
"print(y_test.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The features are simply the position of each point in the 2 dimension plane.\n",
"\n",
"In other words, a point $\\mathbf{x}$ is represented by its values $x_1$ and $x_2$:\n",
"\n",
"$\\mathbf{x} = [x_1, x_2] $"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Perform the classification task on several classifiers"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Following, the classification on the spiral is done with several classifiers. We can see the performance on each class (each spiral), and their decision surfaces."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Logistic Regression"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2017-03-16T18:10:18.028423",
"start_time": "2017-03-16T18:10:17.089748"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"LR\n",
" precision recall f1-score support\n",
"\n",
" 0 0.29 0.35 0.32 83\n",
" 1 0.31 0.28 0.29 90\n",
" 2 0.20 0.23 0.21 79\n",
" 3 0.34 0.29 0.31 109\n",
" 4 0.25 0.25 0.25 89\n",
"\n",
"avg / total 0.28 0.28 0.28 450\n",
"\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlQAAAGmCAYAAAC6KhqmAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XeYnGd18P/vU6a3ndmd7U1a9W7Lli13uTuAwQaCwfi1\nKe8vEANJKAkl7xsnNCcvSUgIqUCCDQnEgGmxLcC4yZa7rbLqdXvf2Z0+85TfHyOttFrJkrVldnbP\n57p8XatnZp4583h29sy57/vcim3bNkIIIYQQ4rypxQ5ACCGEEKLUSUIlhBBCCDFJklAJIYQQQkyS\nJFRCCCGEEJMkCZUQQgghxCRJQiWEEEIIMUl6MZ+8vz8+9nM47GV4OFXEaGY/uUbnRq7TuZHrdHZy\njc6NXKdzI9fp7Gb7NYpGA2e8bdZUqHRdK3YIs55co3Mj1+ncyHU6O7lG50au07mR63R2pXyNZk1C\nJYQQQghRqiShEkIIIYSYJEmohBBCCCEmSRIqIYQQQohJkoRKCCGEEGKSJKESQgghhJgkSaiEEEII\nISZJEiohhBBCiEmShEoIIYQQYpIkoRJCCCGEmCRJqIQQQgghJkkSKiGEEEKISZKESgghhBBikiSh\nEkIIIYSYJEmohBBCCCEmSS92ANPt8dv+V7FDmDK6rmIYVrHDmPXkOp0buU5nJ9fo3LyZ67SwKkeg\nZcE0RzQ7jegqeXk/vaHJXKOKz3x1iqN5c6RCJYQQQggxSZJQCSGEmBHzuTol5j5JqIQQQgghJmnO\nz6ESYr55KdsPfhhJ5rlcr8Sjjv81z1omW4xeQk4nRtbiUl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
"text/plain": [
"<matplotlib.figure.Figure at 0x7f5f87e3a978>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from sklearn.linear_model import LogisticRegression\n",
"\n",
"lr = LogisticRegression(n_jobs=-1)\n",
"lr.fit(X,y)\n",
"\n",
"lr_preds = lr.predict(X_test)\n",
"\n",
"print('LR')\n",
"print(classification_report(y_test, lr_preds))\n",
"\n",
"plt.figure(figsize=(10,7))\n",
"plot_decision_surface(X, y, lr)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### k-NN"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2017-03-16T18:10:23.248268",
"start_time": "2017-03-16T18:10:18.031503"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"K-NN\n",
" precision recall f1-score support\n",
"\n",
" 0 1.00 1.00 1.00 83\n",
" 1 1.00 1.00 1.00 90\n",
" 2 1.00 1.00 1.00 79\n",
" 3 1.00 1.00 1.00 109\n",
" 4 1.00 1.00 1.00 89\n",
"\n",
"avg / total 1.00 1.00 1.00 450\n",
"\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlQAAAGmCAYAAAC6KhqmAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xd0pGd9//33XaY3jaRR79qi1TbXXfeCCxhXisFgbIMh\nhQCBEPiFcJ6cX87v4SEOIcSE5JeEEJLYAUJsbMDghhu4t7W3F616rzOj6TN3ef7QSiuttKtVnRnp\nep2z50gzo5lr7h3NfHRd3/t7SaZpmgiCIAiCIAiLJmd7AIIgCIIgCPlOBCpBEARBEIQlEoFKEARB\nEARhiUSgEgRBEARBWCIRqARBEARBEJZIBCpBEARBEIQlUrP54MPDkamv/X4nwWA8i6PJfeIYnR1x\nnM6OOE7zE8fo7IjjdHbEcZpfrh+jQMBz2utyZoZKVZVsDyHniWN0dsRxOjviOM1PHKOz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/vRFVP7tIeCMxukrdU6hz7bOWztfZ8Vko+H6euUfusy2qL\nkkRPCVn+zY1LfixLHh+n1ZLrx+j25k08euQ40UyajzZvnhpvPKPxXFcXf3JZM692DfFqXz9XTit0\nX265fpxyRa4ep+KmUpBAKcp+QbpqUea/0RwCxcufIRZiWQPVH//xH/PVr36V6upqXn/9dTZt2nTG\n2weD8amvAwEPw8OR5RwOANoc+3DlK1WV19TzWSlr8Ti1Ds5eXm+gddZlC5nFsqjynPvUCSflyzG6\nbdOGqa8nxzsWT1Jd4ESWJHaWFfKTdzpnPBfTnNhQZDlmrfLlOGVbLh8nx65rkVQrWkaf/8YrSLUo\nix5DcAUyxKnONPGz6EB
"text/plain": [
"<matplotlib.figure.Figure at 0x7f5f4c074780>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from sklearn.neighbors import KNeighborsClassifier\n",
"\n",
"knn = KNeighborsClassifier()\n",
"knn.fit(X_train, y_train)\n",
"\n",
"knn_preds = knn.predict(X_test)\n",
"\n",
"print('K-NN')\n",
"print(classification_report(y_test, knn_preds))\n",
"\n",
"plt.figure(figsize=(10,7))\n",
"plot_decision_surface(X, y, knn)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Gaussian Naive Bayes"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2017-03-16T18:10:24.946571",
"start_time": "2017-03-16T18:10:23.251722"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"GaussianNB\n",
" precision recall f1-score support\n",
"\n",
" 0 0.24 0.36 0.29 83\n",
" 1 0.22 0.16 0.18 90\n",
" 2 0.22 0.29 0.25 79\n",
" 3 0.33 0.27 0.30 109\n",
" 4 0.29 0.22 0.25 89\n",
"\n",
"avg / total 0.26 0.26 0.26 450\n",
"\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlQAAAGmCAYAAAC6KhqmAAAABHNCSVQICAgIfAhkiA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LF0jzJTXauG22hqi\nEYUPrarFbTn+3vtSVzf/57JV3LVlJQPhYAqjFISlbVbrwzfccAN33303P//5z6mqquIHP/jBbB5e\nEBYkUa0SZoPbYmZtrg/nW77IOkwmOvwTxBIJJmKJKftaRv08cOAQf2rtwDCM+QxXEJacWb3L76KL\nLpr8+eqrr+arX/3qaR/v9dpR1eOrtPt80yfJm6kTj78YLPTno55k3bOFfJ6z1TGcnHuo9OidgN6q\nilSGM8mUZtcpHaX7NXpfTSWP1jcRjMd4f03VZLyhuMafOzr43CU17OwYYGdPL5ef0Og+29L9OqUL\ncZ3O7Hyv0VzkEOdiVhOqz372s9xzzz0UFxfz2muvUVlZedrHj46GJn/2+VwMDo7PZjgAaFrizA9a\nIFRVWfDPRzvJumizTVXleTnP+Wg+2rQ+cqQ55Q3rJlU+6Tp1wnEL5RrdWLl88udj8Y6EIhRn2JEl\nibV5mfzyzfYpz+VYxWo2lr5ZKNcp1cR1OrOZXKO5yCHe6nRJ23knVHV1ddx333309PSgqipPP/00\n27dv53Of+xw2mw2Hw8HXv/718z28ICxa4k5AYT7k2G0MBGL8+PVG+sbD/FXNysl9b/YN8Hp/HxKw\nPjeXjXm5qQtUEBaJ806oamtrefDBB6dt37Zt24wCEha339x+D+994FupDiPlxELLwlyTJIntq2sI\naxrWt9w5uHegny9cWgv
"text/plain": [
"<matplotlib.figure.Figure at 0x7f5f44666c88>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from sklearn.naive_bayes import GaussianNB\n",
"\n",
"gnb = GaussianNB()\n",
"gnb.fit(X_train, y_train)\n",
"\n",
"gnb_preds = gnb.predict(X_test)\n",
"\n",
"print('GaussianNB')\n",
"print(classification_report(y_test, gnb_preds))\n",
"\n",
"plt.figure(figsize=(10,7))\n",
"plot_decision_surface(X, y, gnb)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### SVM"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2017-03-16T18:11:56.862050",
"start_time": "2017-03-16T18:10:24.949917"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"SVM\n",
" precision recall f1-score support\n",
"\n",
" 0 1.00 1.00 1.00 83\n",
" 1 1.00 1.00 1.00 90\n",
" 2 1.00 1.00 1.00 79\n",
" 3 1.00 1.00 1.00 109\n",
" 4 1.00 1.00 1.00 89\n",
"\n",
"avg / total 1.00 1.00 1.00 450\n",
"\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlQAAAGmCAYAAAC6KhqmAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xd83Fed//vXt0xvGkmj3iVbsuSSYsdxeqel0AIhIQmw\nbGGBbcBd4HHv/e3j9+OyYeFHWXZ/29hCQlk2kLC0xCEhgcTpceJeZFu9l5nR9JnvfL/3j7FkyZJt\nWW2KzvPxyOOhfGc8Ojqa8tY5n3OOZBiGgSAIgiAIgrBkcrYbIAiCIAiCkO9EoBIEQRAEQVgmEagE\nQRAEQRCWSQQqQRAEQRCEZRKBShAEQRAEYZlEoBIEQRAEQVgmNZvffGwsNPO112vH749msTW5T/TR\n4oh+WhzRTxcm+mhxRD8tjuinC8v1PvL5XOe8LWdGqFRVyXYTcp7oo8UR/bQ4op8uTPTR4oh+WhzR\nTxeWz32UM4FKEARBEAQhX4lAJQiCIAiCsEwiUAmCIAiCICyTCFSCIAiCIAjLJAKVIAiCIAjCMolA\nJQiCIAiCsEwiUAmCIAiCICyTCFSCIAiCIAjLJAKVIAiCIAjCMolAJQiCIAiCsEwiUAmCIAiCICyT\nCFSCIAiCIAjLJAKVIAiCIAjCMolAJQiCIAiCsEwiUAmCIAiCICyTmu0GrLbxr34h201YMUFVJqXp\n2W5GzhP9tDiiny5M9NHiiH5aHNFPF7acPir93F+vcGsujhihEgRBEARBWCYRqARBEARBEJZJBCpB\nEARBEIRlKvgaKkFYb37WeZJgMk4smebejk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.Figure at 0x7f5f446048d0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from sklearn.svm import SVC\n",
"\n",
"svc = SVC()\n",
"svc.fit(X_train, y_train)\n",
"\n",
"svc_preds = svc.predict(X_test)\n",
"\n",
"print('SVM')\n",
"print(classification_report(y_test, svc_preds))\n",
"\n",
"plt.figure(figsize=(10,7))\n",
"plot_decision_surface(X, y, svc)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### MLP"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2017-03-16T18:12:01.663538",
"start_time": "2017-03-16T18:11:56.864322"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python3.5/dist-packages/sklearn/neural_network/multilayer_perceptron.py:563: ConvergenceWarning: Stochastic Optimizer: Maximum iterations reached and the optimization hasn't converged yet.\n",
" % (), ConvergenceWarning)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"MLP\n",
" precision recall f1-score support\n",
"\n",
" 0 0.28 0.42 0.34 83\n",
" 1 0.46 0.26 0.33 90\n",
" 2 0.32 0.49 0.39 79\n",
" 3 0.67 0.36 0.47 109\n",
" 4 0.34 0.37 0.36 89\n",
"\n",
"avg / total 0.43 0.38 0.38 450\n",
"\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlQAAAGmCAYAAAC6KhqmAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XdgXFeZ8P/vLdOLNJJGXbKKbcmSW+LEiZ3qdAipkBBI\nob67S18ILAR2WX5LfgvsspQXdtkC2SWhbDaQsKRhQgrEcUucxEXusmyrt9FIM5o+975/jC1blrvK\nqDyfv2buHd955ng088w5zzlHMU3TRAghhBBCnDc12wEIIYQQQsx0klAJIYQQQoyTJFRCCCGEEOMk\nCZUQQgghxDhJQiWEEEIIMU6SUAkhhBBCjJOezS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LV1NTEN7/5TTo6OtB1nbVr13L//ffz2c9+FofDgcvl4u///u/P9/JCCCHGodDp\noGcowU/e2EdXKMr7GhaNnHurq4c3urtQgAuKilhRXJS9QIWYJc47oWpsbOSxxx4bc/z6668fV0BC\niNkh1NyCp7Y622HMWYqicP+SBqKpFPYTZg6+3dPN569oBOB763ZKQiXEBJDBXCHEhJOZftOHQ9fH\nDOsZJoTjScKJJOkTatX3BQZ4vaOLtCFDU0KcC9l6Rggh5pi76uv46ZZmTEzuqj9W6/rHw20kiVOZ\n6+an25v48LIlWYxSiJlFEiohhJhjvDYr9y1eNOb4oaFBPrU6s9fg6219Ux2WEDOaDPkJISaNrJg+\ns8zz5vBk0yFeb+sjnjw25Jc2DJ7Zf4Bf7tzNYDyexQiFmL4koRJCTAqpo5p5rqosp9LlIxg2+cCS\nxpHjT+7ZzyXzfLz/gnn
"text/plain": [
"<matplotlib.figure.Figure at 0x7f5f44580c88>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from sklearn.neural_network import MLPClassifier\n",
"\n",
"mlp = MLPClassifier()\n",
"mlp.fit(X_train, y_train)\n",
"\n",
"mlp_preds = mlp.predict(X_test)\n",
"\n",
"print('MLP')\n",
"print(classification_report(y_test, mlp_preds))\n",
"\n",
"plt.figure(figsize=(10,7))\n",
"plot_decision_surface(X, y, mlp)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"We see that some classifiers (kNN, SVM) successfully learn the spiral problem. They can classify correctly in any part of the plane.\n",
"\n",
"Nevertheless, some classifiers (Logistic Regression, Gaussian Naive Bayes) are not able to learn the spiral pattern with their default configurations.\n",
"\n",
"In particular, the MLP performs very bad: it is not able to learn the spiral function. Nevertheless, it should be able to."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"**QUESTION: Why do you think that MLP does not learn the spiral pattern?**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Answer here:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Try to make the MLP learn the spiral!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Your task is to learn the spiral with the MLP classifier.\n",
"\n",
"Write the necessary code in the following cells.\n",
"\n",
"You should try to change some parameters of the MLPClassifier. Some parameters that you can change are:\n",
"- complexity of the network\n",
"- regularization of the network\n",
"- new features that are passed to the network\n",
"\n",
"You can search inspiration on [this playground](http://playground.tensorflow.org)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2017-03-27T11:56:47.325519",
"start_time": "2017-03-27T11:56:47.316384"
},
"collapsed": true
},
"outputs": [],
"source": [
"# write your code in here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# References"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* [MLP documentation](http://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPClassifier.html)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Licence"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n",
"\n",
2019-03-06 16:46:12 +00:00
"© Óscar Araque, Universidad Politécnica de Madrid."
]
}
],
"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",
2019-03-06 16:46:12 +00:00
"version": "3.7.1"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
"autocomplete": true,
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 1,
"hotkeys": {
"equation": "Ctrl-E",
"itemize": "Ctrl-I"
},
"labels_anchors": false,
"latex_user_defs": false,
"report_style_numbering": false,
"user_envs_cfg": false
}
},
"nbformat": 4,
"nbformat_minor": 1
}