{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"BreastCancer.ipynb","version":"0.3.2","provenance":[]},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"markdown","metadata":{"id":"Oex88qd38Thn","colab_type":"text"},"source":["In this first cell we will upload the CSV files that contain the pre-sorted and pre-normalized data. \n","For these I used the Wisconsin breast cancer dataset, sorted it into training and test sets with the 'input' values to the Neural Network as 'X' values, and the expected 'output' (a 0 if benign and a 1 if malignant) as the 'Y' values.\n"]},{"cell_type":"code","metadata":{"id":"m_NRnXsJzphf","colab_type":"code","outputId":"2c052eeb-46d0-47bc-dd17-32b092ed9df7","executionInfo":{"status":"ok","timestamp":1562008054635,"user_tz":300,"elapsed":15474,"user":{"displayName":"Luis Tedeschi","photoUrl":"","userId":"17261487412438553177"}},"colab":{"resources":{"http://localhost:8080/nbextensions/google.colab/files.js":{"data":"Ly8gQ29weXJpZ2h0IDIwMTcgR29vZ2xlIExMQwovLwovLyBMaWNlbnNlZCB1bmRlciB0aGUgQXBhY2hlIExpY2Vuc2UsIFZlcnNpb24gMi4wICh0aGUgIkxpY2Vuc2UiKTsKLy8geW91IG1heSBub3QgdXNlIHRoaXMgZmlsZSBleGNlcHQgaW4gY29tcGxpYW5jZSB3aXRoIHRoZSBMaWNlbnNlLgovLyBZb3UgbWF5IG9idGFpbiBhIGNvcHkgb2YgdGhlIExpY2Vuc2UgYXQKLy8KLy8gICAgICBodHRwOi8vd3d3LmFwYWNoZS5vcmcvbGljZW5zZXMvTElDRU5TRS0yLjAKLy8KLy8gVW5sZXNzIHJlcXVpcmVkIGJ5IGFwcGxpY2FibGUgbGF3IG9yIGFncmVlZCB0byBpbiB3cml0aW5nLCBzb2Z0d2FyZQovLyBkaXN0cmlidXRlZCB1bmRlciB0aGUgTGljZW5zZSBpcyBkaXN0cmlidXRlZCBvbiBhbiAiQVMgSVMiIEJBU0lTLAovLyBXSVRIT1VUIFdBUlJBTlRJRVMgT1IgQ09ORElUSU9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iA9IHNjb3BlLmdvb2dsZS5jb2xhYiB8fCB7fTsKc2NvcGUuZ29vZ2xlLmNvbGFiLl9maWxlcyA9IHsKICBfdXBsb2FkRmlsZXMsCiAgX3VwbG9hZEZpbGVzQ29udGludWUsCn07Cn0pKHNlbGYpOwo=","ok":true,"headers":[["content-type","application/javascript"]],"status":200,"status_text":""}},"base_uri":"https://localhost:8080/","height":176}},"source":["import pandas as pd\n","\n","from google.colab import files\n","file = files.upload()\n","X_train = pd.read_csv(\"xtrain.csv\", header=None)\n","Y_train = pd.read_csv(\"ytrain.csv\", header=None)\n","X_test = pd.read_csv(\"xtest.csv\", header=None)\n","Y_test = pd.read_csv(\"ytest.csv\", header=None)"],"execution_count":1,"outputs":[{"output_type":"display_data","data":{"text/html":["\n"," \n"," \n"," "],"text/plain":[""]},"metadata":{"tags":[]}},{"output_type":"stream","text":["Saving xtest.csv to xtest.csv\n","Saving xtrain.csv to xtrain.csv\n","Saving ytest.csv to ytest.csv\n","Saving ytrain.csv to ytrain.csv\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"mexz-YiQIfp7","colab_type":"code","outputId":"d171bdf3-c240-4061-ddd6-26f13b7b8adc","executionInfo":{"status":"ok","timestamp":1562008062924,"user_tz":300,"elapsed":260,"user":{"displayName":"Luis Tedeschi","photoUrl":"","userId":"17261487412438553177"}},"colab":{"base_uri":"https://localhost:8080/","height":1000}},"source":["print(X_train)"],"execution_count":2,"outputs":[{"output_type":"stream","text":[" 0 1 2 ... 27 28 29\n","0 -1.150365 -0.390642 -1.128550 ... -0.757984 -0.016148 -0.385034\n","1 -0.937990 0.680514 -0.948201 ... -0.606870 0.096690 -0.386158\n","2 0.574121 -1.033336 0.513941 ... -0.023719 -0.200502 -0.751443\n","3 -0.547220 -0.316022 -0.577622 ... -0.852545 -1.076186 -0.546883\n","4 -0.527398 0.791240 -0.561563 ... -0.783118 0.311240 -0.082129\n","5 -0.235736 -1.307744 -0.248218 ... -0.348169 -0.267251 -0.578354\n","6 -0.671813 -1.235531 -0.723795 ... -0.483497 0.312830 -0.933523\n","7 -0.408468 -0.446005 -0.435155 ... -0.895457 -0.343536 -0.821128\n","8 0.030441 0.954923 0.105068 ... 0.961736 3.594657 3.434157\n","9 -0.804901 -1.490683 -0.768264 ... 0.265940 0.697431 0.486591\n","10 1.723778 1.847954 1.724501 ... 1.544121 0.583004 -0.403017\n","11 -0.705793 -0.234181 -0.635679 ... 0.416134 -0.388035 0.517500\n","12 0.064421 -1.367921 0.028482 ... -0.088088 0.250849 -1.279700\n","13 -0.799238 -1.471426 -0.806558 ... -0.412998 -0.272019 -0.183847\n","14 -1.090900 -0.619316 -1.069257 ... -0.476907 -0.338768 0.634953\n","15 -0.921000 0.560159 -0.959731 ... -1.754014 -0.709066 -0.987473\n","16 1.103643 0.338707 1.094516 ... 0.528013 0.551219 -0.406951\n","17 -1.141870 -0.973158 -1.155314 ... -1.594318 0.045834 -0.366489\n","18 -0.258389 -0.802255 -0.319040 ... -1.370867 -0.402338 0.020151\n","19 -0.283874 0.815311 -0.197572 ... -0.028317 1.945002 1.354844\n","20 -0.796406 -0.231774 -0.736559 ... 0.112681 0.641807 2.596812\n","21 -0.728446 -2.029872 -0.744794 ... -1.031398 0.772127 -0.514289\n","22 0.177687 1.508554 0.118656 ... -0.075828 -0.289501 -0.782913\n","23 0.265469 -0.019950 0.224065 ... -0.037513 -0.988776 -0.784037\n","24 2.576110 1.881653 2.539776 ... 2.219993 0.316008 0.184248\n","25 -1.020108 0.249645 -0.954378 ... -0.813617 -0.828261 0.380378\n","26 -0.501913 -1.656772 -0.530270 ... -0.114142 0.327133 -0.425496\n","27 -0.164944 -1.976916 -0.160926 ... -0.618824 -0.308572 -0.650849\n","28 1.551046 -0.241402 1.600975 ... 1.689717 1.174210 0.491087\n","29 -0.640665 -0.407492 -0.670267 ... -0.353226 0.312830 -0.754252\n",".. ... ... ... ... ... ... ...\n","425 -1.233899 2.177725 -1.240959 ... -1.008256 -0.926795 -0.639609\n","426 -0.671813 -1.074256 -0.638562 ... -0.135599 -0.477034 0.859743\n","427 -1.668843 0.372406 -1.587245 ... -0.546486 0.664057 3.658947\n","428 1.497245 1.046392 1.535094 ... 0.642957 0.025173 -0.413133\n","429 -0.255558 2.144026 -0.285687 ... -0.534379 -0.685227 -1.071207\n","430 -0.068668 0.372406 -0.084339 ... -0.704956 -1.041222 0.080282\n","431 1.873857 2.880596 1.806852 ... 0.788554 -0.057468 -0.073699\n","432 -0.807733 0.194281 -0.745206 ... -0.326559 0.080797 -0.753128\n","433 -0.128133 -0.005507 -0.097104 ... 0.402341 -0.254537 0.275288\n","434 0.237153 -0.092163 0.247947 ... -0.465872 -0.082897 0.488277\n","435 0.664734 0.230388 0.719819 ... 1.364808 0.238134 0.657432\n","436 0.087074 0.148547 0.109597 ... 0.644490 -0.295858 0.564144\n","437 0.118222 0.045042 0.100127 ... -0.283647 -0.688406 -0.497991\n","438 -0.484923 -0.354536 -0.426096 ... -0.624495 -0.553318 0.567516\n","439 -0.618011 0.572195 -0.629503 ... -0.643346 -0.740851 0.107819\n","440 0.874278 1.291915 0.921579 ... 1.156375 0.649753 2.832842\n","441 -0.685971 1.272659 -0.636091 ... 0.250614 -0.160771 0.910883\n","442 -0.977633 -0.963530 -1.001730 ... -1.754014 -0.308572 -1.227998\n","443 1.389641 1.308765 1.242748 ... 1.243733 1.671649 0.527615\n","444 -0.844545 0.791240 -0.837028 ... -0.771164 0.420900 -0.178227\n","445 0.149371 -0.554324 0.098480 ... -0.554302 -0.713834 -0.985225\n","446 -1.561806 -0.133083 -1.553481 ... -1.754014 0.377990 -0.131021\n","447 -0.170608 -0.063277 -0.153102 ... 0.472840 -0.378499 -0.372671\n","448 0.228658 -1.016486 0.190713 ... 0.003867 -0.343536 -0.042790\n","449 1.366988 0.519239 1.308628 ... 0.987790 -0.561265 -0.989158\n","450 1.330176 0.199096 1.197454 ... 0.229158 -0.535836 -1.467962\n","451 -0.467933 1.176375 -0.323157 ... 1.633011 2.323247 6.968987\n","452 -1.324229 -0.200482 -1.317546 ... -0.979750 -0.715423 -0.119781\n","453 -1.243810 -0.224553 -1.280076 ... -1.754014 -1.581571 -1.006018\n","454 -0.736941 1.149897 -0.712266 ... -0.274605 -1.258951 0.215157\n","\n","[455 rows x 30 columns]\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"bL4z0BArzqff","colab_type":"text"},"source":["Now I'm going to use Keras to define a Neural network that will be trained off of this data. This Neural Network can then be used to predict future values for breast cancer presence. "]},{"cell_type":"code","metadata":{"id":"LoWtmalR80De","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":173},"outputId":"58b07d97-7abb-4e30-953d-58ec1ef9e4ab","executionInfo":{"status":"ok","timestamp":1562008438154,"user_tz":300,"elapsed":2158,"user":{"displayName":"Luis Tedeschi","photoUrl":"","userId":"17261487412438553177"}}},"source":["from keras.models import Sequential\n","from keras.layers import Dense\n","\n","classifier = Sequential() # Initialising the ANN\n","\n","classifier.add(Dense(units = 16, activation = 'relu', input_dim = 30))\n","classifier.add(Dense(units = 8, activation = 'relu'))\n","classifier.add(Dense(units = 6, activation = 'relu'))\n","classifier.add(Dense(units = 1, activation = 'sigmoid'))"],"execution_count":3,"outputs":[{"output_type":"stream","text":["Using TensorFlow backend.\n","WARNING: Logging before flag parsing goes to stderr.\n","W0701 19:13:57.853029 140094321915776 deprecation_wrapper.py:119] From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:74: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.\n","\n","W0701 19:13:57.911168 140094321915776 deprecation_wrapper.py:119] From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.\n","\n","W0701 19:13:57.921385 140094321915776 deprecation_wrapper.py:119] From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:4138: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.\n","\n"],"name":"stderr"}]},{"cell_type":"markdown","metadata":{"id":"cJ_MStxe9CbN","colab_type":"text"},"source":["Once the Neural Network is defined, I now have to specify is the optimizer and loss function"]},{"cell_type":"code","metadata":{"id":"wR2O00E39C7E","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":156},"outputId":"4a299e3d-cb67-4f24-f008-6ee7539659c4","executionInfo":{"status":"ok","timestamp":1562008453074,"user_tz":300,"elapsed":192,"user":{"displayName":"Luis Tedeschi","photoUrl":"","userId":"17261487412438553177"}}},"source":["classifier.compile(optimizer = 'rmsprop', loss = 'binary_crossentropy')"],"execution_count":4,"outputs":[{"output_type":"stream","text":["W0701 19:14:13.012996 140094321915776 deprecation_wrapper.py:119] From /usr/local/lib/python3.6/dist-packages/keras/optimizers.py:790: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.\n","\n","W0701 19:14:13.047867 140094321915776 deprecation_wrapper.py:119] From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:3376: The name tf.log is deprecated. Please use tf.math.log instead.\n","\n","W0701 19:14:13.058299 140094321915776 deprecation.py:323] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/nn_impl.py:180: add_dispatch_support..wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.\n","Instructions for updating:\n","Use tf.where in 2.0, which has the same broadcast rule as np.where\n"],"name":"stderr"}]},{"cell_type":"markdown","metadata":{"id":"Mn9WPqaP9RmN","colab_type":"text"},"source":["You now train the neural network using Classifier.fit, passing it the training data -- i.e. for this set of X, this is what the Y should look like. The NN will then spot the patterns in the data, and build a neural network that could replicate that. "]},{"cell_type":"code","metadata":{"id":"1QlYnFgH9N49","colab_type":"code","outputId":"84751125-d66b-47b0-de95-c440e310a804","executionInfo":{"status":"ok","timestamp":1562008512765,"user_tz":300,"elapsed":54994,"user":{"displayName":"Luis Tedeschi","photoUrl":"","userId":"17261487412438553177"}},"colab":{"base_uri":"https://localhost:8080/","height":1000}},"source":["classifier.fit(X_train, Y_train, batch_size = 1, epochs = 100)"],"execution_count":5,"outputs":[{"output_type":"stream","text":["W0701 19:14:18.163034 140094321915776 deprecation_wrapper.py:119] From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:986: The name tf.assign_add is deprecated. Please use tf.compat.v1.assign_add instead.\n","\n"],"name":"stderr"},{"output_type":"stream","text":["Epoch 1/100\n","455/455 [==============================] - 1s 3ms/step - loss: 0.3337\n","Epoch 2/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.1260\n","Epoch 3/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0972\n","Epoch 4/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0900\n","Epoch 5/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0886\n","Epoch 6/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0852\n","Epoch 7/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0834\n","Epoch 8/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0841\n","Epoch 9/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0886\n","Epoch 10/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0921\n","Epoch 11/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0832\n","Epoch 12/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0805\n","Epoch 13/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0844\n","Epoch 14/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0799\n","Epoch 15/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0892\n","Epoch 16/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0798\n","Epoch 17/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0857\n","Epoch 18/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0838\n","Epoch 19/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0841\n","Epoch 20/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0778\n","Epoch 21/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0801\n","Epoch 22/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0767\n","Epoch 23/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0736\n","Epoch 24/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0740\n","Epoch 25/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0752\n","Epoch 26/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0709\n","Epoch 27/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0636\n","Epoch 28/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0752\n","Epoch 29/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0738\n","Epoch 30/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0717\n","Epoch 31/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0807\n","Epoch 32/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0735\n","Epoch 33/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0646\n","Epoch 34/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0690\n","Epoch 35/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0729\n","Epoch 36/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0692\n","Epoch 37/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0661\n","Epoch 38/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0616\n","Epoch 39/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0794\n","Epoch 40/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0700\n","Epoch 41/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0767\n","Epoch 42/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0705\n","Epoch 43/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0660\n","Epoch 44/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0655\n","Epoch 45/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0653\n","Epoch 46/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0573\n","Epoch 47/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0647\n","Epoch 48/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0703\n","Epoch 49/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0713\n","Epoch 50/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0660\n","Epoch 51/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0785\n","Epoch 52/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0704\n","Epoch 53/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0633\n","Epoch 54/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0644\n","Epoch 55/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0648\n","Epoch 56/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0605\n","Epoch 57/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0614\n","Epoch 58/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0647\n","Epoch 59/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0550\n","Epoch 60/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0632\n","Epoch 61/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0760\n","Epoch 62/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0649\n","Epoch 63/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0806\n","Epoch 64/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0726\n","Epoch 65/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0687\n","Epoch 66/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0652\n","Epoch 67/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0649\n","Epoch 68/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0494\n","Epoch 69/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0539\n","Epoch 70/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0602\n","Epoch 71/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0537\n","Epoch 72/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0461\n","Epoch 73/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0598\n","Epoch 74/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0633\n","Epoch 75/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0631\n","Epoch 76/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0497\n","Epoch 77/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0667\n","Epoch 78/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0540\n","Epoch 79/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0458\n","Epoch 80/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0596\n","Epoch 81/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0526\n","Epoch 82/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0556\n","Epoch 83/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0492\n","Epoch 84/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0580\n","Epoch 85/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0558\n","Epoch 86/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0560\n","Epoch 87/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0475\n","Epoch 88/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0544\n","Epoch 89/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0523\n","Epoch 90/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0450\n","Epoch 91/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0499\n","Epoch 92/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0541\n","Epoch 93/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0552\n","Epoch 94/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0569\n","Epoch 95/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0533\n","Epoch 96/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0534\n","Epoch 97/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0489\n","Epoch 98/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0637\n","Epoch 99/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0521\n","Epoch 100/100\n","455/455 [==============================] - 1s 1ms/step - loss: 0.0571\n"],"name":"stdout"},{"output_type":"execute_result","data":{"text/plain":[""]},"metadata":{"tags":[]},"execution_count":5}]},{"cell_type":"markdown","metadata":{"id":"yrnGgtfn9gsE","colab_type":"text"},"source":["To predict new values, the Neural Network uses classifier.predict. I'm going to pass it the test values for X (which the Neural Network hasn't previously seen) and it will give me back a set of predictions. These predicitons will be probabilities, so I will clean them up by saying that if thye are greater than .5, I'll make them 1, else I'll make them 0."]},{"cell_type":"code","metadata":{"id":"xEaG0Tkx9fUU","colab_type":"code","colab":{}},"source":["Y_pred = classifier.predict(X_test)\n","Y_pred = [ 1 if y>=0.5 else 0 for y in Y_pred ]"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"oQK3Qo9h97OW","colab_type":"text"},"source":["Now we can loop through the set of predicitons for the test set (called Y_pred) and the actual values for the test set (celled Y_test), and see how alike they are -- if they are the same, I'll increment 'correct', otherwise I'll incrememnt 'wrong'. \n","\n","You'll see the result is 100% accurate, even though the neural network reported a lower accuracy than that. Why? "]},{"cell_type":"code","metadata":{"id":"he_2VHJ_9yC1","colab_type":"code","outputId":"83eb595b-0908-45fb-ff8b-f7e2293e1eae","executionInfo":{"status":"ok","timestamp":1562008551987,"user_tz":300,"elapsed":207,"user":{"displayName":"Luis Tedeschi","photoUrl":"","userId":"17261487412438553177"}},"colab":{"base_uri":"https://localhost:8080/","height":68}},"source":["total = 0\n","correct = 0\n","wrong = 0\n","for i in Y_pred:\n"," total=total+1\n"," if(Y_test.at[i,0] == Y_pred[i]):\n"," correct=correct+1\n"," else:\n"," wrong=wrong+1\n","\n","print(\"Total \" + str(total))\n","print(\"Correct \" + str(correct))\n","print(\"Wrong \" + str(wrong))"],"execution_count":8,"outputs":[{"output_type":"stream","text":["Total 114\n","Correct 114\n","Wrong 0\n"],"name":"stdout"}]}]}