Created
February 4, 2018 14:13
-
-
Save uchidama/9435d9322d452ed577df937fe78279a1 to your computer and use it in GitHub Desktop.
IMDB Prediction in Keras.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": { | |
| "collapsed": false, | |
| "scrolled": true | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stderr", | |
| "output_type": "stream", | |
| "text": [ | |
| "Using TensorFlow backend.\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "from __future__ import print_function\n", | |
| "import keras\n", | |
| "\n", | |
| "from keras.datasets import imdb\n", | |
| "from keras.models import load_model\n", | |
| "from keras.preprocessing import sequence\n", | |
| "from keras.models import Sequential\n", | |
| "import os\n", | |
| "import numpy as np\n", | |
| "\n", | |
| "import pandas as pd\n", | |
| "import random\n", | |
| "import enum\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "PredictionMode = enum.Enum(\"PredictionMode\", \"cnn lstm fasttext cnn_lstm bidirectional_lstm\")\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Loading data...\n", | |
| "25000 train sequences\n", | |
| "25000 test sequences\n", | |
| "x_train shape: (25000,)\n", | |
| "y_train shape: (25000,)\n", | |
| "x_test shape: (25000,)\n", | |
| "y_test shape: (25000,)\n", | |
| "Pad sequences (samples x time)\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "'''\n", | |
| "SWITCH PREDICTION MODE\n", | |
| "'''\n", | |
| "prediction_mode = PredictionMode.cnn\n", | |
| "#prediction_mode = PredictionMode.lstm\n", | |
| "#prediction_mode = PredictionMode.fasttext\n", | |
| "#prediction_mode = PredictionMode.cnn_lstm\n", | |
| "#prediction_mode = PredictionMode.bidirectional_lstm\n", | |
| "\n", | |
| "max_features = 5000\n", | |
| "maxlen = 400\n", | |
| "model_filename = ''\n", | |
| "INDEX_FROM=3 # word index offset\n", | |
| "\n", | |
| "if prediction_mode == PredictionMode.cnn: \n", | |
| " max_features = 5000\n", | |
| " maxlen = 400 \n", | |
| " model_filename = 'models/model_imdb_cnn.h5'\n", | |
| "elif prediction_mode == PredictionMode.lstm: \n", | |
| " max_features = 20000 \n", | |
| " maxlen = 80 \n", | |
| " model_filename = 'models/model_imdb_lstm.h5' \n", | |
| "elif prediction_mode == PredictionMode.fasttext: \n", | |
| " max_features = 20000 \n", | |
| " maxlen = 400 \n", | |
| " model_filename = 'models/model_imdb_fasttext.h5' \n", | |
| "elif prediction_mode == PredictionMode.cnn_lstm: \n", | |
| " max_features = 20000 \n", | |
| " maxlen = 100 \n", | |
| " model_filename = 'models/model_imdb_cnn_lstm.h5' \n", | |
| "elif prediction_mode == PredictionMode.bidirectional_lstm: \n", | |
| " max_features = 20000 \n", | |
| " maxlen = 100 \n", | |
| " model_filename = 'models/model_imdb_bidirectional_lstm.h5'\n", | |
| "\n", | |
| "print('Loading data...') \n", | |
| "(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features, index_from=INDEX_FROM)\n", | |
| "\n", | |
| "print(len(x_train), 'train sequences') \n", | |
| "print(len(x_test), 'test sequences') \n", | |
| "print('x_train shape:', x_train.shape) \n", | |
| "print('y_train shape:', y_train.shape) \n", | |
| "print('x_test shape:', x_test.shape) \n", | |
| "print('y_test shape:', y_test.shape)\n", | |
| "\n", | |
| "print('Pad sequences (samples x time)') \n", | |
| "original_x_test = x_test\n", | |
| "\n", | |
| "x_train = sequence.pad_sequences(x_train, maxlen=maxlen) \n", | |
| "x_test = sequence.pad_sequences(x_test, maxlen=maxlen)\n", | |
| "\n", | |
| "''' LOAD MODEL '''\n", | |
| "model = load_model(model_filename)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# Make Word to ID dictionary\n", | |
| "word_to_id = imdb.get_word_index()\n", | |
| "word_to_id = {k:(v+INDEX_FROM) for k,v in word_to_id.items()}\n", | |
| "word_to_id[\"[PAD]\"] = 0\n", | |
| "#word_to_id[\"\"] = 0\n", | |
| "word_to_id[\"[π]\"] = 1 # START\n", | |
| "word_to_id[\"[β]\"] = 2 # UNKNOWN\n", | |
| "\n", | |
| "# Make ID to Word dictionary\n", | |
| "id_to_word = {value:key for key,value in word_to_id.items()}\n", | |
| "\n", | |
| "def restore_original_text(imdb_x_array):\n", | |
| " return (' '.join(id_to_word[id] for id in imdb_x_array ))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "PredictionMode.cnn\n", | |
| "right : 86\n", | |
| "mistake : 14\n", | |
| "accuracy: 0.86\n" | |
| ] | |
| }, | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>index</th>\n", | |
| " <th>x_train original_text</th>\n", | |
| " <th>probability</th>\n", | |
| " <th>pred_class</th>\n", | |
| " <th>y_test</th>\n", | |
| " <th>is_fail</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>10606</td>\n", | |
| " <td>[π] since i am not a big steven seagal fan i t...</td>\n", | |
| " <td>0.032832</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>negative</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>10466</td>\n", | |
| " <td>[π] one dark night is a [β] in the [β] low bud...</td>\n", | |
| " <td>0.959998</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>371</td>\n", | |
| " <td>[π] another great tom [β] performance [β] sepa...</td>\n", | |
| " <td>0.843495</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>23193</td>\n", | |
| " <td>[π] i am so disappointed in this movie i can't...</td>\n", | |
| " <td>0.053851</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>negative</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>4</th>\n", | |
| " <td>19655</td>\n", | |
| " <td>[π] when i first saw this show i was 9 and it ...</td>\n", | |
| " <td>0.974339</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>5</th>\n", | |
| " <td>1384</td>\n", | |
| " <td>[π] this may not be [β] as a review on any fil...</td>\n", | |
| " <td>0.040330</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>Fail</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>6</th>\n", | |
| " <td>9061</td>\n", | |
| " <td>[π] it's hard to imagine in this day and age h...</td>\n", | |
| " <td>0.760508</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>7</th>\n", | |
| " <td>12961</td>\n", | |
| " <td>[π] from the brilliant mind that brought us th...</td>\n", | |
| " <td>0.021393</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>negative</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>8</th>\n", | |
| " <td>23342</td>\n", | |
| " <td>[π] this movie cannot be serious because it ha...</td>\n", | |
| " <td>0.000289</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>negative</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>9</th>\n", | |
| " <td>20873</td>\n", | |
| " <td>[π] so one day i was in the video store lookin...</td>\n", | |
| " <td>0.001086</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>negative</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>10</th>\n", | |
| " <td>16702</td>\n", | |
| " <td>[π] burt [β] and [β] in this great cop film [β...</td>\n", | |
| " <td>0.483076</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>Fail</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>11</th>\n", | |
| " <td>4376</td>\n", | |
| " <td>[π] the last dinosaur was one of those out of ...</td>\n", | |
| " <td>0.346612</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>Fail</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>12</th>\n", | |
| " <td>20768</td>\n", | |
| " <td>[π] this movie was amazing i was in tears by t...</td>\n", | |
| " <td>0.944461</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>13</th>\n", | |
| " <td>398</td>\n", | |
| " <td>[π] i bought this dvd from [β] for cheap think...</td>\n", | |
| " <td>0.969885</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>14</th>\n", | |
| " <td>8880</td>\n", | |
| " <td>[π] this movie [β] something most other movies...</td>\n", | |
| " <td>0.625466</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>15</th>\n", | |
| " <td>8783</td>\n", | |
| " <td>[π] as anthony said i wanted to stick my head ...</td>\n", | |
| " <td>0.000191</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>negative</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>16</th>\n", | |
| " <td>6635</td>\n", | |
| " <td>[π] ok chuck [β] has shown up in many an enter...</td>\n", | |
| " <td>0.065776</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>negative</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>17</th>\n", | |
| " <td>20714</td>\n", | |
| " <td>[π] maybe you have to be a former hippie to fu...</td>\n", | |
| " <td>0.000838</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>negative</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>18</th>\n", | |
| " <td>16122</td>\n", | |
| " <td>[π] i have not seen and heard the original ver...</td>\n", | |
| " <td>0.016674</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>negative</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>19</th>\n", | |
| " <td>5018</td>\n", | |
| " <td>[π] feels like an [β] film if there is such a ...</td>\n", | |
| " <td>0.972986</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>20</th>\n", | |
| " <td>2019</td>\n", | |
| " <td>[π] this film made me so angry because of its ...</td>\n", | |
| " <td>0.005343</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>negative</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>21</th>\n", | |
| " <td>8338</td>\n", | |
| " <td>[π] [β] loves his only he'd love her better si...</td>\n", | |
| " <td>0.791030</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>22</th>\n", | |
| " <td>19156</td>\n", | |
| " <td>[π] i must have been around ten years old when...</td>\n", | |
| " <td>0.636000</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>23</th>\n", | |
| " <td>9497</td>\n", | |
| " <td>[π] if i could rate it a zero i would coming f...</td>\n", | |
| " <td>0.001099</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>negative</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>24</th>\n", | |
| " <td>18809</td>\n", | |
| " <td>[π] clint howard brother of more talented ron ...</td>\n", | |
| " <td>0.004429</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>negative</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>25</th>\n", | |
| " <td>9583</td>\n", | |
| " <td>[π] this is per [β] an above average film but ...</td>\n", | |
| " <td>0.005798</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>negative</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>26</th>\n", | |
| " <td>10199</td>\n", | |
| " <td>[π] the plot involves a new [β] franchise [β] ...</td>\n", | |
| " <td>0.067615</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>negative</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>27</th>\n", | |
| " <td>9132</td>\n", | |
| " <td>[π] [β] is another day is not the sequel to go...</td>\n", | |
| " <td>0.350507</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>Fail</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>28</th>\n", | |
| " <td>9710</td>\n", | |
| " <td>[π] cuba [β] jr and ed harris are touching thi...</td>\n", | |
| " <td>0.951614</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>29</th>\n", | |
| " <td>8463</td>\n", | |
| " <td>[π] we just saw this movie in [β] texas at the...</td>\n", | |
| " <td>0.858546</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>...</th>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>70</th>\n", | |
| " <td>15341</td>\n", | |
| " <td>[π] another hand held horror means another [β]...</td>\n", | |
| " <td>0.214579</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>Fail</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>71</th>\n", | |
| " <td>3591</td>\n", | |
| " <td>[π] i first saw this as a kid the little [β] f...</td>\n", | |
| " <td>0.448800</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>Fail</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>72</th>\n", | |
| " <td>542</td>\n", | |
| " <td>[π] this 8 minute gem is not only timeless but...</td>\n", | |
| " <td>0.992204</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>73</th>\n", | |
| " <td>6806</td>\n", | |
| " <td>[π] i remember catching this movie on one of t...</td>\n", | |
| " <td>0.321714</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>negative</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>74</th>\n", | |
| " <td>13228</td>\n", | |
| " <td>[π] i have watched [β] from first episode to n...</td>\n", | |
| " <td>0.989564</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>75</th>\n", | |
| " <td>18244</td>\n", | |
| " <td>[π] a modern scare film [β] it is br br the [β...</td>\n", | |
| " <td>0.144496</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>negative</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>76</th>\n", | |
| " <td>2612</td>\n", | |
| " <td>[π] it actually [β] me to say it but this movi...</td>\n", | |
| " <td>0.002021</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>negative</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>77</th>\n", | |
| " <td>6501</td>\n", | |
| " <td>[π] as far as fake documentaries go anyone thi...</td>\n", | |
| " <td>0.464712</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>Fail</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>78</th>\n", | |
| " <td>4746</td>\n", | |
| " <td>[π] [β] scott does what he does best i e plays...</td>\n", | |
| " <td>0.614056</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>79</th>\n", | |
| " <td>20426</td>\n", | |
| " <td>[π] this is another notorious mexican horror f...</td>\n", | |
| " <td>0.609815</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>Fail</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>80</th>\n", | |
| " <td>12506</td>\n", | |
| " <td>[π] remember back when this movie was made by ...</td>\n", | |
| " <td>0.995255</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>81</th>\n", | |
| " <td>20941</td>\n", | |
| " <td>[π] this is a prime example of [β] [β] at its ...</td>\n", | |
| " <td>0.799772</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>82</th>\n", | |
| " <td>6699</td>\n", | |
| " <td>[π] this has got to be the funniest movie i ha...</td>\n", | |
| " <td>0.759316</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>83</th>\n", | |
| " <td>16111</td>\n", | |
| " <td>[π] the odd mixture of comedy and horror somet...</td>\n", | |
| " <td>0.821335</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>Fail</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>84</th>\n", | |
| " <td>11799</td>\n", | |
| " <td>[π] [β] [β] is where the sequel [β] of the [β]...</td>\n", | |
| " <td>0.012494</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>negative</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>85</th>\n", | |
| " <td>7432</td>\n", | |
| " <td>[π] pretty [β] good looking cast the story [β]...</td>\n", | |
| " <td>0.034115</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>negative</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>86</th>\n", | |
| " <td>17735</td>\n", | |
| " <td>[π] strong [β] and it clearly shows government...</td>\n", | |
| " <td>0.650083</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>87</th>\n", | |
| " <td>13640</td>\n", | |
| " <td>[π] nothing dull about this movie which is hel...</td>\n", | |
| " <td>0.137810</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>Fail</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>88</th>\n", | |
| " <td>10862</td>\n", | |
| " <td>[π] [β] intense and [β] is a documentary like ...</td>\n", | |
| " <td>0.131923</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>negative</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>89</th>\n", | |
| " <td>854</td>\n", | |
| " <td>[π] most critics have written [β] about that m...</td>\n", | |
| " <td>0.116712</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>negative</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>90</th>\n", | |
| " <td>2507</td>\n", | |
| " <td>[π] this is truly a documentary of love about ...</td>\n", | |
| " <td>0.997620</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>91</th>\n", | |
| " <td>6581</td>\n", | |
| " <td>[π] if you like madonna or not this movie is h...</td>\n", | |
| " <td>0.870057</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>92</th>\n", | |
| " <td>14488</td>\n", | |
| " <td>[π] essential viewing for anyone who watches t...</td>\n", | |
| " <td>0.985214</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>93</th>\n", | |
| " <td>8473</td>\n", | |
| " <td>[π] i love [β] terrific characters and poignan...</td>\n", | |
| " <td>0.910778</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>94</th>\n", | |
| " <td>8394</td>\n", | |
| " <td>[π] it's painfully obvious that the people who...</td>\n", | |
| " <td>0.001130</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>negative</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>95</th>\n", | |
| " <td>13822</td>\n", | |
| " <td>[π] you might think that a show about regular ...</td>\n", | |
| " <td>0.015575</td>\n", | |
| " <td>negative</td>\n", | |
| " <td>negative</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>96</th>\n", | |
| " <td>427</td>\n", | |
| " <td>[π] it has taken me about a year now after see...</td>\n", | |
| " <td>0.672148</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>97</th>\n", | |
| " <td>482</td>\n", | |
| " <td>[π] the world is going to miss john [β] this w...</td>\n", | |
| " <td>0.757354</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>98</th>\n", | |
| " <td>288</td>\n", | |
| " <td>[π] there is something about true stories that...</td>\n", | |
| " <td>0.980066</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>99</th>\n", | |
| " <td>8323</td>\n", | |
| " <td>[π] blade was a thrilling horror masterpiece a...</td>\n", | |
| " <td>0.986180</td>\n", | |
| " <td>positive</td>\n", | |
| " <td>positive</td>\n", | |
| " <td></td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "<p>100 rows Γ 6 columns</p>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " index x_train original_text probability \\\n", | |
| "0 10606 [π] since i am not a big steven seagal fan i t... 0.032832 \n", | |
| "1 10466 [π] one dark night is a [β] in the [β] low bud... 0.959998 \n", | |
| "2 371 [π] another great tom [β] performance [β] sepa... 0.843495 \n", | |
| "3 23193 [π] i am so disappointed in this movie i can't... 0.053851 \n", | |
| "4 19655 [π] when i first saw this show i was 9 and it ... 0.974339 \n", | |
| "5 1384 [π] this may not be [β] as a review on any fil... 0.040330 \n", | |
| "6 9061 [π] it's hard to imagine in this day and age h... 0.760508 \n", | |
| "7 12961 [π] from the brilliant mind that brought us th... 0.021393 \n", | |
| "8 23342 [π] this movie cannot be serious because it ha... 0.000289 \n", | |
| "9 20873 [π] so one day i was in the video store lookin... 0.001086 \n", | |
| "10 16702 [π] burt [β] and [β] in this great cop film [β... 0.483076 \n", | |
| "11 4376 [π] the last dinosaur was one of those out of ... 0.346612 \n", | |
| "12 20768 [π] this movie was amazing i was in tears by t... 0.944461 \n", | |
| "13 398 [π] i bought this dvd from [β] for cheap think... 0.969885 \n", | |
| "14 8880 [π] this movie [β] something most other movies... 0.625466 \n", | |
| "15 8783 [π] as anthony said i wanted to stick my head ... 0.000191 \n", | |
| "16 6635 [π] ok chuck [β] has shown up in many an enter... 0.065776 \n", | |
| "17 20714 [π] maybe you have to be a former hippie to fu... 0.000838 \n", | |
| "18 16122 [π] i have not seen and heard the original ver... 0.016674 \n", | |
| "19 5018 [π] feels like an [β] film if there is such a ... 0.972986 \n", | |
| "20 2019 [π] this film made me so angry because of its ... 0.005343 \n", | |
| "21 8338 [π] [β] loves his only he'd love her better si... 0.791030 \n", | |
| "22 19156 [π] i must have been around ten years old when... 0.636000 \n", | |
| "23 9497 [π] if i could rate it a zero i would coming f... 0.001099 \n", | |
| "24 18809 [π] clint howard brother of more talented ron ... 0.004429 \n", | |
| "25 9583 [π] this is per [β] an above average film but ... 0.005798 \n", | |
| "26 10199 [π] the plot involves a new [β] franchise [β] ... 0.067615 \n", | |
| "27 9132 [π] [β] is another day is not the sequel to go... 0.350507 \n", | |
| "28 9710 [π] cuba [β] jr and ed harris are touching thi... 0.951614 \n", | |
| "29 8463 [π] we just saw this movie in [β] texas at the... 0.858546 \n", | |
| ".. ... ... ... \n", | |
| "70 15341 [π] another hand held horror means another [β]... 0.214579 \n", | |
| "71 3591 [π] i first saw this as a kid the little [β] f... 0.448800 \n", | |
| "72 542 [π] this 8 minute gem is not only timeless but... 0.992204 \n", | |
| "73 6806 [π] i remember catching this movie on one of t... 0.321714 \n", | |
| "74 13228 [π] i have watched [β] from first episode to n... 0.989564 \n", | |
| "75 18244 [π] a modern scare film [β] it is br br the [β... 0.144496 \n", | |
| "76 2612 [π] it actually [β] me to say it but this movi... 0.002021 \n", | |
| "77 6501 [π] as far as fake documentaries go anyone thi... 0.464712 \n", | |
| "78 4746 [π] [β] scott does what he does best i e plays... 0.614056 \n", | |
| "79 20426 [π] this is another notorious mexican horror f... 0.609815 \n", | |
| "80 12506 [π] remember back when this movie was made by ... 0.995255 \n", | |
| "81 20941 [π] this is a prime example of [β] [β] at its ... 0.799772 \n", | |
| "82 6699 [π] this has got to be the funniest movie i ha... 0.759316 \n", | |
| "83 16111 [π] the odd mixture of comedy and horror somet... 0.821335 \n", | |
| "84 11799 [π] [β] [β] is where the sequel [β] of the [β]... 0.012494 \n", | |
| "85 7432 [π] pretty [β] good looking cast the story [β]... 0.034115 \n", | |
| "86 17735 [π] strong [β] and it clearly shows government... 0.650083 \n", | |
| "87 13640 [π] nothing dull about this movie which is hel... 0.137810 \n", | |
| "88 10862 [π] [β] intense and [β] is a documentary like ... 0.131923 \n", | |
| "89 854 [π] most critics have written [β] about that m... 0.116712 \n", | |
| "90 2507 [π] this is truly a documentary of love about ... 0.997620 \n", | |
| "91 6581 [π] if you like madonna or not this movie is h... 0.870057 \n", | |
| "92 14488 [π] essential viewing for anyone who watches t... 0.985214 \n", | |
| "93 8473 [π] i love [β] terrific characters and poignan... 0.910778 \n", | |
| "94 8394 [π] it's painfully obvious that the people who... 0.001130 \n", | |
| "95 13822 [π] you might think that a show about regular ... 0.015575 \n", | |
| "96 427 [π] it has taken me about a year now after see... 0.672148 \n", | |
| "97 482 [π] the world is going to miss john [β] this w... 0.757354 \n", | |
| "98 288 [π] there is something about true stories that... 0.980066 \n", | |
| "99 8323 [π] blade was a thrilling horror masterpiece a... 0.986180 \n", | |
| "\n", | |
| " pred_class y_test is_fail \n", | |
| "0 negative negative \n", | |
| "1 positive positive \n", | |
| "2 positive positive \n", | |
| "3 negative negative \n", | |
| "4 positive positive \n", | |
| "5 negative positive Fail \n", | |
| "6 positive positive \n", | |
| "7 negative negative \n", | |
| "8 negative negative \n", | |
| "9 negative negative \n", | |
| "10 negative positive Fail \n", | |
| "11 negative positive Fail \n", | |
| "12 positive positive \n", | |
| "13 positive positive \n", | |
| "14 positive positive \n", | |
| "15 negative negative \n", | |
| "16 negative negative \n", | |
| "17 negative negative \n", | |
| "18 negative negative \n", | |
| "19 positive positive \n", | |
| "20 negative negative \n", | |
| "21 positive positive \n", | |
| "22 positive positive \n", | |
| "23 negative negative \n", | |
| "24 negative negative \n", | |
| "25 negative negative \n", | |
| "26 negative negative \n", | |
| "27 negative positive Fail \n", | |
| "28 positive positive \n", | |
| "29 positive positive \n", | |
| ".. ... ... ... \n", | |
| "70 negative positive Fail \n", | |
| "71 negative positive Fail \n", | |
| "72 positive positive \n", | |
| "73 negative negative \n", | |
| "74 positive positive \n", | |
| "75 negative negative \n", | |
| "76 negative negative \n", | |
| "77 negative positive Fail \n", | |
| "78 positive positive \n", | |
| "79 positive negative Fail \n", | |
| "80 positive positive \n", | |
| "81 positive positive \n", | |
| "82 positive positive \n", | |
| "83 positive negative Fail \n", | |
| "84 negative negative \n", | |
| "85 negative negative \n", | |
| "86 positive positive \n", | |
| "87 negative positive Fail \n", | |
| "88 negative negative \n", | |
| "89 negative negative \n", | |
| "90 positive positive \n", | |
| "91 positive positive \n", | |
| "92 positive positive \n", | |
| "93 positive positive \n", | |
| "94 negative negative \n", | |
| "95 negative negative \n", | |
| "96 positive positive \n", | |
| "97 positive positive \n", | |
| "98 positive positive \n", | |
| "99 positive positive \n", | |
| "\n", | |
| "[100 rows x 6 columns]" | |
| ] | |
| }, | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "'''\n", | |
| "PREDICT\n", | |
| "'''\n", | |
| "def imdb_class_to_str(imdb_class):\n", | |
| " if imdb_class == 0:\n", | |
| " return 'negative'\n", | |
| " return 'positive'\n", | |
| "\n", | |
| "right = 0\n", | |
| "mistake = 0\n", | |
| "\n", | |
| "index_list = []\n", | |
| "original_text_list = []\n", | |
| "pred_prob_list = []\n", | |
| "pred_class_list = []\n", | |
| "y_test_list = []\n", | |
| "fail_str_list = []\n", | |
| "\n", | |
| "for i in range(100):\n", | |
| " index = random.randint(0, len(x_test))\n", | |
| " \n", | |
| " pred_prob = model.predict(x_test[index:(index+1)])[0][0] \n", | |
| " pred_class = model.predict_classes(x_test[index:(index+1)])[0][0]\n", | |
| " \n", | |
| " '''\n", | |
| " print('pred_prod:', pred_prod)\n", | |
| " print('pred_class:', pred_class)\n", | |
| " print('y_test[index] :', y_test[index])\n", | |
| " '''\n", | |
| " fail_str = '' \n", | |
| " \n", | |
| " if y_test[index] == pred_class:\n", | |
| " right += 1\n", | |
| " else:\n", | |
| " mistake += 1\n", | |
| " fail_str = 'Fail'\n", | |
| " \n", | |
| " original_text = restore_original_text(original_x_test[index])\n", | |
| "\n", | |
| " index_list.append(index)\n", | |
| " original_text_list.append(original_text)\n", | |
| " pred_prob_list.append(pred_prob)\n", | |
| " pred_class_list.append(imdb_class_to_str(pred_class))\n", | |
| " y_test_list.append(imdb_class_to_str(y_test[index]))\n", | |
| " fail_str_list.append(fail_str)\n", | |
| "\n", | |
| "print(prediction_mode)\n", | |
| "print(\"right : \", right)\n", | |
| "print(\"mistake : \", mistake)\n", | |
| "print(\"accuracy:\", right/(right+mistake))\n", | |
| "\n", | |
| "df = pd.DataFrame({'index': index_list, \n", | |
| " 'x_train original_text': original_text_list, \n", | |
| " 'probability': pred_prob_list, \n", | |
| " 'pred_class': pred_class_list,\n", | |
| " 'y_test': y_test_list,\n", | |
| " 'is_fail': fail_str_list\n", | |
| " })\n", | |
| "\n", | |
| "df[['index', 'x_train original_text','probability','pred_class','y_test','is_fail']]\n" | |
| ] | |
| } | |
| ], | |
| "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 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment