{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": false, "deletable": true, "editable": true }, "source": [ "##
Neural Networks in scikit-learn
\n", "\n", "#### Linear models:\n", "\n", "
ลท = w[0] \\* x[0] + w[1] \\* x[1] + ... + w[p] \\* x[p] + b
" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "image/svg+xml": [ "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "%3\r\n", "\r\n", "cluster_0\r\n", "\r\n", "inputs\r\n", "\r\n", "cluster_2\r\n", "\r\n", "output\r\n", "\r\n", "\r\n", "x[0]\r\n", "\r\n", "x[0]\r\n", "\r\n", "\r\n", "y\r\n", "\r\n", "y\r\n", "\r\n", "\r\n", "x[0]->y\r\n", "\r\n", "\r\n", "w[0]\r\n", "\r\n", "\r\n", "x[1]\r\n", "\r\n", "x[1]\r\n", "\r\n", "\r\n", "x[1]->y\r\n", "\r\n", "\r\n", "w[1]\r\n", "\r\n", "\r\n", "x[2]\r\n", "\r\n", "x[2]\r\n", "\r\n", "\r\n", "x[2]->y\r\n", "\r\n", "\r\n", "w[2]\r\n", "\r\n", "\r\n", "x[3]\r\n", "\r\n", "x[3]\r\n", "\r\n", "\r\n", "x[3]->y\r\n", "\r\n", "\r\n", "w[3]\r\n", "\r\n", "\r\n", "\r\n" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import mglearn\n", "\n", "mglearn.plots.plot_logistic_regression_graph()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "image/svg+xml": [ "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "%3\r\n", "\r\n", "cluster_1\r\n", "\r\n", "hidden layer\r\n", "\r\n", "cluster_2\r\n", "\r\n", "output\r\n", "\r\n", "cluster_0\r\n", "\r\n", "inputs\r\n", "\r\n", "\r\n", "x[0]\r\n", "\r\n", "x[0]\r\n", "\r\n", "\r\n", "h0\r\n", "\r\n", "h[0]\r\n", "\r\n", "\r\n", "x[0]->h0\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "h1\r\n", "\r\n", "h[1]\r\n", "\r\n", "\r\n", "x[0]->h1\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "h2\r\n", "\r\n", "h[2]\r\n", "\r\n", "\r\n", "x[0]->h2\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "x[1]\r\n", "\r\n", "x[1]\r\n", "\r\n", "\r\n", "x[1]->h0\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "x[1]->h1\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "x[1]->h2\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "x[2]\r\n", "\r\n", "x[2]\r\n", "\r\n", "\r\n", "x[2]->h0\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "x[2]->h1\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "x[2]->h2\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "x[3]\r\n", "\r\n", "x[3]\r\n", "\r\n", "\r\n", "x[3]->h0\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "x[3]->h1\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "x[3]->h2\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "y\r\n", "\r\n", "y\r\n", "\r\n", "\r\n", "h0->y\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "h1->y\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "h2->y\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mglearn.plots.plot_single_hidden_layer_graph()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "image/svg+xml": [ "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "%3\r\n", "\r\n", "cluster_1\r\n", "\r\n", "hidden layer 1\r\n", "\r\n", "cluster_0\r\n", "\r\n", "inputs\r\n", "\r\n", "cluster_2\r\n", "\r\n", "hidden layer 2\r\n", "\r\n", "cluster_3\r\n", "\r\n", "output\r\n", "\r\n", "\r\n", "x[0]\r\n", "\r\n", "x[0]\r\n", "\r\n", "\r\n", "h1[0]\r\n", "\r\n", "h1[0]\r\n", "\r\n", "\r\n", "x[0]->h1[0]\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "h1[1]\r\n", "\r\n", "h1[1]\r\n", "\r\n", "\r\n", "x[0]->h1[1]\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "h1[2]\r\n", "\r\n", "h1[2]\r\n", "\r\n", "\r\n", "x[0]->h1[2]\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "x[1]\r\n", "\r\n", "x[1]\r\n", "\r\n", "\r\n", "x[1]->h1[0]\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "x[1]->h1[1]\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "x[1]->h1[2]\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "x[2]\r\n", "\r\n", "x[2]\r\n", "\r\n", "\r\n", "x[2]->h1[0]\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "x[2]->h1[1]\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "x[2]->h1[2]\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "x[3]\r\n", "\r\n", "x[3]\r\n", "\r\n", "\r\n", "x[3]->h1[0]\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "x[3]->h1[1]\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "x[3]->h1[2]\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "h2[0]\r\n", "\r\n", "h2[0]\r\n", "\r\n", "\r\n", "h1[0]->h2[0]\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "h2[1]\r\n", "\r\n", "h2[1]\r\n", "\r\n", "\r\n", "h1[0]->h2[1]\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "h2[2]\r\n", "\r\n", "h2[2]\r\n", "\r\n", "\r\n", "h1[0]->h2[2]\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "h1[1]->h2[0]\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "h1[1]->h2[1]\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "h1[1]->h2[2]\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "h1[2]->h2[0]\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "h1[2]->h2[1]\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "h1[2]->h2[2]\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "y\r\n", "\r\n", "y\r\n", "\r\n", "\r\n", "h2[0]->y\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "h2[1]->y\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "h2[2]->y\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mglearn.plots.plot_two_hidden_layer_graph()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Accuracy on the training subset: 0.906\n", "Accuracy on the test subset: 0.881\n" ] } ], "source": [ "from sklearn.neural_network import MLPClassifier\n", "from sklearn.datasets import load_breast_cancer\n", "from sklearn.model_selection import train_test_split\n", "\n", "cancer = load_breast_cancer()\n", "\n", "X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target, random_state=0)\n", "\n", "mlp = MLPClassifier(random_state=42)\n", "mlp.fit(X_train, y_train)\n", "\n", "print('Accuracy on the training subset: {:.3f}'.format(mlp.score(X_train, y_train)))\n", "print('Accuracy on the test subset: {:.3f}'.format(mlp.score(X_test, y_test)))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The maximum per each feature:\n", "[ 2.81100000e+01 3.92800000e+01 1.88500000e+02 2.50100000e+03\n", " 1.63400000e-01 3.45400000e-01 4.26800000e-01 2.01200000e-01\n", " 3.04000000e-01 9.74400000e-02 2.87300000e+00 4.88500000e+00\n", " 2.19800000e+01 5.42200000e+02 3.11300000e-02 1.35400000e-01\n", " 3.96000000e-01 5.27900000e-02 7.89500000e-02 2.98400000e-02\n", " 3.60400000e+01 4.95400000e+01 2.51200000e+02 4.25400000e+03\n", " 2.22600000e-01 1.05800000e+00 1.25200000e+00 2.91000000e-01\n", " 6.63800000e-01 2.07500000e-01]\n" ] } ], "source": [ "print('The maximum per each feature:\\n{}'.format(cancer.data.max(axis=0)))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Accuracy on the training subset: 0.995\n", "Accuracy on the test subset: 0.958\n" ] } ], "source": [ "from sklearn.preprocessing import StandardScaler\n", "\n", "scaler = StandardScaler()\n", "X_train_scaled = scaler.fit(X_train).transform(X_train)\n", "X_test_scaled = scaler.fit(X_test).transform(X_test)\n", "\n", "mlp = MLPClassifier(max_iter=1000, random_state=42) \n", "\n", "mlp.fit(X_train_scaled, y_train)\n", "\n", "print('Accuracy on the training subset: {:.3f}'.format(mlp.score(X_train_scaled, y_train)))\n", "print('Accuracy on the test subset: {:.3f}'.format(mlp.score(X_test_scaled, y_test)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [] } ], "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": 2 }