{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Sentiment Analysis\n", "We will use the IMDB sentiment analysis database in this tutorial. The main idea that is used in this tutorial is that certain words are enough to establish the sentiment of a given sentence. The word order is discarded in this particular tutorial.\n", "<img src=\"./images/angry_happy_dogo.png\" alt=\"dogo\" style=\"width: 500px;\"/>\n", "\n", "## References\n", "1. http://ai.stanford.edu/~amaas/data/sentiment/" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import os\n", "import urllib\n", "\n", "from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer\n", "\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "\n", "np.random.seed(1)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<div>\n", "<style>\n", " .dataframe thead tr:only-child th {\n", " text-align: right;\n", " }\n", "\n", " .dataframe thead th {\n", " text-align: left;\n", " }\n", "\n", " .dataframe tbody tr th {\n", " vertical-align: top;\n", " }\n", "</style>\n", "<table border=\"1\" class=\"dataframe\">\n", " <thead>\n", " <tr style=\"text-align: right;\">\n", " <th></th>\n", " <th>Reviews</th>\n", " <th>Sentiment</th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <th>0</th>\n", " <td>Bromwell High is a cartoon comedy. It ran at t...</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>1</th>\n", " <td>Homelessness (or Houselessness as George Carli...</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>2</th>\n", " <td>Brilliant over-acting by Lesley Ann Warren. Be...</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>3</th>\n", " <td>This is easily the most underrated film inn th...</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>4</th>\n", " <td>This is not the typical Mel Brooks film. It wa...</td>\n", " <td>1</td>\n", " </tr>\n", " </tbody>\n", "</table>\n", "</div>" ], "text/plain": [ " Reviews Sentiment\n", "0 Bromwell High is a cartoon comedy. It ran at t... 1\n", "1 Homelessness (or Houselessness as George Carli... 1\n", "2 Brilliant over-acting by Lesley Ann Warren. Be... 1\n", "3 This is easily the most underrated film inn th... 1\n", "4 This is not the typical Mel Brooks film. It wa... 1" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "if not os.path.isfile('data/reviews2.pkl'):\n", " urllib.request.urlretrieve('https://www.dropbox.com/s/15tfttuzqe7fimg/reviews2.pkl?dl=1','./data/reviews2.pkl')\n", " \n", "df = pd.read_pickle('data/reviews2.pkl') \n", "df.head()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Bromwell High is a cartoon comedy. It ran at the same time as some other programs about school life, such as \"Teachers\". My 35 years in the teaching profession lead me to believe that Bromwell High\\'s satire is much closer to reality than is \"Teachers\". The scramble to survive financially, the insightful students who can see right through their pathetic teachers\\' pomp, the pettiness of the whole situation, all remind me of the schools I knew and their students. When I saw the episode in which a student repeatedly tried to burn down the school, I immediately recalled ......... at .......... High. A classic line: INSPECTOR: I\\'m here to sack one of your teachers. STUDENT: Welcome to Bromwell High. I expect that many adults of my age think that Bromwell High is far fetched. What a pity that it isn\\'t!'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.Reviews.values[0]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(25000, 2)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the first model that we look at we simply count the number of words in each document. This method is called **Bag of words**. It's considered a bag since we lose all ordering of the words. The features are simply the count of each word. \n", "\n", "Lucky for us Scikit Learn contains this feature extractor as a method. See http://scikit-learn.org/stable/modules/feature_extraction.html#common-vectorizer-usage for an example. the `fit_transform` method gives us the matrix that we are after." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "tf_vectorizer = CountVectorizer()\n", "tf = tf_vectorizer.fit_transform(df.Reviews.values)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of unique words: 74849\n", "['00', '000', '0000000000001', '00001', '00015']\n" ] } ], "source": [ "print('Number of unique words: ',len(tf_vectorizer.get_feature_names()))\n", "print(tf_vectorizer.get_feature_names()[:5])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Unfortunately this is extremely noisy as can be seen by the existence of random numbers such as `00` etc. Hence we will do two additional things. We will only consider words that have appeared atleast 5 times. Also words such as `the, a, to` are irrelevant in figuring out what the sentiment is. Hence we shall remove such words. These are commonly known as _stop words_ in Natural Language Processing (NLP)." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "New number of unique words: 26967\n" ] } ], "source": [ "tf_vectorizer = CountVectorizer(min_df=5,stop_words='english')\n", "tf = tf_vectorizer.fit_transform(df.Reviews.values)\n", "print('New number of unique words: ',len(tf_vectorizer.get_feature_names()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that tf is a sparse matrix" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "<class 'scipy.sparse.csr.csr_matrix'>\n" ] }, { "data": { "text/plain": [ "(25000, 26967)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(type(tf))\n", "tf.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will use `np.random.permutation` to shuffle the data. It is really important to shuffle the data before we put it into any machine learning algorithm. See below as to how `np.random.permutation` works." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 1, 4, 0, 3])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.permutation(5)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# TODO\n", "idx = #fill out what is supposed to go in here (what is the length of all data)\n", "X_train = tf[idx][:12500].todense()\n", "X_test = tf[idx][12500:].todense()\n", "y_train = df.Sentiment.values[idx][:12500]\n", "y_test = df.Sentiment.values[idx][12500:]" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(12500, 26967)" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X_train.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Keras Model\n", "Below we create our first deep learning model." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Using TensorFlow backend.\n" ] } ], "source": [ "from keras.models import Sequential\n", "from keras.layers import Dense, Activation, Dropout\n", "from keras.regularizers import l2, l1" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "_________________________________________________________________\n", "Layer (type) Output Shape Param # \n", "=================================================================\n", "dense_9 (Dense) (None, 100) 2696800 \n", "_________________________________________________________________\n", "activation_9 (Activation) (None, 100) 0 \n", "_________________________________________________________________\n", "dense_10 (Dense) (None, 1) 101 \n", "_________________________________________________________________\n", "activation_10 (Activation) (None, 1) 0 \n", "=================================================================\n", "Total params: 2,696,901.0\n", "Trainable params: 2,696,901.0\n", "Non-trainable params: 0.0\n", "_________________________________________________________________\n" ] } ], "source": [ "model = Sequential()\n", "model.add(Dense(units=100, input_dim=tf.shape[1]))\n", "model.add(Activation(\"relu\"))\n", "model.add(Dense(units=1))\n", "model.add(Activation(\"sigmoid\"))\n", "model.compile(loss='binary_crossentropy', optimizer='adagrad', metrics=[\"binary_accuracy\"])\n", "model.summary()" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 1/2\n", "12500/12500 [==============================] - 3s - loss: 0.3610 - binary_accuracy: 0.8493 \n", "Epoch 2/2\n", "12500/12500 [==============================] - 3s - loss: 0.1066 - binary_accuracy: 0.9694 \n" ] }, { "data": { "text/plain": [ "<keras.callbacks.History at 0x7f6b54e6a860>" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.fit(X_train, y_train, epochs=2, batch_size=128)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Keras has two ways of predicting `model.predict(...)` which predicts the **probability** of belonging to a class (basically the value it gets after passing through final activation), and `model.predict_class(...)` which as the name suggests outputs the class. Here we will use the `predict` method.\n", "\n", "**This is the probability of the class being 1 (positive review in this case).**" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": true }, "outputs": [], "source": [ "y_test_pred = model.predict(X_test)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(12500, 1)\n" ] }, { "data": { "text/plain": [ "array([[ 8.76996852e-03],\n", " [ 9.30667948e-03],\n", " [ 3.95858325e-02],\n", " ..., \n", " [ 9.96227503e-01],\n", " [ 9.82810378e-01],\n", " [ 1.46510490e-06]], dtype=float32)" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(y_test_pred.shape)\n", "y_test_pred" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us manually convert this to 0 or 1 class. Using numpy arrays" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.88016" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# TODO\n", "# a probability of less than 0.5 is considered class 0\n", "# Overwrite y_test to get the 0 classes\n", "y_test_pred[y_test_pred>=0.5] = 1\n", "np.count_nonzero(y_test_pred==y_test[:,None])*1.0/len(y_test)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us test some manual test cases. Fee free to change the sentence below. Really important to note that I am using `transform` and not `fit_transform` below." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.39534414]], dtype=float32)" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test_case = tf_vectorizer.transform([\"I really hated this movie\"])\n", "model.predict(test_case.todense())" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.68749398]], dtype=float32)" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test_case = tf_vectorizer.transform([\"I really loved this movie\"])\n", "model.predict(test_case.todense())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Improving the model\n", "\n", "Counting is one of the most basic things you could do when it comes to NLP applications. A more popular method that tends to be a bit better is Term Frequency - inverse Document Frequency (Tfidf) method. Not only does it take frequncy (term frequency) into account, but it also penalises words for being overused (for example the word 'the' would be downvoted since it is too popular). The intuition being that it does not have any discriminatory power if the word is used in every sentence. Whereas rare words might be able to distinguish the contents of the sentence better." ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": true }, "outputs": [], "source": [ "tfidf_vectorizer = TfidfVectorizer(min_df=5,stop_words='english')\n", "\n", "# TODO\n", "tfidf = #get the tfidf feature matrix (same as above)\n", "\n", "# get the new features\n", "X_train = tfidf[idx][:12500].todense()\n", "X_test = tfidf[idx][12500:].todense()" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "_________________________________________________________________\n", "Layer (type) Output Shape Param # \n", "=================================================================\n", "dense_11 (Dense) (None, 100) 2696800 \n", "_________________________________________________________________\n", "activation_11 (Activation) (None, 100) 0 \n", "_________________________________________________________________\n", "dense_12 (Dense) (None, 1) 101 \n", "_________________________________________________________________\n", "activation_12 (Activation) (None, 1) 0 \n", "=================================================================\n", "Total params: 2,696,901.0\n", "Trainable params: 2,696,901.0\n", "Non-trainable params: 0.0\n", "_________________________________________________________________\n" ] } ], "source": [ "model = Sequential()\n", "# TODO:\n", "# Complete the model, same/ similar as above.\n", "# 1. Have two `Dense` layers\n", "# 2. Compile the model (what is the loss in this case)\n", "model.summary()" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 1/2\n", "12500/12500 [==============================] - 4s - loss: 0.4093 - binary_accuracy: 0.8491 \n", "Epoch 2/2\n", "12500/12500 [==============================] - 3s - loss: 0.1829 - binary_accuracy: 0.9507 \n" ] }, { "data": { "text/plain": [ "<keras.callbacks.History at 0x7f6b5378fc88>" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.fit(X_train, y_train, epochs=2, batch_size=128)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.8868" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_test_pred = model.predict(X_test)\n", "y_test_pred[y_test_pred<0.5] = 0\n", "y_test_pred[y_test_pred>=0.5] = 1\n", "np.count_nonzero(y_test_pred==y_test[:,None])/len(y_test)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As can be seen the model is much better with our test data as seen below" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.10386406]], dtype=float32)" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test_case = tf_vectorizer.transform([\"I really hated this movie\"])\n", "model.predict(test_case.todense())" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.98505175]], dtype=float32)" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test_case = tf_vectorizer.transform([\"I really loved this movie\"])\n", "model.predict(test_case.todense())" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGg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Lqqokh709LckvAg8Dn66qA3Pb\ny/kzV+OXlpokv8UgNN4/6r7MlKExA1X124d7L8nrSVZU1e52+eGNwxz3cwwC48tV9ZV56uq8mIvx\nH2OmfdRN5zGL1bE8th5d40/yq8CXgA9V1d4F6tuc8/LU3NsGbGz7G4FHJh+QJMAW4LtV9ScL2LeF\nMO34j0E9j7rZBlzd7qK6CHhz6DLeYrfUH/Uz7fiTnAV8Bfh4VX1/BH2cO1XlNocbcCqDu4ZeAr4B\nnNLqfx94tO2/HyjgO8AzbfvwqPu+UONvr+8DdjNYGN0JbBp132c57g8D3wdeBj7Tap8EPtn2w+AO\nm5eBZ4G1o+7zAo//l9vf8wHgf7f9k0bd7wUc/5eA/UP/fx8bdZ9nuvmNcElSNy9PSZK6GRqSpG6G\nhiSpm6EhSepmaEiSuhkakqRuhoYkqZuhIUnq9v8BKmH1DKNSffwAAAAASUVORK5CYII=\n", "text/plain": [ "<matplotlib.figure.Figure at 0x7f6b40fc6828>" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "plt.hist(model.get_weights()[0].ravel(),100)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[<tf.Variable 'dense_11/kernel:0' shape=(26967, 100) dtype=float32_ref>,\n", " <tf.Variable 'dense_11/bias:0' shape=(100,) dtype=float32_ref>,\n", " <tf.Variable 'dense_12/kernel:0' shape=(100, 1) dtype=float32_ref>,\n", " <tf.Variable 'dense_12/bias:0' shape=(1,) dtype=float32_ref>]" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.weights" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": 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.6.1" }, "latex_envs": { "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 0 } }, "nbformat": 4, "nbformat_minor": 1 }