{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[Sebastian Raschka](http://sebastianraschka.com), 2015\n", "\n", "https://github.com/rasbt/python-machine-learning-book" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Machine Learning - Code Examples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 8 - Applying Machine Learning To Sentiment Analysis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the optional watermark extension is a small IPython notebook plugin that I developed to make the code reproducible. You can just skip the following line(s)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sebastian Raschka \n", "Last updated: 01/20/2016 \n", "\n", "CPython 3.5.1\n", "IPython 4.0.1\n", "\n", "numpy 1.10.1\n", "pandas 0.17.1\n", "matplotlib 1.5.0\n", "scikit-learn 0.17\n", "nltk 3.1\n" ] } ], "source": [ "%load_ext watermark\n", "%watermark -a 'Sebastian Raschka' -u -d -v -p numpy,pandas,matplotlib,scikit-learn,nltk" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# to install watermark just uncomment the following line:\n", "#%install_ext https://raw.githubusercontent.com/rasbt/watermark/master/watermark.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Overview" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- [Obtaining the IMDb movie review dataset](#Obtaining-the-IMDb-movie-review-dataset)\n", "- [Introducing the bag-of-words model](#Introducing-the-bag-of-words-model)\n", " - [Transforming words into feature vectors](#Transforming-words-into-feature-vectors)\n", " - [Assessing word relevancy via term frequency-inverse document frequency](#Assessing-word-relevancy-via-term-frequency-inverse-document-frequency)\n", " - [Cleaning text data](#Cleaning-text-data)\n", " - [Processing documents into tokens](#Processing-documents-into-tokens)\n", "- [Training a logistic regression model for document classification](#Training-a-logistic-regression-model-for-document-classification)\n", "- [Working with bigger data – online algorithms and out-of-core learning](#Working-with-bigger-data-–-online-algorithms-and-out-of-core-learning)\n", "- [Summary](#Summary)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Obtaining the IMDb movie review dataset" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The IMDB movie review set can be downloaded from [http://ai.stanford.edu/~amaas/data/sentiment/](http://ai.stanford.edu/~amaas/data/sentiment/).\n", "After downloading the dataset, decompress the files.\n", "\n", "A) If you are working with Linux or MacOS X, open a new terminal windowm `cd` into the download directory and execute \n", "\n", "`tar -zxf aclImdb_v1.tar.gz`\n", "\n", "B) If you are working with Windows, download an archiver such as [7Zip](http://www.7-zip.org) to extract the files from the download archive." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Compatibility Note:\n", "\n", "I received an email from a reader who was having troubles with reading the movie review texts due to encoding issues. Typically, Python's default encoding is set to `'utf-8'`, which shouldn't cause troubles when running this IPython notebook. You can simply check the encoding on your machine by firing up a new Python interpreter from the command line terminal and execute\n", "\n", " >>> import sys\n", " >>> sys.getdefaultencoding()\n", " \n", "If the returned result is **not** `'utf-8'`, you probably need to change your Python's encoding to `'utf-8'`, for example by typing `export PYTHONIOENCODING=utf8` in your terminal shell prior to running this IPython notebook. (Note that this is a temporary change, and it needs to be executed in the same shell that you'll use to launch `ipython notebook`.\n", "\n", "Alternatively, you can replace the lines \n", "\n", " with open(os.path.join(path, file), 'r') as infile:\n", " ...\n", " pd.read_csv('./movie_data.csv')\n", " ...\n", " df.to_csv('./movie_data.csv', index=False)\n", "\n", "by \n", "\n", " with open(os.path.join(path, file), 'r', encoding='utf-8') as infile:\n", " ...\n", " pd.read_csv('./movie_data.csv', encoding='utf-8')\n", " ...\n", " df.to_csv('./movie_data.csv', index=False, encoding='utf-8')\n", " \n", "in the following cells to achieve the desired effect." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "0% 100%\n", "[##############################] | ETA: 00:00:00\n", "Total time elapsed: 00:06:23\n" ] } ], "source": [ "import pyprind\n", "import pandas as pd\n", "import os\n", "\n", "# change the `basepath` to the directory of the\n", "# unzipped movie dataset\n", "\n", "#basepath = '/Users/Sebastian/Desktop/aclImdb/'\n", "basepath = './aclImdb'\n", "\n", "labels = {'pos':1, 'neg':0}\n", "pbar = pyprind.ProgBar(50000)\n", "df = pd.DataFrame()\n", "for s in ('test', 'train'):\n", " for l in ('pos', 'neg'):\n", " path = os.path.join(basepath, s, l)\n", " for file in os.listdir(path):\n", " with open(os.path.join(path, file), 'r', encoding='utf-8') as infile:\n", " txt = infile.read()\n", " df = df.append([[txt, labels[l]]], ignore_index=True)\n", " pbar.update()\n", "df.columns = ['review', 'sentiment']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Shuffling the DataFrame:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np\n", "np.random.seed(0)\n", "df = df.reindex(np.random.permutation(df.index))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Optional: Saving the assembled data as CSV file:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "df.to_csv('./movie_data.csv', index=False)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
reviewsentiment
0In 1974, the teenager Martha Moxley (Maggie Gr...1
1OK... so... I really like Kris Kristofferson a...0
2***SPOILER*** Do not read this, if you think a...0
\n", "
" ], "text/plain": [ " review sentiment\n", "0 In 1974, the teenager Martha Moxley (Maggie Gr... 1\n", "1 OK... so... I really like Kris Kristofferson a... 0\n", "2 ***SPOILER*** Do not read this, if you think a... 0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "df = pd.read_csv('./movie_data.csv')\n", "df.head(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "### Note\n", "\n", "If you have problems with creating the `movie_data.csv` file in the previous chapter, you can find a download a zip archive at \n", "https://github.com/rasbt/python-machine-learning-book/tree/master/code/datasets/movie\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Introducing the bag-of-words model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Transforming documents into feature vectors" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "from sklearn.feature_extraction.text import CountVectorizer\n", "count = CountVectorizer()\n", "docs = np.array([\n", " 'The sun is shining',\n", " 'The weather is sweet',\n", " 'The sun is shining and the weather is sweet'])\n", "bag = count.fit_transform(docs)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'sweet': 4, 'and': 0, 'is': 1, 'shining': 2, 'sun': 3, 'the': 5, 'weather': 6}\n" ] } ], "source": [ "print(count.vocabulary_)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 1 1 1 0 1 0]\n", " [0 1 0 0 1 1 1]\n", " [1 2 1 1 1 2 1]]\n" ] } ], "source": [ "print(bag.toarray())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Assessing word relevancy via term frequency-inverse document frequency" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.set_printoptions(precision=2)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0. 0.43 0.56 0.56 0. 0.43 0. ]\n", " [ 0. 0.43 0. 0. 0.56 0.43 0.56]\n", " [ 0.4 0.48 0.31 0.31 0.31 0.48 0.31]]\n" ] } ], "source": [ "from sklearn.feature_extraction.text import TfidfTransformer\n", "\n", "tfidf = TfidfTransformer(use_idf=True, norm='l2', smooth_idf=True)\n", "print(tfidf.fit_transform(count.fit_transform(docs)).toarray())" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tf-idf of term \"is\" = 2.00\n" ] } ], "source": [ "tf_is = 2 \n", "n_docs = 3\n", "idf_is = np.log((n_docs+1) / (3+1) )\n", "tfidf_is = tf_is * (idf_is + 1)\n", "print('tf-idf of term \"is\" = %.2f' % tfidf_is)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1.69, 2. , 1.29, 1.29, 1.29, 2. , 1.29])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tfidf = TfidfTransformer(use_idf=True, norm=None, smooth_idf=True)\n", "raw_tfidf = tfidf.fit_transform(count.fit_transform(docs)).toarray()[-1]\n", "raw_tfidf " ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0.4 , 0.48, 0.31, 0.31, 0.31, 0.48, 0.31])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2_tfidf = raw_tfidf / np.sqrt(np.sum(raw_tfidf**2))\n", "l2_tfidf" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Cleaning text data" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'is seven.

Title (Brazil): Not Available'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc[0, 'review'][-50:]" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import re\n", "def preprocessor(text):\n", " text = re.sub('<[^>]*>', '', text)\n", " emoticons = re.findall('(?::|;|=)(?:-)?(?:\\)|\\(|D|P)', text)\n", " text = re.sub('[\\W]+', ' ', text.lower()) + \\\n", " ' '.join(emoticons).replace('-', '')\n", " return text" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'is seven title brazil not available'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "preprocessor(df.loc[0, 'review'][-50:])" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'this is a test :) :( :)'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "preprocessor(\"This :) is :( a test :-)!\")" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [], "source": [ "df['review'] = df['review'].apply(preprocessor)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Processing documents into tokens" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from nltk.stem.porter import PorterStemmer\n", "\n", "porter = PorterStemmer()\n", "\n", "def tokenizer(text):\n", " return text.split()\n", "def tokenizer_porter(text):\n", " return [porter.stem(word) for word in text.split()]" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['runners', 'like', 'running', 'and', 'thus', 'they', 'run']" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tokenizer('runners like running and thus they run')" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['runner', 'like', 'run', 'and', 'thu', 'they', 'run']" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tokenizer_porter('runners like running and thus they run')" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[nltk_data] Downloading package stopwords to\n", "[nltk_data] /Users/Sebastian/nltk_data...\n", "[nltk_data] Package stopwords is already up-to-date!\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import nltk\n", "nltk.download('stopwords')" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['runner', 'like', 'run', 'run', 'lot']" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from nltk.corpus import stopwords\n", "stop = stopwords.words('english')\n", "[w for w in tokenizer_porter('a runner likes running and runs a lot')[-10:] if w not in stop]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Training a logistic regression model for document classification" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Strip HTML and punctuation to speed up the GridSearch later:" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [], "source": [ "X_train = df.loc[:25000, 'review'].values\n", "y_train = df.loc[:25000, 'sentiment'].values\n", "X_test = df.loc[25000:, 'review'].values\n", "y_test = df.loc[25000:, 'sentiment'].values" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from sklearn.grid_search import GridSearchCV\n", "from sklearn.pipeline import Pipeline\n", "from sklearn.linear_model import LogisticRegression\n", "from sklearn.feature_extraction.text import TfidfVectorizer\n", "\n", "tfidf = TfidfVectorizer(strip_accents=None, \n", " lowercase=False, \n", " preprocessor=None)\n", "\n", "param_grid = [{'vect__ngram_range': [(1,1)],\n", " 'vect__stop_words': [stop, None],\n", " 'vect__tokenizer': [tokenizer, tokenizer_porter],\n", " 'clf__penalty': ['l1', 'l2'],\n", " 'clf__C': [1.0, 10.0, 100.0]},\n", " {'vect__ngram_range': [(1,1)],\n", " 'vect__stop_words': [stop, None],\n", " 'vect__tokenizer': [tokenizer, tokenizer_porter],\n", " 'vect__use_idf':[False],\n", " 'vect__norm':[None],\n", " 'clf__penalty': ['l1', 'l2'],\n", " 'clf__C': [1.0, 10.0, 100.0]},\n", " ]\n", "\n", "lr_tfidf = Pipeline([('vect', tfidf),\n", " ('clf', LogisticRegression(random_state=0))])\n", "\n", "gs_lr_tfidf = GridSearchCV(lr_tfidf, param_grid, \n", " scoring='accuracy',\n", " cv=5, verbose=1,\n", " n_jobs=-1)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Fitting 5 folds for each of 48 candidates, totalling 240 fits\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "[Parallel(n_jobs=-1)]: Done 42 tasks | elapsed: 12.0min\n", "[Parallel(n_jobs=-1)]: Done 192 tasks | elapsed: 53.2min\n", "[Parallel(n_jobs=-1)]: Done 240 out of 240 | elapsed: 69.4min finished\n" ] }, { "data": { "text/plain": [ "GridSearchCV(cv=5, error_score='raise',\n", " estimator=Pipeline(steps=[('vect', TfidfVectorizer(analyzer='word', binary=False, decode_error='strict',\n", " dtype=, encoding='utf-8', input='content',\n", " lowercase=False, max_df=1.0, max_features=None, min_df=1,\n", " ngram_range=(1, 1), norm='l2', preprocessor=None, smooth_idf=True,\n", " ...nalty='l2', random_state=0, solver='liblinear', tol=0.0001,\n", " verbose=0, warm_start=False))]),\n", " fit_params={}, iid=True, n_jobs=-1,\n", " param_grid=[{'vect__tokenizer': [, ], 'vect__ngram_range': [(1, 1)], 'clf__C': [1.0, 10.0, 100.0], 'clf__penalty': ['l1', 'l2'], 'vect__stop_words': [['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'you...kenizer at 0x111594400>, ], 'clf__penalty': ['l1', 'l2']}],\n", " pre_dispatch='2*n_jobs', refit=True, scoring='accuracy', verbose=1)" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gs_lr_tfidf.fit(X_train, y_train)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Best parameter set: {'vect__tokenizer': , 'vect__ngram_range': (1, 1), 'clf__C': 10.0, 'clf__penalty': 'l2', 'vect__stop_words': None} \n", "CV Accuracy: 0.897\n" ] } ], "source": [ "print('Best parameter set: %s ' % gs_lr_tfidf.best_params_)\n", "print('CV Accuracy: %.3f' % gs_lr_tfidf.best_score_)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Test Accuracy: 0.899\n" ] } ], "source": [ "clf = gs_lr_tfidf.best_estimator_\n", "print('Test Accuracy: %.3f' % clf.score(X_test, y_test))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Start comment:\n", " \n", "Please note that `gs_lr_tfidf.best_score_` is the average k-fold cross-validation score. I.e., if we have a `GridSearchCV` object with 5-fold cross-validation (like the one above), the `best_score_` attribute returns the average score over the 5-folds of the best model. To illustrate this with an example:" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0.6, 0.4, 0.6, 0.2, 0.6])" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.cross_validation import StratifiedKFold, cross_val_score\n", "from sklearn.linear_model import LogisticRegression\n", "import numpy as np\n", "\n", "np.random.seed(0)\n", "np.set_printoptions(precision=6)\n", "y = [np.random.randint(3) for i in range(25)]\n", "X = (y + np.random.randn(25)).reshape(-1, 1)\n", "\n", "cv5_idx = list(StratifiedKFold(y, n_folds=5, shuffle=False, random_state=0))\n", "cross_val_score(LogisticRegression(random_state=123), X, y, cv=cv5_idx)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By executing the code above, we created a simple data set of random integers that shall represent our class labels. Next, we fed the indices of 5 cross-validation folds (`cv3_idx`) to the `cross_val_score` scorer, which returned 5 accuracy scores -- these are the 5 accuracy values for the 5 test folds. \n", "\n", "Next, let us use the `GridSearchCV` object and feed it the same 5 cross-validation sets (via the pre-generated `cv3_idx` indices):" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Fitting 5 folds for each of 1 candidates, totalling 5 fits\n", "[CV] ................................................................\n", "[CV] ....................................... , score=0.600000 - 0.0s\n", "[CV] ................................................................\n", "[CV] ....................................... , score=0.400000 - 0.0s\n", "[CV] ................................................................\n", "[CV] ....................................... , score=0.600000 - 0.0s\n", "[CV] ................................................................\n", "[CV] ....................................... , score=0.200000 - 0.0s\n", "[CV] ................................................................\n", "[CV] ....................................... , score=0.600000 - 0.0s\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "[Parallel(n_jobs=1)]: Done 5 out of 5 | elapsed: 0.0s finished\n" ] } ], "source": [ "from sklearn.grid_search import GridSearchCV\n", "gs = GridSearchCV(LogisticRegression(), {}, cv=cv5_idx, verbose=3).fit(X, y) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As we can see, the scores for the 5 folds are exactly the same as the ones from `cross_val_score` earlier." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, the best_score_ attribute of the `GridSearchCV` object, which becomes available after `fit`ting, returns the average accuracy score of the best model:" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.47999999999999998" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gs.best_score_" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As we can see, the result above is consistent with the average score computed the `cross_val_score`." ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.47999999999999998" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cross_val_score(LogisticRegression(), X, y, cv=cv5_idx).mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### End comment.\n", "\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Working with bigger data - online algorithms and out-of-core learning" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np\n", "import re\n", "from nltk.corpus import stopwords\n", "\n", "stop = stopwords.words('english')\n", "\n", "def tokenizer(text):\n", " text = re.sub('<[^>]*>', '', text)\n", " emoticons = re.findall('(?::|;|=)(?:-)?(?:\\)|\\(|D|P)', text.lower())\n", " text = re.sub('[\\W]+', ' ', text.lower()) + ' '.join(emoticons).replace('-', '')\n", " tokenized = [w for w in text.split() if w not in stop]\n", " return tokenized\n", "\n", "def stream_docs(path):\n", " with open(path, 'r', encoding='utf-8') as csv:\n", " next(csv) # skip header\n", " for line in csv:\n", " text, label = line[:-3], int(line[-2])\n", " yield text, label" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "('\"In 1974, the teenager Martha Moxley (Maggie Grace) moves to the high-class area of Belle Haven, Greenwich, Connecticut. On the Mischief Night, eve of Halloween, she was murdered in the backyard of her house and her murder remained unsolved. Twenty-two years later, the writer Mark Fuhrman (Christopher Meloni), who is a former LA detective that has fallen in disgrace for perjury in O.J. Simpson trial and moved to Idaho, decides to investigate the case with his partner Stephen Weeks (Andrew Mitchell) with the purpose of writing a book. The locals squirm and do not welcome them, but with the support of the retired detective Steve Carroll (Robert Forster) that was in charge of the investigation in the 70\\'s, they discover the criminal and a net of power and money to cover the murder.

\"\"Murder in Greenwich\"\" is a good TV movie, with the true story of a murder of a fifteen years old girl that was committed by a wealthy teenager whose mother was a Kennedy. The powerful and rich family used their influence to cover the murder for more than twenty years. However, a snoopy detective and convicted perjurer in disgrace was able to disclose how the hideous crime was committed. The screenplay shows the investigation of Mark and the last days of Martha in parallel, but there is a lack of the emotion in the dramatization. My vote is seven.

Title (Brazil): Not Available\"',\n", " 1)" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(stream_docs(path='./movie_data.csv'))" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def get_minibatch(doc_stream, size):\n", " docs, y = [], []\n", " try:\n", " for _ in range(size):\n", " text, label = next(doc_stream)\n", " docs.append(text)\n", " y.append(label)\n", " except StopIteration:\n", " return None, None\n", " return docs, y" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from sklearn.feature_extraction.text import HashingVectorizer\n", "from sklearn.linear_model import SGDClassifier\n", "\n", "vect = HashingVectorizer(decode_error='ignore', \n", " n_features=2**21,\n", " preprocessor=None, \n", " tokenizer=tokenizer)\n", "\n", "clf = SGDClassifier(loss='log', random_state=1, n_iter=1)\n", "doc_stream = stream_docs(path='./movie_data.csv')" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "0% 100%\n", "[##############################] | ETA: 00:00:00\n", "Total time elapsed: 00:00:33\n" ] } ], "source": [ "import pyprind\n", "pbar = pyprind.ProgBar(45)\n", "\n", "classes = np.array([0, 1])\n", "for _ in range(45):\n", " X_train, y_train = get_minibatch(doc_stream, size=1000)\n", " if not X_train:\n", " break\n", " X_train = vect.transform(X_train)\n", " clf.partial_fit(X_train, y_train, classes=classes)\n", " pbar.update()" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Accuracy: 0.868\n" ] } ], "source": [ "X_test, y_test = get_minibatch(doc_stream, size=5000)\n", "X_test = vect.transform(X_test)\n", "print('Accuracy: %.3f' % clf.score(X_test, y_test))" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [], "source": [ "clf = clf.partial_fit(X_test, y_test)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Summary" ] } ], "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 }