diff --git a/.gitignore b/.gitignore index 266fe2b..d25cdef 100644 --- a/.gitignore +++ b/.gitignore @@ -435,4 +435,5 @@ TSWLatexianTemp* # REVTeX puts footnotes in the bibliography by default, unless the nofootinbib # option is specified. Footnotes are the stored in a file with suffix Notes.bib. # Uncomment the next line to have this generated file ignored. -#*Notes.bib \ No newline at end of file +#*Notes.bib +*.ipynb:Zone.Identifier diff --git a/building-features-from-text-data/01-TokenizingTextIntoSentencesAndWords.ipynb b/building-features-from-text-data/01-TokenizingTextIntoSentencesAndWords.ipynb new file mode 100644 index 0000000..e8da7eb --- /dev/null +++ b/building-features-from-text-data/01-TokenizingTextIntoSentencesAndWords.ipynb @@ -0,0 +1,813 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Tokenize text into words and sentences" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.16.1\n" + ] + } + ], + "source": [ + "import numpy\n", + "\n", + "print(numpy.__version__)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.4\n" + ] + } + ], + "source": [ + "import nltk\n", + "\n", + "print(nltk.__version__)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Uses NLTK's recommended sentence tokenizer, the PunktSentenceTokenizer\n", + "#### Uses NLTK's recommended word tokenizer, the TreebankWordTokenizer and the PunktSentencetokenizer" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "from nltk.tokenize import word_tokenize\n", + "from nltk.tokenize import sent_tokenize" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "ename": "LookupError", + "evalue": "\n**********************************************************************\n Resource \u001b[93mpunkt\u001b[0m not found.\n Please use the NLTK Downloader to obtain the resource:\n\n \u001b[31m>>> import nltk\n >>> nltk.download('punkt')\n \u001b[0m\n Attempted to load \u001b[93mtokenizers/punkt/PY3/english.pickle\u001b[0m\n\n Searched in:\n - '/Users/loonycorn/nltk_data'\n - '/anaconda3/nltk_data'\n - '/anaconda3/share/nltk_data'\n - '/anaconda3/lib/nltk_data'\n - '/usr/share/nltk_data'\n - '/usr/local/share/nltk_data'\n - '/usr/lib/nltk_data'\n - '/usr/local/lib/nltk_data'\n - ''\n**********************************************************************\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m--------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mLookupError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msent_tokens\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msent_tokenize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Does this tokenizer work? These are two different sentences'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msent_tokens\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/anaconda3/lib/python3.7/site-packages/nltk/tokenize/__init__.py\u001b[0m in \u001b[0;36msent_tokenize\u001b[0;34m(text, language)\u001b[0m\n\u001b[1;32m 102\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0mparam\u001b[0m \u001b[0mlanguage\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mthe\u001b[0m \u001b[0mmodel\u001b[0m \u001b[0mname\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mthe\u001b[0m \u001b[0mPunkt\u001b[0m \u001b[0mcorpus\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 103\u001b[0m \"\"\"\n\u001b[0;32m--> 104\u001b[0;31m \u001b[0mtokenizer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'tokenizers/punkt/{0}.pickle'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlanguage\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 105\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mtokenizer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtokenize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtext\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 106\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/anaconda3/lib/python3.7/site-packages/nltk/data.py\u001b[0m in \u001b[0;36mload\u001b[0;34m(resource_url, format, cache, verbose, logic_parser, fstruct_reader, encoding)\u001b[0m\n\u001b[1;32m 866\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 867\u001b[0m \u001b[0;31m# Load the resource.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 868\u001b[0;31m \u001b[0mopened_resource\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_open\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresource_url\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 869\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 870\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mformat\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'raw'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/anaconda3/lib/python3.7/site-packages/nltk/data.py\u001b[0m in \u001b[0;36m_open\u001b[0;34m(resource_url)\u001b[0m\n\u001b[1;32m 991\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 992\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mprotocol\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mprotocol\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlower\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'nltk'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 993\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfind\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpath_\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpath\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m''\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 994\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mprotocol\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlower\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'file'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 995\u001b[0m \u001b[0;31m# urllib might not use mode='rb', so handle this one ourselves:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/anaconda3/lib/python3.7/site-packages/nltk/data.py\u001b[0m in \u001b[0;36mfind\u001b[0;34m(resource_name, paths)\u001b[0m\n\u001b[1;32m 697\u001b[0m \u001b[0msep\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'*'\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0;36m70\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 698\u001b[0m \u001b[0mresource_not_found\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'\\n%s\\n%s\\n%s\\n'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0msep\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmsg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msep\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 699\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mLookupError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresource_not_found\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 700\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 701\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mLookupError\u001b[0m: \n**********************************************************************\n Resource \u001b[93mpunkt\u001b[0m not found.\n Please use the NLTK Downloader to obtain the resource:\n\n \u001b[31m>>> import nltk\n >>> nltk.download('punkt')\n \u001b[0m\n Attempted to load \u001b[93mtokenizers/punkt/PY3/english.pickle\u001b[0m\n\n Searched in:\n - '/Users/loonycorn/nltk_data'\n - '/anaconda3/nltk_data'\n - '/anaconda3/share/nltk_data'\n - '/anaconda3/lib/nltk_data'\n - '/usr/share/nltk_data'\n - '/usr/local/share/nltk_data'\n - '/usr/lib/nltk_data'\n - '/usr/local/lib/nltk_data'\n - ''\n**********************************************************************\n" + ] + } + ], + "source": [ + "sent_tokens = sent_tokenize('Does this tokenizer work? These are two different sentences')\n", + "\n", + "print(sent_tokens)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "ename": "LookupError", + "evalue": "\n**********************************************************************\n Resource \u001b[93mpunkt\u001b[0m not found.\n Please use the NLTK Downloader to obtain the resource:\n\n \u001b[31m>>> import nltk\n >>> nltk.download('punkt')\n \u001b[0m\n Attempted to load \u001b[93mtokenizers/punkt/PY3/english.pickle\u001b[0m\n\n Searched in:\n - '/Users/loonycorn/nltk_data'\n - '/anaconda3/nltk_data'\n - '/anaconda3/share/nltk_data'\n - '/anaconda3/lib/nltk_data'\n - '/usr/share/nltk_data'\n - '/usr/local/share/nltk_data'\n - '/usr/lib/nltk_data'\n - '/usr/local/lib/nltk_data'\n - ''\n**********************************************************************\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m--------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mLookupError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mword_tokens\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mword_tokenize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Does this tokenizer work?'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mword_tokens\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/anaconda3/lib/python3.7/site-packages/nltk/tokenize/__init__.py\u001b[0m in \u001b[0;36mword_tokenize\u001b[0;34m(text, language, preserve_line)\u001b[0m\n\u001b[1;32m 141\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0mtype\u001b[0m \u001b[0mpreserve_line\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mbool\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 142\u001b[0m \"\"\"\n\u001b[0;32m--> 143\u001b[0;31m \u001b[0msentences\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mtext\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mpreserve_line\u001b[0m \u001b[0;32melse\u001b[0m \u001b[0msent_tokenize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtext\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlanguage\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 144\u001b[0m return [\n\u001b[1;32m 145\u001b[0m \u001b[0mtoken\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0msent\u001b[0m \u001b[0;32min\u001b[0m \u001b[0msentences\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mtoken\u001b[0m \u001b[0;32min\u001b[0m \u001b[0m_treebank_word_tokenizer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtokenize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msent\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/anaconda3/lib/python3.7/site-packages/nltk/tokenize/__init__.py\u001b[0m in \u001b[0;36msent_tokenize\u001b[0;34m(text, language)\u001b[0m\n\u001b[1;32m 102\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0mparam\u001b[0m \u001b[0mlanguage\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mthe\u001b[0m \u001b[0mmodel\u001b[0m \u001b[0mname\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mthe\u001b[0m \u001b[0mPunkt\u001b[0m \u001b[0mcorpus\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 103\u001b[0m \"\"\"\n\u001b[0;32m--> 104\u001b[0;31m \u001b[0mtokenizer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'tokenizers/punkt/{0}.pickle'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlanguage\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 105\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mtokenizer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtokenize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtext\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 106\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/anaconda3/lib/python3.7/site-packages/nltk/data.py\u001b[0m in \u001b[0;36mload\u001b[0;34m(resource_url, format, cache, verbose, logic_parser, fstruct_reader, encoding)\u001b[0m\n\u001b[1;32m 866\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 867\u001b[0m \u001b[0;31m# Load the resource.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 868\u001b[0;31m \u001b[0mopened_resource\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_open\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresource_url\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 869\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 870\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mformat\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'raw'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/anaconda3/lib/python3.7/site-packages/nltk/data.py\u001b[0m in \u001b[0;36m_open\u001b[0;34m(resource_url)\u001b[0m\n\u001b[1;32m 991\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 992\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mprotocol\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mprotocol\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlower\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'nltk'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 993\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfind\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpath_\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpath\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m''\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 994\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mprotocol\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlower\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'file'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 995\u001b[0m \u001b[0;31m# urllib might not use mode='rb', so handle this one ourselves:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/anaconda3/lib/python3.7/site-packages/nltk/data.py\u001b[0m in \u001b[0;36mfind\u001b[0;34m(resource_name, paths)\u001b[0m\n\u001b[1;32m 697\u001b[0m \u001b[0msep\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'*'\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0;36m70\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 698\u001b[0m \u001b[0mresource_not_found\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'\\n%s\\n%s\\n%s\\n'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0msep\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmsg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msep\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 699\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mLookupError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresource_not_found\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 700\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 701\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mLookupError\u001b[0m: \n**********************************************************************\n Resource \u001b[93mpunkt\u001b[0m not found.\n Please use the NLTK Downloader to obtain the resource:\n\n \u001b[31m>>> import nltk\n >>> nltk.download('punkt')\n \u001b[0m\n Attempted to load \u001b[93mtokenizers/punkt/PY3/english.pickle\u001b[0m\n\n Searched in:\n - '/Users/loonycorn/nltk_data'\n - '/anaconda3/nltk_data'\n - '/anaconda3/share/nltk_data'\n - '/anaconda3/lib/nltk_data'\n - '/usr/share/nltk_data'\n - '/usr/local/share/nltk_data'\n - '/usr/lib/nltk_data'\n - '/usr/local/lib/nltk_data'\n - ''\n**********************************************************************\n" + ] + } + ], + "source": [ + "word_tokens = word_tokenize('Does this tokenizer work?')\n", + "\n", + "print(word_tokens)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Punkt tokenizer\n", + "\n", + "https://www.nltk.org/_modules/nltk/tokenize/punkt.html\n", + "http://www.nltk.org/api/nltk.tokenize.html#module-nltk.tokenize.punkt\n", + "\n", + "A sentence tokenizer which uses an unsupervised algorithm to build a model for abbreviation words, collocations, and words that start sentences; and then uses that model to find sentence boundaries. This approach has been shown to work well for many European languages.\n", + "\n", + "It must be trained on a large collection of plaintext in the target language before it can be used.\n", + "\n", + "The NLTK data package includes a pre-trained Punkt tokenizer for English." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "[nltk_data] Downloading package punkt to /Users/loonycorn/nltk_data...\n", + "[nltk_data] Package punkt is already up-to-date!\n" + ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "nltk.download('punkt')" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Does this tokenizer work?', 'These are two different sentences']\n" + ] + } + ], + "source": [ + "sent_tokens = sent_tokenize('Does this tokenizer work? These are two different sentences')\n", + "\n", + "print(sent_tokens)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Does', 'this', 'tokenizer', 'work', '?']\n" + ] + } + ], + "source": [ + "word_tokens = word_tokenize('Does this tokenizer work?')\n", + "\n", + "print(word_tokens)" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['A', 'bird', 'in', 'hand', 'is', 'worth', 'two', 'in', 'the', 'bush', '.', 'Good', 'things', 'come', 'to', 'those', 'who', 'wait', '.', 'These', 'watches', 'cost', '$', '1500', '!', 'The', 'ball', 'is', 'in', 'your', 'court', '.', 'Mr.', 'Smith', 'Goes', 'to', 'Washington', 'Doogie', 'Howser', 'M.D', '.']\n" + ] + } + ], + "source": [ + "text = \"A bird in hand is worth two in the bush. \" +\\\n", + " \"Good things come to those who wait. \" +\\\n", + " \"These watches cost $1500! \" +\\\n", + " \"The ball is in your court. \" +\\\n", + " \"Mr. Smith Goes to Washington \" +\\\n", + " \"Doogie Howser M.D.\"\n", + "\n", + "word_tokens = word_tokenize(text, language='english')\n", + "print(word_tokens)" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "41" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(word_tokens)" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['hand', 'is', 'worth', 'two', 'in']" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "word_tokens[3:8]" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [], + "source": [ + "from nltk.tokenize.punkt import PunktSentenceTokenizer" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [], + "source": [ + "pst = PunktSentenceTokenizer()" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['A bird in hand is worth two in the bush.', 'Good things come to those who wait.', 'These watches cost $1500!', 'The ball is in your court.', 'Mr.', 'Smith Goes to Washington Doogie Howser M.D.']\n" + ] + } + ], + "source": [ + "sent_tokens = pst.tokenize(text)\n", + "\n", + "print(sent_tokens)" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(0, 40), (41, 76), (77, 102), (103, 129), (130, 133), (134, 177)]\n" + ] + } + ], + "source": [ + "span_tokens = pst.span_tokenize(text)\n", + "\n", + "print(list(span_tokens))" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[['A', 'bird', 'in', 'hand', 'is', 'worth', 'two', 'in', 'the', 'bush', '.'],\n", + " ['Good', 'things', 'come', 'to', 'those', 'who', 'wait', '.'],\n", + " ['These', 'watches', 'cost', '$', '1500', '!'],\n", + " ['The', 'ball', 'is', 'in', 'your', 'court', '.'],\n", + " ['Mr.'],\n", + " ['Smith', 'Goes', 'to', 'Washington', 'Doogie', 'Howser', 'M.D', '.']]" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sentences = pst.sentences_from_tokens(word_tokens)\n", + "\n", + "list(sentences)" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [], + "source": [ + "from nltk.tokenize import WhitespaceTokenizer" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [], + "source": [ + "wt = WhitespaceTokenizer()" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['A', 'bird', 'in', 'hand', 'is', 'worth', 'two', 'in', 'the', 'bush.', 'Good', 'things', 'come', 'to', 'those', 'who', 'wait.', 'These', 'watches', 'cost', '$1500!', 'The', 'ball', 'is', 'in', 'your', 'court.', 'Mr.', 'Smith', 'Goes', 'to', 'Washington', 'Doogie', 'Howser', 'M.D.']\n" + ] + } + ], + "source": [ + "word_tokens = wt.tokenize(text)\n", + "\n", + "print(word_tokens)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Reading Local Files" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Marie Curie was a Polish-born physicist and chemist and one of the most famous scientists of her time.\n", + "Together with her husband Pierre, she was awarded the Nobel Prize in 1903, and she went on to win another in 1911.\n", + "Marie Sklodowska was born in Warsaw on 7 November 1867, the daughter of a teacher.\n", + "In 1891, she went to Paris to study physics and mathematics at the Sorbonne where she met Pierre Curie, professor of the School of Physics.\n", + "They were married in 1895.\n", + "The Curies worked together investigating radioactivity, building on the work of the German physicist Roentgen and the French physicist Becquerel.\n", + "In July 1898, the Curies announced the discovery of a new chemical element, polonium.\n", + "At the end of the year, they announced the discovery of another, radium.\n", + "The Curies, along with Becquerel, were awarded the Nobel Prize for Physics in 1903.\n", + "Pierre's life was cut short in 1906 when he was knocked down and killed by a carriage.\n", + "Marie took over his teaching post, becoming the first woman to teach at the Sorbonne, and devoted herself to continuing the work that they had begun together.\n", + "She received a second Nobel Prize, for Chemistry, in 1911.\n", + "The Curie's research was crucial in the development of x-rays in surgery.\n", + "During World War One Curie helped to equip ambulances with x-ray equipment, which she herself drove to the front lines.\n", + "The International Red Cross made her head of its radiological service and she held training courses for medical orderlies and doctors in the new techniques.\n", + "Despite her success, Marie continued to face great opposition from male scientists in France, and she never received significant financial benefits from her work.\n", + "By the late 1920s her health was beginning to deteriorate.\n", + "She died on 4 July 1934 from leukaemia, caused by exposure to high-energy radiation from her research.\n", + "The Curies' eldest daughter Irene was herself a scientist and winner of the Nobel Prize for Chemistry.\n" + ] + } + ], + "source": [ + "with open('./datasets/biography.txt', 'r') as f:\n", + " file_contents = f.read()\n", + "\n", + "print(file_contents)" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Marie', 'Curie', 'was', 'a', 'Polish-born', 'physicist', 'and', 'chemist', 'and', 'one', 'of', 'the', 'most', 'famous', 'scientists', 'of', 'her', 'time', '.', 'Together', 'with', 'her', 'husband', 'Pierre', ',', 'she', 'was', 'awarded', 'the', 'Nobel', 'Prize', 'in', '1903', ',', 'and', 'she', 'went', 'on', 'to', 'win', 'another', 'in', '1911', '.', 'Marie', 'Sklodowska', 'was', 'born', 'in', 'Warsaw', 'on', '7', 'November', '1867', ',', 'the', 'daughter', 'of', 'a', 'teacher', '.', 'In', '1891', ',', 'she', 'went', 'to', 'Paris', 'to', 'study', 'physics', 'and', 'mathematics', 'at', 'the', 'Sorbonne', 'where', 'she', 'met', 'Pierre', 'Curie', ',', 'professor', 'of', 'the', 'School', 'of', 'Physics', '.', 'They', 'were', 'married', 'in', '1895', '.', 'The', 'Curies', 'worked', 'together', 'investigating', 'radioactivity', ',', 'building', 'on', 'the', 'work', 'of', 'the', 'German', 'physicist', 'Roentgen', 'and', 'the', 'French', 'physicist', 'Becquerel', '.', 'In', 'July', '1898', ',', 'the', 'Curies', 'announced', 'the', 'discovery', 'of', 'a', 'new', 'chemical', 'element', ',', 'polonium', '.', 'At', 'the', 'end', 'of', 'the', 'year', ',', 'they', 'announced', 'the', 'discovery', 'of', 'another', ',', 'radium', '.', 'The', 'Curies', ',', 'along', 'with', 'Becquerel', ',', 'were', 'awarded', 'the', 'Nobel', 'Prize', 'for', 'Physics', 'in', '1903', '.', 'Pierre', \"'s\", 'life', 'was', 'cut', 'short', 'in', '1906', 'when', 'he', 'was', 'knocked', 'down', 'and', 'killed', 'by', 'a', 'carriage', '.', 'Marie', 'took', 'over', 'his', 'teaching', 'post', ',', 'becoming', 'the', 'first', 'woman', 'to', 'teach', 'at', 'the', 'Sorbonne', ',', 'and', 'devoted', 'herself', 'to', 'continuing', 'the', 'work', 'that', 'they', 'had', 'begun', 'together', '.', 'She', 'received', 'a', 'second', 'Nobel', 'Prize', ',', 'for', 'Chemistry', ',', 'in', '1911', '.', 'The', 'Curie', \"'s\", 'research', 'was', 'crucial', 'in', 'the', 'development', 'of', 'x-rays', 'in', 'surgery', '.', 'During', 'World', 'War', 'One', 'Curie', 'helped', 'to', 'equip', 'ambulances', 'with', 'x-ray', 'equipment', ',', 'which', 'she', 'herself', 'drove', 'to', 'the', 'front', 'lines', '.', 'The', 'International', 'Red', 'Cross', 'made', 'her', 'head', 'of', 'its', 'radiological', 'service', 'and', 'she', 'held', 'training', 'courses', 'for', 'medical', 'orderlies', 'and', 'doctors', 'in', 'the', 'new', 'techniques', '.', 'Despite', 'her', 'success', ',', 'Marie', 'continued', 'to', 'face', 'great', 'opposition', 'from', 'male', 'scientists', 'in', 'France', ',', 'and', 'she', 'never', 'received', 'significant', 'financial', 'benefits', 'from', 'her', 'work', '.', 'By', 'the', 'late', '1920s', 'her', 'health', 'was', 'beginning', 'to', 'deteriorate', '.', 'She', 'died', 'on', '4', 'July', '1934', 'from', 'leukaemia', ',', 'caused', 'by', 'exposure', 'to', 'high-energy', 'radiation', 'from', 'her', 'research', '.', 'The', 'Curies', \"'\", 'eldest', 'daughter', 'Irene', 'was', 'herself', 'a', 'scientist', 'and', 'winner', 'of', 'the', 'Nobel', 'Prize', 'for', 'Chemistry', '.']\n" + ] + } + ], + "source": [ + "word_tokens = word_tokenize(file_contents)\n", + "\n", + "print(word_tokens)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Frequency distributions" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": {}, + "outputs": [], + "source": [ + "from nltk.probability import FreqDist" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "freq_dist = FreqDist(word_tokens)\n", + "\n", + "print(freq_dist)" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('the', 22),\n", + " (',', 20),\n", + " ('.', 19),\n", + " ('of', 12),\n", + " ('and', 11),\n", + " ('in', 11),\n", + " ('to', 10),\n", + " ('was', 8),\n", + " ('her', 7),\n", + " ('she', 7),\n", + " ('a', 6),\n", + " ('The', 5),\n", + " ('Marie', 4),\n", + " ('Curie', 4),\n", + " ('Nobel', 4),\n", + " ('Prize', 4),\n", + " ('on', 4),\n", + " ('Curies', 4),\n", + " ('for', 4),\n", + " ('from', 4)]" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "freq_dist.most_common(20)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Return the frequency of a given sample. The frequency of a sample is defined as the count of that sample divided by the total number of sample outcomes that have been recorded by this FreqDist. The count of a sample is defined as the number of times that sample outcome was recorded by this FreqDist. Frequencies are always real numbers in the range [0, 1]." + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.05994550408719346" + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "freq_dist.freq('the')" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.0027247956403269754" + ] + }, + "execution_count": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "freq_dist.freq('exposure')" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": {}, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAtoAAAH5CAYAAAC/Ppk2AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3Xd4leX9x/HPfTJJSAgzJKyIQJQgAROhiKRqK8u6J63W0Yq2ioLVWrvUTjtUEK2jztZfcS9AwFErGw0jENmbsGdCSMi8f39w0IgJBMlz7jPer+s6lznPeZ58P/GPXp8+3ue5jbVWAAAAAJqWz3UAAAAAIBxRtAEAAAAPULQBAAAAD1C0AQAAAA9QtAEAAAAPULQBAAAAD1C0AQAAAA9QtAEAAAAPULQBAAAAD0S7DtCU2rRpYzMyMgI+t7y8XM2aNQv4XOYzn/nMZ35kzw+GDMxnfiTOnz9//i5rbdtjnmitDZtXTk6OdSE/P9/JXOYzn/nMZ35kzw+GDMxnfiTOl5RvG9FNWToCAAAAeICiDQAAAHiAog0AAAB4wLOibYzpZIz52BizzBjzuTHmDv/xvxljlhtjFhtj3jLGpDRw/XpjzBJjzCJjTL5XOQEAAAAveHlHu1rSz6y1p0r6lqRbjTE9JX0gqZe1treklZLuPcrvOMda28dam+thTgAAAKDJeVa0rbVbrbUL/D/vl7RMUgdr7fvW2mr/aXMldfQqAwAAAOBKQNZoG2MyJPWVNO+Ij26UNKWBy6yk940x840xI71LBwAAADQ9c+hRgB4OMKa5pE8k/dFa+2ad47+SlCvpUltPCGNMurV2izGmnQ4tNxllrZ1ez3kjJY2UpLS0tJyJEyd69Jc0rKysTAkJC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V+LEEJoaTHRDiGEnuUOYDVJf5b0K0nb5PsvsD0kb9G8JGnVu2Gm7a2BC4EbgJGk9mAHSVox/5t1gIttbwC8CxzRHDSvnJ8I7GB7I+CPwDGS+gN7Al/Ozz29gu85hBB6pJhohxBCD2L7fWBj4FDgDeB3kg4CtpX0kKSpwHbAl5uedmP+PJXUd/fVvCL+HLBafuxF2+Pz178GtmwXeigwGBgvaTJwIDCANCn/CLhU0l7Ahwvsmw0hhB4ucrRDCKGHsT0bGAuMzRPrw4ANgE1svyjpZKBv01Nm5M+fNH3duN04D7TfVKH9bQF32h7WfjySNgW2B/4Z+C5poh9CCL1erGiHEEIPImkdSWs13bUh8FT++s2cN713N/7rL+VCS4BhwP3tHp8AbCFpzTyOpSStneP1s30LcHQeTwghBGJFO4QQepplgPMlLQ/MAp4hpZG8TUoNeQGY2I3/9wngQEkXAU8Do5oftP1GTlG5StIS+e4TgfeAGyT1Ja16/1s3YocQwkIptmAPIYReTtJAYEwupAwhhLCAROpICCGEEEIIFYgV7RBCCCGEECoQK9ohhBBCCCFUICbaIYQQQgghVCAm2iGEEEIIIVQgJtohhBBCCCFUICbaIYQQQgghVCAm2iGEEEIIIVTg/wE2saA4ATrh0AAAAABJRU5ErkJggg==\n", + "text/plain": [ + "
" + ] + }, + "metadata": { + "needs_background": "light" + }, + "output_type": "display_data" + } + ], + "source": [ + "import matplotlib.pyplot as plt\n", + "\n", + "fig, ax = plt.subplots(figsize=(12, 8))\n", + "\n", + "freq_dist.plot(20, cumulative=False)\n", + "\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": 89, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['marie', 'curie', 'was', 'a', 'polish-born', 'physicist', 'and', 'chemist', 'and', 'one', 'of', 'the', 'most', 'famous', 'scientists', 'of', 'her', 'time.', 'together', 'with', 'her', 'husband', 'pierre,', 'she', 'was', 'awarded', 'the', 'nobel', 'prize', 'in', '1903,', 'and', 'she', 'went', 'on', 'to', 'win', 'another', 'in', '1911.', 'marie', 'sklodowska', 'was', 'born', 'in', 'warsaw', 'on', '7', 'november', '1867,', 'the', 'daughter', 'of', 'a', 'teacher.', 'in', '1891,', 'she', 'went', 'to', 'paris', 'to', 'study', 'physics', 'and', 'mathematics', 'at', 'the', 'sorbonne', 'where', 'she', 'met', 'pierre', 'curie,', 'professor', 'of', 'the', 'school', 'of', 'physics.', 'they', 'were', 'married', 'in', '1895.', 'the', 'curies', 'worked', 'together', 'investigating', 'radioactivity,', 'building', 'on', 'the', 'work', 'of', 'the', 'german', 'physicist', 'roentgen', 'and', 'the', 'french', 'physicist', 'becquerel.', 'in', 'july', '1898,', 'the', 'curies', 'announced', 'the', 'discovery', 'of', 'a', 'new', 'chemical', 'element,', 'polonium.', 'at', 'the', 'end', 'of', 'the', 'year,', 'they', 'announced', 'the', 'discovery', 'of', 'another,', 'radium.', 'the', 'curies,', 'along', 'with', 'becquerel,', 'were', 'awarded', 'the', 'nobel', 'prize', 'for', 'physics', 'in', '1903.', \"pierre's\", 'life', 'was', 'cut', 'short', 'in', '1906', 'when', 'he', 'was', 'knocked', 'down', 'and', 'killed', 'by', 'a', 'carriage.', 'marie', 'took', 'over', 'his', 'teaching', 'post,', 'becoming', 'the', 'first', 'woman', 'to', 'teach', 'at', 'the', 'sorbonne,', 'and', 'devoted', 'herself', 'to', 'continuing', 'the', 'work', 'that', 'they', 'had', 'begun', 'together.', 'she', 'received', 'a', 'second', 'nobel', 'prize,', 'for', 'chemistry,', 'in', '1911.', 'the', \"curie's\", 'research', 'was', 'crucial', 'in', 'the', 'development', 'of', 'x-rays', 'in', 'surgery.', 'during', 'world', 'war', 'one', 'curie', 'helped', 'to', 'equip', 'ambulances', 'with', 'x-ray', 'equipment,', 'which', 'she', 'herself', 'drove', 'to', 'the', 'front', 'lines.', 'the', 'international', 'red', 'cross', 'made', 'her', 'head', 'of', 'its', 'radiological', 'service', 'and', 'she', 'held', 'training', 'courses', 'for', 'medical', 'orderlies', 'and', 'doctors', 'in', 'the', 'new', 'techniques.', 'despite', 'her', 'success,', 'marie', 'continued', 'to', 'face', 'great', 'opposition', 'from', 'male', 'scientists', 'in', 'france,', 'and', 'she', 'never', 'received', 'significant', 'financial', 'benefits', 'from', 'her', 'work.', 'by', 'the', 'late', '1920s', 'her', 'health', 'was', 'beginning', 'to', 'deteriorate.', 'she', 'died', 'on', '4', 'july', '1934', 'from', 'leukaemia,', 'caused', 'by', 'exposure', 'to', 'high-energy', 'radiation', 'from', 'her', 'research.', 'the', \"curies'\", 'eldest', 'daughter', 'irene', 'was', 'herself', 'a', 'scientist', 'and', 'winner', 'of', 'the', 'nobel', 'prize', 'for', 'chemistry.']\n" + ] + } + ], + "source": [ + "file_contents = file_contents.lower()\n", + "\n", + "word_tokens = wt.tokenize(file_contents)\n", + "\n", + "print(word_tokens)" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "[nltk_data] Downloading package stopwords to\n", + "[nltk_data] /Users/loonycorn/nltk_data...\n", + "[nltk_data] Package stopwords is already up-to-date!\n" + ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 90, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "nltk.download('stopwords')" + ] + }, + { + "cell_type": "code", + "execution_count": 91, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'an', 'should', \"needn't\", 'o', 'ours', 'yourselves', 'am', 't', 'd', 'our', 'yourself', 'himself', 'wasn', 'above', 'you', 'me', 'did', \"hasn't\", 'shan', 'about', 'wouldn', 'shouldn', 'because', 'myself', 'where', 'hasn', 'hadn', 'own', 'hers', 'at', \"mightn't\", 'now', 'with', 'how', 'some', 'or', 'had', 'having', 'aren', 'm', \"shouldn't\", 'was', 'don', 'same', \"didn't\", 'against', 'why', 'y', 'any', 'do', 'being', 'll', 'up', 'doesn', 'which', 'again', 'as', 'in', 'mightn', \"you'll\", \"that'll\", 'can', 'for', 'haven', 's', 'him', 'ain', 'from', 'then', 'once', 'that', \"you're\", 'such', 'does', '.', 'these', 'be', 'only', 'just', 'will', 'into', 'she', \"aren't\", 'and', 'they', 'isn', 'been', 'until', 'theirs', \"don't\", \"couldn't\", 'we', 'ourselves', 'under', 'is', 'by', 'what', \"hadn't\", \"weren't\", 'too', 'ma', 'needn', 'those', 'are', 'more', 'there', 'to', 'between', 'herself', 'has', 'a', 'the', 'yours', 'who', 'than', 'not', 'of', 'nor', \"isn't\", 'when', 'whom', \"it's\", 'i', 'my', 'were', 'mustn', 'doing', 'down', \"won't\", 'all', 'have', \"you'd\", 'his', 'other', 'but', 'if', 'very', 'off', 'before', 'both', 'each', 'didn', 'further', 'no', 'most', \"doesn't\", 'couldn', 'them', 'through', 'weren', 'won', \"wouldn't\", 'The', 're', \"wasn't\", 'few', 'here', 'so', ',', 'its', 'their', 'below', \"shan't\", 'out', \"mustn't\", 'it', 'over', 'itself', 'while', \"you've\", 'themselves', 'after', 'your', 'her', \"should've\", \"haven't\", 'this', 'on', 've', \"she's\", 'he', 'during'}\n" + ] + } + ], + "source": [ + "from nltk.corpus import stopwords\n", + "\n", + "stop_words = set(stopwords.words('english'))\n", + "stop_words.update(['.', ',', 'The'])\n", + "\n", + "print(stop_words)" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['marie', 'curie', 'polish-born', 'physicist', 'chemist', 'one', 'famous', 'scientists', 'time.', 'together', 'husband', 'pierre,', 'awarded', 'nobel', 'prize', '1903,', 'went', 'win', 'another', '1911.', 'marie', 'sklodowska', 'born', 'warsaw', '7', 'november', '1867,', 'daughter', 'teacher.', '1891,', 'went', 'paris', 'study', 'physics', 'mathematics', 'sorbonne', 'met', 'pierre', 'curie,', 'professor', 'school', 'physics.', 'married', '1895.', 'curies', 'worked', 'together', 'investigating', 'radioactivity,', 'building', 'work', 'german', 'physicist', 'roentgen', 'french', 'physicist', 'becquerel.', 'july', '1898,', 'curies', 'announced', 'discovery', 'new', 'chemical', 'element,', 'polonium.', 'end', 'year,', 'announced', 'discovery', 'another,', 'radium.', 'curies,', 'along', 'becquerel,', 'awarded', 'nobel', 'prize', 'physics', '1903.', \"pierre's\", 'life', 'cut', 'short', '1906', 'knocked', 'killed', 'carriage.', 'marie', 'took', 'teaching', 'post,', 'becoming', 'first', 'woman', 'teach', 'sorbonne,', 'devoted', 'continuing', 'work', 'begun', 'together.', 'received', 'second', 'nobel', 'prize,', 'chemistry,', '1911.', \"curie's\", 'research', 'crucial', 'development', 'x-rays', 'surgery.', 'world', 'war', 'one', 'curie', 'helped', 'equip', 'ambulances', 'x-ray', 'equipment,', 'drove', 'front', 'lines.', 'international', 'red', 'cross', 'made', 'head', 'radiological', 'service', 'held', 'training', 'courses', 'medical', 'orderlies', 'doctors', 'new', 'techniques.', 'despite', 'success,', 'marie', 'continued', 'face', 'great', 'opposition', 'male', 'scientists', 'france,', 'never', 'received', 'significant', 'financial', 'benefits', 'work.', 'late', '1920s', 'health', 'beginning', 'deteriorate.', 'died', '4', 'july', '1934', 'leukaemia,', 'caused', 'exposure', 'high-energy', 'radiation', 'research.', \"curies'\", 'eldest', 'daughter', 'irene', 'scientist', 'winner', 'nobel', 'prize', 'chemistry.']\n" + ] + } + ], + "source": [ + "filtered_words = []\n", + "\n", + "for w in word_tokens:\n", + " if w not in stop_words:\n", + " filtered_words.append(w)\n", + " \n", + "print(filtered_words)" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": {}, + "outputs": [], + "source": [ + "freq_dist = FreqDist(filtered_words)" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('marie', 4),\n", + " ('nobel', 4),\n", + " ('physicist', 3),\n", + " ('prize', 3),\n", + " ('curie', 2),\n", + " ('one', 2),\n", + " ('scientists', 2),\n", + " ('together', 2),\n", + " ('awarded', 2),\n", + " ('went', 2),\n", + " ('1911.', 2),\n", + " ('daughter', 2),\n", + " ('physics', 2),\n", + " ('curies', 2),\n", + " ('work', 2),\n", + " ('july', 2),\n", + " ('announced', 2),\n", + " ('discovery', 2),\n", + " ('new', 2),\n", + " ('received', 2)]" + ] + }, + "execution_count": 94, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "freq_dist.most_common(20)" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "metadata": {}, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAtoAAAIRCAYAAAB0wRpEAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3XucXXdd7//XZybXSdKmbS6kt7TNjKAiFzOggMpFBPRovXE9gqhw6jl4AEU5WFQuBRWPiHIUgR7K/Wa5aVPB0iPXAoUmpbSU4i9N7y3m0rRpksltJp/fH2vtye50JjOTzFprZ+/X8/GYR2bWXns+n2knk/f+znd9VmQmkiRJkuZWX9MNSJIkSd3IoC1JkiRVwKAtSZIkVcCgLUmSJFXAoC1JkiRVwKAtSZIkVcCgLUmSJFXAoC1JkiRVwKAtSZIkVWBe0w3MpRUrVuQ555xTe919+/axePHi2uta3/rWt771e7t+J/Rgfev3Yv1NmzbtyMyV056YmV3ztn79+mzCxo0bG6lrfetb3/rW7+36ndCD9a3fi/WBjTmDbOrWEUmSJKkCBm1JkiSpAgZtSZIkqQIGbUmSJKkCBm1JkiSpAgZtSZIkqQIGbUmSJKkCBm1JkiSpAgZtSZIkqQIGbUmSJKkCBm1JkiSpAgZtSZIkqQIGbUmSJKkCBm1JkiSpApUH7Yjoj4hvR8Tlkzy2MCL+KSJujohvRsQ5bY9dWB7/j4h4ZtV9SpIkSXOpjhXtVwI3TfHYS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Vwk6WBgd0l3S5oH7AG8u+tpV5fbecCPIuJ/y474Y8Am5bGfRsRd5evzgV17ht4F2Aa4S9Jc4CDgHeSifCFwtqR9gRffsB/WzGycc4y2mdk4ExGvANOB6WVhfRiwHbBTRPxU0teAiV1PeancLu76unO/8znQ21Sh976AmyLigN75SHovsCewP/BFcqFvZjbyvKNtZjaOSJosacuuQzsAD5evny5x058c4K/etCRaAhwA3Nnz+Czg/ZL+oMxjDUlblfHWjohrgaPKfMzMDO9om5mNN5OA0yStA7wM/JgMI3mWDA35b+DeAf7e+cBBks4EHgVO734wIp4qISoXSppQDh8LvABcJWkiuev9pQHGNjNbKbkFu5nZiJO0GXBNSaQ0M7M3iENHzMzMzMwa4B1tMzMzM7MGeEfbzMzMzKwBXmibmZmZmTXAC20zMzMzswZ4oW1mZmZm1gAvtM3MzMzMGuCFtpmZmZlZA/4fRl9Kv4SIxE8AAAAASUVORK5CYII=\n", + "text/plain": [ + "
" + ] + }, + "metadata": { + "needs_background": "light" + }, + "output_type": "display_data" + } + ], + "source": [ + "fig, ax = plt.subplots(figsize=(12, 8))\n", + "\n", + "freq_dist.plot(20, cumulative=False)\n", + "\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "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.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/building-features-from-text-data/02-VectorizeTextAsABagOfWords_CountVectorizer.ipynb b/building-features-from-text-data/02-VectorizeTextAsABagOfWords_CountVectorizer.ipynb new file mode 100644 index 0000000..d7cfba2 --- /dev/null +++ b/building-features-from-text-data/02-VectorizeTextAsABagOfWords_CountVectorizer.ipynb @@ -0,0 +1,741 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Vectorize text as a bag-of-words" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.20.3\n" + ] + } + ], + "source": [ + "import sklearn\n", + "\n", + "print(sklearn.__version__)" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.feature_extraction.text import CountVectorizer" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [], + "source": [ + "train_text = [\"A bird in hand is worth two in the bush.\",\n", + " \"Good things come to those who wait.\",\n", + " \"These watches cost $1500! \",\n", + " \"There are other fish in the sea.\",\n", + " \"The ball is in your court.\",\n", + " \"Mr. Smith Goes to Washington \",\n", + " \"Doogie Howser M.D.\"]\n", + "\n", + "count_vectorizer = CountVectorizer()" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "CountVectorizer(analyzer='word', binary=False, decode_error='strict',\n", + " dtype=, encoding='utf-8', input='content',\n", + " lowercase=True, max_df=1.0, max_features=None, min_df=1,\n", + " ngram_range=(1, 1), preprocessor=None, stop_words=None,\n", + " strip_accents=None, token_pattern='(?u)\\\\b\\\\w\\\\w+\\\\b',\n", + " tokenizer=None, vocabulary=None)" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "count_vectorizer.fit(train_text)" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['1500',\n", + " 'are',\n", + " 'ball',\n", + " 'bird',\n", + " 'bush',\n", + " 'come',\n", + " 'cost',\n", + " 'court',\n", + " 'doogie',\n", + " 'fish',\n", + " 'goes',\n", + " 'good',\n", + " 'hand',\n", + " 'howser',\n", + " 'in',\n", + " 'is',\n", + " 'mr',\n", + " 'other',\n", + " 'sea',\n", + " 'smith',\n", + " 'the',\n", + " 'there',\n", + " 'these',\n", + " 'things',\n", + " 'those',\n", + " 'to',\n", + " 'two',\n", + " 'wait',\n", + " 'washington',\n", + " 'watches',\n", + " 'who',\n", + " 'worth',\n", + " 'your']" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "count_vectorizer.get_feature_names()" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [], + "source": [ + "count_vectorizer.get_stop_words()" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'bird': 3,\n", + " 'in': 14,\n", + " 'hand': 12,\n", + " 'is': 15,\n", + " 'worth': 31,\n", + " 'two': 26,\n", + " 'the': 20,\n", + " 'bush': 4,\n", + " 'good': 11,\n", + " 'things': 23,\n", + " 'come': 5,\n", + " 'to': 25,\n", + " 'those': 24,\n", + " 'who': 30,\n", + " 'wait': 27,\n", + " 'these': 22,\n", + " 'watches': 29,\n", + " 'cost': 6,\n", + " '1500': 0,\n", + " 'there': 21,\n", + " 'are': 1,\n", + " 'other': 17,\n", + " 'fish': 9,\n", + " 'sea': 18,\n", + " 'ball': 2,\n", + " 'your': 32,\n", + " 'court': 7,\n", + " 'mr': 16,\n", + " 'smith': 19,\n", + " 'goes': 10,\n", + " 'washington': 28,\n", + " 'doogie': 8,\n", + " 'howser': 13}" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "count_vectorizer.vocabulary_" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "28" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "count_vectorizer.vocabulary_.get('things')" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['A bird in hand is worth two in the bush.',\n", + " 'Good things come to those who wait.',\n", + " 'These watches cost $1500! ',\n", + " 'There are other fish in the sea.',\n", + " 'The ball is in your court.',\n", + " 'Mr. Smith Goes to Washington ',\n", + " 'Doogie Howser M.D.']" + ] + }, + "execution_count": 72, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "train_text" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [], + "source": [ + "transformed_vector = count_vectorizer.transform(train_text)" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(7, 33)\n" + ] + } + ], + "source": [ + "print(transformed_vector.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0 0 0 1 1 0 0 0 0 0 0 0 1 0 2 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0]\n", + " [0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 0 0 1 0 0]\n", + " [1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0]\n", + " [0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0]\n", + " [0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1]\n", + " [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0]\n", + " [0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]\n" + ] + } + ], + "source": [ + "print(transformed_vector.toarray())" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", + " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "test_text = [\"Every cloud has a silver lining.\"]\n", + "\n", + "count_vectorizer.transform(test_text).toarray()" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "CountVectorizer(analyzer='word', binary=False, decode_error='strict',\n", + " dtype=, encoding='utf-8', input='content',\n", + " lowercase=True, max_df=1.0, max_features=None, min_df=1,\n", + " ngram_range=(1, 1), preprocessor=None, stop_words=None,\n", + " strip_accents=None, token_pattern='(?u)\\\\b\\\\w\\\\w+\\\\b',\n", + " tokenizer=None, vocabulary=None)" + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "count_vectorizer.fit(train_text + test_text)" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'bird': 3, 'in': 17, 'hand': 14, 'is': 18, 'worth': 36, 'two': 31, 'the': 25, 'bush': 4, 'good': 13, 'things': 28, 'come': 6, 'to': 30, 'those': 29, 'who': 35, 'wait': 32, 'these': 27, 'watches': 34, 'cost': 7, '1500': 0, 'there': 26, 'are': 1, 'other': 21, 'fish': 11, 'sea': 22, 'ball': 2, 'your': 37, 'court': 8, 'mr': 20, 'smith': 24, 'goes': 12, 'washington': 33, 'doogie': 9, 'howser': 16, 'every': 10, 'cloud': 5, 'has': 15, 'silver': 23, 'lining': 19}\n" + ] + } + ], + "source": [ + "print(count_vectorizer.vocabulary_)" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0,\n", + " 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])" + ] + }, + "execution_count": 81, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "count_vectorizer.transform(test_text).toarray()" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "<3x38 sparse matrix of type ''\n", + "\twith 9 stored elements in Compressed Sparse Row format>" + ] + }, + "execution_count": 86, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "text = [\"That bird is sitting in the bush and this bird is in hand.\",\n", + " \"Wait and then walk\",\n", + " \"Watches are cool \"]\n", + "\n", + "transformed_vector = count_vectorizer.transform(text)\n", + "\n", + "transformed_vector" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " (0, 3)\t2\n", + " (0, 4)\t1\n", + " (0, 14)\t1\n", + " (0, 17)\t2\n", + " (0, 18)\t2\n", + " (0, 25)\t1\n", + " (1, 32)\t1\n", + " (2, 1)\t1\n", + " (2, 34)\t1\n" + ] + } + ], + "source": [ + "print(transformed_vector)" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(3, 38)" + ] + }, + "execution_count": 88, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "transformed_vector.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 89, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0 0 0 2 1 0 0 0 0 0 0 0 0 0 1 0 0 2 2 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0\n", + " 0 0]\n", + " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0\n", + " 0 0]\n", + " [0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0\n", + " 0 0]]\n" + ] + } + ], + "source": [ + "print(transformed_vector.toarray())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### CountVectorizer on text file" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Marie Curie was a Polish-born physicist and chemist and one of the most famous scientists of her time.\n", + "Together with her husband Pierre, she was awarded the Nobel Prize in 1903, and she went on to win another in 1911.\n", + "Marie Sklodowska was born in Warsaw on 7 November 1867, the daughter of a teacher.\n", + "In 1891, she went to Paris to study physics and mathematics at the Sorbonne where she met Pierre Curie, professor of the School of Physics.\n", + "They were married in 1895.\n", + "The Curies worked together investigating radioactivity, building on the work of the German physicist Roentgen and the French physicist Becquerel.\n", + "In July 1898, the Curies announced the discovery of a new chemical element, polonium.\n", + "At the end of the year, they announced the discovery of another, radium.\n", + "The Curies, along with Becquerel, were awarded the Nobel Prize for Physics in 1903.\n", + "Pierre's life was cut short in 1906 when he was knocked down and killed by a carriage.\n", + "Marie took over his teaching post, becoming the first woman to teach at the Sorbonne, and devoted herself to continuing the work that they had begun together.\n", + "She received a second Nobel Prize, for Chemistry, in 1911.\n", + "The Curie's research was crucial in the development of x-rays in surgery.\n", + "During World War One Curie helped to equip ambulances with x-ray equipment, which she herself drove to the front lines.\n", + "The International Red Cross made her head of its radiological service and she held training courses for medical orderlies and doctors in the new techniques.\n", + "Despite her success, Marie continued to face great opposition from male scientists in France, and she never received significant financial benefits from her work.\n", + "By the late 1920s her health was beginning to deteriorate.\n", + "She died on 4 July 1934 from leukaemia, caused by exposure to high-energy radiation from her research.\n", + "The Curies' eldest daughter Irene was herself a scientist and winner of the Nobel Prize for Chemistry.\n" + ] + } + ], + "source": [ + "with open('./datasets/biography.txt', 'r') as f:\n", + " file_contents = f.read()\n", + "\n", + "print(file_contents)" + ] + }, + { + "cell_type": "code", + "execution_count": 96, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Marie Curie was a Polish-born physicist and chemist and one of the most famous scientists of her time.', 'Together with her husband Pierre, she was awarded the Nobel Prize in 1903, and she went on to win another in 1911.', 'Marie Sklodowska was born in Warsaw on 7 November 1867, the daughter of a teacher.', 'In 1891, she went to Paris to study physics and mathematics at the Sorbonne where she met Pierre Curie, professor of the School of Physics.', 'They were married in 1895.', 'The Curies worked together investigating radioactivity, building on the work of the German physicist Roentgen and the French physicist Becquerel.', 'In July 1898, the Curies announced the discovery of a new chemical element, polonium.', 'At the end of the year, they announced the discovery of another, radium.', 'The Curies, along with Becquerel, were awarded the Nobel Prize for Physics in 1903.', \"Pierre's life was cut short in 1906 when he was knocked down and killed by a carriage.\", 'Marie took over his teaching post, becoming the first woman to teach at the Sorbonne, and devoted herself to continuing the work that they had begun together.', 'She received a second Nobel Prize, for Chemistry, in 1911.', \"The Curie's research was crucial in the development of x-rays in surgery.\", 'During World War One Curie helped to equip ambulances with x-ray equipment, which she herself drove to the front lines.', 'The International Red Cross made her head of its radiological service and she held training courses for medical orderlies and doctors in the new techniques.', 'Despite her success, Marie continued to face great opposition from male scientists in France, and she never received significant financial benefits from her work.', 'By the late 1920s her health was beginning to deteriorate.', 'She died on 4 July 1934 from leukaemia, caused by exposure to high-energy radiation from her research.', \"The Curies' eldest daughter Irene was herself a scientist and winner of the Nobel Prize for Chemistry.\"]\n" + ] + } + ], + "source": [ + "sentences = file_contents.split('\\n')\n", + " \n", + "print(sentences)" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(19, 167)" + ] + }, + "execution_count": 97, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "transformed_vector = count_vectorizer.fit_transform(sentences)\n", + "\n", + "transformed_vector.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " (0, 146)\t1\n", + " (0, 72)\t1\n", + " (0, 128)\t1\n", + " (0, 56)\t1\n", + " (0, 96)\t1\n", + " (0, 144)\t1\n", + " (0, 101)\t2\n", + " (0, 103)\t1\n", + " (0, 27)\t1\n", + " (0, 11)\t2\n", + " (0, 108)\t1\n", + " (0, 21)\t1\n", + " (0, 111)\t1\n", + " (0, 153)\t1\n", + " (0, 34)\t1\n", + " (0, 91)\t1\n", + " (1, 6)\t1\n", + " (1, 13)\t1\n", + " (1, 159)\t1\n", + " (1, 147)\t1\n", + " (1, 102)\t1\n", + " (1, 154)\t1\n", + " (1, 4)\t1\n", + " (1, 77)\t2\n", + " (1, 114)\t1\n", + " :\t:\n", + " (17, 8)\t1\n", + " (17, 42)\t1\n", + " (17, 62)\t2\n", + " (17, 124)\t1\n", + " (17, 23)\t1\n", + " (17, 82)\t1\n", + " (17, 147)\t1\n", + " (17, 102)\t1\n", + " (17, 131)\t1\n", + " (17, 72)\t1\n", + " (18, 160)\t1\n", + " (18, 127)\t1\n", + " (18, 80)\t1\n", + " (18, 48)\t1\n", + " (18, 28)\t1\n", + " (18, 73)\t1\n", + " (18, 59)\t1\n", + " (18, 35)\t1\n", + " (18, 37)\t1\n", + " (18, 114)\t1\n", + " (18, 99)\t1\n", + " (18, 144)\t2\n", + " (18, 101)\t1\n", + " (18, 11)\t1\n", + " (18, 153)\t1\n" + ] + } + ], + "source": [ + "print(transformed_vector)" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'marie': 91, 'curie': 34, 'was': 153, 'polish': 111, 'born': 21, 'physicist': 108, 'and': 11, 'chemist': 27, 'one': 103, 'of': 101, 'the': 144, 'most': 96, 'famous': 56, 'scientists': 128, 'her': 72, 'time': 146, 'together': 148, 'with': 161, 'husband': 76, 'pierre': 110, 'she': 131, 'awarded': 15, 'nobel': 99, 'prize': 114, 'in': 77, '1903': 4, 'went': 154, 'on': 102, 'to': 147, 'win': 159, 'another': 13, '1911': 6, 'sklodowska': 134, 'warsaw': 152, 'november': 100, '1867': 0, 'daughter': 37, 'teacher': 140, '1891': 1, 'paris': 107, 'study': 136, 'physics': 109, 'mathematics': 93, 'at': 14, 'sorbonne': 135, 'where': 157, 'met': 95, 'professor': 115, 'school': 126, 'they': 145, 'were': 155, 'married': 92, '1895': 2, 'curies': 35, 'worked': 164, 'investigating': 79, 'radioactivity': 117, 'building': 22, 'work': 163, 'german': 64, 'roentgen': 125, 'french': 61, 'becquerel': 17, 'july': 82, '1898': 3, 'announced': 12, 'discovery': 43, 'new': 98, 'chemical': 26, 'element': 49, 'polonium': 112, 'end': 50, 'year': 166, 'radium': 119, 'along': 9, 'for': 59, 'life': 87, 'cut': 36, 'short': 132, '1906': 5, 'when': 156, 'he': 67, 'knocked': 84, 'down': 45, 'killed': 83, 'by': 23, 'carriage': 24, 'took': 149, 'over': 106, 'his': 75, 'teaching': 141, 'post': 113, 'becoming': 16, 'first': 58, 'woman': 162, 'teach': 139, 'devoted': 41, 'herself': 73, 'continuing': 30, 'that': 143, 'had': 66, 'begun': 19, 'received': 122, 'second': 129, 'chemistry': 28, 'research': 124, 'crucial': 33, 'development': 40, 'rays': 121, 'surgery': 138, 'during': 47, 'world': 165, 'war': 151, 'helped': 71, 'equip': 52, 'ambulances': 10, 'ray': 120, 'equipment': 53, 'which': 158, 'drove': 46, 'front': 63, 'lines': 88, 'international': 78, 'red': 123, 'cross': 32, 'made': 89, 'head': 68, 'its': 81, 'radiological': 118, 'service': 130, 'held': 70, 'training': 150, 'courses': 31, 'medical': 94, 'orderlies': 105, 'doctors': 44, 'techniques': 142, 'despite': 38, 'success': 137, 'continued': 29, 'face': 55, 'great': 65, 'opposition': 104, 'from': 62, 'male': 90, 'france': 60, 'never': 97, 'significant': 133, 'financial': 57, 'benefits': 20, 'late': 85, '1920s': 7, 'health': 69, 'beginning': 18, 'deteriorate': 39, 'died': 42, '1934': 8, 'leukaemia': 86, 'caused': 25, 'exposure': 54, 'high': 74, 'energy': 51, 'radiation': 116, 'eldest': 48, 'irene': 80, 'scientist': 127, 'winner': 160}\n" + ] + } + ], + "source": [ + "print(count_vectorizer.vocabulary_)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We lost:\n", + "\n", + "* The meaning of text corpus\n", + "* The ordering of the words" + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[array(['time', 'her', 'scientists', 'famous', 'most', 'the', 'of', 'one',\n", + " 'chemist', 'and', 'physicist', 'born', 'polish', 'was', 'curie',\n", + " 'marie'], dtype='TfidfTransformer :\n", + "\n", + "* Transform a count matrix to a normalized tf or tf-idf representation\n", + "* Tf means term-frequency while tf-idf means term-frequency times inverse document-frequency. This is a common term weighting scheme in information retrieval, that has also found good use in document classification.\n", + "\n", + "https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.TfidfTransformer.html#sklearn.feature_extraction.text.TfidfTransformer" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "* A Term Frequency is a count of how many times a word occurs in a given document (synonymous with bag of words).\n", + "* The Inverse Document Frequency is the the number of times a word occurs in a corpus of documents." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The first step is to create our training and testing document set and computing the term frequency matrix\n", + "\n", + "https://scikit-learn.org/stable/modules/feature_extraction.html#tfidf-term-weighting" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a count vector\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.feature_extraction.text import CountVectorizer\n", + "\n", + "train_text = [\"A bird in hand is worth two in the bush.\",\n", + " \"Good things come to those who wait.\",\n", + " \"These watches cost $1500! \",\n", + " \"There are other fish in the sea.\",\n", + " \"The ball is in your court.\",\n", + " \"Mr. Smith Goes to Washington \",\n", + " \"Doogie Howser M.D.\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "count_vectorizer = CountVectorizer()\n", + "\n", + "frequency_term_matrix = count_vectorizer.fit_transform(train_text)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "33" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(count_vectorizer.vocabulary_)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(7, 33)" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "frequency_term_matrix.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 1, 0, 0, 0, 0, 1, 0,\n", + " 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0],\n", + " [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", + " 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0],\n", + " [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", + " 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],\n", + " [0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1,\n", + " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", + " [0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0,\n", + " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n", + " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0,\n", + " 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0],\n", + " [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,\n", + " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int64)" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "frequency_term_matrix.toarray()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Building the tf-idf matrix\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.feature_extraction.text import TfidfTransformer\n", + "\n", + "tfidf_transformer = TfidfTransformer()" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(7, 33)" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tfidf_vector1 = tfidf_transformer.fit_transform(frequency_term_matrix)\n", + "\n", + "tfidf_vector1.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[0. , 0. , 0. , 0.34908308, 0.34908308,\n", + " 0. , 0. , 0. , 0. , 0. ,\n", + " 0. , 0. , 0.34908308, 0. , 0.49536976,\n", + " 0.28976893, 0. , 0. , 0. , 0. ,\n", + " 0.24768488, 0. , 0. , 0. , 0. ,\n", + " 0. , 0.34908308, 0. , 0. , 0. ,\n", + " 0. , 0.34908308, 0. ],\n", + " [0. , 0. , 0. , 0. , 0. ,\n", + " 0.38665001, 0. , 0. , 0. , 0. ,\n", + " 0. , 0.38665001, 0. , 0. , 0. ,\n", + " 0. , 0. , 0. , 0. , 0. ,\n", + " 0. , 0. , 0. , 0.38665001, 0.38665001,\n", + " 0.32095271, 0. , 0.38665001, 0. , 0. ,\n", + " 0.38665001, 0. , 0. ],\n", + " [0.5 , 0. , 0. , 0. , 0. ,\n", + " 0. , 0.5 , 0. , 0. , 0. ,\n", + " 0. , 0. , 0. , 0. , 0. ,\n", + " 0. , 0. , 0. , 0. , 0. ,\n", + " 0. , 0. , 0.5 , 0. , 0. ,\n", + " 0. , 0. , 0. , 0. , 0.5 ,\n", + " 0. , 0. , 0. ],\n", + " [0. , 0.40801493, 0. , 0. , 0. ,\n", + " 0. , 0. , 0. , 0. , 0.40801493,\n", + " 0. , 0. , 0. , 0. , 0.28949879,\n", + " 0. , 0. , 0.40801493, 0.40801493, 0. ,\n", + " 0.28949879, 0.40801493, 0. , 0. , 0. ,\n", + " 0. , 0. , 0. , 0. , 0. ,\n", + " 0. , 0. , 0. ],\n", + " [0. , 0. , 0.46146654, 0. , 0. ,\n", + " 0. , 0. , 0.46146654, 0. , 0. ,\n", + " 0. , 0. , 0. , 0. , 0.3274243 ,\n", + " 0.38305686, 0. , 0. , 0. , 0. ,\n", + " 0.3274243 , 0. , 0. , 0. , 0. ,\n", + " 0. , 0. , 0. , 0. , 0. ,\n", + " 0. , 0. , 0.46146654],\n", + " [0. , 0. , 0. , 0. , 0. ,\n", + " 0. , 0. , 0. , 0. , 0. ,\n", + " 0.46180424, 0. , 0. , 0. , 0. ,\n", + " 0. , 0.46180424, 0. , 0. , 0.46180424,\n", + " 0. , 0. , 0. , 0. , 0. ,\n", + " 0.38333718, 0. , 0. , 0.46180424, 0. ,\n", + " 0. , 0. , 0. ],\n", + " [0. , 0. , 0. , 0. , 0. ,\n", + " 0. , 0. , 0. , 0.70710678, 0. ,\n", + " 0. , 0. , 0. , 0.70710678, 0. ,\n", + " 0. , 0. , 0. , 0. , 0. ,\n", + " 0. , 0. , 0. , 0. , 0. ,\n", + " 0. , 0. , 0. , 0. , 0. ,\n", + " 0. , 0. , 0. ]])" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tfidf_vector1.toarray()" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0. 0. 0. 0.34908308 0.34908308 0.\n", + " 0. 0. 0. 0. 0. 0.\n", + " 0.34908308 0. 0.49536976 0.28976893 0. 0.\n", + " 0. 0. 0.24768488 0. 0. 0.\n", + " 0. 0. 0.34908308 0. 0. 0.\n", + " 0. 0.34908308 0. ]\n", + " [0. 0. 0. 0. 0. 0.38665001\n", + " 0. 0. 0. 0. 0. 0.38665001\n", + " 0. 0. 0. 0. 0. 0.\n", + " 0. 0. 0. 0. 0. 0.38665001\n", + " 0.38665001 0.32095271 0. 0.38665001 0. 0.\n", + " 0.38665001 0. 0. ]\n", + " [0.5 0. 0. 0. 0. 0.\n", + " 0.5 0. 0. 0. 0. 0.\n", + " 0. 0. 0. 0. 0. 0.\n", + " 0. 0. 0. 0. 0.5 0.\n", + " 0. 0. 0. 0. 0. 0.5\n", + " 0. 0. 0. ]\n", + " [0. 0.40801493 0. 0. 0. 0.\n", + " 0. 0. 0. 0.40801493 0. 0.\n", + " 0. 0. 0.28949879 0. 0. 0.40801493\n", + " 0.40801493 0. 0.28949879 0.40801493 0. 0.\n", + " 0. 0. 0. 0. 0. 0.\n", + " 0. 0. 0. ]\n", + " [0. 0. 0.46146654 0. 0. 0.\n", + " 0. 0.46146654 0. 0. 0. 0.\n", + " 0. 0. 0.3274243 0.38305686 0. 0.\n", + " 0. 0. 0.3274243 0. 0. 0.\n", + " 0. 0. 0. 0. 0. 0.\n", + " 0. 0. 0.46146654]\n", + " [0. 0. 0. 0. 0. 0.\n", + " 0. 0. 0. 0. 0.46180424 0.\n", + " 0. 0. 0. 0. 0.46180424 0.\n", + " 0. 0.46180424 0. 0. 0. 0.\n", + " 0. 0.38333718 0. 0. 0.46180424 0.\n", + " 0. 0. 0. ]\n", + " [0. 0. 0. 0. 0. 0.\n", + " 0. 0. 0.70710678 0. 0. 0.\n", + " 0. 0.70710678 0. 0. 0. 0.\n", + " 0. 0. 0. 0. 0. 0.\n", + " 0. 0. 0. 0. 0. 0.\n", + " 0. 0. 0. ]]\n" + ] + } + ], + "source": [ + "print(tfidf_vector1.toarray())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## TfidfVectorizer = CountVectorizer + TfidfTransformer" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.feature_extraction.text import TfidfVectorizer\n", + "\n", + "tfidf_vectorizer = TfidfVectorizer()" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'bird': 3,\n", + " 'in': 14,\n", + " 'hand': 12,\n", + " 'is': 15,\n", + " 'worth': 31,\n", + " 'two': 26,\n", + " 'the': 20,\n", + " 'bush': 4,\n", + " 'good': 11,\n", + " 'things': 23,\n", + " 'come': 5,\n", + " 'to': 25,\n", + " 'those': 24,\n", + " 'who': 30,\n", + " 'wait': 27,\n", + " 'these': 22,\n", + " 'watches': 29,\n", + " 'cost': 6,\n", + " '1500': 0,\n", + " 'there': 21,\n", + " 'are': 1,\n", + " 'other': 17,\n", + " 'fish': 9,\n", + " 'sea': 18,\n", + " 'ball': 2,\n", + " 'your': 32,\n", + " 'court': 7,\n", + " 'mr': 16,\n", + " 'smith': 19,\n", + " 'goes': 10,\n", + " 'washington': 28,\n", + " 'doogie': 8,\n", + " 'howser': 13}" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tfidf_vector2 = tfidf_vectorizer.fit_transform(train_text)\n", + "\n", + "tfidf_vectorizer.vocabulary_" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(7, 33)" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tfidf_vector2.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([2.38629436, 2.38629436, 2.38629436, 2.38629436, 2.38629436,\n", + " 2.38629436, 2.38629436, 2.38629436, 2.38629436, 2.38629436,\n", + " 2.38629436, 2.38629436, 2.38629436, 2.38629436, 1.69314718,\n", + " 1.98082925, 2.38629436, 2.38629436, 2.38629436, 2.38629436,\n", + " 1.69314718, 2.38629436, 2.38629436, 2.38629436, 2.38629436,\n", + " 1.98082925, 2.38629436, 2.38629436, 2.38629436, 2.38629436,\n", + " 2.38629436, 2.38629436, 2.38629436])" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tfidf_vectorizer.idf_" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'1500': 2.386294361119891,\n", + " 'are': 2.386294361119891,\n", + " 'ball': 2.386294361119891,\n", + " 'bird': 2.386294361119891,\n", + " 'bush': 2.386294361119891,\n", + " 'come': 2.386294361119891,\n", + " 'cost': 2.386294361119891,\n", + " 'court': 2.386294361119891,\n", + " 'doogie': 2.386294361119891,\n", + " 'fish': 2.386294361119891,\n", + " 'goes': 2.386294361119891,\n", + " 'good': 2.386294361119891,\n", + " 'hand': 2.386294361119891,\n", + " 'howser': 2.386294361119891,\n", + " 'in': 1.6931471805599454,\n", + " 'is': 1.9808292530117262,\n", + " 'mr': 2.386294361119891,\n", + " 'other': 2.386294361119891,\n", + " 'sea': 2.386294361119891,\n", + " 'smith': 2.386294361119891,\n", + " 'the': 1.6931471805599454,\n", + " 'there': 2.386294361119891,\n", + " 'these': 2.386294361119891,\n", + " 'things': 2.386294361119891,\n", + " 'those': 2.386294361119891,\n", + " 'to': 1.9808292530117262,\n", + " 'two': 2.386294361119891,\n", + " 'wait': 2.386294361119891,\n", + " 'washington': 2.386294361119891,\n", + " 'watches': 2.386294361119891,\n", + " 'who': 2.386294361119891,\n", + " 'worth': 2.386294361119891,\n", + " 'your': 2.386294361119891}" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dict(zip(tfidf_vectorizer.get_feature_names(), tfidf_vectorizer.idf_))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Final scorings of each word from the other words in the vocabulary.\n", + "* The scores are normalized to values between 0 and 1" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[0. , 0. , 0. , 0.34908308, 0.34908308,\n", + " 0. , 0. , 0. , 0. , 0. ,\n", + " 0. , 0. , 0.34908308, 0. , 0.49536976,\n", + " 0.28976893, 0. , 0. , 0. , 0. ,\n", + " 0.24768488, 0. , 0. , 0. , 0. ,\n", + " 0. , 0.34908308, 0. , 0. , 0. ,\n", + " 0. , 0.34908308, 0. ],\n", + " [0. , 0. , 0. , 0. , 0. ,\n", + " 0.38665001, 0. , 0. , 0. , 0. ,\n", + " 0. , 0.38665001, 0. , 0. , 0. ,\n", + " 0. , 0. , 0. , 0. , 0. ,\n", + " 0. , 0. , 0. , 0.38665001, 0.38665001,\n", + " 0.32095271, 0. , 0.38665001, 0. , 0. ,\n", + " 0.38665001, 0. , 0. ],\n", + " [0.5 , 0. , 0. , 0. , 0. ,\n", + " 0. , 0.5 , 0. , 0. , 0. ,\n", + " 0. , 0. , 0. , 0. , 0. ,\n", + " 0. , 0. , 0. , 0. , 0. ,\n", + " 0. , 0. , 0.5 , 0. , 0. ,\n", + " 0. , 0. , 0. , 0. , 0.5 ,\n", + " 0. , 0. , 0. ],\n", + " [0. , 0.40801493, 0. , 0. , 0. ,\n", + " 0. , 0. , 0. , 0. , 0.40801493,\n", + " 0. , 0. , 0. , 0. , 0.28949879,\n", + " 0. , 0. , 0.40801493, 0.40801493, 0. ,\n", + " 0.28949879, 0.40801493, 0. , 0. , 0. ,\n", + " 0. , 0. , 0. , 0. , 0. ,\n", + " 0. , 0. , 0. ],\n", + " [0. , 0. , 0.46146654, 0. , 0. ,\n", + " 0. , 0. , 0.46146654, 0. , 0. ,\n", + " 0. , 0. , 0. , 0. , 0.3274243 ,\n", + " 0.38305686, 0. , 0. , 0. , 0. ,\n", + " 0.3274243 , 0. , 0. , 0. , 0. ,\n", + " 0. , 0. , 0. , 0. , 0. ,\n", + " 0. , 0. , 0.46146654],\n", + " [0. , 0. , 0. , 0. , 0. ,\n", + " 0. , 0. , 0. , 0. , 0. ,\n", + " 0.46180424, 0. , 0. , 0. , 0. ,\n", + " 0. , 0.46180424, 0. , 0. , 0.46180424,\n", + " 0. , 0. , 0. , 0. , 0. ,\n", + " 0.38333718, 0. , 0. , 0.46180424, 0. ,\n", + " 0. , 0. , 0. ],\n", + " [0. , 0. , 0. , 0. , 0. ,\n", + " 0. , 0. , 0. , 0.70710678, 0. ,\n", + " 0. , 0. , 0. , 0.70710678, 0. ,\n", + " 0. , 0. , 0. , 0. , 0. ,\n", + " 0. , 0. , 0. , 0. , 0. ,\n", + " 0. , 0. , 0. , 0. , 0. ,\n", + " 0. , 0. , 0. ]])" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tfidf_vector2.toarray()" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " (0, 3)\t0.3490830767264469\n", + " (0, 14)\t0.49536975552604884\n", + " (0, 12)\t0.3490830767264469\n", + " (0, 15)\t0.2897689326921819\n", + " (0, 31)\t0.3490830767264469\n", + " (0, 26)\t0.3490830767264469\n", + " (0, 20)\t0.24768487776302442\n", + " (0, 4)\t0.3490830767264469\n", + " (1, 11)\t0.386650005027498\n", + " (1, 23)\t0.386650005027498\n", + " (1, 5)\t0.386650005027498\n", + " (1, 25)\t0.32095270940344806\n", + " (1, 24)\t0.386650005027498\n", + " (1, 30)\t0.386650005027498\n", + " (1, 27)\t0.386650005027498\n", + " (2, 22)\t0.5\n", + " (2, 29)\t0.5\n", + " (2, 6)\t0.5\n", + " (2, 0)\t0.5\n", + " (3, 14)\t0.2894987873064995\n", + " (3, 20)\t0.2894987873064995\n", + " (3, 21)\t0.40801492725049476\n", + " (3, 1)\t0.40801492725049476\n", + " (3, 17)\t0.40801492725049476\n", + " (3, 9)\t0.40801492725049476\n", + " (3, 18)\t0.40801492725049476\n", + " (4, 14)\t0.3274243027464032\n", + " (4, 15)\t0.38305685676572565\n", + " (4, 20)\t0.3274243027464032\n", + " (4, 2)\t0.4614665377636916\n", + " (4, 32)\t0.4614665377636916\n", + " (4, 7)\t0.4614665377636916\n", + " (5, 25)\t0.38333717539523177\n", + " (5, 16)\t0.4618042361109319\n", + " (5, 19)\t0.4618042361109319\n", + " (5, 10)\t0.4618042361109319\n", + " (5, 28)\t0.4618042361109319\n", + " (6, 8)\t0.7071067811865476\n", + " (6, 13)\t0.7071067811865476\n" + ] + } + ], + "source": [ + "print(tfidf_vector2)" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " (0, 31)\t0.34908307672644684\n", + " (0, 26)\t0.34908307672644684\n", + " (0, 20)\t0.2476848777630244\n", + " (0, 15)\t0.2897689326921819\n", + " (0, 14)\t0.4953697555260488\n", + " (0, 12)\t0.34908307672644684\n", + " (0, 4)\t0.34908307672644684\n", + " (0, 3)\t0.34908307672644684\n", + " (1, 30)\t0.386650005027498\n", + " (1, 27)\t0.386650005027498\n", + " (1, 25)\t0.32095270940344806\n", + " (1, 24)\t0.386650005027498\n", + " (1, 23)\t0.386650005027498\n", + " (1, 11)\t0.386650005027498\n", + " (1, 5)\t0.386650005027498\n", + " (2, 29)\t0.5\n", + " (2, 22)\t0.5\n", + " (2, 6)\t0.5\n", + " (2, 0)\t0.5\n", + " (3, 21)\t0.40801492725049476\n", + " (3, 20)\t0.2894987873064995\n", + " (3, 18)\t0.40801492725049476\n", + " (3, 17)\t0.40801492725049476\n", + " (3, 14)\t0.2894987873064995\n", + " (3, 9)\t0.40801492725049476\n", + " (3, 1)\t0.40801492725049476\n", + " (4, 32)\t0.4614665377636916\n", + " (4, 20)\t0.3274243027464032\n", + " (4, 15)\t0.38305685676572565\n", + " (4, 14)\t0.3274243027464032\n", + " (4, 7)\t0.4614665377636916\n", + " (4, 2)\t0.4614665377636916\n", + " (5, 28)\t0.4618042361109319\n", + " (5, 25)\t0.38333717539523177\n", + " (5, 19)\t0.4618042361109319\n", + " (5, 16)\t0.4618042361109319\n", + " (5, 10)\t0.4618042361109319\n", + " (6, 13)\t0.7071067811865476\n", + " (6, 8)\t0.7071067811865476\n" + ] + } + ], + "source": [ + "print(tfidf_vector1)" + ] + }, + { + "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.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/building-features-from-text-data/05-StopwordAndFrequencyFiltering.ipynb b/building-features-from-text-data/05-StopwordAndFrequencyFiltering.ipynb new file mode 100644 index 0000000..ce7980a --- /dev/null +++ b/building-features-from-text-data/05-StopwordAndFrequencyFiltering.ipynb @@ -0,0 +1,1245 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Remove Stop Words Using NLTK" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "from nltk import word_tokenize\n", + "from nltk.corpus import stopwords" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['arabic', 'azerbaijani', 'danish', 'dutch', 'english', 'finnish', 'french', 'german', 'greek', 'hungarian', 'indonesian', 'italian', 'kazakh', 'nepali', 'norwegian', 'portuguese', 'romanian', 'russian', 'spanish', 'swedish', 'turkish']\n" + ] + } + ], + "source": [ + "print(stopwords.fileids())" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['i',\n", + " 'me',\n", + " 'my',\n", + " 'myself',\n", + " 'we',\n", + " 'our',\n", + " 'ours',\n", + " 'ourselves',\n", + " 'you',\n", + " \"you're\",\n", + " \"you've\",\n", + " \"you'll\",\n", + " \"you'd\",\n", + " 'your',\n", + " 'yours',\n", + " 'yourself',\n", + " 'yourselves',\n", + " 'he',\n", + " 'him',\n", + " 'his',\n", + " 'himself',\n", + " 'she',\n", + " \"she's\",\n", + " 'her',\n", + " 'hers',\n", + " 'herself',\n", + " 'it',\n", + " \"it's\",\n", + " 'its',\n", + " 'itself',\n", + " 'they',\n", + " 'them',\n", + " 'their',\n", + " 'theirs',\n", + " 'themselves',\n", + " 'what',\n", + " 'which',\n", + " 'who',\n", + " 'whom',\n", + " 'this',\n", + " 'that',\n", + " \"that'll\",\n", + " 'these',\n", + " 'those',\n", + " 'am',\n", + " 'is',\n", + " 'are',\n", + " 'was',\n", + " 'were',\n", + " 'be',\n", + " 'been',\n", + " 'being',\n", + " 'have',\n", + " 'has',\n", + " 'had',\n", + " 'having',\n", + " 'do',\n", + " 'does',\n", + " 'did',\n", + " 'doing',\n", + " 'a',\n", + " 'an',\n", + " 'the',\n", + " 'and',\n", + " 'but',\n", + " 'if',\n", + " 'or',\n", + " 'because',\n", + " 'as',\n", + " 'until',\n", + " 'while',\n", + " 'of',\n", + " 'at',\n", + " 'by',\n", + " 'for',\n", + " 'with',\n", + " 'about',\n", + " 'against',\n", + " 'between',\n", + " 'into',\n", + " 'through',\n", + " 'during',\n", + " 'before',\n", + " 'after',\n", + " 'above',\n", + " 'below',\n", + " 'to',\n", + " 'from',\n", + " 'up',\n", + " 'down',\n", + " 'in',\n", + " 'out',\n", + " 'on',\n", + " 'off',\n", + " 'over',\n", + " 'under',\n", + " 'again',\n", + " 'further',\n", + " 'then',\n", + " 'once',\n", + " 'here',\n", + " 'there',\n", + " 'when',\n", + " 'where',\n", + " 'why',\n", + " 'how',\n", + " 'all',\n", + " 'any',\n", + " 'both',\n", + " 'each',\n", + " 'few',\n", + " 'more',\n", + " 'most',\n", + " 'other',\n", + " 'some',\n", + " 'such',\n", + " 'no',\n", + " 'nor',\n", + " 'not',\n", + " 'only',\n", + " 'own',\n", + " 'same',\n", + " 'so',\n", + " 'than',\n", + " 'too',\n", + " 'very',\n", + " 's',\n", + " 't',\n", + " 'can',\n", + " 'will',\n", + " 'just',\n", + " 'don',\n", + " \"don't\",\n", + " 'should',\n", + " \"should've\",\n", + " 'now',\n", + " 'd',\n", + " 'll',\n", + " 'm',\n", + " 'o',\n", + " 're',\n", + " 've',\n", + " 'y',\n", + " 'ain',\n", + " 'aren',\n", + " \"aren't\",\n", + " 'couldn',\n", + " \"couldn't\",\n", + " 'didn',\n", + " \"didn't\",\n", + " 'doesn',\n", + " \"doesn't\",\n", + " 'hadn',\n", + " \"hadn't\",\n", + " 'hasn',\n", + " \"hasn't\",\n", + " 'haven',\n", + " \"haven't\",\n", + " 'isn',\n", + " \"isn't\",\n", + " 'ma',\n", + " 'mightn',\n", + " \"mightn't\",\n", + " 'mustn',\n", + " \"mustn't\",\n", + " 'needn',\n", + " \"needn't\",\n", + " 'shan',\n", + " \"shan't\",\n", + " 'shouldn',\n", + " \"shouldn't\",\n", + " 'wasn',\n", + " \"wasn't\",\n", + " 'weren',\n", + " \"weren't\",\n", + " 'won',\n", + " \"won't\",\n", + " 'wouldn',\n", + " \"wouldn't\"]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "stopwords.words('english')" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['إذ',\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", + " 'أما',\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", + " 'أين',\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", + " 'تلك',\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", + " 'ذه',\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", + " 'في',\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", + " 'لا',\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", + " 'لو',\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", + " 'نعم',\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", + " 'هيت',\n", + " 'هيهات',\n", + " 'والذي',\n", + " 'والذين',\n", + " 'وإذ',\n", + " 'وإذا',\n", + " 'وإن',\n", + " 'ولا',\n", + " 'ولكن',\n", + " 'ولو',\n", + " 'وما',\n", + " 'ومن',\n", + " 'وهو',\n", + " 'يا']" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "stopwords.words('arabic')" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'A bird in hand is worth two in the bush. Good things come to those who wait. These watches cost $1500! There are other fish in the sea. The ball is in your court. Mr. Smith Goes to Washington Doogie Howser M.D.'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "text_array = [\"A bird in hand is worth two in the bush.\",\n", + " \"Good things come to those who wait.\",\n", + " \"These watches cost $1500! \",\n", + " \"There are other fish in the sea.\",\n", + " \"The ball is in your court.\",\n", + " \"Mr. Smith Goes to Washington \",\n", + " \"Doogie Howser M.D.\"]\n", + "\n", + "text = \" \".join(text_array)\n", + "\n", + "text" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['A',\n", + " 'bird',\n", + " 'in',\n", + " 'hand',\n", + " 'is',\n", + " 'worth',\n", + " 'two',\n", + " 'in',\n", + " 'the',\n", + " 'bush',\n", + " '.',\n", + " 'Good',\n", + " 'things',\n", + " 'come',\n", + " 'to',\n", + " 'those',\n", + " 'who',\n", + " 'wait',\n", + " '.',\n", + " 'These',\n", + " 'watches',\n", + " 'cost',\n", + " '$',\n", + " '1500',\n", + " '!',\n", + " 'There',\n", + " 'are',\n", + " 'other',\n", + " 'fish',\n", + " 'in',\n", + " 'the',\n", + " 'sea',\n", + " '.',\n", + " 'The',\n", + " 'ball',\n", + " 'is',\n", + " 'in',\n", + " 'your',\n", + " 'court',\n", + " '.',\n", + " 'Mr.',\n", + " 'Smith',\n", + " 'Goes',\n", + " 'to',\n", + " 'Washington',\n", + " 'Doogie',\n", + " 'Howser',\n", + " 'M.D',\n", + " '.']" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "word_tokens = word_tokenize(text)\n", + "\n", + "word_tokens" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['A', 'bird', 'hand', 'worth', 'two', 'bush', '.', 'Good', 'things', 'come', 'wait', '.', 'These', 'watches', 'cost', '$', '1500', '!', 'There', 'fish', 'sea', '.', 'The', 'ball', 'court', '.', 'Mr.', 'Smith', 'Goes', 'Washington', 'Doogie', 'Howser', 'M.D', '.']\n" + ] + } + ], + "source": [ + "stop_words = set(stopwords.words('english'))\n", + "\n", + "filtered_words = []\n", + "\n", + "for word in word_tokens:\n", + " if word not in stop_words:\n", + " filtered_words.append(word)\n", + " \n", + "print(filtered_words)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "with open(\"./datasets/file.txt\", \"w\") as f:\n", + " for word in filtered_words:\n", + " f.write(word)\n", + " f.write(' ')" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "A bird hand worth two bush . Good things come wait . These watches cost $ 1500 ! There fish sea . The ball court . Mr. Smith Goes Washington Doogie Howser M.D . \n" + ] + } + ], + "source": [ + "with open(\"./datasets/file.txt\", \"r\") as f:\n", + " file_contents = f.read()\n", + "\n", + "print(file_contents)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "CountVectorizer(analyzer='word', binary=False, decode_error='strict',\n", + " dtype=, encoding='utf-8', input='content',\n", + " lowercase=True, max_df=1.0, max_features=None, min_df=1,\n", + " ngram_range=(1, 1), preprocessor=None, stop_words=None,\n", + " strip_accents=None, token_pattern='(?u)\\\\b\\\\w\\\\w+\\\\b',\n", + " tokenizer=None, vocabulary=None)" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from sklearn.feature_extraction.text import CountVectorizer\n", + "\n", + "count_vectorizer = CountVectorizer()\n", + "count_vectorizer.fit([file_contents])" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(7, 25)" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "transformed_vector = count_vectorizer.transform(text_array)\n", + "\n", + "transformed_vector.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [], + "source": [ + "feature_names_nltk = count_vectorizer.get_feature_names()" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'bird': 2,\n", + " 'hand': 11,\n", + " 'worth': 24,\n", + " 'two': 20,\n", + " 'bush': 3,\n", + " 'good': 10,\n", + " 'things': 19,\n", + " 'come': 4,\n", + " 'wait': 21,\n", + " 'these': 18,\n", + " 'watches': 23,\n", + " 'cost': 5,\n", + " '1500': 0,\n", + " 'there': 17,\n", + " 'fish': 8,\n", + " 'sea': 14,\n", + " 'the': 16,\n", + " 'ball': 1,\n", + " 'court': 6,\n", + " 'mr': 13,\n", + " 'smith': 15,\n", + " 'goes': 9,\n", + " 'washington': 22,\n", + " 'doogie': 7,\n", + " 'howser': 12}" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "count_vectorizer.vocabulary_" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0,\n", + " 0, 0, 1],\n", + " [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1,\n", + " 0, 0, 0],\n", + " [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,\n", + " 0, 1, 0],\n", + " [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0,\n", + " 0, 0, 0],\n", + " [0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,\n", + " 0, 0, 0],\n", + " [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0,\n", + " 1, 0, 0],\n", + " [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", + " 0, 0, 0]])" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "transformed_vector.toarray()" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[array(['bird', 'bush', 'hand', 'the', 'two', 'worth'], dtype='\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", + " \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", + " \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", + " \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", + " \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", + " \n", + " \n", + "
wordsPorter Stemmer
0overwhelmingoverwhelm
1overwhelminglyoverwhelmingli
2hushedhush
3hushhush
4functionalfunction
5functionallyfunction
6lyinglie
7liedlie
8fairlyfairli
9destabilizedestabil
10stabilitystabil
11friendshipfriendship
12friendshipsfriendship
13friendlyfriendli
14friendlessfriendless
15connectconnect
16connectionsconnect
17connectedconnect
18thethe
19thesethese
20thosethose
21motivationalmotiv
22motivatemotiv
23motivatingmotiv
\n", + "" + ], + "text/plain": [ + " words Porter Stemmer\n", + "0 overwhelming overwhelm\n", + "1 overwhelmingly overwhelmingli\n", + "2 hushed hush\n", + "3 hush hush\n", + "4 functional function\n", + "5 functionally function\n", + "6 lying lie\n", + "7 lied lie\n", + "8 fairly fairli\n", + "9 destabilize destabil\n", + "10 stability stabil\n", + "11 friendship friendship\n", + "12 friendships friendship\n", + "13 friendly friendli\n", + "14 friendless friendless\n", + "15 connect connect\n", + "16 connections connect\n", + "17 connected connect\n", + "18 the the\n", + "19 these these\n", + "20 those those\n", + "21 motivational motiv\n", + "22 motivate motiv\n", + "23 motivating motiv" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "stems_df = pd.DataFrame({\n", + " 'words': input_tokens,\n", + " 'Porter Stemmer': ps_stemmed_tokens\n", + "})\n", + "\n", + "stems_df" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### LancasterStemmer\n", + "* The LancasterStemmer (Paice-Husk stemmer) is an iterative algorithm with rules saved externally.\n", + "* LancasterStemmer is simple, but heavy stemming due to iterations and over-stemming may occur. \n", + "* Over-stemming causes the stems to be not linguistic, or they may have no meaning." + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [], + "source": [ + "ls = LancasterStemmer()\n", + "\n", + "ls_stemmed_tokens = []\n", + "for token in input_tokens:\n", + " ls_stemmed_tokens.append(ls.stem(token))" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "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", + " \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", + " \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", + " \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", + " \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", + " \n", + " \n", + "
wordsLancaster Stemmer
0overwhelmingoverwhelm
1overwhelminglyoverwhelm
2hushedhush
3hushhush
4functionalfunct
5functionallyfunct
6lyinglying
7liedlied
8fairlyfair
9destabilizedest
10stabilitystabl
11friendshipfriend
12friendshipsfriend
13friendlyfriend
14friendlessfriendless
15connectconnect
16connectionsconnect
17connectedconnect
18thethe
19thesethes
20thosethos
21motivationalmot
22motivatemot
23motivatingmot
\n", + "
" + ], + "text/plain": [ + " words Lancaster Stemmer\n", + "0 overwhelming overwhelm\n", + "1 overwhelmingly overwhelm\n", + "2 hushed hush\n", + "3 hush hush\n", + "4 functional funct\n", + "5 functionally funct\n", + "6 lying lying\n", + "7 lied lied\n", + "8 fairly fair\n", + "9 destabilize dest\n", + "10 stability stabl\n", + "11 friendship friend\n", + "12 friendships friend\n", + "13 friendly friend\n", + "14 friendless friendless\n", + "15 connect connect\n", + "16 connections connect\n", + "17 connected connect\n", + "18 the the\n", + "19 these thes\n", + "20 those thos\n", + "21 motivational mot\n", + "22 motivate mot\n", + "23 motivating mot" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "stems_df = pd.DataFrame({\n", + " 'words': input_tokens,\n", + " 'Lancaster Stemmer': ls_stemmed_tokens\n", + "})\n", + "\n", + "stems_df" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "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", + " \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", + " \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", + " \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", + " \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", + " \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", + " \n", + "
wordsPorter StemmerLancaster Stemmer
0overwhelmingoverwhelmoverwhelm
1overwhelminglyoverwhelminglioverwhelm
2hushedhushhush
3hushhushhush
4functionalfunctionfunct
5functionallyfunctionfunct
6lyinglielying
7liedlielied
8fairlyfairlifair
9destabilizedestabildest
10stabilitystabilstabl
11friendshipfriendshipfriend
12friendshipsfriendshipfriend
13friendlyfriendlifriend
14friendlessfriendlessfriendless
15connectconnectconnect
16connectionsconnectconnect
17connectedconnectconnect
18thethethe
19thesethesethes
20thosethosethos
21motivationalmotivmot
22motivatemotivmot
23motivatingmotivmot
\n", + "
" + ], + "text/plain": [ + " words Porter Stemmer Lancaster Stemmer\n", + "0 overwhelming overwhelm overwhelm\n", + "1 overwhelmingly overwhelmingli overwhelm\n", + "2 hushed hush hush\n", + "3 hush hush hush\n", + "4 functional function funct\n", + "5 functionally function funct\n", + "6 lying lie lying\n", + "7 lied lie lied\n", + "8 fairly fairli fair\n", + "9 destabilize destabil dest\n", + "10 stability stabil stabl\n", + "11 friendship friendship friend\n", + "12 friendships friendship friend\n", + "13 friendly friendli friend\n", + "14 friendless friendless friendless\n", + "15 connect connect connect\n", + "16 connections connect connect\n", + "17 connected connect connect\n", + "18 the the the\n", + "19 these these thes\n", + "20 those those thos\n", + "21 motivational motiv mot\n", + "22 motivate motiv mot\n", + "23 motivating motiv mot" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "stems_df = pd.DataFrame({\n", + " 'words': input_tokens,\n", + " 'Porter Stemmer': ps_stemmed_tokens,\n", + " 'Lancaster Stemmer': ls_stemmed_tokens\n", + "})\n", + "\n", + "stems_df" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### SnowballStemmer\n", + "* One can generate its own set of rules for any language that is why Python nltk introduced SnowballStemmers that are used to create non-English Stemmers!" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('arabic', 'danish', 'dutch', 'english', 'finnish', 'french', 'german', 'hungarian', 'italian', 'norwegian', 'porter', 'portuguese', 'romanian', 'russian', 'spanish', 'swedish')\n" + ] + } + ], + "source": [ + "print(SnowballStemmer.languages)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [], + "source": [ + "ss = SnowballStemmer('english')\n", + "\n", + "ss_stemmed_tokens = []\n", + "for token in input_tokens:\n", + " ss_stemmed_tokens.append(ss.stem(token))" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "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", + " \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", + " \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", + " \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", + " \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", + " \n", + " \n", + "
wordsSnowball Stemmer
0overwhelmingoverwhelm
1overwhelminglyoverwhelm
2hushedhush
3hushhush
4functionalfunction
5functionallyfunction
6lyinglie
7liedlie
8fairlyfair
9destabilizedestabil
10stabilitystabil
11friendshipfriendship
12friendshipsfriendship
13friendlyfriend
14friendlessfriendless
15connectconnect
16connectionsconnect
17connectedconnect
18thethe
19thesethese
20thosethose
21motivationalmotiv
22motivatemotiv
23motivatingmotiv
\n", + "
" + ], + "text/plain": [ + " words Snowball Stemmer\n", + "0 overwhelming overwhelm\n", + "1 overwhelmingly overwhelm\n", + "2 hushed hush\n", + "3 hush hush\n", + "4 functional function\n", + "5 functionally function\n", + "6 lying lie\n", + "7 lied lie\n", + "8 fairly fair\n", + "9 destabilize destabil\n", + "10 stability stabil\n", + "11 friendship friendship\n", + "12 friendships friendship\n", + "13 friendly friend\n", + "14 friendless friendless\n", + "15 connect connect\n", + "16 connections connect\n", + "17 connected connect\n", + "18 the the\n", + "19 these these\n", + "20 those those\n", + "21 motivational motiv\n", + "22 motivate motiv\n", + "23 motivating motiv" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "stems_df = pd.DataFrame({\n", + " 'words': input_tokens,\n", + " 'Snowball Stemmer': ss_stemmed_tokens\n", + "})\n", + "\n", + "stems_df" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "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", + " \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", + " \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", + " \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", + " \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", + " \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", + " \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", + "
wordsPorter StemmerLancaster StemmerSnowball Stemmer
0overwhelmingoverwhelmoverwhelmoverwhelm
1overwhelminglyoverwhelminglioverwhelmoverwhelm
2hushedhushhushhush
3hushhushhushhush
4functionalfunctionfunctfunction
5functionallyfunctionfunctfunction
6lyinglielyinglie
7liedlieliedlie
8fairlyfairlifairfair
9destabilizedestabildestdestabil
10stabilitystabilstablstabil
11friendshipfriendshipfriendfriendship
12friendshipsfriendshipfriendfriendship
13friendlyfriendlifriendfriend
14friendlessfriendlessfriendlessfriendless
15connectconnectconnectconnect
16connectionsconnectconnectconnect
17connectedconnectconnectconnect
18thethethethe
19thesethesethesthese
20thosethosethosthose
21motivationalmotivmotmotiv
22motivatemotivmotmotiv
23motivatingmotivmotmotiv
\n", + "
" + ], + "text/plain": [ + " words Porter Stemmer Lancaster Stemmer Snowball Stemmer\n", + "0 overwhelming overwhelm overwhelm overwhelm\n", + "1 overwhelmingly overwhelmingli overwhelm overwhelm\n", + "2 hushed hush hush hush\n", + "3 hush hush hush hush\n", + "4 functional function funct function\n", + "5 functionally function funct function\n", + "6 lying lie lying lie\n", + "7 lied lie lied lie\n", + "8 fairly fairli fair fair\n", + "9 destabilize destabil dest destabil\n", + "10 stability stabil stabl stabil\n", + "11 friendship friendship friend friendship\n", + "12 friendships friendship friend friendship\n", + "13 friendly friendli friend friend\n", + "14 friendless friendless friendless friendless\n", + "15 connect connect connect connect\n", + "16 connections connect connect connect\n", + "17 connected connect connect connect\n", + "18 the the the the\n", + "19 these these thes these\n", + "20 those those thos those\n", + "21 motivational motiv mot motiv\n", + "22 motivate motiv mot motiv\n", + "23 motivating motiv mot motiv" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "stems_df = pd.DataFrame({\n", + " 'words': input_tokens,\n", + " 'Porter Stemmer': ps_stemmed_tokens,\n", + " 'Lancaster Stemmer': ls_stemmed_tokens,\n", + " 'Snowball Stemmer': ss_stemmed_tokens\n", + "})\n", + "\n", + "stems_df" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Suffix stripping algorithms may differ in results for a variety of reasons. One such reason is whether the algorithm constrains whether the output word must be a real word in the given language. Some approaches do not require the word to actually exist in the language lexicon (the set of all words in the language). Alternatively, some suffix stripping approaches maintain a database (a large list) of all known morphological word roots that exist as real words. These approaches check the list for the existence of the term prior to making a decision. Typically, if the term does not exist, alternate action is taken. This alternate action may involve several other criteria. The non-existence of an output term may serve to cause the algorithm to try alternate suffix stripping rules.\n", + "\n", + "It can be the case that two or more suffix stripping rules apply to the same input term, which creates an ambiguity as to which rule to apply. The algorithm may assign (by human hand or stochastically) a priority to one rule or another. Or the algorithm may reject one rule application because it results in a non-existent term whereas the other overlapping rule does not. For example, given the English term friendlies, the algorithm may identify the ies suffix and apply the appropriate rule and achieve the result of friendl. friendl is likely not found in the lexicon, and therefore the rule is rejected.\n", + "\n", + "One improvement upon basic suffix stripping is the use of suffix substitution. Similar to a stripping rule, a substitution rule replaces a suffix with an alternate suffix. For example, there could exist a rule that replaces ies with y. How this affects the algorithm varies on the algorithm's design. To illustrate, the algorithm may identify that both the ies suffix stripping rule as well as the suffix substitution rule apply. Since the stripping rule results in a non-existent term in the lexicon, but the substitution rule does not, the substitution rule is applied instead. In this example, friendlies becomes friendly instead of friendl.\n", + "\n", + "Diving further into the details, a common technique is to apply rules in a cyclical fashion (recursively, as computer scientists would say). After applying the suffix substitution rule in this example scenario, a second pass is made to identify matching rules on the term friendly, where the ly stripping rule is likely identified and accepted. In summary, friendlies becomes (via substitution) friendly which becomes (via stripping) friend.\n", + "\n", + "This example also helps illustrate the difference between a rule-based approach and a brute force approach. In a brute force approach, the algorithm would search for friendlies in the set of hundreds of thousands of inflected word forms and ideally find the corresponding root form friend. In the rule-based approach, the three rules mentioned above would be applied in succession to converge on the same solution. Chances are that the rule-based approach would be slower, as lookup algorithms have a direct access to the solution, while rule-based should try several options, and combinations of them, and then choose which result seems to be the best.\n", + "\n", + "There are two error measurements in stemming algorithms, overstemming and understemming. Overstemming is an error where two separate inflected words are stemmed to the same root, but should not have been—a false positive. Understemming is an error where two separate inflected words should be stemmed to the same root, but are not—a false negative. Stemming algorithms attempt to minimize each type of error, although reducing one type can lead to increasing the other.\n", + "\n", + "For example, the widely used Porter stemmer stems \"universal\", \"university\", and \"universe\" to \"univers\". This is a case of overstemming: though these three words are etymologically related, their modern meanings are in widely different domains, so treating them as synonyms in a search engine will likely reduce the relevance of the search results.\n", + "\n", + "An example of understemming in the Porter stemmer is \"alumnus\" → \"alumnu\", \"alumni\" → \"alumni\", \"alumna\"/\"alumnae\" → \"alumna\". This English word keeps Latin morphology, and so these near-synonyms are not conflated.\n", + "\n", + "Stochastic algorithms involve using probability to identify the root form of a word. Stochastic algorithms are trained (they \"learn\") on a table of root form to inflected form relations to develop a probabilistic model. This model is typically expressed in the form of complex linguistic rules, similar in nature to those in suffix stripping or lemmatisation. Stemming is performed by inputting an inflected form to the trained model and having the model produce the root form according to its internal ruleset, which again is similar to suffix stripping and lemmatisation, except that the decisions involved in applying the most appropriate rule, or whether or not to stem the word and just return the same word, or whether to apply two different rules sequentially, are applied on the grounds that the output word will have the highest probability of being correct (which is to say, the smallest probability of being incorrect, which is how it is typically measured).\n", + "\n", + "Some lemmatisation algorithms are stochastic in that, given a word which may belong to multiple parts of speech, a probability is assigned to each possible part. This may take into account the surrounding words, called the context, or not. Context-free grammars do not take into account any additional information. In either case, after assigning the probabilities to each possible part of speech, the most likely part of speech is chosen, and from there the appropriate normalization rules are applied to the input word to produce the normalized (root) form.\n" + ] + } + ], + "source": [ + "with open('./datasets/stemming.txt', 'r') as f:\n", + " file_contents = f.read()\n", + "\n", + "print(file_contents)" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [], + "source": [ + "word_tokens = word_tokenize(file_contents)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [], + "source": [ + "ss = SnowballStemmer('english', ignore_stopwords=True)\n", + "\n", + "ss_stemmed_words = []\n", + "for word in word_tokens:\n", + " ss_stemmed_words.append(ss.stem(word))" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"suffix strip algorithm may differ in result for a varieti of reason . one such reason is whether the algorithm constrain whether the output word must be a real word in the given languag . some approach do not requir the word to actual exist in the languag lexicon ( the set of all word in the languag ) . altern , some suffix strip approach maintain a databas ( a larg list ) of all known morpholog word root that exist as real word . these approach check the list for the exist of the term prior to make a decis . typic , if the term does not exist , altern action is taken . this altern action may involv sever other criteria . the non-exist of an output term may serv to caus the algorithm to tri altern suffix strip rule . it can be the case that two or more suffix strip rule appli to the same input term , which creat an ambigu as to which rule to appli . the algorithm may assign ( by human hand or stochast ) a prioriti to one rule or anoth . or the algorithm may reject one rule applic because it result in a non-exist term wherea the other overlap rule does not . for exampl , given the english term friend , the algorithm may identifi the ie suffix and appli the appropri rule and achiev the result of friendl . friendl is like not found in the lexicon , and therefor the rule is reject . one improv upon basic suffix strip is the use of suffix substitut . similar to a strip rule , a substitut rule replac a suffix with an altern suffix . for exampl , there could exist a rule that replac ie with y . how this affect the algorithm vari on the algorithm 's design . to illustr , the algorithm may identifi that both the ie suffix strip rule as well as the suffix substitut rule appli . sinc the strip rule result in a non-exist term in the lexicon , but the substitut rule does not , the substitut rule is appli instead . in this exampl , friend becom friend instead of friendl . dive further into the detail , a common techniqu is to appli rule in a cyclic fashion ( recurs , as comput scientist would say ) . after appli the suffix substitut rule in this exampl scenario , a second pass is made to identifi match rule on the term friend , where the ly strip rule is like identifi and accept . in summari , friend becom ( via substitut ) friend which becom ( via strip ) friend . this exampl also help illustr the differ between a rule-bas approach and a brute forc approach . in a brute forc approach , the algorithm would search for friend in the set of hundr of thousand of inflect word form and ideal find the correspond root form friend . in the rule-bas approach , the three rule mention above would be appli in success to converg on the same solut . chanc are that the rule-bas approach would be slower , as lookup algorithm have a direct access to the solut , while rule-bas should tri sever option , and combin of them , and then choos which result seem to be the best . there are two error measur in stem algorithm , overstem and understem . overstem is an error where two separ inflect word are stem to the same root , but should not have been—a fals posit . understem is an error where two separ inflect word should be stem to the same root , but are not—a fals negat . stem algorithm attempt to minim each type of error , although reduc one type can lead to increas the other . for exampl , the wide use porter stemmer stem `` univers '' , `` univers '' , and `` univers '' to `` univ '' . this is a case of overstem : though these three word are etymolog relat , their modern mean are in wide differ domain , so treat them as synonym in a search engin will like reduc the relev of the search result . an exampl of understem in the porter stemmer is `` alumnus '' → `` alumnu '' , `` alumni '' → `` alumni '' , `` alumna '' / '' alumna '' → `` alumna '' . this english word keep latin morpholog , and so these near-synonym are not conflat . stochast algorithm involv use probabl to identifi the root form of a word . stochast algorithm are train ( they `` learn '' ) on a tabl of root form to inflect form relat to develop a probabilist model . this model is typic express in the form of complex linguist rule , similar in natur to those in suffix strip or lemmatis . stem is perform by input an inflect form to the train model and having the model produc the root form accord to its intern ruleset , which again is similar to suffix strip and lemmatis , except that the decis involv in appli the most appropri rule , or whether or not to stem the word and just return the same word , or whether to appli two differ rule sequenti , are appli on the ground that the output word will have the highest probabl of being correct ( which is to say , the smallest probabl of being incorrect , which is how it is typic measur ) . some lemmatis algorithm are stochast in that , given a word which may belong to multipl part of speech , a probabl is assign to each possibl part . this may take into account the surround word , call the context , or not . context-fre grammar do not take into account any addit inform . in either case , after assign the probabl to each possibl part of speech , the most like part of speech is chosen , and from there the appropri normal rule are appli to the input word to produc the normal ( root ) form .\"" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\" \".join(ss_stemmed_words)" + ] + }, + { + "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.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/building-features-from-text-data/07-Lemmatization.ipynb b/building-features-from-text-data/07-Lemmatization.ipynb new file mode 100644 index 0000000..e9a09fd --- /dev/null +++ b/building-features-from-text-data/07-Lemmatization.ipynb @@ -0,0 +1,530 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Lemmatizing Words Using WordNet\n", + "* Part-of-speech constants:\n", + "* ADJ: a\n", + "* ADV: r\n", + "* NOUN: n\n", + "* VERB: v" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [], + "source": [ + "import nltk\n", + "\n", + "from nltk.stem import *\n", + "\n", + "import pandas as pd" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Stemming words" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "definit\n" + ] + } + ], + "source": [ + "from nltk.stem import PorterStemmer \n", + "\n", + "stemmer = PorterStemmer() \n", + "print(stemmer.stem('definitions'))" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "[nltk_data] Downloading package wordnet to\n", + "[nltk_data] /Users/loonycorn/nltk_data...\n", + "[nltk_data] Package wordnet is already up-to-date!\n" + ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "nltk.download('wordnet')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Lemmatizing Words" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "definition\n" + ] + } + ], + "source": [ + "from nltk.stem import WordNetLemmatizer\n", + "\n", + "wnl = WordNetLemmatizer()\n", + "print(wnl.lemmatize('definitions'))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Lemmatizing words by specifying parts-of-speech" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Adjective: running\n", + "Adverb: running\n", + "Noun: running\n", + "Verb: run\n" + ] + } + ], + "source": [ + "print('Adjective: ', wnl.lemmatize('running', pos='a'))\n", + "print('Adverb: ', wnl.lemmatize('running', pos='r'))\n", + "print('Noun: ', wnl.lemmatize('running', pos='n'))\n", + "print('Verb: ', wnl.lemmatize('running', pos='v'))" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [], + "source": [ + "input_tokens = ['dictionaries', 'dictionary', \n", + " 'hushed', 'hush', 'hushing',\n", + " 'functional', 'functionally',\n", + " 'lying', 'lied', 'lies',\n", + " 'flawed', 'flaws', 'flawless', \n", + " 'friendship', 'friendships', 'friendly', 'friendless', \n", + " 'definitions', 'definition', 'definitely', \n", + " 'the', 'these', 'those',\n", + " 'motivational', 'motivate', 'motivating']" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": {}, + "outputs": [], + "source": [ + "ss = SnowballStemmer('english')\n", + "\n", + "ss_stemmed_tokens = []\n", + "for token in input_tokens:\n", + " ss_stemmed_tokens.append(ss.stem(token))" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [], + "source": [ + "wnl_lemmatized_tokens = []\n", + "for token in input_tokens:\n", + " wnl_lemmatized_tokens.append(wnl.lemmatize(token, pos='v'))" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "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", + " \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", + " \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", + " \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", + " \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", + " \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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
wordsSnowball StemmerWordNet Lemmatizer
0dictionariesdictionaridictionaries
1dictionarydictionaridictionary
2hushedhushhush
3hushhushhush
4hushinghushhush
5functionalfunctionfunctional
6functionallyfunctionfunctionally
7lyinglielie
8liedlielie
9lieslielie
10flawedflawflaw
11flawsflawflaw
12flawlessflawlessflawless
13friendshipfriendshipfriendship
14friendshipsfriendshipfriendships
15friendlyfriendfriendly
16friendlessfriendlessfriendless
17definitionsdefinitdefinitions
18definitiondefinitdefinition
19definitelydefinitdefinitely
20thethethe
21thesethesethese
22thosethosethose
23motivationalmotivmotivational
24motivatemotivmotivate
25motivatingmotivmotivate
\n", + "
" + ], + "text/plain": [ + " words Snowball Stemmer WordNet Lemmatizer\n", + "0 dictionaries dictionari dictionaries\n", + "1 dictionary dictionari dictionary\n", + "2 hushed hush hush\n", + "3 hush hush hush\n", + "4 hushing hush hush\n", + "5 functional function functional\n", + "6 functionally function functionally\n", + "7 lying lie lie\n", + "8 lied lie lie\n", + "9 lies lie lie\n", + "10 flawed flaw flaw\n", + "11 flaws flaw flaw\n", + "12 flawless flawless flawless\n", + "13 friendship friendship friendship\n", + "14 friendships friendship friendships\n", + "15 friendly friend friendly\n", + "16 friendless friendless friendless\n", + "17 definitions definit definitions\n", + "18 definition definit definition\n", + "19 definitely definit definitely\n", + "20 the the the\n", + "21 these these these\n", + "22 those those those\n", + "23 motivational motiv motivational\n", + "24 motivate motiv motivate\n", + "25 motivating motiv motivate" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "stems_lemmas_df = pd.DataFrame({\n", + " 'words': input_tokens,\n", + " 'Snowball Stemmer': ss_stemmed_tokens,\n", + " 'WordNet Lemmatizer': wnl_lemmatized_tokens\n", + "})\n", + "\n", + "stems_lemmas_df" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Marie Curie was a Polish-born physicist and chemist and one of the most famous scientists of her time.\n", + "Together with her husband Pierre, she was awarded the Nobel Prize in 1903, and she went on to win another in 1911.\n", + "Marie Sklodowska was born in Warsaw on 7 November 1867, the daughter of a teacher.\n", + "In 1891, she went to Paris to study physics and mathematics at the Sorbonne where she met Pierre Curie, professor of the School of Physics.\n", + "They were married in 1895.\n", + "The Curies worked together investigating radioactivity, building on the work of the German physicist Roentgen and the French physicist Becquerel.\n", + "In July 1898, the Curies announced the discovery of a new chemical element, polonium.\n", + "At the end of the year, they announced the discovery of another, radium.\n", + "The Curies, along with Becquerel, were awarded the Nobel Prize for Physics in 1903.\n", + "Pierre's life was cut short in 1906 when he was knocked down and killed by a carriage.\n", + "Marie took over his teaching post, becoming the first woman to teach at the Sorbonne, and devoted herself to continuing the work that they had begun together.\n", + "She received a second Nobel Prize, for Chemistry, in 1911.\n", + "The Curie's research was crucial in the development of x-rays in surgery.\n", + "During World War One Curie helped to equip ambulances with x-ray equipment, which she herself drove to the front lines.\n", + "The International Red Cross made her head of its radiological service and she held training courses for medical orderlies and doctors in the new techniques.\n", + "Despite her success, Marie continued to face great opposition from male scientists in France, and she never received significant financial benefits from her work.\n", + "By the late 1920s her health was beginning to deteriorate.\n", + "She died on 4 July 1934 from leukaemia, caused by exposure to high-energy radiation from her research.\n", + "The Curies' eldest daughter Irene was herself a scientist and winner of the Nobel Prize for Chemistry.\n" + ] + } + ], + "source": [ + "from nltk.tokenize import word_tokenize\n", + " \n", + "with open('./datasets/biography.txt', 'r') as f:\n", + " file_contents = f.read()\n", + " \n", + "print(file_contents)" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "metadata": {}, + "outputs": [], + "source": [ + "word_tokens = word_tokenize(file_contents)" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": {}, + "outputs": [], + "source": [ + "wnl = WordNetLemmatizer()\n", + "lemmatized_words = []\n", + "\n", + "for word in word_tokens:\n", + " lemmatized_words.append(wnl.lemmatize(word, pos=\"v\"))" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"Marie Curie be a Polish-born physicist and chemist and one of the most famous scientists of her time . Together with her husband Pierre , she be award the Nobel Prize in 1903 , and she go on to win another in 1911 . Marie Sklodowska be bear in Warsaw on 7 November 1867 , the daughter of a teacher . In 1891 , she go to Paris to study physics and mathematics at the Sorbonne where she meet Pierre Curie , professor of the School of Physics . They be marry in 1895 . The Curies work together investigate radioactivity , build on the work of the German physicist Roentgen and the French physicist Becquerel . In July 1898 , the Curies announce the discovery of a new chemical element , polonium . At the end of the year , they announce the discovery of another , radium . The Curies , along with Becquerel , be award the Nobel Prize for Physics in 1903 . Pierre 's life be cut short in 1906 when he be knock down and kill by a carriage . Marie take over his teach post , become the first woman to teach at the Sorbonne , and devote herself to continue the work that they have begin together . She receive a second Nobel Prize , for Chemistry , in 1911 . The Curie 's research be crucial in the development of x-ray in surgery . During World War One Curie help to equip ambulances with x-ray equipment , which she herself drive to the front line . The International Red Cross make her head of its radiological service and she hold train course for medical orderlies and doctor in the new techniques . Despite her success , Marie continue to face great opposition from male scientists in France , and she never receive significant financial benefit from her work . By the late 1920s her health be begin to deteriorate . She die on 4 July 1934 from leukaemia , cause by exposure to high-energy radiation from her research . The Curies ' eldest daughter Irene be herself a scientist and winner of the Nobel Prize for Chemistry .\"" + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\" \".join(lemmatized_words)" + ] + }, + { + "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.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/building-features-from-text-data/08-PartOfSpeechTagging.ipynb b/building-features-from-text-data/08-PartOfSpeechTagging.ipynb new file mode 100644 index 0000000..d9986f0 --- /dev/null +++ b/building-features-from-text-data/08-PartOfSpeechTagging.ipynb @@ -0,0 +1,571 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Part-of-Speech Tagging" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "import nltk\n", + "from nltk import word_tokenize" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "[nltk_data] Downloading package tagsets to\n", + "[nltk_data] /Users/loonycorn/nltk_data...\n", + "[nltk_data] Unzipping help/tagsets.zip.\n" + ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "nltk.download('tagsets')" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "[nltk_data] Downloading package averaged_perceptron_tagger to\n", + "[nltk_data] /Users/loonycorn/nltk_data...\n", + "[nltk_data] Unzipping taggers/averaged_perceptron_tagger.zip.\n" + ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "nltk.download('averaged_perceptron_tagger')" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "$: dollar\n", + " $ -$ --$ A$ C$ HK$ M$ NZ$ S$ U.S.$ US$\n", + "'': closing quotation mark\n", + " ' ''\n", + "(: opening parenthesis\n", + " ( [ {\n", + "): closing parenthesis\n", + " ) ] }\n", + ",: comma\n", + " ,\n", + "--: dash\n", + " --\n", + ".: sentence terminator\n", + " . ! ?\n", + ":: colon or ellipsis\n", + " : ; ...\n", + "CC: conjunction, coordinating\n", + " & 'n and both but either et for less minus neither nor or plus so\n", + " therefore times v. versus vs. whether yet\n", + "CD: numeral, cardinal\n", + " mid-1890 nine-thirty forty-two one-tenth ten million 0.5 one forty-\n", + " seven 1987 twenty '79 zero two 78-degrees eighty-four IX '60s .025\n", + " fifteen 271,124 dozen quintillion DM2,000 ...\n", + "DT: determiner\n", + " all an another any both del each either every half la many much nary\n", + " neither no some such that the them these this those\n", + "EX: existential there\n", + " there\n", + "FW: foreign word\n", + " gemeinschaft hund ich jeux habeas Haementeria Herr K'ang-si vous\n", + " lutihaw alai je jour objets salutaris fille quibusdam pas trop Monte\n", + " terram fiche oui corporis ...\n", + "IN: preposition or conjunction, subordinating\n", + " astride among uppon whether out inside pro despite on by throughout\n", + " below within for towards near behind atop around if like until below\n", + " next into if beside ...\n", + "JJ: adjective or numeral, ordinal\n", + " third ill-mannered pre-war regrettable oiled calamitous first separable\n", + " ectoplasmic battery-powered participatory fourth still-to-be-named\n", + " multilingual multi-disciplinary ...\n", + "JJR: adjective, comparative\n", + " bleaker braver breezier briefer brighter brisker broader bumper busier\n", + " calmer cheaper choosier cleaner clearer closer colder commoner costlier\n", + " cozier creamier crunchier cuter ...\n", + "JJS: adjective, superlative\n", + " calmest cheapest choicest classiest cleanest clearest closest commonest\n", + " corniest costliest crassest creepiest crudest cutest darkest deadliest\n", + " dearest deepest densest dinkiest ...\n", + "LS: list item marker\n", + " A A. B B. C C. D E F First G H I J K One SP-44001 SP-44002 SP-44005\n", + " SP-44007 Second Third Three Two * a b c d first five four one six three\n", + " two\n", + "MD: modal auxiliary\n", + " can cannot could couldn't dare may might must need ought shall should\n", + " shouldn't will would\n", + "NN: noun, common, singular or mass\n", + " common-carrier cabbage knuckle-duster Casino afghan shed thermostat\n", + " investment slide humour falloff slick wind hyena override subhumanity\n", + " machinist ...\n", + "NNP: noun, proper, singular\n", + " Motown Venneboerger Czestochwa Ranzer Conchita Trumplane Christos\n", + " Oceanside Escobar Kreisler Sawyer Cougar Yvette Ervin ODI Darryl CTCA\n", + " Shannon A.K.C. Meltex Liverpool ...\n", + "NNPS: noun, proper, plural\n", + " Americans Americas Amharas Amityvilles Amusements Anarcho-Syndicalists\n", + " Andalusians Andes Andruses Angels Animals Anthony Antilles Antiques\n", + " Apache Apaches Apocrypha ...\n", + "NNS: noun, common, plural\n", + " undergraduates scotches bric-a-brac products bodyguards facets coasts\n", + " divestitures storehouses designs clubs fragrances averages\n", + " subjectivists apprehensions muses factory-jobs ...\n", + "PDT: pre-determiner\n", + " all both half many quite such sure this\n", + "POS: genitive marker\n", + " ' 's\n", + "PRP: pronoun, personal\n", + " hers herself him himself hisself it itself me myself one oneself ours\n", + " ourselves ownself self she thee theirs them themselves they thou thy us\n", + "PRP$: pronoun, possessive\n", + " her his mine my our ours their thy your\n", + "RB: adverb\n", + " occasionally unabatingly maddeningly adventurously professedly\n", + " stirringly prominently technologically magisterially predominately\n", + " swiftly fiscally pitilessly ...\n", + "RBR: adverb, comparative\n", + " further gloomier grander graver greater grimmer harder harsher\n", + " healthier heavier higher however larger later leaner lengthier less-\n", + " perfectly lesser lonelier longer louder lower more ...\n", + "RBS: adverb, superlative\n", + " best biggest bluntest earliest farthest first furthest hardest\n", + " heartiest highest largest least less most nearest second tightest worst\n", + "RP: particle\n", + " aboard about across along apart around aside at away back before behind\n", + " by crop down ever fast for forth from go high i.e. in into just later\n", + " low more off on open out over per pie raising start teeth that through\n", + " under unto up up-pp upon whole with you\n", + "SYM: symbol\n", + " % & ' '' ''. ) ). * + ,. < = > @ A[fj] U.S U.S.S.R * ** ***\n", + "TO: \"to\" as preposition or infinitive marker\n", + " to\n", + "UH: interjection\n", + " Goodbye Goody Gosh Wow Jeepers Jee-sus Hubba Hey Kee-reist Oops amen\n", + " huh howdy uh dammit whammo shucks heck anyways whodunnit honey golly\n", + " man baby diddle hush sonuvabitch ...\n", + "VB: verb, base form\n", + " ask assemble assess assign assume atone attention avoid bake balkanize\n", + " bank begin behold believe bend benefit bevel beware bless boil bomb\n", + " boost brace break bring broil brush build ...\n", + "VBD: verb, past tense\n", + " dipped pleaded swiped regummed soaked tidied convened halted registered\n", + " cushioned exacted snubbed strode aimed adopted belied figgered\n", + " speculated wore appreciated contemplated ...\n", + "VBG: verb, present participle or gerund\n", + " telegraphing stirring focusing angering judging stalling lactating\n", + " hankerin' alleging veering capping approaching traveling besieging\n", + " encrypting interrupting erasing wincing ...\n", + "VBN: verb, past participle\n", + " multihulled dilapidated aerosolized chaired languished panelized used\n", + " experimented flourished imitated reunifed factored condensed sheared\n", + " unsettled primed dubbed desired ...\n", + "VBP: verb, present tense, not 3rd person singular\n", + " predominate wrap resort sue twist spill cure lengthen brush terminate\n", + " appear tend stray glisten obtain comprise detest tease attract\n", + " emphasize mold postpone sever return wag ...\n", + "VBZ: verb, present tense, 3rd person singular\n", + " bases reconstructs marks mixes displeases seals carps weaves snatches\n", + " slumps stretches authorizes smolders pictures emerges stockpiles\n", + " seduces fizzes uses bolsters slaps speaks pleads ...\n", + "WDT: WH-determiner\n", + " that what whatever which whichever\n", + "WP: WH-pronoun\n", + " that what whatever whatsoever which who whom whosoever\n", + "WP$: WH-pronoun, possessive\n", + " whose\n", + "WRB: Wh-adverb\n", + " how however whence whenever where whereby whereever wherein whereof why\n", + "``: opening quotation mark\n", + " ` ``\n" + ] + } + ], + "source": [ + "nltk.help.upenn_tagset()" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('I', 'PRP'),\n", + " ('refuse', 'VBP'),\n", + " ('to', 'TO'),\n", + " ('let', 'VB'),\n", + " ('this', 'DT'),\n", + " ('refuse', 'NN'),\n", + " ('get', 'VB'),\n", + " ('me', 'PRP'),\n", + " ('down', 'RP')]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "text = \"I refuse to let this refuse get me down\"\n", + "\n", + "tokenized_words = word_tokenize(text)\n", + "tagged_words = nltk.pos_tag(tokenized_words)\n", + "tagged_words" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('Bear', 'NNP'),\n", + " ('with', 'IN'),\n", + " ('me', 'PRP'),\n", + " (',', ','),\n", + " ('this', 'DT'),\n", + " ('effort', 'NN'),\n", + " ('with', 'IN'),\n", + " ('soon', 'RB'),\n", + " ('bear', 'JJ'),\n", + " ('fruit', 'NN'),\n", + " (',', ','),\n", + " ('otherwise', 'RB'),\n", + " ('we', 'PRP'),\n", + " (\"'ll\", 'MD'),\n", + " ('have', 'VB'),\n", + " ('to', 'TO'),\n", + " ('run', 'VB'),\n", + " ('from', 'IN'),\n", + " ('the', 'DT'),\n", + " ('bear', 'NN')]" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "text = \"\"\"Bear with me, this effort with soon bear fruit, \n", + " otherwise we'll have to run from the bear\"\"\"\n", + "\n", + "tokenized_words = word_tokenize(text)\n", + "tagged_words = nltk.pos_tag(tokenized_words)\n", + "tagged_words" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('A', 'DT'),\n", + " ('bird', 'NN'),\n", + " ('in', 'IN'),\n", + " ('hand', 'NN'),\n", + " ('is', 'VBZ'),\n", + " ('worth', 'JJ'),\n", + " ('two', 'CD'),\n", + " ('in', 'IN'),\n", + " ('the', 'DT'),\n", + " ('bush', 'NN'),\n", + " ('.', '.'),\n", + " ('Good', 'JJ'),\n", + " ('things', 'NNS'),\n", + " ('come', 'VBP'),\n", + " ('to', 'TO'),\n", + " ('those', 'DT'),\n", + " ('who', 'WP'),\n", + " ('wait', 'VBP'),\n", + " ('.', '.'),\n", + " ('There', 'EX'),\n", + " ('are', 'VBP'),\n", + " ('other', 'JJ'),\n", + " ('fish', 'NN'),\n", + " ('in', 'IN'),\n", + " ('the', 'DT'),\n", + " ('sea', 'NN'),\n", + " ('.', '.'),\n", + " ('The', 'DT'),\n", + " ('ball', 'NN'),\n", + " ('is', 'VBZ'),\n", + " ('in', 'IN'),\n", + " ('your', 'PRP$'),\n", + " ('court', 'NN'),\n", + " ('.', '.')]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "text = \"A bird in hand is worth two in the bush. \" +\\\n", + " \"Good things come to those who wait. \" +\\\n", + " \"There are other fish in the sea. \" +\\\n", + " \"The ball is in your court.\"\n", + "\n", + "tokenized_words = word_tokenize(text)\n", + "tagged_words = nltk.pos_tag(tokenized_words)\n", + "tagged_words" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('NN', 7),\n", + " ('DT', 5),\n", + " ('IN', 4),\n", + " ('.', 4),\n", + " ('JJ', 3),\n", + " ('VBP', 3),\n", + " ('VBZ', 2),\n", + " ('CD', 1),\n", + " ('NNS', 1),\n", + " ('TO', 1)]" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from nltk.probability import FreqDist\n", + "\n", + "fd = FreqDist(tagged_words)\n", + "fd_tagged = FreqDist(tag for (word, tag) in tagged_words)\n", + "fd_tagged.most_common(10)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The Brown Corpus was the first million-word electronic corpus of English, created in 1961 at Brown University. This corpus contains text from 500 sources, and the sources have been categorized by genre, such as news, editorial, and so on. 1.1 gives an example of each genre (for a complete list, see http://icame.uib.no/brown/bcm-los.html)." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "[nltk_data] Downloading package brown to /Users/loonycorn/nltk_data...\n", + "[nltk_data] Package brown is already up-to-date!\n" + ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "nltk.download('brown')" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['The',\n", + " 'Fulton',\n", + " 'County',\n", + " 'Grand',\n", + " 'Jury',\n", + " 'said',\n", + " 'Friday',\n", + " 'an',\n", + " 'investigation',\n", + " 'of']" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "nltk.corpus.brown.words()[:10]" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "text = nltk.Text(word.lower() for word in nltk.corpus.brown.words())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Lexical categories like \"noun\" and part-of-speech tags like NN seem to have their uses, but the details will be obscure to many readers. You might wonder what justification there is for introducing this extra level of information. Many of these categories arise from superficial analysis the distribution of words in text. Consider the following analysis involving woman (a noun), bought (a verb), over (a preposition), and the (a determiner). The text.similar() method takes a word w, finds all contexts w1w w2, then finds all words w' that appear in the same context, i.e. w1w'w2." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Similar words here belong to the same part of speech" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "man time day way girl year house people world city family state room\n", + "country car woman program church government job\n" + ] + } + ], + "source": [ + "text.similar('boy')" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "get be do in see work go have take make put and find time look day say\n", + "use come show\n" + ] + } + ], + "source": [ + "text.similar('run')" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "in on to of and for with from at by that into as up out down through\n", + "is all about\n" + ] + } + ], + "source": [ + "text.similar('over')" + ] + }, + { + "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.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/building-features-from-text-data/09-ReduceDimensionsInText_HashingTrick.ipynb b/building-features-from-text-data/09-ReduceDimensionsInText_HashingTrick.ipynb new file mode 100644 index 0000000..164cd1f --- /dev/null +++ b/building-features-from-text-data/09-ReduceDimensionsInText_HashingTrick.ipynb @@ -0,0 +1,197 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Hashing trick" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Instead of building a hash table of the features encountered in training, as the vectorizers do, instances of FeatureHasher apply a hash function to the features to determine their column index in sample matrices directly. The result is increased speed and reduced memory usage, at the expense of inspectability; the hasher does not remember what the input features looked like and has no inverse_transform method." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "* https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.FeatureHasher.html\n", + "* https://scikit-learn.org/stable/modules/feature_extraction.html#feature-hashing" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(7, 8)" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from sklearn.feature_extraction import FeatureHasher\n", + "\n", + "text = [\"A bird in hand is worth two in the bush.\",\n", + " \"Good things come to those who wait.\",\n", + " \"These watches cost $1500! \",\n", + " \"These are other fish in the sea.\",\n", + " \"The ball is in your court.\",\n", + " \"Mr. Smith Goes to Washington \",\n", + " \"Doogie Howser M.D.\"]\n", + "\n", + "hasher = FeatureHasher(n_features=8, input_type='string')\n", + "hashed_features = hasher.fit_transform(text)\n", + "\n", + "hashed_features.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 10., 0., 1., 1., -3., -11., 1., -1.],\n", + " [ 6., -2., 0., 3., 0., -6., 0., 0.],\n", + " [ 4., -5., 1., -1., 2., -3., 0., 0.],\n", + " [ 6., 2., 2., 0., -1., -8., 0., 3.],\n", + " [ 7., 1., 1., 1., 1., -6., 1., 0.],\n", + " [ 4., 2., 1., 0., -2., -5., 0., -1.],\n", + " [ 2., 1., 0., 3., 0., -3., 2., 1.]])" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "hashed_features.toarray()" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(7, 16)" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "hasher = FeatureHasher(n_features=16, input_type='string')\n", + "hashed_features = hasher.fit_transform(text)\n", + "\n", + "hashed_features.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 1., 0., 1., 1., -3., -4., 0., 1., 9., 0., 0., 0., 0.,\n", + " -7., 1., -2.],\n", + " [ 0., -3., 1., 3., -1., -2., 0., 2., 6., 1., -1., 0., 1.,\n", + " -4., 0., -2.],\n", + " [ 0., -5., 1., -1., 2., 1., 0., 3., 4., 0., 0., 0., 0.,\n", + " -4., 0., -3.],\n", + " [ 0., 2., 2., -1., -1., -2., 0., 6., 6., 0., 0., 1., 0.,\n", + " -6., 0., -3.],\n", + " [ 2., 1., 1., 1., -1., -2., 1., 1., 5., 0., 0., 0., 2.,\n", + " -4., 0., -1.],\n", + " [-1., 1., 1., 0., -2., -2., 0., 1., 5., 1., 0., 0., 0.,\n", + " -3., 0., -2.],\n", + " [ 0., 0., 0., 3., 0., -1., 2., 2., 2., 1., 0., 0., 0.,\n", + " -2., 0., -1.]])" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "hashed_features.toarray()" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "ename": "AttributeError", + "evalue": "'FeatureHasher' object has no attribute 'inverse_transform'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m--------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mhasher\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minverse_transform\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhashed_features\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mAttributeError\u001b[0m: 'FeatureHasher' object has no attribute 'inverse_transform'" + ] + } + ], + "source": [ + "hasher.inverse_transform(hashed_features)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "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.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/building-features-from-text-data/10-VectorizingTextData_HashingVectorizer.ipynb b/building-features-from-text-data/10-VectorizingTextData_HashingVectorizer.ipynb new file mode 100644 index 0000000..40dec5e --- /dev/null +++ b/building-features-from-text-data/10-VectorizingTextData_HashingVectorizer.ipynb @@ -0,0 +1,486 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## HashingVectorizer\n", + "* HashingVectorizer is the combination of FeatureHasher and CountVectorizer i.e, we get the Term frequency of words as well as the reduced dimension\n", + "* When we used FeatureHasher the dimension of vector reduced but we couldn't get any understandable format in output" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### If we have large vocabulary of words we can choose to use the HashingVectorizer rather than the CountVectorizer\n", + "* The use of hashing buckets to represent words allows us to scale large data sets when we use the HashingVectorizer.\n", + "* The input argument to the vectorizer is the number of hash buckets (n_features)\n", + "* Result : numeric representation of all the words in documents.\n", + "* Word ids are from 0 to (n_features - 1) because total of n_features buckets.\n", + "* Because the size of vocabulary is larger than the number of buckets, multiple words can hash to the same bucket.\n", + "* No way to get back to the original value from the hash bucket value.\n", + "* Frequencies of each is represented in normalized from\n", + "\n", + "https://scikit-learn.org/stable/modules/feature_extraction.html#vectorizing-a-large-text-corpus-with-the-hashing-trick" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### words are mapped directly to indices with a hashing function" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(6, 27)" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from sklearn.feature_extraction.text import CountVectorizer\n", + "\n", + "text_array = [\"Good things come to those who wait.\",\n", + " \"These watches cost $1500! \",\n", + " \"These are other fish in the sea.\",\n", + " \"The ball is in your court.\",\n", + " \"Mr. Smith Goes to Washington \",\n", + " \"Doogie Howser M.D.\"]\n", + "\n", + "count_vectorizer = CountVectorizer()\n", + "feature_vector = count_vectorizer.fit_transform(text_array)\n", + "\n", + "feature_vector.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,\n", + " 1, 0, 0, 1, 0],\n", + " [1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,\n", + " 0, 0, 1, 0, 0],\n", + " [0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0,\n", + " 0, 0, 0, 0, 0],\n", + " [0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0,\n", + " 0, 0, 0, 0, 1],\n", + " [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1,\n", + " 0, 1, 0, 0, 0],\n", + " [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", + " 0, 0, 0, 0, 0]], dtype=int64)" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "feature_vector.toarray()" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'good': 9,\n", + " 'things': 19,\n", + " 'come': 3,\n", + " 'to': 21,\n", + " 'those': 20,\n", + " 'who': 25,\n", + " 'wait': 22,\n", + " 'these': 18,\n", + " 'watches': 24,\n", + " 'cost': 4,\n", + " '1500': 0,\n", + " 'are': 1,\n", + " 'other': 14,\n", + " 'fish': 7,\n", + " 'in': 11,\n", + " 'the': 17,\n", + " 'sea': 15,\n", + " 'ball': 2,\n", + " 'is': 12,\n", + " 'your': 26,\n", + " 'court': 5,\n", + " 'mr': 13,\n", + " 'smith': 16,\n", + " 'goes': 8,\n", + " 'washington': 23,\n", + " 'doogie': 6,\n", + " 'howser': 10}" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "count_vectorizer.vocabulary_" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [], + "source": [ + "analyzer = count_vectorizer.build_analyzer()" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['good', 'things', 'come', 'to', 'those', 'who', 'wait']" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "word_tokens = analyzer(text_array[0])\n", + "\n", + "word_tokens" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [], + "source": [ + "frequency_list = []\n", + "\n", + "for i, text in enumerate(text_array):\n", + " tokens = analyzer(text)\n", + " \n", + " word_frequency = {}\n", + "\n", + " for token in tokens:\n", + " word_idx = count_vectorizer.vocabulary_[token]\n", + " \n", + " word_frequency[token] = feature_vector[i, word_idx]\n", + " \n", + " frequency_list.append(word_frequency)" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'good': 1, 'things': 1, 'come': 1, 'to': 1, 'those': 1, 'who': 1, 'wait': 1},\n", + " {'these': 1, 'watches': 1, 'cost': 1, '1500': 1},\n", + " {'these': 1, 'are': 1, 'other': 1, 'fish': 1, 'in': 1, 'the': 1, 'sea': 1},\n", + " {'the': 1, 'ball': 1, 'is': 1, 'in': 1, 'your': 1, 'court': 1},\n", + " {'mr': 1, 'smith': 1, 'goes': 1, 'to': 1, 'washington': 1},\n", + " {'doogie': 1, 'howser': 1}]" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "frequency_list" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(6, 8)" + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from sklearn.feature_extraction import FeatureHasher\n", + "\n", + "hasher = FeatureHasher(n_features=8, input_type='string')\n", + "hashed_features = hasher.fit_transform(frequency_list)\n", + "\n", + "hashed_features.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 0., 3., 0., 1., -2., -1., 0., 0.],\n", + " [ 0., 0., -1., 0., -1., 0., 2., 0.],\n", + " [ 0., 1., -1., 0., 0., 0., 0., 1.],\n", + " [ 0., 1., 0., 0., 0., 1., 0., 0.],\n", + " [ 0., -1., 0., 1., 0., -1., 0., 0.],\n", + " [ 0., 1., 0., 0., 1., 0., 0., 0.]])" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "hashed_features.toarray()" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(6, 8)" + ] + }, + "execution_count": 69, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from sklearn.feature_extraction.text import HashingVectorizer\n", + "\n", + "vectorizer = HashingVectorizer(n_features=8, norm=None)\n", + "feature_vector = vectorizer.transform(text_array)\n", + "\n", + "feature_vector.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 0., 3., 0., 1., -2., -1., 0., 0.],\n", + " [ 0., 0., -1., 0., -1., 0., 2., 0.],\n", + " [ 0., 1., -1., 0., 0., 0., 0., 1.],\n", + " [ 0., 1., 0., 0., 0., 1., 0., 0.],\n", + " [ 0., -1., 0., 1., 0., -1., 0., 0.],\n", + " [ 0., 1., 0., 0., 1., 0., 0., 0.]])" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "feature_vector.toarray()" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(6, 8)" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "vectorizer = HashingVectorizer(n_features=8, norm='l1')\n", + "feature_vector = vectorizer.transform(text_array)\n", + "\n", + "feature_vector.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 0. , 0.42857143, 0. , 0.14285714, -0.28571429,\n", + " -0.14285714, 0. , 0. ],\n", + " [ 0. , 0. , -0.25 , 0. , -0.25 ,\n", + " 0. , 0.5 , 0. ],\n", + " [ 0. , 0.33333333, -0.33333333, 0. , 0. ,\n", + " 0. , 0. , 0.33333333],\n", + " [ 0. , 0.5 , 0. , 0. , 0. ,\n", + " 0.5 , 0. , 0. ],\n", + " [ 0. , -0.33333333, 0. , 0.33333333, 0. ,\n", + " -0.33333333, 0. , 0. ],\n", + " [ 0. , 0.5 , 0. , 0. , 0.5 ,\n", + " 0. , 0. , 0. ]])" + ] + }, + "execution_count": 72, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "feature_vector.toarray()" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(6, 8)" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "vectorizer = HashingVectorizer(n_features=8, norm='l2')\n", + "feature_vector = vectorizer.transform(text_array)\n", + "\n", + "feature_vector.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 0. , 0.77459667, 0. , 0.25819889, -0.51639778,\n", + " -0.25819889, 0. , 0. ],\n", + " [ 0. , 0. , -0.40824829, 0. , -0.40824829,\n", + " 0. , 0.81649658, 0. ],\n", + " [ 0. , 0.57735027, -0.57735027, 0. , 0. ,\n", + " 0. , 0. , 0.57735027],\n", + " [ 0. , 0.70710678, 0. , 0. , 0. ,\n", + " 0.70710678, 0. , 0. ],\n", + " [ 0. , -0.57735027, 0. , 0.57735027, 0. ,\n", + " -0.57735027, 0. , 0. ],\n", + " [ 0. , 0.70710678, 0. , 0. , 0.70710678,\n", + " 0. , 0. , 0. ]])" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "feature_vector.toarray()" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[0., 3., 0., 1., 2., 1., 0., 0.],\n", + " [0., 0., 1., 0., 1., 0., 2., 0.],\n", + " [2., 1., 1., 0., 0., 0., 2., 1.],\n", + " [0., 1., 0., 0., 2., 1., 2., 0.],\n", + " [0., 1., 0., 3., 0., 1., 0., 0.],\n", + " [0., 1., 0., 0., 1., 0., 0., 0.]])" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "vectorizer = HashingVectorizer(n_features=8, norm=None, alternate_sign=False)\n", + "feature_vector = vectorizer.transform(text_array)\n", + "\n", + "feature_vector.toarray()" + ] + }, + { + "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.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/building-features-from-text-data/11-SimplifyTextDataUsingLocallySensitiveHashing .ipynb b/building-features-from-text-data/11-SimplifyTextDataUsingLocallySensitiveHashing .ipynb new file mode 100644 index 0000000..2a9110d --- /dev/null +++ b/building-features-from-text-data/11-SimplifyTextDataUsingLocallySensitiveHashing .ipynb @@ -0,0 +1,297 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Datasketch\n", + "https://pypi.org/project/datasketch/" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: datasketch in /anaconda3/lib/python3.7/site-packages (1.4.3)\n", + "Requirement already satisfied: numpy>=1.11 in /anaconda3/lib/python3.7/site-packages (from datasketch) (1.16.1)\n", + "Requirement already satisfied: redis>=2.10.0 in /anaconda3/lib/python3.7/site-packages (from datasketch) (3.2.1)\n" + ] + } + ], + "source": [ + "!pip install datasketch" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [], + "source": [ + "from datasketch import MinHash, MinHashLSH\n", + "\n", + "from nltk import ngrams\n", + "from nltk import word_tokenize" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [], + "source": [ + "text_array = [\"A bird in hand is worth two in the bush.\",\n", + " \"Good things come to those who wait.\",\n", + " \"There are other fish in the sea.\",\n", + " \"The ball is in your court.\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[['A', 'bird', 'in', 'hand', 'is', 'worth', 'two', 'in', 'the', 'bush', '.'],\n", + " ['Good', 'things', 'come', 'to', 'those', 'who', 'wait', '.'],\n", + " ['There', 'are', 'other', 'fish', 'in', 'the', 'sea', '.'],\n", + " ['The', 'ball', 'is', 'in', 'your', 'court', '.']]" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "word_token_array = [word_tokenize(text) for text in text_array]\n", + "\n", + "word_token_array" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 ('A', 'bird', 'in')\n", + "0 ('bird', 'in', 'hand')\n", + "0 ('in', 'hand', 'is')\n", + "0 ('hand', 'is', 'worth')\n", + "0 ('is', 'worth', 'two')\n", + "0 ('worth', 'two', 'in')\n", + "0 ('two', 'in', 'the')\n", + "0 ('in', 'the', 'bush')\n", + "0 ('the', 'bush', '.')\n", + "1 ('Good', 'things', 'come')\n", + "1 ('things', 'come', 'to')\n", + "1 ('come', 'to', 'those')\n", + "1 ('to', 'those', 'who')\n", + "1 ('those', 'who', 'wait')\n", + "1 ('who', 'wait', '.')\n", + "2 ('There', 'are', 'other')\n", + "2 ('are', 'other', 'fish')\n", + "2 ('other', 'fish', 'in')\n", + "2 ('fish', 'in', 'the')\n", + "2 ('in', 'the', 'sea')\n", + "2 ('the', 'sea', '.')\n", + "3 ('The', 'ball', 'is')\n", + "3 ('ball', 'is', 'in')\n", + "3 ('is', 'in', 'your')\n", + "3 ('in', 'your', 'court')\n", + "3 ('your', 'court', '.')\n" + ] + } + ], + "source": [ + "for index, word_tokens in enumerate(word_token_array):\n", + " for n_gram in ngrams(word_tokens, 3):\n", + " print(index, n_gram)" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [], + "source": [ + "min_hash_lsh = MinHashLSH(threshold=0.5, num_perm=128)" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [], + "source": [ + "min_hashes = {}\n", + "\n", + "for index, text in enumerate(text_array):\n", + " min_hash = MinHash(num_perm=128)\n", + "\n", + " for n_gram in ngrams(text, 3):\n", + " min_hash.update(\"\".join(n_gram).encode('utf-8'))\n", + " \n", + " min_hash_lsh.insert(index, min_hash)\n", + " min_hashes[index] = min_hash" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{0: ,\n", + " 1: ,\n", + " 2: ,\n", + " 3: }" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "min_hashes" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Candidate pairs with Jaccard similarity > 0.5 for input 0 : [0]\n", + "Candidate pairs with Jaccard similarity > 0.5 for input 1 : [1]\n", + "Candidate pairs with Jaccard similarity > 0.5 for input 2 : [2]\n", + "Candidate pairs with Jaccard similarity > 0.5 for input 3 : [3]\n" + ] + } + ], + "source": [ + "for i in min_hashes.keys():\n", + " result = min_hash_lsh.query(min_hashes[i])\n", + " print(\"Candidate pairs with Jaccard similarity > 0.5 for input\", i, \":\", result)" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [], + "source": [ + "text_array = [\"A bird in hand is worth two in the bush.\",\n", + " \"A bird in hands is worth three in the bushes.\",\n", + " \"Good things come to those who wait.\",\n", + " \"Good tpings cxme to those who wait long.\",\n", + " \"There are other fish in the sea.\",\n", + " \"The ball is in your court.\"\n", + " ]" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [], + "source": [ + "min_hash_lsh = MinHashLSH(threshold=0.5, num_perm=128)" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [], + "source": [ + "min_hashes = {}\n", + "\n", + "for index, text in enumerate(text_array):\n", + " min_hash = MinHash(num_perm=128)\n", + "\n", + " for n_gram in ngrams(text, 3):\n", + " min_hash.update(\"\".join(n_gram).encode('utf-8'))\n", + " \n", + " min_hash_lsh.insert(index, min_hash)\n", + " min_hashes[index] = min_hash" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Candidate pairs with Jaccard similarity > 0.5 for input 0 : [0, 1]\n", + "Candidate pairs with Jaccard similarity > 0.5 for input 1 : [0, 1]\n", + "Candidate pairs with Jaccard similarity > 0.5 for input 2 : [2, 3]\n", + "Candidate pairs with Jaccard similarity > 0.5 for input 3 : [2, 3]\n", + "Candidate pairs with Jaccard similarity > 0.5 for input 4 : [4]\n", + "Candidate pairs with Jaccard similarity > 0.5 for input 5 : [5]\n" + ] + } + ], + "source": [ + "for i in min_hashes.keys():\n", + " result = min_hash_lsh.query(min_hashes[i])\n", + " print(\"Candidate pairs with Jaccard similarity > 0.5 for input\", i, \":\", result)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "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.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/building-features-from-text-data/12a-HashingVectorizer_NaiveBayesClassifier.ipynb b/building-features-from-text-data/12a-HashingVectorizer_NaiveBayesClassifier.ipynb new file mode 100644 index 0000000..c78c439 --- /dev/null +++ b/building-features-from-text-data/12a-HashingVectorizer_NaiveBayesClassifier.ipynb @@ -0,0 +1,711 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Dataset link : https://github.com/pyk/dbpedia_csv" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "\n", + "from sklearn.model_selection import train_test_split\n", + "\n", + "from sklearn.metrics import accuracy_score\n", + "from sklearn.metrics import precision_score\n", + "from sklearn.metrics import recall_score\n", + "\n", + "from sklearn.feature_extraction.text import HashingVectorizer\n", + "from sklearn.naive_bayes import GaussianNB" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "dbpedia_df = pd.read_csv('./datasets/dbpedia_csv/train.csv', \n", + " skiprows=1, names = ['Label', 'Name', 'Text'])" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(559999, 3)" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### DBPedia classes\n", + "\n", + "- Company\n", + "- EducationalInstitution\n", + "- Artist\n", + "- Athlete\n", + "- OfficeHolder\n", + "- MeanOfTransportation\n", + "- Building\n", + "- NaturalPlace\n", + "- Village\n", + "- Animal\n", + "- Plant\n", + "- Album\n", + "- Film\n", + "- WrittenWork" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df['Label'].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "dbpedia_df = dbpedia_df.sample(10000, replace=False)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "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", + " \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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
LabelNameText
49448313A Wedding SuitA Wedding Suit (Persian: لباسی برای عروسی‎ Le...
54849114Into Thin AirInto Thin Air: A Personal Account of the Mt. ...
3393699BanavchanBanavchan (Persian: بناوچان‎ also Romanized a...
2958288Icalma LakeIcalma Lake is a lake located in the Andes of...
41655211Pouteria sandwicensisPouteria sandwicensis is a species of floweri...
50384913Pepo (film)Pepo (Armenian: Պեպո) is a 1935 Soviet film d...
36438410Puebla deer mouseThe Puebla deer mouse (Peromyscus mekisturus)...
2654657Waleffe CastleWaleffe Castle is a castle in Belgium.
52836414Through the Arc of the Rain ForestThrough the Arc of the Rain Forest is a novel...
42381611GuariantheGuarianthe abbreviated Gur. in the horticultu...
\n", + "
" + ], + "text/plain": [ + " Label Name \\\n", + "494483 13 A Wedding Suit \n", + "548491 14 Into Thin Air \n", + "339369 9 Banavchan \n", + "295828 8 Icalma Lake \n", + "416552 11 Pouteria sandwicensis \n", + "503849 13 Pepo (film) \n", + "364384 10 Puebla deer mouse \n", + "265465 7 Waleffe Castle \n", + "528364 14 Through the Arc of the Rain Forest \n", + "423816 11 Guarianthe \n", + "\n", + " Text \n", + "494483 A Wedding Suit (Persian: لباسی برای عروسی‎ Le... \n", + "548491 Into Thin Air: A Personal Account of the Mt. ... \n", + "339369 Banavchan (Persian: بناوچان‎ also Romanized a... \n", + "295828 Icalma Lake is a lake located in the Andes of... \n", + "416552 Pouteria sandwicensis is a species of floweri... \n", + "503849 Pepo (Armenian: Պեպո) is a 1935 Soviet film d... \n", + "364384 The Puebla deer mouse (Peromyscus mekisturus)... \n", + "265465 Waleffe Castle is a castle in Belgium. \n", + "528364 Through the Arc of the Rain Forest is a novel... \n", + "423816 Guarianthe abbreviated Gur. in the horticultu... " + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.sample(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 3)" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "X = dbpedia_df['Text']\n", + "\n", + "Y = dbpedia_df['Label']" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "187313 Bruce Cameron Young is an Australian Liberal ...\n", + "94935 Daniel Douglas Orlando (born May 29 1981) als...\n", + "93329 John Hultberg (February 8 1922 – April 15 200...\n", + "530642 The Oxford University Commonwealth Law Journa...\n", + "361874 Stensioella heintzi (Heintz's Little Stensio)...\n", + "Name: Text, dtype: object" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "187313 5\n", + "94935 3\n", + "93329 3\n", + "530642 14\n", + "361874 10\n", + "Name: Label, dtype: int64" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Y.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "def summarize_classification(y_test, y_pred):\n", + "\n", + " acc = accuracy_score(y_test, y_pred, normalize=True)\n", + " num_acc = accuracy_score(y_test, y_pred, normalize=False)\n", + " prec = precision_score(y_test, y_pred, average='weighted')\n", + " recall = recall_score(y_test, y_pred, average='weighted')\n", + " \n", + " print(\"Length of testing data: \", len(y_test))\n", + " print(\"accuracy_count : \" , num_acc)\n", + " print(\"accuracy_score : \" , acc)\n", + " print(\"precision_score : \" , prec)\n", + " print(\"recall_score : \", recall)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 1024)" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "vectorizer = HashingVectorizer(n_features=2**10, norm='l2')\n", + "\n", + "feature_vector = vectorizer.transform(X)\n", + "\n", + "feature_vector.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " (0, 39)\t0.15811388300841897\n", + " (0, 53)\t-0.15811388300841897\n", + " (0, 54)\t0.15811388300841897\n", + " (0, 61)\t-0.15811388300841897\n", + " (0, 152)\t0.15811388300841897\n", + " (0, 158)\t-0.4743416490252569\n", + " (0, 175)\t-0.15811388300841897\n", + " (0, 203)\t-0.15811388300841897\n", + " (0, 300)\t0.31622776601683794\n", + " (0, 308)\t-0.15811388300841897\n", + " (0, 311)\t-0.15811388300841897\n", + " (0, 352)\t-0.15811388300841897\n", + " (0, 362)\t-0.15811388300841897\n", + " (0, 363)\t0.15811388300841897\n", + " (0, 365)\t0.31622776601683794\n", + " (0, 399)\t0.15811388300841897\n", + " (0, 426)\t0.15811388300841897\n", + " (0, 470)\t0.15811388300841897\n", + " (0, 513)\t-0.15811388300841897\n", + " (0, 534)\t0.15811388300841897\n", + " (0, 540)\t-0.15811388300841897\n", + " (0, 663)\t-0.15811388300841897\n", + " (0, 665)\t-0.15811388300841897\n", + " (0, 819)\t-0.15811388300841897\n", + " (0, 1002)\t-0.15811388300841897\n", + " (0, 1011)\t-0.15811388300841897\n" + ] + } + ], + "source": [ + "print(feature_vector[0])" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "X_dense = feature_vector.todense()" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 1024)" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X_dense.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "x_train, x_test, y_train, y_test = train_test_split(X_dense, Y, test_size = 0.2)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "((8000, 1024), (2000, 1024))" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x_train.shape, x_test.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "((8000,), (2000,))" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y_train.shape, y_test.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "clf = GaussianNB().fit(x_train, y_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 7, 11, 13, ..., 5, 6, 7])" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y_pred = clf.predict(x_test)\n", + "y_pred" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Length of testing data: 2000\n", + "accuracy_count : 1153\n", + "accuracy_score : 0.5765\n", + "precision_score : 0.5891110304184962\n", + "recall_score : 0.5765\n" + ] + } + ], + "source": [ + "summarize_classification(y_test, y_pred)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "267097 7\n", + "364326 10\n", + "509624 13\n", + "466990 12\n", + "145918 4\n", + "Name: Label, dtype: int64" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y_test.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "y_test = np.array(y_test)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "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", + " \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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
y_testy_pred
148611
49498
103933
98599
711810
53211
12491110
1746811
76617
1851124
\n", + "
" + ], + "text/plain": [ + " y_test y_pred\n", + "1486 1 1\n", + "494 9 8\n", + "1039 3 3\n", + "985 9 9\n", + "711 8 10\n", + "532 1 1\n", + "1249 11 10\n", + "1746 8 11\n", + "766 1 7\n", + "1851 12 4" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pred_results = pd.DataFrame({'y_test': pd.Series(y_test),\n", + " 'y_pred': pd.Series(y_pred)})\n", + "\n", + "pred_results.sample(10)" + ] + }, + { + "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.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/building-features-from-text-data/12b-Stemmer_HashingVectorizer_NaiveBayesClassifier.ipynb b/building-features-from-text-data/12b-Stemmer_HashingVectorizer_NaiveBayesClassifier.ipynb new file mode 100644 index 0000000..4c00ea6 --- /dev/null +++ b/building-features-from-text-data/12b-Stemmer_HashingVectorizer_NaiveBayesClassifier.ipynb @@ -0,0 +1,582 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "\n", + "from sklearn.model_selection import train_test_split\n", + "\n", + "from sklearn.metrics import accuracy_score\n", + "from sklearn.metrics import precision_score\n", + "from sklearn.metrics import recall_score\n", + "\n", + "from sklearn.feature_extraction.text import HashingVectorizer\n", + "from nltk.stem.snowball import SnowballStemmer\n", + "from sklearn.naive_bayes import GaussianNB" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "dbpedia_df = pd.read_csv('./datasets/dbpedia_csv/train.csv', \n", + " skiprows=1, names = ['Label', 'Name', 'Text'])" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(559999, 3)" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### DBPedia classes\n", + "\n", + "- Company\n", + "- EducationalInstitution\n", + "- Artist\n", + "- Athlete\n", + "- OfficeHolder\n", + "- MeanOfTransportation\n", + "- Building\n", + "- NaturalPlace\n", + "- Village\n", + "- Animal\n", + "- Plant\n", + "- Album\n", + "- Film\n", + "- WrittenWork" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df['Label'].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "dbpedia_df = dbpedia_df.sample(10000, replace=False)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "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", + " \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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
LabelNameText
3287549Sadabad-e SoflaSadabad-e Sofla (Persian: سعدابادسفلي‎ also R...
2682047Westgate Mall (Spartanburg)Westgate Mall is a shopping mall in Spartanbu...
535242North Rockland High SchoolNorth Rockland High School is a high school l...
43564611Tillandsia diguetiiTillandsia diguetii is a species of the genus...
2547527Palais WilczekPalais Wilczek is a palace in Vienna Austria....
290741CanforCanfor Corporation is a Canadian integrated f...
1550594Gerry RiouxGerard Rioux (born February 17 1959) is a Can...
1374514Jerry DePoysterJerry Dean DePoyster (born July 6 1946 in Oma...
36302210Pieris (butterfly)Pieris the whites or garden whites is a wides...
49984613Gunah Aur KanoonGunah Aur Kanoon is a 1970 Bollywood drama fi...
\n", + "
" + ], + "text/plain": [ + " Label Name \\\n", + "328754 9 Sadabad-e Sofla \n", + "268204 7 Westgate Mall (Spartanburg) \n", + "53524 2 North Rockland High School \n", + "435646 11 Tillandsia diguetii \n", + "254752 7 Palais Wilczek \n", + "29074 1 Canfor \n", + "155059 4 Gerry Rioux \n", + "137451 4 Jerry DePoyster \n", + "363022 10 Pieris (butterfly) \n", + "499846 13 Gunah Aur Kanoon \n", + "\n", + " Text \n", + "328754 Sadabad-e Sofla (Persian: سعدابادسفلي‎ also R... \n", + "268204 Westgate Mall is a shopping mall in Spartanbu... \n", + "53524 North Rockland High School is a high school l... \n", + "435646 Tillandsia diguetii is a species of the genus... \n", + "254752 Palais Wilczek is a palace in Vienna Austria.... \n", + "29074 Canfor Corporation is a Canadian integrated f... \n", + "155059 Gerard Rioux (born February 17 1959) is a Can... \n", + "137451 Jerry Dean DePoyster (born July 6 1946 in Oma... \n", + "363022 Pieris the whites or garden whites is a wides... \n", + "499846 Gunah Aur Kanoon is a 1970 Bollywood drama fi... " + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.sample(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 3)" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "X = dbpedia_df['Text']\n", + "\n", + "Y = dbpedia_df['Label']" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "346354 Hoseynabad-e Sarzeh (Persian: حسين ابادسرزه‎ ...\n", + "198538 Milton Berkes (born September 29 1924) is a f...\n", + "550592 R.M. Williams Outback (or simply Outback) is ...\n", + "387637 Metasia carnealis is a species of moth in the...\n", + "366618 The Father Basilio's Striped Mouse or Bioko H...\n", + "Name: Text, dtype: object" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "346354 9\n", + "198538 5\n", + "550592 14\n", + "387637 10\n", + "366618 10\n", + "Name: Label, dtype: int64" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Y.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "def summarize_classification(y_test, y_pred):\n", + "\n", + " acc = accuracy_score(y_test, y_pred, normalize=True)\n", + " num_acc = accuracy_score(y_test, y_pred, normalize=False)\n", + " prec = precision_score(y_test, y_pred, average='weighted')\n", + " recall = recall_score(y_test, y_pred, average='weighted')\n", + " \n", + " print(\"Length of testing data: \", len(y_test))\n", + " print(\"accuracy_count : \" , num_acc)\n", + " print(\"accuracy_score : \" , acc)\n", + " print(\"precision_score : \" , prec)\n", + " print(\"recall_score : \", recall)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "stemmer = SnowballStemmer('english')\n", + "analyzer = HashingVectorizer().build_analyzer()\n", + "\n", + "def stemmed_words(doc):\n", + " return (stemmer.stem(w) for w in analyzer(doc))" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 1024)" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "stem_vectorizer = HashingVectorizer(n_features=2**10, norm='l2', analyzer=stemmed_words)\n", + "\n", + "feature_vector = stem_vectorizer.transform(X)\n", + "\n", + "feature_vector.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " (0, 21)\t0.25607375986579195\n", + " (0, 24)\t-0.12803687993289598\n", + " (0, 27)\t0.12803687993289598\n", + " (0, 61)\t-0.12803687993289598\n", + " (0, 62)\t0.12803687993289598\n", + " (0, 69)\t-0.12803687993289598\n", + " (0, 71)\t-0.25607375986579195\n", + " (0, 145)\t-0.12803687993289598\n", + " (0, 158)\t-0.12803687993289598\n", + " (0, 215)\t0.12803687993289598\n", + " (0, 273)\t0.25607375986579195\n", + " (0, 301)\t-0.12803687993289598\n", + " (0, 304)\t0.12803687993289598\n", + " (0, 355)\t0.12803687993289598\n", + " (0, 365)\t0.12803687993289598\n", + " (0, 424)\t-0.12803687993289598\n", + " (0, 540)\t0.25607375986579195\n", + " (0, 550)\t-0.12803687993289598\n", + " (0, 569)\t0.12803687993289598\n", + " (0, 595)\t-0.3841106397986879\n", + " (0, 643)\t0.12803687993289598\n", + " (0, 659)\t0.12803687993289598\n", + " (0, 697)\t0.12803687993289598\n", + " (0, 745)\t-0.12803687993289598\n", + " (0, 758)\t-0.12803687993289598\n", + " (0, 799)\t0.12803687993289598\n", + " (0, 832)\t-0.12803687993289598\n", + " (0, 877)\t0.12803687993289598\n", + " (0, 883)\t0.12803687993289598\n", + " (0, 884)\t-0.12803687993289598\n", + " (0, 885)\t0.25607375986579195\n", + " (0, 913)\t0.12803687993289598\n", + " (0, 914)\t-0.12803687993289598\n", + " (0, 971)\t-0.25607375986579195\n", + " (0, 983)\t-0.12803687993289598\n" + ] + } + ], + "source": [ + "print(feature_vector[0])" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "X_dense = feature_vector.todense()" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 1024)" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X_dense.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [], + "source": [ + "x_train, x_test, y_train, y_test = train_test_split(X_dense, Y, test_size = 0.2)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "((8000, 1024), (2000, 1024))" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x_train.shape, x_test.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "((8000,), (2000,))" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y_train.shape, y_test.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [], + "source": [ + "clf = GaussianNB().fit(x_train, y_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 3, 3, 11, ..., 2, 2, 4])" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y_pred = clf.predict(x_test)\n", + "y_pred" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Length of testing data: 2000\n", + "accuracy_count : 1120\n", + "accuracy_score : 0.56\n", + "precision_score : 0.5663465185830068\n", + "recall_score : 0.56\n" + ] + } + ], + "source": [ + "summarize_classification(y_test, y_pred)" + ] + }, + { + "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.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/building-features-from-text-data/13a-CountVectorizer_NaiveBayesClassifier.ipynb b/building-features-from-text-data/13a-CountVectorizer_NaiveBayesClassifier.ipynb new file mode 100644 index 0000000..bcab90d --- /dev/null +++ b/building-features-from-text-data/13a-CountVectorizer_NaiveBayesClassifier.ipynb @@ -0,0 +1,571 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "\n", + "from sklearn.model_selection import train_test_split\n", + "\n", + "from sklearn.metrics import accuracy_score\n", + "from sklearn.metrics import precision_score\n", + "from sklearn.metrics import recall_score\n", + "\n", + "from sklearn.feature_extraction.text import CountVectorizer\n", + "from sklearn.naive_bayes import GaussianNB" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "dbpedia_df = pd.read_csv('./datasets/dbpedia_csv/train.csv', \n", + " skiprows=1, names = ['Label', 'Name', 'Text'])" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(559999, 3)" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### DBPedia classes\n", + "\n", + "- Company\n", + "- EducationalInstitution\n", + "- Artist\n", + "- Athlete\n", + "- OfficeHolder\n", + "- MeanOfTransportation\n", + "- Building\n", + "- NaturalPlace\n", + "- Village\n", + "- Animal\n", + "- Plant\n", + "- Album\n", + "- Film\n", + "- WrittenWork" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df['Label'].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "dbpedia_df = dbpedia_df.sample(10000, replace=False)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "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", + " \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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
LabelNameText
50099613The County ChairmanThe County Chairman is a 1935 comedy film dir...
407952Our Lady of Mount Carmel High School (Baltimor...Our Lady of Mount Carmel High School (OLMC HS...
1314534Jim KleinsasserJimmy Carter Kleinsasser (/ˈklaɪnsɑːsər/; bor...
1222914August KlinglerAugust Klingler (24 February 1918 – 23 Novemb...
389651Fine FareFine Fare was the name of a chain of supermar...
898523Desmond DevlinDesmond Devlin is an American comedy writer. ...
48193013A Sister to Assist 'Er (1938 film)A Sister to Assist 'Er is a 1938 British come...
950083Megumi MakiharaMegumi Makihara (槇原めぐみ or 槙原めぐみ or 慎原めぐみ Maki...
54357114Annals of ScienceAnnals of Science is a peer-reviewed academic...
3567919Dula GavabarDula Gavabar (Persian: دولاگوابر‎ also Romani...
\n", + "
" + ], + "text/plain": [ + " Label Name \\\n", + "500996 13 The County Chairman \n", + "40795 2 Our Lady of Mount Carmel High School (Baltimor... \n", + "131453 4 Jim Kleinsasser \n", + "122291 4 August Klingler \n", + "38965 1 Fine Fare \n", + "89852 3 Desmond Devlin \n", + "481930 13 A Sister to Assist 'Er (1938 film) \n", + "95008 3 Megumi Makihara \n", + "543571 14 Annals of Science \n", + "356791 9 Dula Gavabar \n", + "\n", + " Text \n", + "500996 The County Chairman is a 1935 comedy film dir... \n", + "40795 Our Lady of Mount Carmel High School (OLMC HS... \n", + "131453 Jimmy Carter Kleinsasser (/ˈklaɪnsɑːsər/; bor... \n", + "122291 August Klingler (24 February 1918 – 23 Novemb... \n", + "38965 Fine Fare was the name of a chain of supermar... \n", + "89852 Desmond Devlin is an American comedy writer. ... \n", + "481930 A Sister to Assist 'Er is a 1938 British come... \n", + "95008 Megumi Makihara (槇原めぐみ or 槙原めぐみ or 慎原めぐみ Maki... \n", + "543571 Annals of Science is a peer-reviewed academic... \n", + "356791 Dula Gavabar (Persian: دولاگوابر‎ also Romani... " + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.sample(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 3)" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "X = dbpedia_df['Text']\n", + "\n", + "Y = dbpedia_df['Label']" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "113209 Eisuke Yoshiyuki (吉行 エイスケ Yoshiyuki Eisuke Ma...\n", + "10443 Schroders plc is a British multinational asse...\n", + "152897 Patrice Ferri is a retired French association...\n", + "485929 Honey 2 is a dance film which is a sequel to ...\n", + "312910 Marjanah is an impact crater in the northern ...\n", + "Name: Text, dtype: object" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "113209 3\n", + "10443 1\n", + "152897 4\n", + "485929 13\n", + "312910 8\n", + "Name: Label, dtype: int64" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Y.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "def summarize_classification(y_test, y_pred):\n", + "\n", + " acc = accuracy_score(y_test, y_pred, normalize=True)\n", + " num_acc = accuracy_score(y_test, y_pred, normalize=False)\n", + " prec = precision_score(y_test, y_pred, average='weighted')\n", + " recall = recall_score(y_test, y_pred, average='weighted')\n", + " \n", + " print(\"Length of testing data: \", len(y_test))\n", + " print(\"accuracy_count : \" , num_acc)\n", + " print(\"accuracy_score : \" , acc)\n", + " print(\"precision_score : \" , prec)\n", + " print(\"recall_score : \", recall)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 48133)" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "count_vectorizer = CountVectorizer()\n", + "\n", + "feature_vector = count_vectorizer.fit_transform(X)\n", + "\n", + "feature_vector.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " (0, 44965)\t1\n", + " (0, 45159)\t1\n", + " (0, 24477)\t1\n", + " (0, 30174)\t1\n", + " (0, 5353)\t1\n", + " (0, 2617)\t2\n", + " (0, 4213)\t1\n", + " (0, 29184)\t1\n", + " (0, 770)\t1\n", + " (0, 41370)\t1\n", + " (0, 2229)\t1\n", + " (0, 22994)\t1\n", + " (0, 44731)\t1\n", + " (0, 22447)\t1\n", + " (0, 11806)\t1\n", + " (0, 3406)\t1\n", + " (0, 29564)\t1\n", + " (0, 3087)\t1\n", + " (0, 21905)\t1\n", + " (0, 38741)\t1\n", + " (0, 19410)\t3\n", + " (0, 32904)\t1\n", + " (0, 30043)\t1\n", + " (0, 20446)\t1\n", + " (0, 6733)\t1\n", + " (0, 18881)\t1\n", + " (0, 4662)\t2\n", + " (0, 21408)\t1\n", + " (0, 44526)\t4\n", + " (0, 696)\t1\n", + " (0, 21879)\t1\n", + " (0, 653)\t1\n", + " (0, 27)\t1\n", + " (0, 26417)\t1\n", + " (0, 47763)\t1\n", + " (0, 47850)\t1\n", + " (0, 45736)\t4\n", + " (0, 13969)\t3\n" + ] + } + ], + "source": [ + "print(feature_vector[0])" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "X_dense = feature_vector.todense()" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 48133)" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X_dense.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "x_train, x_test, y_train, y_test = train_test_split(X_dense, Y, test_size = 0.2)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "((8000, 48133), (2000, 48133))" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x_train.shape, x_test.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "((8000,), (2000,))" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y_train.shape, y_test.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "clf = GaussianNB().fit(x_train, y_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 5, 10, 4, ..., 5, 1, 14])" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y_pred = clf.predict(x_test)\n", + "y_pred" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Length of testing data: 2000\n", + "accuracy_count : 1464\n", + "accuracy_score : 0.732\n", + "precision_score : 0.7480649828741391\n", + "recall_score : 0.732\n" + ] + } + ], + "source": [ + "summarize_classification(y_test, y_pred)" + ] + }, + { + "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.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/building-features-from-text-data/13b-StopwordRemoval_CountVectorizer_NaiveBayesClassifier.ipynb b/building-features-from-text-data/13b-StopwordRemoval_CountVectorizer_NaiveBayesClassifier.ipynb new file mode 100644 index 0000000..24ba457 --- /dev/null +++ b/building-features-from-text-data/13b-StopwordRemoval_CountVectorizer_NaiveBayesClassifier.ipynb @@ -0,0 +1,568 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "\n", + "from sklearn.model_selection import train_test_split\n", + "\n", + "from sklearn.metrics import accuracy_score\n", + "from sklearn.metrics import precision_score\n", + "from sklearn.metrics import recall_score\n", + "\n", + "from sklearn.feature_extraction.text import CountVectorizer\n", + "from sklearn.naive_bayes import GaussianNB" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "dbpedia_df = pd.read_csv('./datasets/dbpedia_csv/train.csv', \n", + " skiprows=1, names = ['Label', 'Name', 'Text'])" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(559999, 3)" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### DBPedia classes\n", + "\n", + "- Company\n", + "- EducationalInstitution\n", + "- Artist\n", + "- Athlete\n", + "- OfficeHolder\n", + "- MeanOfTransportation\n", + "- Building\n", + "- NaturalPlace\n", + "- Village\n", + "- Animal\n", + "- Plant\n", + "- Album\n", + "- Film\n", + "- WrittenWork" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df['Label'].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "dbpedia_df = dbpedia_df.sample(10000, replace=False)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "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", + " \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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
LabelNameText
52446314Open Sesame (manga)Open Sesame is a Japanese manga series writte...
2149546USS Porter (DDG-78)USS Porter (DDG-78) is an Arleigh Burke-class...
36918810Chionodes luctuellaChionodes luctuella is a moth of the Gelechii...
155371Rashtriya Chemicals & FertilizersRashtriya Chemicals & Fertilizers Ltd. (RCF) ...
43442611Salix gilgianaSalix gilgiana is a species of willow native ...
3150868Devlins CreekDevlins Creek an urban watercourse that is pa...
1331944Warren DonaldWarren Donald (born 7 October 1964) is an Eng...
1380904Chris Hatcher (outfielder)Christopher Kenneth Hatcher (born January 7 1...
3588739Królewskie Ostrzeszów CountyKrólewskie [kruˈlɛfskʲɛ] is a village in the ...
1080483Hamuera Tamahau MahupukuHamuera Tamahau Mahupuku (c.1842 – 14 January...
\n", + "
" + ], + "text/plain": [ + " Label Name \\\n", + "524463 14 Open Sesame (manga) \n", + "214954 6 USS Porter (DDG-78) \n", + "369188 10 Chionodes luctuella \n", + "15537 1 Rashtriya Chemicals & Fertilizers \n", + "434426 11 Salix gilgiana \n", + "315086 8 Devlins Creek \n", + "133194 4 Warren Donald \n", + "138090 4 Chris Hatcher (outfielder) \n", + "358873 9 Królewskie Ostrzeszów County \n", + "108048 3 Hamuera Tamahau Mahupuku \n", + "\n", + " Text \n", + "524463 Open Sesame is a Japanese manga series writte... \n", + "214954 USS Porter (DDG-78) is an Arleigh Burke-class... \n", + "369188 Chionodes luctuella is a moth of the Gelechii... \n", + "15537 Rashtriya Chemicals & Fertilizers Ltd. (RCF) ... \n", + "434426 Salix gilgiana is a species of willow native ... \n", + "315086 Devlins Creek an urban watercourse that is pa... \n", + "133194 Warren Donald (born 7 October 1964) is an Eng... \n", + "138090 Christopher Kenneth Hatcher (born January 7 1... \n", + "358873 Królewskie [kruˈlɛfskʲɛ] is a village in the ... \n", + "108048 Hamuera Tamahau Mahupuku (c.1842 – 14 January... " + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.sample(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 3)" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "X = dbpedia_df['Text']\n", + "\n", + "Y = dbpedia_df['Label']" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "77514 Phillips University was a private coeducation...\n", + "341059 Nowiny [nɔˈvinɨ] is a village in the administ...\n", + "517834 Capricious Summer (Czech: Rozmarné léto) is a...\n", + "215388 Maumelle Ordnance Works Locomotive 1 is a gas...\n", + "250923 Grace Reformed Church is a historic church lo...\n", + "Name: Text, dtype: object" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "77514 2\n", + "341059 9\n", + "517834 13\n", + "215388 6\n", + "250923 7\n", + "Name: Label, dtype: int64" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Y.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [], + "source": [ + "def summarize_classification(y_test, y_pred):\n", + "\n", + " acc = accuracy_score(y_test, y_pred, normalize=True)\n", + " num_acc = accuracy_score(y_test, y_pred, normalize=False)\n", + " prec = precision_score(y_test, y_pred, average='weighted')\n", + " recall = recall_score(y_test, y_pred, average='weighted')\n", + " \n", + " print(\"Length of testing data: \", len(y_test))\n", + " print(\"accuracy_count : \" , num_acc)\n", + " print(\"accuracy_score : \" , acc)\n", + " print(\"precision_score : \" , prec)\n", + " print(\"recall_score : \", recall)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 47886)" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "count_vectorizer = CountVectorizer(stop_words='english')\n", + "\n", + "feature_vector = count_vectorizer.fit_transform(X)\n", + "\n", + "feature_vector.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " (0, 15329)\t1\n", + " (0, 28116)\t1\n", + " (0, 39106)\t1\n", + " (0, 42005)\t1\n", + " (0, 14627)\t1\n", + " (0, 19453)\t1\n", + " (0, 34423)\t1\n", + " (0, 7888)\t1\n", + " (0, 30095)\t1\n", + " (0, 40200)\t1\n", + " (0, 19281)\t1\n", + " (0, 36984)\t1\n", + " (0, 17534)\t1\n", + " (0, 9770)\t1\n", + " (0, 42720)\t1\n", + " (0, 20178)\t1\n", + " (0, 9116)\t1\n", + " (0, 12475)\t1\n", + " (0, 9188)\t1\n", + " (0, 9127)\t1\n", + " (0, 2428)\t1\n", + " (0, 786)\t1\n", + " (0, 666)\t1\n", + " (0, 39115)\t1\n", + " (0, 42819)\t1\n", + " (0, 29834)\t1\n", + " (0, 14115)\t2\n", + " (0, 24524)\t1\n", + " (0, 13596)\t1\n", + " (0, 19001)\t1\n", + " (0, 20458)\t1\n", + " (0, 9679)\t1\n", + " (0, 32870)\t1\n", + " (0, 42838)\t2\n", + " (0, 31628)\t2\n" + ] + } + ], + "source": [ + "print(feature_vector[0])" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "X_dense = feature_vector.todense()" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 47886)" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X_dense.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "x_train, x_test, y_train, y_test = train_test_split(X_dense, Y, test_size = 0.2)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "((8000, 47886), (2000, 47886))" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x_train.shape, x_test.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "((8000,), (2000,))" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y_train.shape, y_test.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "clf = GaussianNB().fit(x_train, y_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 8, 13, 4, ..., 6, 4, 13])" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y_pred = clf.predict(x_test)\n", + "y_pred" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Length of testing data: 2000\n", + "accuracy_count : 1472\n", + "accuracy_score : 0.736\n", + "precision_score : 0.746912711601029\n", + "recall_score : 0.736\n" + ] + } + ], + "source": [ + "summarize_classification(y_test, y_pred)" + ] + }, + { + "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.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/building-features-from-text-data/13c-StopwordRemoval_FrequencyFiltering_CountVectorizer_NaiveBayesClassifier.ipynb b/building-features-from-text-data/13c-StopwordRemoval_FrequencyFiltering_CountVectorizer_NaiveBayesClassifier.ipynb new file mode 100644 index 0000000..c3c18e8 --- /dev/null +++ b/building-features-from-text-data/13c-StopwordRemoval_FrequencyFiltering_CountVectorizer_NaiveBayesClassifier.ipynb @@ -0,0 +1,539 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "\n", + "from sklearn.model_selection import train_test_split\n", + "from sklearn.metrics import accuracy_score\n", + "from sklearn.metrics import precision_score\n", + "from sklearn.metrics import recall_score\n", + "\n", + "from sklearn.feature_extraction.text import CountVectorizer\n", + "from sklearn.feature_extraction import text \n", + "\n", + "from sklearn.naive_bayes import GaussianNB\n", + "\n", + "from nltk.tokenize import word_tokenize\n", + "from nltk.probability import FreqDist" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "dbpedia_df = pd.read_csv('./datasets/dbpedia_csv/train.csv', \n", + " skiprows=1, names = ['Label', 'Name', 'Text'])" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(559999, 3)" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df['Label'].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "dbpedia_df = dbpedia_df.sample(10000, replace=False)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "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", + " \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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
LabelNameText
2938168Sheep Mountain (Flathead County Montana)Sheep Mountain (8530 feet (2600 m)) is locate...
37938910Argyropelecus hemigymnusArgyropelecus hemigymnus the half-naked hatch...
55360214Double Identity (novel)Double Identity is a 2005 young adult novel b...
1045033Picaflor de los AndesVíctor Alberto Gil Mallma (1930; Huancayo - J...
3382889Jerry City OhioJerry City is a village in Wood County Ohio U...
2441407John Hosford HouseThe John Hosford House built in 1860 is an hi...
1825655Hunt DownerMajor General Huntington Blair Downer Jr. kno...
2520377Brea City Hall and ParkBrea City Hall and Park in Brea California wa...
987213Christian Jacob (musician)Christian Jacob is a lyrical jazz pianist. He...
1888155George Forbes (New Zealand politician)George William Forbes (12 March 1869 – 17 May...
\n", + "
" + ], + "text/plain": [ + " Label Name \\\n", + "293816 8 Sheep Mountain (Flathead County Montana) \n", + "379389 10 Argyropelecus hemigymnus \n", + "553602 14 Double Identity (novel) \n", + "104503 3 Picaflor de los Andes \n", + "338288 9 Jerry City Ohio \n", + "244140 7 John Hosford House \n", + "182565 5 Hunt Downer \n", + "252037 7 Brea City Hall and Park \n", + "98721 3 Christian Jacob (musician) \n", + "188815 5 George Forbes (New Zealand politician) \n", + "\n", + " Text \n", + "293816 Sheep Mountain (8530 feet (2600 m)) is locate... \n", + "379389 Argyropelecus hemigymnus the half-naked hatch... \n", + "553602 Double Identity is a 2005 young adult novel b... \n", + "104503 Víctor Alberto Gil Mallma (1930; Huancayo - J... \n", + "338288 Jerry City is a village in Wood County Ohio U... \n", + "244140 The John Hosford House built in 1860 is an hi... \n", + "182565 Major General Huntington Blair Downer Jr. kno... \n", + "252037 Brea City Hall and Park in Brea California wa... \n", + "98721 Christian Jacob is a lyrical jazz pianist. He... \n", + "188815 George William Forbes (12 March 1869 – 17 May... " + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.sample(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 3)" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "X = dbpedia_df['Text']\n", + "\n", + "Y = dbpedia_df['Label']" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "def summarize_classification(y_test, y_pred):\n", + "\n", + " acc = accuracy_score(y_test, y_pred, normalize=True)\n", + " num_acc = accuracy_score(y_test, y_pred, normalize=False)\n", + " prec = precision_score(y_test, y_pred, average='weighted')\n", + " recall = recall_score(y_test, y_pred, average='weighted')\n", + " \n", + " print(\"Length of testing data: \", len(y_test))\n", + " print(\"accuracy_count : \" , num_acc)\n", + " print(\"accuracy_score : \" , acc)\n", + " print(\"precision_score : \" , prec)\n", + " print(\"recall_score : \", recall)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "503033" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tokens = word_tokenize(\"\\n\".join(X.values))\n", + "\n", + "len(tokens)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "FreqDist({'the': 23584, '.': 23365, 'in': 16237, 'of': 15558, 'is': 13472, 'a': 13075, 'and': 12741, '(': 7078, ')': 7048, 'was': 6816, ...})" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "freq = FreqDist(tokens)\n", + "freq" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "491" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "frequent_words = []\n", + "\n", + "for key, value in freq.items():\n", + " if value >= 100:\n", + " frequent_words.append(key.lower())\n", + " \n", + "len(frequent_words)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['columbia',\n", + " '(',\n", + " ')',\n", + " 'was',\n", + " 'an',\n", + " 'american',\n", + " 'company',\n", + " 'that',\n", + " 'from',\n", + " '1994',\n", + " 'to',\n", + " '2002',\n", + " '.',\n", + " 'it',\n", + " 'operated',\n", + " 'as',\n", + " 'the',\n", + " 'third',\n", + " 'name',\n", + " 'of',\n", + " 'early',\n", + " 'studio',\n", + " 'and',\n", + " 'part',\n", + " 'second']" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "frequent_words[:25]" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "stop_words = text.ENGLISH_STOP_WORDS.union(frequent_words)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/anaconda3/lib/python3.7/site-packages/sklearn/feature_extraction/text.py:301: UserWarning: Your stop_words may be inconsistent with your preprocessing. Tokenizing the stop words generated tokens ['st'] not in stop_words.\n", + " 'stop_words.' % sorted(inconsistent))\n" + ] + }, + { + "data": { + "text/plain": [ + "(10000, 47434)" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "count_vectorizer = CountVectorizer(stop_words=stop_words)\n", + "\n", + "feature_vector = count_vectorizer.fit_transform(X)\n", + "\n", + "feature_vector.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "X_dense = feature_vector.todense()" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 47434)" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X_dense.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "x_train, x_test, y_train, y_test = train_test_split(X_dense, Y, test_size = 0.2)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "clf = GaussianNB().fit(x_train, y_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([13, 9, 13, ..., 7, 11, 5])" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y_pred = clf.predict(x_test)\n", + "y_pred" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Length of testing data: 2000\n", + "accuracy_count : 1397\n", + "accuracy_score : 0.6985\n", + "precision_score : 0.7009864007615241\n", + "recall_score : 0.6985\n" + ] + } + ], + "source": [ + "summarize_classification(y_test, y_pred)" + ] + }, + { + "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.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/building-features-from-text-data/13d-Bigrams_CountVectorizer_NaiveBayesClassifier.ipynb b/building-features-from-text-data/13d-Bigrams_CountVectorizer_NaiveBayesClassifier.ipynb new file mode 100644 index 0000000..7d63c74 --- /dev/null +++ b/building-features-from-text-data/13d-Bigrams_CountVectorizer_NaiveBayesClassifier.ipynb @@ -0,0 +1,409 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "\n", + "from sklearn.model_selection import train_test_split\n", + "from sklearn.metrics import accuracy_score\n", + "from sklearn.metrics import precision_score\n", + "from sklearn.metrics import recall_score\n", + "\n", + "from sklearn.feature_extraction.text import CountVectorizer\n", + "from sklearn.feature_extraction import text \n", + "\n", + "from sklearn.naive_bayes import GaussianNB\n", + "\n", + "from nltk.tokenize import word_tokenize\n", + "from nltk.probability import FreqDist" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "dbpedia_df = pd.read_csv('./datasets/dbpedia_csv/train.csv', \n", + " skiprows=1, names = ['Label', 'Name', 'Text'])" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(559999, 3)" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df['Label'].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "dbpedia_df = dbpedia_df.sample(10000, replace=False)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "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", + " \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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
LabelNameText
42752011Guzmania remyiGuzmania remyi is a species of plant in the B...
52214514Villa AuroreVilla Aurore is a novel written in French by ...
46255212Monk's CasinoMonk's Casino is a live album by German free ...
24601Kiss TechnologyKiss Technology is an entertainment technolog...
837433S.M. ZakirS.M. Zakir (born 4 February 1969 in Kota Bhar...
55488714New York Law JournalThe New York Law Journal founded in 1888 is a...
2708577Latham United Methodist ChurchLatham United Methodist Church is a historic ...
3239899NalavadiNalavadi is a village in Dharwad district in ...
1933525Richard Ottley (judge)Sir Richard Ottley was the 5th Chief Justice ...
781042Umatilla High School (Oregon)Umatilla High School is a public high school ...
\n", + "
" + ], + "text/plain": [ + " Label Name \\\n", + "427520 11 Guzmania remyi \n", + "522145 14 Villa Aurore \n", + "462552 12 Monk's Casino \n", + "2460 1 Kiss Technology \n", + "83743 3 S.M. Zakir \n", + "554887 14 New York Law Journal \n", + "270857 7 Latham United Methodist Church \n", + "323989 9 Nalavadi \n", + "193352 5 Richard Ottley (judge) \n", + "78104 2 Umatilla High School (Oregon) \n", + "\n", + " Text \n", + "427520 Guzmania remyi is a species of plant in the B... \n", + "522145 Villa Aurore is a novel written in French by ... \n", + "462552 Monk's Casino is a live album by German free ... \n", + "2460 Kiss Technology is an entertainment technolog... \n", + "83743 S.M. Zakir (born 4 February 1969 in Kota Bhar... \n", + "554887 The New York Law Journal founded in 1888 is a... \n", + "270857 Latham United Methodist Church is a historic ... \n", + "323989 Nalavadi is a village in Dharwad district in ... \n", + "193352 Sir Richard Ottley was the 5th Chief Justice ... \n", + "78104 Umatilla High School is a public high school ... " + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.sample(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 3)" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "X = dbpedia_df['Text']\n", + "\n", + "Y = dbpedia_df['Label']" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "def summarize_classification(y_test, y_pred):\n", + "\n", + " acc = accuracy_score(y_test, y_pred, normalize=True)\n", + " num_acc = accuracy_score(y_test, y_pred, normalize=False)\n", + " prec = precision_score(y_test, y_pred, average='weighted')\n", + " recall = recall_score(y_test, y_pred, average='weighted')\n", + " \n", + " print(\"Length of testing data: \", len(y_test))\n", + " print(\"accuracy_count : \" , num_acc)\n", + " print(\"accuracy_score : \" , acc)\n", + " print(\"precision_score : \" , prec)\n", + " print(\"recall_score : \", recall)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 217709)" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "count_vectorizer = CountVectorizer(ngram_range=(2, 2))\n", + "\n", + "feature_vector = count_vectorizer.fit_transform(X)\n", + "\n", + "feature_vector.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "X_dense = feature_vector.todense()" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 217709)" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X_dense.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "x_train, x_test, y_train, y_test = train_test_split(X_dense, Y, test_size = 0.2)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "clf = GaussianNB().fit(x_train, y_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([2, 1, 5, ..., 4, 7, 8])" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y_pred = clf.predict(x_test)\n", + "y_pred" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Length of testing data: 2000\n", + "accuracy_count : 1812\n", + "accuracy_score : 0.906\n", + "precision_score : 0.9071432275572098\n", + "recall_score : 0.906\n" + ] + } + ], + "source": [ + "summarize_classification(y_test, y_pred)" + ] + }, + { + "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.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/building-features-from-text-data/14-TfidfVectorizer_NaiveBayesClassifier.ipynb b/building-features-from-text-data/14-TfidfVectorizer_NaiveBayesClassifier.ipynb new file mode 100644 index 0000000..d28c93c --- /dev/null +++ b/building-features-from-text-data/14-TfidfVectorizer_NaiveBayesClassifier.ipynb @@ -0,0 +1,405 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "\n", + "from sklearn.model_selection import train_test_split\n", + "from sklearn.metrics import accuracy_score\n", + "from sklearn.metrics import precision_score\n", + "from sklearn.metrics import recall_score\n", + "\n", + "from sklearn.feature_extraction.text import TfidfVectorizer\n", + "\n", + "from sklearn.naive_bayes import GaussianNB" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "dbpedia_df = pd.read_csv('./datasets/dbpedia_csv/train.csv', \n", + " skiprows=1, names = ['Label', 'Name', 'Text'])" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(559999, 3)" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df['Label'].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "dbpedia_df = dbpedia_df.sample(10000, replace=False)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "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", + " \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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
LabelNameText
2524057Prowers BridgeThe Prowers Bridge over the Arkansas River ne...
50445713Que sera sera (film)Que sera sera (Portuguese: Seja o que Deus Qu...
2704947The Esplanade (Kenner Louisiana)The Esplanade also known as the Esplanade Mal...
54605814The 1974 Annual World's Best SFThe 1974 Annual World's Best SF is an antholo...
46127912Just Go (album)Just Go is the ninth studio album by American...
2771437Steyning Methodist ChurchSteyning Methodist Church is a Methodist plac...
2668677Smith Estate (Ridge New York)Smith Estate also known as Longwood Estate - ...
2837128Yr EiflYr Eifl is a mountain on the north coast of t...
338611IBM IndiaIBM India Private Limited is the Indian subsi...
709532Loreto College of Rose-HillLoreto College Rose Hill is a private seconda...
\n", + "
" + ], + "text/plain": [ + " Label Name \\\n", + "252405 7 Prowers Bridge \n", + "504457 13 Que sera sera (film) \n", + "270494 7 The Esplanade (Kenner Louisiana) \n", + "546058 14 The 1974 Annual World's Best SF \n", + "461279 12 Just Go (album) \n", + "277143 7 Steyning Methodist Church \n", + "266867 7 Smith Estate (Ridge New York) \n", + "283712 8 Yr Eifl \n", + "33861 1 IBM India \n", + "70953 2 Loreto College of Rose-Hill \n", + "\n", + " Text \n", + "252405 The Prowers Bridge over the Arkansas River ne... \n", + "504457 Que sera sera (Portuguese: Seja o que Deus Qu... \n", + "270494 The Esplanade also known as the Esplanade Mal... \n", + "546058 The 1974 Annual World's Best SF is an antholo... \n", + "461279 Just Go is the ninth studio album by American... \n", + "277143 Steyning Methodist Church is a Methodist plac... \n", + "266867 Smith Estate also known as Longwood Estate - ... \n", + "283712 Yr Eifl is a mountain on the north coast of t... \n", + "33861 IBM India Private Limited is the Indian subsi... \n", + "70953 Loreto College Rose Hill is a private seconda... " + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.sample(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 3)" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "X = dbpedia_df['Text']\n", + "\n", + "Y = dbpedia_df['Label']" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "def summarize_classification(y_test, y_pred):\n", + "\n", + " acc = accuracy_score(y_test, y_pred, normalize=True)\n", + " num_acc = accuracy_score(y_test, y_pred, normalize=False)\n", + " prec = precision_score(y_test, y_pred, average='weighted')\n", + " recall = recall_score(y_test, y_pred, average='weighted')\n", + " \n", + " print(\"Length of testing data: \", len(y_test))\n", + " print(\"accuracy_count : \" , num_acc)\n", + " print(\"accuracy_score : \" , acc)\n", + " print(\"precision_score : \" , prec)\n", + " print(\"recall_score : \", recall)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 48401)" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tfidf_vectorizer = TfidfVectorizer()\n", + "\n", + "feature_vector = tfidf_vectorizer.fit_transform(X)\n", + "\n", + "feature_vector.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "X_dense = feature_vector.todense()" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 48401)" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X_dense.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "x_train, x_test, y_train, y_test = train_test_split(X_dense, Y, test_size = 0.2)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "clf = GaussianNB().fit(x_train, y_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([10, 7, 7, ..., 7, 4, 12])" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y_pred = clf.predict(x_test)\n", + "y_pred" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Length of testing data: 2000\n", + "accuracy_count : 1398\n", + "accuracy_score : 0.699\n", + "precision_score : 0.7100351307095724\n", + "recall_score : 0.699\n" + ] + } + ], + "source": [ + "summarize_classification(y_test, y_pred)" + ] + }, + { + "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.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/building-features-from-text-data/15a-CountVectorizer_TfidfTransformer_NaiveBayesClassifier.ipynb b/building-features-from-text-data/15a-CountVectorizer_TfidfTransformer_NaiveBayesClassifier.ipynb new file mode 100644 index 0000000..b95d42a --- /dev/null +++ b/building-features-from-text-data/15a-CountVectorizer_TfidfTransformer_NaiveBayesClassifier.ipynb @@ -0,0 +1,430 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "\n", + "from sklearn.model_selection import train_test_split\n", + "from sklearn.metrics import accuracy_score\n", + "from sklearn.metrics import precision_score\n", + "from sklearn.metrics import recall_score\n", + "\n", + "from sklearn.feature_extraction.text import CountVectorizer\n", + "from sklearn.feature_extraction.text import TfidfTransformer\n", + "\n", + "from sklearn.naive_bayes import GaussianNB" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "dbpedia_df = pd.read_csv('./datasets/dbpedia_csv/train.csv', \n", + " skiprows=1, names = ['Label', 'Name', 'Text'])" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(559999, 3)" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df['Label'].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "dbpedia_df = dbpedia_df.sample(10000, replace=False)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "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", + " \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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
LabelNameText
44587912Lucas (album)Lucas is the second album by Ghostly Internat...
1208544Pat McGeheePatrick Henry McGehee (July 2 1888 – December...
44960812One Too Many HeartsOne Too Many Hearts is the third EP by Americ...
1715125Arken ArystanovArkén Kenesbékovich Arystánov (Kazakh: Аркен ...
268681Cineplex Odeon FilmsCineplex Odeon Films (also known as Cineplex ...
40249811SteirachneSteirachne is a genus of grass in the Poaceae...
1023533Harry ThumannHarry Thumann (28 February 1952 – 2001) was a...
3373859Chah SheykhChah Sheykh (Persian: چاه شيخ‎ also Romanized...
48517613Emmtan-MaganEm Magan is a 2006 Tamil drama film directed ...
2951298Hot Springs RangeThe Hot Springs Range is a mountain range in ...
\n", + "
" + ], + "text/plain": [ + " Label Name \\\n", + "445879 12 Lucas (album) \n", + "120854 4 Pat McGehee \n", + "449608 12 One Too Many Hearts \n", + "171512 5 Arken Arystanov \n", + "26868 1 Cineplex Odeon Films \n", + "402498 11 Steirachne \n", + "102353 3 Harry Thumann \n", + "337385 9 Chah Sheykh \n", + "485176 13 Emmtan-Magan \n", + "295129 8 Hot Springs Range \n", + "\n", + " Text \n", + "445879 Lucas is the second album by Ghostly Internat... \n", + "120854 Patrick Henry McGehee (July 2 1888 – December... \n", + "449608 One Too Many Hearts is the third EP by Americ... \n", + "171512 Arkén Kenesbékovich Arystánov (Kazakh: Аркен ... \n", + "26868 Cineplex Odeon Films (also known as Cineplex ... \n", + "402498 Steirachne is a genus of grass in the Poaceae... \n", + "102353 Harry Thumann (28 February 1952 – 2001) was a... \n", + "337385 Chah Sheykh (Persian: چاه شيخ‎ also Romanized... \n", + "485176 Em Magan is a 2006 Tamil drama film directed ... \n", + "295129 The Hot Springs Range is a mountain range in ... " + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.sample(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 3)" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "X = dbpedia_df['Text']\n", + "\n", + "Y = dbpedia_df['Label']" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "def summarize_classification(y_test, y_pred):\n", + "\n", + " acc = accuracy_score(y_test, y_pred, normalize=True)\n", + " num_acc = accuracy_score(y_test, y_pred, normalize=False)\n", + " prec = precision_score(y_test, y_pred, average='weighted')\n", + " recall = recall_score(y_test, y_pred, average='weighted')\n", + " \n", + " print(\"Length of testing data: \", len(y_test))\n", + " print(\"accuracy_count : \" , num_acc)\n", + " print(\"accuracy_score : \" , acc)\n", + " print(\"precision_score : \" , prec)\n", + " print(\"recall_score : \", recall)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 48318)" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "count_vectorizer = CountVectorizer()\n", + "\n", + "feature_vector = count_vectorizer.fit_transform(X)\n", + "\n", + "feature_vector.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 48318)" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tfidf_transformer = TfidfTransformer()\n", + "\n", + "feature_vector = tfidf_transformer.fit_transform(feature_vector)\n", + "\n", + "feature_vector.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "X_dense = feature_vector.todense()" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 48318)" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X_dense.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "x_train, x_test, y_train, y_test = train_test_split(X_dense, Y, test_size = 0.2)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "clf = GaussianNB().fit(x_train, y_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 1, 1, 3, ..., 12, 10, 5])" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y_pred = clf.predict(x_test)\n", + "y_pred" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Length of testing data: 2000\n", + "accuracy_count : 1438\n", + "accuracy_score : 0.719\n", + "precision_score : 0.7284791775245764\n", + "recall_score : 0.719\n" + ] + } + ], + "source": [ + "summarize_classification(y_test, y_pred)" + ] + }, + { + "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.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/building-features-from-text-data/15b-FrequencyFiltering_CountVectorizer_TfidfTransformer_NaiveBayesClassifier.ipynb b/building-features-from-text-data/15b-FrequencyFiltering_CountVectorizer_TfidfTransformer_NaiveBayesClassifier.ipynb new file mode 100644 index 0000000..283554b --- /dev/null +++ b/building-features-from-text-data/15b-FrequencyFiltering_CountVectorizer_TfidfTransformer_NaiveBayesClassifier.ipynb @@ -0,0 +1,430 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "\n", + "from sklearn.model_selection import train_test_split\n", + "from sklearn.metrics import accuracy_score\n", + "from sklearn.metrics import precision_score\n", + "from sklearn.metrics import recall_score\n", + "\n", + "from sklearn.feature_extraction.text import CountVectorizer\n", + "from sklearn.feature_extraction.text import TfidfTransformer\n", + "\n", + "from sklearn.naive_bayes import GaussianNB" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "dbpedia_df = pd.read_csv('./datasets/dbpedia_csv/train.csv', \n", + " skiprows=1, names = ['Label', 'Name', 'Text'])" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(559999, 3)" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df['Label'].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "dbpedia_df = dbpedia_df.sample(10000, replace=False)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "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", + " \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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
LabelNameText
51037813Unknown (2006 film)Unknown is a 2006 American crime-thriller fil...
51066513World Without SunWorld Without Sun (French: Le Monde sans sole...
39265510Argyresthia pseudotsugaArgyresthia pseudotsuga is a moth of the Ypon...
41982711Erythronium multiscapoideumErythronium multiscapoideum is a species of f...
2178676Simca 5The Simca 5 is a small Franco-Italian passeng...
37740810Bucculatrix zophopastaBucculatrix zophopasta is a moth in the Buccu...
2324856HMS Ajax (F114)HMS Ajax was a Leander-class frigate of the R...
3271959Ruś Ostróda CountyRuś [ruɕ] (German Reussen) is a village in th...
47829312Mortal Kombat: The AlbumMortal Kombat: The Album is an album by The I...
53336014Makers (novel)Makers is a novel by Cory Doctorow. It was re...
\n", + "
" + ], + "text/plain": [ + " Label Name \\\n", + "510378 13 Unknown (2006 film) \n", + "510665 13 World Without Sun \n", + "392655 10 Argyresthia pseudotsuga \n", + "419827 11 Erythronium multiscapoideum \n", + "217867 6 Simca 5 \n", + "377408 10 Bucculatrix zophopasta \n", + "232485 6 HMS Ajax (F114) \n", + "327195 9 Ruś Ostróda County \n", + "478293 12 Mortal Kombat: The Album \n", + "533360 14 Makers (novel) \n", + "\n", + " Text \n", + "510378 Unknown is a 2006 American crime-thriller fil... \n", + "510665 World Without Sun (French: Le Monde sans sole... \n", + "392655 Argyresthia pseudotsuga is a moth of the Ypon... \n", + "419827 Erythronium multiscapoideum is a species of f... \n", + "217867 The Simca 5 is a small Franco-Italian passeng... \n", + "377408 Bucculatrix zophopasta is a moth in the Buccu... \n", + "232485 HMS Ajax was a Leander-class frigate of the R... \n", + "327195 Ruś [ruɕ] (German Reussen) is a village in th... \n", + "478293 Mortal Kombat: The Album is an album by The I... \n", + "533360 Makers is a novel by Cory Doctorow. It was re... " + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.sample(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 3)" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "X = dbpedia_df['Text']\n", + "\n", + "Y = dbpedia_df['Label']" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "def summarize_classification(y_test, y_pred):\n", + "\n", + " acc = accuracy_score(y_test, y_pred, normalize=True)\n", + " num_acc = accuracy_score(y_test, y_pred, normalize=False)\n", + " prec = precision_score(y_test, y_pred, average='weighted')\n", + " recall = recall_score(y_test, y_pred, average='weighted')\n", + " \n", + " print(\"Length of testing data: \", len(y_test))\n", + " print(\"accuracy_count : \" , num_acc)\n", + " print(\"accuracy_score : \" , acc)\n", + " print(\"precision_score : \" , prec)\n", + " print(\"recall_score : \", recall)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 47544)" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "count_vectorizer = CountVectorizer(min_df=0, max_df=80)\n", + "\n", + "feature_vector = count_vectorizer.fit_transform(X)\n", + "\n", + "feature_vector.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 47544)" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tfidf_transformer = TfidfTransformer()\n", + "\n", + "feature_vector = tfidf_transformer.fit_transform(feature_vector)\n", + "\n", + "feature_vector.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [], + "source": [ + "X_dense = feature_vector.todense()" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 47544)" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X_dense.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "x_train, x_test, y_train, y_test = train_test_split(X_dense, Y, test_size = 0.2)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [], + "source": [ + "clf = GaussianNB().fit(x_train, y_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([10, 4, 1, ..., 13, 6, 2])" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y_pred = clf.predict(x_test)\n", + "y_pred" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Length of testing data: 2000\n", + "accuracy_count : 1316\n", + "accuracy_score : 0.658\n", + "precision_score : 0.6650727642789968\n", + "recall_score : 0.658\n" + ] + } + ], + "source": [ + "summarize_classification(y_test, y_pred)" + ] + }, + { + "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.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/building-features-from-text-data/15c-StopwordRemoval_FrequencyFiltering_CountVectorizer_NaiveBayesClassifier.ipynb b/building-features-from-text-data/15c-StopwordRemoval_FrequencyFiltering_CountVectorizer_NaiveBayesClassifier.ipynb new file mode 100644 index 0000000..6501164 --- /dev/null +++ b/building-features-from-text-data/15c-StopwordRemoval_FrequencyFiltering_CountVectorizer_NaiveBayesClassifier.ipynb @@ -0,0 +1,556 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "\n", + "from sklearn.model_selection import train_test_split\n", + "from sklearn.metrics import accuracy_score\n", + "from sklearn.metrics import precision_score\n", + "from sklearn.metrics import recall_score\n", + "\n", + "from sklearn.feature_extraction.text import CountVectorizer\n", + "from sklearn.feature_extraction.text import TfidfTransformer\n", + "from sklearn.feature_extraction import text \n", + "\n", + "from sklearn.naive_bayes import GaussianNB\n", + "\n", + "from nltk.tokenize import word_tokenize\n", + "from nltk.probability import FreqDist" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "dbpedia_df = pd.read_csv('./datasets/dbpedia_csv/train.csv', \n", + " skiprows=1, names = ['Label', 'Name', 'Text'])" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(559999, 3)" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df['Label'].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "dbpedia_df = dbpedia_df.sample(10000, replace=False)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "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", + " \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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
LabelNameText
1799755Ramreddy Damodar ReddyRamreddy Damodar Reddy (Telugu: రాంరెడ్డి దామ...
55867214Sonora ReviewSonora Review is a biannual graduate student-...
50349213Luz en el páramoLuz en el páramo is a 1953 Venezuelan film di...
409802Oswaldo Cruz FoundationThe Oswaldo Cruz Foundation (Portuguese Funda...
1990655Tate ReevesJonathon Tate Reeves (born June 5 1974) a Rep...
203731Indie BoyzIndie Boyz is a European company based in Lon...
1311924Hanna MazgunovaHanna Mazgunova (Belarusian: Ганна Мазгунова;...
43838911PlatystemonPlatystemon is a monotypic genus of flowering...
2952338Paddys River (South West Slopes New South Wales)Paddys River a watercourse of the Murray catc...
48140113The Ragged Edge (film)The Ragged Edge is a lost 1923 silent film So...
\n", + "
" + ], + "text/plain": [ + " Label Name \\\n", + "179975 5 Ramreddy Damodar Reddy \n", + "558672 14 Sonora Review \n", + "503492 13 Luz en el páramo \n", + "40980 2 Oswaldo Cruz Foundation \n", + "199065 5 Tate Reeves \n", + "20373 1 Indie Boyz \n", + "131192 4 Hanna Mazgunova \n", + "438389 11 Platystemon \n", + "295233 8 Paddys River (South West Slopes New South Wales) \n", + "481401 13 The Ragged Edge (film) \n", + "\n", + " Text \n", + "179975 Ramreddy Damodar Reddy (Telugu: రాంరెడ్డి దామ... \n", + "558672 Sonora Review is a biannual graduate student-... \n", + "503492 Luz en el páramo is a 1953 Venezuelan film di... \n", + "40980 The Oswaldo Cruz Foundation (Portuguese Funda... \n", + "199065 Jonathon Tate Reeves (born June 5 1974) a Rep... \n", + "20373 Indie Boyz is a European company based in Lon... \n", + "131192 Hanna Mazgunova (Belarusian: Ганна Мазгунова;... \n", + "438389 Platystemon is a monotypic genus of flowering... \n", + "295233 Paddys River a watercourse of the Murray catc... \n", + "481401 The Ragged Edge is a lost 1923 silent film So... " + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.sample(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 3)" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "X = dbpedia_df['Text']\n", + "\n", + "Y = dbpedia_df['Label']" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "def summarize_classification(y_test, y_pred):\n", + "\n", + " acc = accuracy_score(y_test, y_pred, normalize=True)\n", + " num_acc = accuracy_score(y_test, y_pred, normalize=False)\n", + " prec = precision_score(y_test, y_pred, average='weighted')\n", + " recall = recall_score(y_test, y_pred, average='weighted')\n", + " \n", + " print(\"Length of testing data: \", len(y_test))\n", + " print(\"accuracy_count : \" , num_acc)\n", + " print(\"accuracy_score : \" , acc)\n", + " print(\"precision_score : \" , prec)\n", + " print(\"recall_score : \", recall)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "504495" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tokens = word_tokenize(\"\\n\".join(X.values))\n", + "\n", + "len(tokens)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "FreqDist({'the': 23494, '.': 23437, 'in': 16379, 'of': 15332, 'is': 13499, 'and': 12934, 'a': 12862, '(': 7204, ')': 7199, 'was': 6950, ...})" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "freq = FreqDist(tokens)\n", + "freq" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "487" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "frequent_words = []\n", + "\n", + "for key, value in freq.items():\n", + " if value >= 100:\n", + " frequent_words.append(key.lower())\n", + " \n", + "len(frequent_words)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['(',\n", + " ':',\n", + " '[',\n", + " ']',\n", + " '.',\n", + " ')',\n", + " 'is',\n", + " 'a',\n", + " 'lake',\n", + " 'in',\n", + " 'located',\n", + " 'north',\n", + " 'of',\n", + " 'the',\n", + " 'town',\n", + " 'district',\n", + " 'not',\n", + " '1',\n", + " 'the',\n", + " 'area',\n", + " 'by',\n", + " 'including',\n", + " '2001',\n", + " 'was',\n", + " 'international']" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "frequent_words[:25]" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "stop_words = text.ENGLISH_STOP_WORDS.union(frequent_words)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 47573)" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "count_vectorizer = CountVectorizer(stop_words=stop_words)\n", + "\n", + "feature_vector = count_vectorizer.fit_transform(X)\n", + "\n", + "feature_vector.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 47573)" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tfidf_transformer = TfidfTransformer()\n", + "\n", + "feature_vector = tfidf_transformer.fit_transform(feature_vector)\n", + "\n", + "feature_vector.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "X_dense = feature_vector.todense()" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 47573)" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X_dense.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [], + "source": [ + "x_train, x_test, y_train, y_test = train_test_split(X_dense, Y, test_size = 0.2)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "clf = GaussianNB().fit(x_train, y_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 7, 7, 1, ..., 13, 14, 11])" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y_pred = clf.predict(x_test)\n", + "y_pred" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Length of testing data: 2000\n", + "accuracy_count : 1335\n", + "accuracy_score : 0.6675\n", + "precision_score : 0.6803619205404001\n", + "recall_score : 0.6675\n" + ] + } + ], + "source": [ + "summarize_classification(y_test, y_pred)" + ] + }, + { + "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.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/building-features-from-text-data/15d-Bigrams_CountVectorizer_TfidfTransformer_NaiveBayesClassifier.ipynb b/building-features-from-text-data/15d-Bigrams_CountVectorizer_TfidfTransformer_NaiveBayesClassifier.ipynb new file mode 100644 index 0000000..e14aa0a --- /dev/null +++ b/building-features-from-text-data/15d-Bigrams_CountVectorizer_TfidfTransformer_NaiveBayesClassifier.ipynb @@ -0,0 +1,430 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "\n", + "from sklearn.model_selection import train_test_split\n", + "from sklearn.metrics import accuracy_score\n", + "from sklearn.metrics import precision_score\n", + "from sklearn.metrics import recall_score\n", + "\n", + "from sklearn.feature_extraction.text import CountVectorizer\n", + "from sklearn.feature_extraction.text import TfidfTransformer\n", + "\n", + "from sklearn.naive_bayes import GaussianNB" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "dbpedia_df = pd.read_csv('./datasets/dbpedia_csv/train.csv', \n", + " skiprows=1, names = ['Label', 'Name', 'Text'])" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(559999, 3)" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df['Label'].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "dbpedia_df = dbpedia_df.sample(10000, replace=False)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "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", + " \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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
LabelNameText
52170514Woodstock (novel)Woodstock or The Cavalier. A Tale of the Year...
37369310CryptophagidaeCryptophagidae is a family of beetles with re...
3339349Dehliz-e YekDehliz-e Yek (Persian: دهليزيك‎ also Romanize...
1253054Roland OsabuteyRoland Osabutey (born 11 March 1980) is a Gha...
1008663Ann VoskampAnn Voskamp (born August 10 1973 in Listowel ...
583202Robert Bateman High SchoolRobert Bateman High School (also known as Rob...
41947011Vriesea languidaVriesea languida is a species of the genus Vr...
1644305Gilbert Wellington OstromGilbert Wellington Ostrom (June 1837 – Decemb...
42609811Banara regiaBanara regia is a species of plant in the Sal...
2812368Calder River (Western Australia)For other Rivers Calder see River Calder (dis...
\n", + "
" + ], + "text/plain": [ + " Label Name \\\n", + "521705 14 Woodstock (novel) \n", + "373693 10 Cryptophagidae \n", + "333934 9 Dehliz-e Yek \n", + "125305 4 Roland Osabutey \n", + "100866 3 Ann Voskamp \n", + "58320 2 Robert Bateman High School \n", + "419470 11 Vriesea languida \n", + "164430 5 Gilbert Wellington Ostrom \n", + "426098 11 Banara regia \n", + "281236 8 Calder River (Western Australia) \n", + "\n", + " Text \n", + "521705 Woodstock or The Cavalier. A Tale of the Year... \n", + "373693 Cryptophagidae is a family of beetles with re... \n", + "333934 Dehliz-e Yek (Persian: دهليزيك‎ also Romanize... \n", + "125305 Roland Osabutey (born 11 March 1980) is a Gha... \n", + "100866 Ann Voskamp (born August 10 1973 in Listowel ... \n", + "58320 Robert Bateman High School (also known as Rob... \n", + "419470 Vriesea languida is a species of the genus Vr... \n", + "164430 Gilbert Wellington Ostrom (June 1837 – Decemb... \n", + "426098 Banara regia is a species of plant in the Sal... \n", + "281236 For other Rivers Calder see River Calder (dis... " + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.sample(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 3)" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dbpedia_df.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "X = dbpedia_df['Text']\n", + "\n", + "Y = dbpedia_df['Label']" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "def summarize_classification(y_test, y_pred):\n", + "\n", + " acc = accuracy_score(y_test, y_pred, normalize=True)\n", + " num_acc = accuracy_score(y_test, y_pred, normalize=False)\n", + " prec = precision_score(y_test, y_pred, average='weighted')\n", + " recall = recall_score(y_test, y_pred, average='weighted')\n", + " \n", + " print(\"Length of testing data: \", len(y_test))\n", + " print(\"accuracy_count : \" , num_acc)\n", + " print(\"accuracy_score : \" , acc)\n", + " print(\"precision_score : \" , prec)\n", + " print(\"recall_score : \", recall)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 217434)" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "count_vectorizer = CountVectorizer(min_df=0, max_df=80, ngram_range=(2, 2))\n", + "\n", + "feature_vector = count_vectorizer.fit_transform(X)\n", + "\n", + "feature_vector.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 217434)" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tfidf_transformer = TfidfTransformer()\n", + "\n", + "feature_vector = tfidf_transformer.fit_transform(feature_vector)\n", + "\n", + "feature_vector.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "X_dense = feature_vector.todense()" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10000, 217434)" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X_dense.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "x_train, x_test, y_train, y_test = train_test_split(X_dense, Y, test_size = 0.2)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "clf = GaussianNB().fit(x_train, y_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 7, 12, 6, ..., 1, 12, 3])" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y_pred = clf.predict(x_test)\n", + "y_pred" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Length of testing data: 2000\n", + "accuracy_count : 1778\n", + "accuracy_score : 0.889\n", + "precision_score : 0.8902761517734769\n", + "recall_score : 0.889\n" + ] + } + ], + "source": [ + "summarize_classification(y_test, y_pred)" + ] + }, + { + "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.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}