{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Model for predicting sentiment value of text entered using deep-learning"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#for cleaning the text removing everything except the characters(letters and numbers)\n",
    "def cleanString(incomingString):\n",
    "    newstring = incomingString\n",
    "    newstring = newstring.replace(\"!\",\"\")\n",
    "    newstring = newstring.replace(\"@\",\"\")\n",
    "    newstring = newstring.replace(\"#\",\"\")\n",
    "    newstring = newstring.replace(\"$\",\"\")\n",
    "    newstring = newstring.replace(\"%\",\"\")\n",
    "    newstring = newstring.replace(\"^\",\"\")\n",
    "    newstring = newstring.replace(\"&\",\"and\")\n",
    "    newstring = newstring.replace(\"*\",\"\")\n",
    "    newstring = newstring.replace(\"(\",\"\")\n",
    "    newstring = newstring.replace(\")\",\"\")\n",
    "    newstring = newstring.replace(\"+\",\"\")\n",
    "    newstring = newstring.replace(\"=\",\"\")\n",
    "    newstring = newstring.replace(\"?\",\"\")\n",
    "    newstring = newstring.replace(\"\\'\",\"\")\n",
    "    newstring = newstring.replace(\"\\\"\",\"\")\n",
    "    newstring = newstring.replace(\"{\",\"\")\n",
    "    newstring = newstring.replace(\"}\",\"\")\n",
    "    newstring = newstring.replace(\"[\",\"\")\n",
    "    newstring = newstring.replace(\"]\",\"\")\n",
    "    newstring = newstring.replace(\"<\",\"\")\n",
    "    newstring = newstring.replace(\">\",\"\")\n",
    "    newstring = newstring.replace(\"~\",\"\")\n",
    "    newstring = newstring.replace(\"`\",\"\")\n",
    "    newstring = newstring.replace(\":\",\"\")\n",
    "    newstring = newstring.replace(\";\",\"\")\n",
    "    newstring = newstring.replace(\"|\",\"\")\n",
    "    newstring = newstring.replace(\"\\\\\",\"\")\n",
    "    newstring = newstring.replace(\"/\",\"\")\n",
    "    newstring = newstring.replace(\",\",\"\")\n",
    "    newstring = newstring.replace(\".\",\"\")\n",
    "    return newstring"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n",
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import itertools\n",
    "import os\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import tensorflow as tf\n",
    "from sklearn.preprocessing import LabelBinarizer, LabelEncoder\n",
    "from sklearn.metrics import confusion_matrix\n",
    "\n",
    "import keras\n",
    "from keras.models import Sequential\n",
    "from keras.layers.core import Dense, Activation, Dropout\n",
    "from keras import utils\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensorflow version 1.5.0\n"
     ]
    }
   ],
   "source": [
    "print(\"Tensorflow version\", tf.__version__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### loading the data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "data = pd.read_csv('/Users/vishalsingh/Desktop/Saber/csvfor16k.csv',encoding = 'ISO-8859-1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.utils import shuffle\n",
    "data = shuffle(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Unnamed: 0', 'Author', 'Category', 'Company', 'Content', 'Date',\n",
       "       'Sentiment', 'Source', 'Subtitle', 'Tags', 'Title', 'Unnamed: 0.1',\n",
       "       'Unnamed: 0.1.1', 'Unnamed: 0.1.1.1', 'Unnamed: 0.1.1.1.1',\n",
       "       'Unnamed: 0.1.1.1.1.1', 'Unnamed: 0.1.1.1.1.1.1',\n",
       "       'Unnamed: 0.1.1.1.1.1.1.1', 'Unnamed: 0.1.1.1.1.1.1.1.1',\n",
       "       'Unnamed: 0.1.1.1.1.1.1.1.1.1', 'Unnamed: 0.1.1.1.1.1.1.1.1.1.1',\n",
       "       'Unnamed: 0.1.1.1.1.1.1.1.1.1.1.1',\n",
       "       'Unnamed: 0.1.1.1.1.1.1.1.1.1.1.1.1',\n",
       "       'Unnamed: 0.1.1.1.1.1.1.1.1.1.1.1.1.1',\n",
       "       'Unnamed: 0.1.1.1.1.1.1.1.1.1.1.1.1.1.1',\n",
       "       'Unnamed: 0.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1',\n",
       "       'Unnamed: 0.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Split the data into train and test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train size: 12800\n"
     ]
    }
   ],
   "source": [
    "train_size = int(len(data)*.80)\n",
    "print('Train size:', train_size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sistema Shyam Tele-Services prefers bandwidth sharing to improve access\n",
      "Apcotex Industries AGM on August 10 2016\n",
      "Apollo Tyres plans 440 million Euro investment in Hungary\n",
      "Rajasthan may face blackout from Sunday as NTPC threatens to snap supply for defaulting on Rs 367 crore pay\n",
      "Railways blames Bihar government for train accident\n",
      "Vibrant Gujarat PM Narendra Modi promises to make India easiest place to do business\n",
      "Apollo Hospitals family to form trust\n",
      "Kingfisher Airlines accepts CEO Sanjay Aggarwals resignation\n",
      "Job losses not a worry as data demand will keep telecom afloat in fiscal storm\n",
      "Ansal Properties and Infrastructure consider dividend\n",
      "ANG Industries Receipt of Order\n",
      "Mamta Banerjee to face Congress Left heat in Delhi\n",
      "Indias growth to accelerate to 75 this fiscal Moodys\n",
      "Apollo Tyres jumps 6 on falling input costs\n",
      "LafargeHolcim increases stake in Ambuja Cements ACC\n",
      "Apollo Hospitals introduces a new paradigm in Cancer Care\n",
      "No proposal to set up new commands for space assets Antony\n",
      "2G case accused Raja admits he met Radia Ratan Tata\n",
      "China accuses US of ignoring facts over Indias NSG bid\n",
      "Bribery barb by Arvind Kejriwal sign of frustration in AAP BJP\n",
      "FTA to help Indian service providers gain higher market access\n",
      "Arvind Ltd to stretch denim portfolio\n",
      "Aarti Industries Q1 net profit jumps 47\n",
      "Arvind Limited signs MoU with Guj for Rs 300-cr apparel park\n",
      "Bull run in pharma space intact but short-term correction likely Sandeep Tandon\n",
      "PPF account can be a good retirement solution\n",
      "After hitting Rs 26000 crore peak VC investors may slow pace of deal-making on profit concerns\n",
      "Varanasi Narendra Modi and possibly Arvind Kejriwals constituency set to witness boom in political tourism\n",
      "Current account deficit narrowed in Q1 RBI\n",
      "Apollo Tyres Outcome of board meeting\n",
      "Delhi CM Arvind Kejriwal to visit rain-hit villages likely to announce relief\n",
      "Expenditure on education health an investment Arvind Kejriwal\n",
      "Nelson Mandela my leader says Malala Yousafzai in her tribute to peace icon\n",
      "Over 190 mergers and acquisitions worth 227 billion take place in last 4 years Report\n",
      "Should you accept a buyback offer for your shares Heres how to evaluate\n",
      "Is your demat account active\n",
      "Buy Arvind with a target of Rs 310 Mitesh Thacker\n",
      "Tata Mutual Fund eyes reforms windfall but new plans face risk hurdle\n",
      "Naga Peace Accord welcome if Assam areas excluded Assam CM Tarun Gogoi\n",
      "Appollo Hospital and FICCI Heal 2014 inaugurate Innovation Enabling Access\n",
      "Jet Airways plans to raise 300 million to replace its high cost rupee debt\n",
      "Accelya Kale ITD Cementation among 86 stocks that hit 52-week high during intraday\n",
      "Home Minister Sushil Kumar Shinde calls Arvind Kejriwal a mad CM\n",
      "BJP rules out holding anyone accountable for Bihar loss\n",
      "DDCA row Finance Minister Arun Jaitley to sue Arvind Kejriwal 5 other AAP leaders\n",
      "Nifty at 8000 marks acche din Maybe Fresh high likely in Modis third year\n",
      "Intellectuals attack jingoist sentiments urge government to promote peace in South Asia\n",
      "3i Infotech consolidated Jun 16 total income from operations at Rs 25513 crore\n",
      "Sebi issues notices to attach bank accounts in IPO fraud case\n",
      "Anjani Portland Cement approves right issue\n",
      "Nifty may face some resistance near 8630-8640 levels Mitesh Thacker\n",
      "General Motors is accused in lawsuit of cheating on diesel truck emissions\n",
      "Railway Budget 2015 Suresh Prabhu has promised to change the face of railways in five years\n",
      "Killing in name of cow sentiment not acceptable Govt\n",
      "Aarti Inds hit new life-time high\n",
      "ICICI Bank account holders can use Twitter to transfer funds\n",
      "Prepare roadmap to implement AAP manifesto Arvind Kejriwal to Delhi Chief Secretary\n",
      "Googles Indian picks for Launchpad Accelerator\n",
      "3i Infotech up 15 approves debt realignment scheme\n",
      "Central Council of Cost Accountants of India resigns\n",
      "Dont want to lose my job by speaking on beef ban CEA Arvind Subramanian\n",
      "West Bengal to allot space for developing sports in state\n",
      "Apollo Tyres plans to set up a new factory in Andhra Pradesh and top corporate news of the day\n",
      "Ansal Properties and Infrastructure director Prabhu Nath Misra resigns\n",
      "ET Review Acer Iconia W3\n",
      "Lok Sabha polls 2014 Jamaat-e-Islami Hind extends support to Arvind Kejriwal in Varanasi\n",
      "Video worth watching BMW R1200GS takes on abandoned building\n",
      "Apollo Hospital Q3 results on Feb 10 2014\n",
      "Acche Din over Lack of clearance leads to increase in number of stalled and abandoned projects\n",
      "From Baap ka Raj to Aap ki Seva its time for politics Arvind ishtyle\n",
      "Life insurace companies may sell minority stake to shore up valuation\n",
      "Sensex falls Midcap gains Tata Motors down ACC up\n",
      "2G accused A Raja is the son of the soil in most backward areas of Tamil Nadu\n",
      "Arvind Kejriwal firm on his promises says will bite the bullet\n",
      "US panel seeks declassification review of Abbottabad documents\n",
      "Indian banks face broader capital challenges says Fitch\n",
      "Maintain a stock-specific approach to markets Arvind Sethi Tata MF\n",
      "Apollo Tyres Q2 net down 46  at Rs 140 crore\n",
      "PDP wants Siachen glacier to be peace park\n",
      "Delhi elections 2015 Arvind Kejriwal-led AAP breaks rules of identity politics dents core vote bases of BJP and Congress\n",
      "Anjani Portland Cement commissions 16 MW captive power plant\n",
      "Hold Maruti Suzuki target of Rs 4053 Arihant capital\n",
      "Nifty has support at 8042-8000 Arihant Capital\n",
      "M3M India awards Rs 133 crore contract to German kitchen appliance maker Miele\n",
      "China GDP may not be growing at 69 as official data suggests Arvind Sanger\n",
      "Sebi re-constituted panel on disclosures accounting standards\n",
      "Delhi Polls BJP brought Barack Obama to sway voters- Arvind Kejriwal\n",
      "Got threat calls warning against speaking on Hafiz Saeed Mukhtar Abbas Naqvi\n",
      "Stocks in news SBI PFC Aptech IOB JK Tyre MandM AstraZeneca\n",
      "NGO accuses CBI director Ranjit Sinha for shielding Dayanidhi Maran Reliance Telecom\n",
      "Buy Apollo Tyres with target price of Rs 69 Prakash Gaba\n",
      "BJPs think tank presents scary data on Aadhaar roll out Cong-led states account for over 50 coverage\n",
      "Badaun case All five accused clear lie-detector test focus shifts to girls family\n",
      "Tata Motors owned JLR in race to buy UK Grand Prix circuit Report\n",
      "10 Indian-Americans in race for 2014 elections in US\n",
      "Obamas India visit For the US to remain relevant in Asia it needs to accommodate Indian interests\n",
      "Jagjit Singh has special place in countrys music history PM Manmohan Singh\n",
      "Earnings growth expected to accelerate to 18-19 in FY16 Anand Radhakrishnan\n",
      "Buy Arvind Ltd with target price of Rs 165 Mitesh Thacker\n",
      "ACC Q4 PAT at Rs 324 crore\n"
     ]
    }
   ],
   "source": [
    "#for training the model\n",
    "train_posts = data['Title'][:train_size]\n",
    "train_posts = [ cleanString(x) for x in train_posts] \n",
    "train_tags = data['Sentiment'][:train_size]  \n",
    "train_tags = [round(x,1)  for x in train_tags]\n",
    "\n",
    "#the model will be trained on 10 labels i.e 0.0 .1 .2 .3 .4 .5 .6 .7 .8 .9 1.0\n",
    "    \n",
    "#for testing the trained model....\n",
    "test_posts = data['Title'][train_size:]\n",
    "test_posts = [cleanString(x) for x in test_posts]\n",
    "test_tags = data['Sentiment'][train_size:]\n",
    "test_tags = [round(x,1)  for x in test_tags]  \n",
    "\n",
    "for i in test_posts[:100]:\n",
    "    print (i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.6\n",
      "0.8\n",
      "1.0\n",
      "0.3\n",
      "0.4\n",
      "0.9\n",
      "0.9\n",
      "0.8\n",
      "0.9\n",
      "0.8\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    print(test_tags[i])\n",
    "#labels for first 10 entries...    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### tokenize the data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from keras.preprocessing import text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "max_words = 3000\n",
    "tokenize = text.Tokenizer(num_words=max_words, char_level=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "tokenize.fit_on_texts(train_posts)\n",
    "x_train = tokenize.texts_to_matrix(train_posts)\n",
    "x_test = tokenize.texts_to_matrix(test_posts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3000"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(x_test[40])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Use sklearn utility to convert label strings to numbered index\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LabelEncoder()"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "encoder = LabelEncoder()\n",
    "encoder.fit(train_tags)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_train = encoder.transform(train_tags)\n",
    "y_test = encoder.transform(test_tags)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 0, 4, ..., 3, 0, 3])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6,  8, 10, ...,  7,  1,  1])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "# Converts the labels to a one-hot representation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "num_classes = np.max(y_train) + 1\n",
    "y_train = keras.utils.to_categorical(y_train, num_classes)\n",
    "y_test = keras.utils.to_categorical(y_test, num_classes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x_train shape: (12800, 3000)\n",
      "x_test shape: (3200, 3000)\n",
      "y_train shape: (12800, 11)\n",
      "y_test shape: (3200, 11)\n"
     ]
    }
   ],
   "source": [
    "print('x_train shape:', x_train.shape)\n",
    "print('x_test shape:', x_test.shape)\n",
    "print('y_train shape:', y_train.shape)\n",
    "print('y_test shape:', y_test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch_size = 32"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "### Build the model\n",
    "model = Sequential()\n",
    "model.add(Dense(512,input_shape=(max_words,)))\n",
    "model.add(Activation('relu'))\n",
    "model.add(Dropout(0.8))\n",
    "model.add(Dense(num_classes))\n",
    "model.add(Activation('softmax'))\n",
    "\n",
    "model.compile(loss='categorical_crossentropy',optimizer = 'adam',metrics=['accuracy'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 11520 samples, validate on 1280 samples\n",
      "Epoch 1/10\n",
      "11520/11520 [==============================] - 6s 532us/step - loss: 2.2319 - acc: 0.1644 - val_loss: 2.0935 - val_acc: 0.2242\n",
      "Epoch 2/10\n",
      "11520/11520 [==============================] - 6s 492us/step - loss: 1.9675 - acc: 0.2859 - val_loss: 1.9645 - val_acc: 0.2562\n",
      "Epoch 3/10\n",
      "11520/11520 [==============================] - 6s 492us/step - loss: 1.7536 - acc: 0.3758 - val_loss: 1.9172 - val_acc: 0.2680\n",
      "Epoch 4/10\n",
      "11520/11520 [==============================] - 6s 491us/step - loss: 1.5736 - acc: 0.4490 - val_loss: 1.9005 - val_acc: 0.2820\n",
      "Epoch 5/10\n",
      "11520/11520 [==============================] - 6s 543us/step - loss: 1.4255 - acc: 0.5008 - val_loss: 1.9192 - val_acc: 0.2875\n",
      "Epoch 6/10\n",
      "11520/11520 [==============================] - 6s 551us/step - loss: 1.3043 - acc: 0.5464 - val_loss: 1.9372 - val_acc: 0.2961\n",
      "Epoch 7/10\n",
      "11520/11520 [==============================] - 7s 612us/step - loss: 1.1980 - acc: 0.5915 - val_loss: 1.9684 - val_acc: 0.2977\n",
      "Epoch 8/10\n",
      "11520/11520 [==============================] - 7s 590us/step - loss: 1.0983 - acc: 0.6217 - val_loss: 2.0127 - val_acc: 0.2992\n",
      "Epoch 9/10\n",
      "11520/11520 [==============================] - 7s 624us/step - loss: 1.0122 - acc: 0.6532 - val_loss: 2.0598 - val_acc: 0.3031\n",
      "Epoch 10/10\n",
      "11520/11520 [==============================] - 7s 609us/step - loss: 0.9366 - acc: 0.6826 - val_loss: 2.1062 - val_acc: 0.3016\n"
     ]
    }
   ],
   "source": [
    "history = model.fit(x_train,y_train,\n",
    "                   batch_size=batch_size,\n",
    "                   epochs=10,\n",
    "                   verbose=1,\n",
    "                   validation_split=0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3200/3200 [==============================] - 0s 123us/step\n",
      "Test score: 2.1175447309017184\n",
      "Test accuracy: 0.2928125\n"
     ]
    }
   ],
   "source": [
    "score = model.evaluate(x_test, y_test,\n",
    "                       batch_size=batch_size, verbose=1)\n",
    "print('Test score:', score[0])\n",
    "print('Test accuracy:', score[1])\n",
    "\n",
    "#the accuracy score below is meant for 11 labels if ... we train the model on binary data then \n",
    "#this model gives a state of art accuracy of 99%"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sistema Shyam Tele-Services prefers bandwidth sharing to improve access\n",
      "Actual label:0.6\n",
      "Predicted label: 0.9\n",
      "\n",
      "Apcotex Industries AGM on August 10 2016\n",
      "Actual label:0.8\n",
      "Predicted label: 0.9\n",
      "\n",
      "Apollo Tyres plans 440 million Euro investment in Hungary\n",
      "Actual label:1.0\n",
      "Predicted label: 0.9\n",
      "\n",
      "Rajasthan may face blackout from Sunday as NTPC threatens to snap supply for defaulting on Rs 367 crore pay\n",
      "Actual label:0.3\n",
      "Predicted label: 0.7\n",
      "\n",
      "Railways blames Bihar government for train accident\n",
      "Actual label:0.4\n",
      "Predicted label: 0.6\n",
      "\n",
      "Vibrant Gujarat PM Narendra Modi promises to make India easiest place to do business\n",
      "Actual label:0.9\n",
      "Predicted label: 0.3\n",
      "\n",
      "Apollo Hospitals family to form trust\n",
      "Actual label:0.9\n",
      "Predicted label: 0.8\n",
      "\n",
      "Kingfisher Airlines accepts CEO Sanjay Aggarwals resignation\n",
      "Actual label:0.8\n",
      "Predicted label: 0.6\n",
      "\n",
      "Job losses not a worry as data demand will keep telecom afloat in fiscal storm\n",
      "Actual label:0.9\n",
      "Predicted label: 0.9\n",
      "\n",
      "Ansal Properties and Infrastructure consider dividend\n",
      "Actual label:0.8\n",
      "Predicted label: 0.8\n",
      "\n"
     ]
    }
   ],
   "source": [
    "text_labels = encoder.classes_ \n",
    "#output below....\n",
    "for i in range(10):\n",
    "    prediction = model.predict(np.array([x_test[i]]))\n",
    "    predicted_label = text_labels[np.argmax(prediction)]\n",
    "    print(test_posts[i])\n",
    "    print('Actual label:' + str(test_tags[i]))\n",
    "    print(\"Predicted label: \" + str(predicted_label) + \"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "#saving the model so that we do not need to train the model again and again....\n",
    "from keras.models import load_model\n",
    "model.save('model_file.h5')\n",
    "my_model = load_model('model_file.h5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<keras.models.Sequential at 0x120ef3da0>"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "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.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
