Repository: dipanjanS/nlp_essentials Branch: master Commit: e4793b51aaba Files: 8 Total size: 599.1 KB Directory structure: gitextract_ngxsk4bi/ ├── .gitignore ├── LICENSE ├── README.md └── notebooks/ ├── 01_Text_Wrangling_Examples.ipynb ├── 02_Text_Representation_Statistical_Models.ipynb ├── 03_Text_Representation_Embedding_Models.ipynb ├── 04_NLP_Applications_Text_Similarity_Content_Recommenders.ipynb └── 05_NLP_Applications_Predicting_E_Commerce_Product_Recommendation_Ratings_from_Reviews_.ipynb ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # C extensions *.so # Distribution / packaging .Python build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ wheels/ pip-wheel-metadata/ share/python-wheels/ *.egg-info/ .installed.cfg *.egg MANIFEST # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .nox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *.cover *.py,cover .hypothesis/ .pytest_cache/ # Translations *.mo *.pot # Django stuff: *.log local_settings.py db.sqlite3 db.sqlite3-journal # Flask stuff: instance/ .webassets-cache # Scrapy stuff: .scrapy # Sphinx documentation docs/_build/ # PyBuilder target/ # Jupyter Notebook .ipynb_checkpoints # IPython profile_default/ ipython_config.py # pyenv .python-version # pipenv # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. # However, in case of collaboration, if having platform-specific dependencies or dependencies # having no cross-platform support, pipenv may install dependencies that don't work, or not # install all needed dependencies. #Pipfile.lock # PEP 582; used by e.g. github.com/David-OConnor/pyflow __pypackages__/ # Celery stuff celerybeat-schedule celerybeat.pid # SageMath parsed files *.sage.py # Environments .env .venv env/ venv/ ENV/ env.bak/ venv.bak/ # Spyder project settings .spyderproject .spyproject # Rope project settings .ropeproject # mkdocs documentation /site # mypy .mypy_cache/ .dmypy.json dmypy.json # Pyre type checker .pyre/ ================================================ FILE: LICENSE ================================================ GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . ================================================ FILE: README.md ================================================ # NLP Essentials Essential and Fundametal aspects of Natural Language Processing with hands-on examples and case-studies ================================================ FILE: notebooks/01_Text_Wrangling_Examples.ipynb ================================================ { "nbformat": 4, "nbformat_minor": 0, "metadata": { "accelerator": "GPU", "anaconda-cloud": {}, "colab": { "name": "01 - Text Wrangling Examples.ipynb", "provenance": [], "collapsed_sections": [] }, "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.6" } }, "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "NtCf5TIaJpEr" }, "source": [ "# Install Dependencies" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "ceSG71XiJoka", "colab": { "base_uri": "https://localhost:8080/", "height": 185 }, "outputId": "c1fa4f8d-9143-42bc-ef76-07166fc0710c" }, "source": [ "import nltk\n", "nltk.download('punkt')\n", "nltk.download('wordnet')\n", "nltk.download('stopwords')\n", "nltk.download('averaged_perceptron_tagger')" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "[nltk_data] Downloading package punkt to /root/nltk_data...\n", "[nltk_data] Unzipping tokenizers/punkt.zip.\n", "[nltk_data] Downloading package wordnet to /root/nltk_data...\n", "[nltk_data] Unzipping corpora/wordnet.zip.\n", "[nltk_data] Downloading package stopwords to /root/nltk_data...\n", "[nltk_data] Unzipping corpora/stopwords.zip.\n", "[nltk_data] Downloading package averaged_perceptron_tagger to\n", "[nltk_data] /root/nltk_data...\n", "[nltk_data] Unzipping taggers/averaged_perceptron_tagger.zip.\n" ], "name": "stdout" }, { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 1 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "AgeSwPsGJFWj" }, "source": [ "# Case Conversion" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "OQp382lJJFWp", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "199a9b30-a3ae-414b-def0-4166e17b7ab8" }, "source": [ "text = 'The quick brown fox jumped over The Big Dog'\n", "text" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'The quick brown fox jumped over The Big Dog'" ] }, "metadata": { "tags": [] }, "execution_count": 2 } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "FaAwb7HZJFWz", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "e2c4bc72-3743-4176-a42b-9720354716b8" }, "source": [ "text.lower()" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'the quick brown fox jumped over the big dog'" ] }, "metadata": { "tags": [] }, "execution_count": 3 } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "ihX9LwVuJFW4", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "34f136ca-0ea5-4531-e206-2c981ae6d6e3" }, "source": [ "text.upper()" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'THE QUICK BROWN FOX JUMPED OVER THE BIG DOG'" ] }, "metadata": { "tags": [] }, "execution_count": 4 } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "U24TBZ82JFW8", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "e3fc2b71-b4f1-4ba4-93e9-7581c85d7566" }, "source": [ "text.title()" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'The Quick Brown Fox Jumped Over The Big Dog'" ] }, "metadata": { "tags": [] }, "execution_count": 5 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "V3GzHq46JFW_" }, "source": [ "# Tokenization" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "zIiPr5JBJFXA", "colab": { "base_uri": "https://localhost:8080/", "height": 54 }, "outputId": "02958ece-7022-4a28-9591-d8caef757b87" }, "source": [ "sample_text = (\"US unveils world's most powerful supercomputer, beats China. \" \n", " \"The US has unveiled the world's most powerful supercomputer called 'Summit', \" \n", " \"beating the previous record-holder China's Sunway TaihuLight. With a peak performance \"\n", " \"of 200,000 trillion calculations per second, it is over twice as fast as Sunway TaihuLight, \"\n", " \"which is capable of 93,000 trillion calculations per second. Summit has 4,608 servers, \"\n", " \"which reportedly take up the size of two tennis courts.\")\n", "sample_text" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "\"US unveils world's most powerful supercomputer, beats China. The US has unveiled the world's most powerful supercomputer called 'Summit', beating the previous record-holder China's Sunway TaihuLight. With a peak performance of 200,000 trillion calculations per second, it is over twice as fast as Sunway TaihuLight, which is capable of 93,000 trillion calculations per second. Summit has 4,608 servers, which reportedly take up the size of two tennis courts.\"" ] }, "metadata": { "tags": [] }, "execution_count": 6 } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "i2m8nEPmJFXD", "colab": { "base_uri": "https://localhost:8080/", "height": 84 }, "outputId": "83fe372f-8901-43d1-d6f5-fed5ee27cd91" }, "source": [ "import nltk\n", "\n", "nltk.sent_tokenize(sample_text)" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[\"US unveils world's most powerful supercomputer, beats China.\",\n", " \"The US has unveiled the world's most powerful supercomputer called 'Summit', beating the previous record-holder China's Sunway TaihuLight.\",\n", " 'With a peak performance of 200,000 trillion calculations per second, it is over twice as fast as Sunway TaihuLight, which is capable of 93,000 trillion calculations per second.',\n", " 'Summit has 4,608 servers, which reportedly take up the size of two tennis courts.']" ] }, "metadata": { "tags": [] }, "execution_count": 7 } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "KjVNIwLoJFXG", "colab": { "base_uri": "https://localhost:8080/", "height": 54 }, "outputId": "093b33f0-6449-4fd0-c1ed-1dfc9bb6e318" }, "source": [ "print(nltk.word_tokenize(sample_text))" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "['US', 'unveils', 'world', \"'s\", 'most', 'powerful', 'supercomputer', ',', 'beats', 'China', '.', 'The', 'US', 'has', 'unveiled', 'the', 'world', \"'s\", 'most', 'powerful', 'supercomputer', 'called', \"'Summit\", \"'\", ',', 'beating', 'the', 'previous', 'record-holder', 'China', \"'s\", 'Sunway', 'TaihuLight', '.', 'With', 'a', 'peak', 'performance', 'of', '200,000', 'trillion', 'calculations', 'per', 'second', ',', 'it', 'is', 'over', 'twice', 'as', 'fast', 'as', 'Sunway', 'TaihuLight', ',', 'which', 'is', 'capable', 'of', '93,000', 'trillion', 'calculations', 'per', 'second', '.', 'Summit', 'has', '4,608', 'servers', ',', 'which', 'reportedly', 'take', 'up', 'the', 'size', 'of', 'two', 'tennis', 'courts', '.']\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "ZjhORAuPJFXL", "colab": {} }, "source": [ "import spacy\n", "nlp = spacy.load('en')\n", "\n", "text_spacy = nlp(sample_text)" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "DR6LA_YHJFXN", "colab": { "base_uri": "https://localhost:8080/", "height": 84 }, "outputId": "630d9f19-8658-4b3b-bbd8-069fb3594601" }, "source": [ "[obj.text for obj in text_spacy.sents]" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[\"US unveils world's most powerful supercomputer, beats China.\",\n", " \"The US has unveiled the world's most powerful supercomputer called 'Summit', beating the previous record-holder China's Sunway TaihuLight.\",\n", " 'With a peak performance of 200,000 trillion calculations per second, it is over twice as fast as Sunway TaihuLight, which is capable of 93,000 trillion calculations per second.',\n", " 'Summit has 4,608 servers, which reportedly take up the size of two tennis courts.']" ] }, "metadata": { "tags": [] }, "execution_count": 10 } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "DBuAHdR8JFXQ", "colab": { "base_uri": "https://localhost:8080/", "height": 54 }, "outputId": "f6acbbf6-e500-42c4-f06d-f1f8942ec548" }, "source": [ "print([obj.text for obj in text_spacy])" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "['US', 'unveils', 'world', \"'s\", 'most', 'powerful', 'supercomputer', ',', 'beats', 'China', '.', 'The', 'US', 'has', 'unveiled', 'the', 'world', \"'s\", 'most', 'powerful', 'supercomputer', 'called', \"'\", 'Summit', \"'\", ',', 'beating', 'the', 'previous', 'record', '-', 'holder', 'China', \"'s\", 'Sunway', 'TaihuLight', '.', 'With', 'a', 'peak', 'performance', 'of', '200,000', 'trillion', 'calculations', 'per', 'second', ',', 'it', 'is', 'over', 'twice', 'as', 'fast', 'as', 'Sunway', 'TaihuLight', ',', 'which', 'is', 'capable', 'of', '93,000', 'trillion', 'calculations', 'per', 'second', '.', 'Summit', 'has', '4,608', 'servers', ',', 'which', 'reportedly', 'take', 'up', 'the', 'size', 'of', 'two', 'tennis', 'courts', '.']\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "fhxnJkIsJFXS" }, "source": [ "# Removing HTML tags & noise" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "E3qV1WOpJFXT", "colab": { "base_uri": "https://localhost:8080/", "height": 470 }, "outputId": "82fbdc9e-df22-410d-bd33-1096b34647e6" }, "source": [ "import requests\n", "\n", "data = requests.get('http://www.gutenberg.org/cache/epub/8001/pg8001.html')\n", "content = data.text\n", "print(content[2745:3948])" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "

*** START OF THE PROJECT GUTENBERG EBOOK, THE BIBLE, KING JAMES, BOOK 1***

\r\n", "\r\n", "

This eBook was produced by David Widger\r\n", "with the help of Derek Andrew's text from January 1992\r\n", "and the work of Bryan Taylor in November 2002.

\r\n", "\r\n", "

Book 01 Genesis

\r\n", "\r\n", "

01:001:001 In the beginning God created the heaven and the earth.

\r\n", "\r\n", "

01:001:002 And the earth was without form, and void; and darkness was\r\n", " upon the face of the deep. And the Spirit of God moved upon\r\n", " the face of the waters.

\r\n", "\r\n", "

01:001:003 And God said, Let there be light: and there was light.

\r\n", "\r\n", "

01:001:004 And God saw the light, that it was good: and God divided the
\r\n", "\r\n", "           light from the darkness.
\r\n", "

\r\n", "\r\n", "

01:001:005 And God called the light Day, and the darkness he called
\r\n", "\r\n", "           Night. And the evening and the morning were the first day.
\r\n", "

\r\n", "\r\n", "

01:001:006 And God said, Let there be a firmament in the mi\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "E6UAz3mjJFXY", "colab": { "base_uri": "https://localhost:8080/", "height": 269 }, "outputId": "0549c5a6-1814-4e82-8046-d32b40647d1f" }, "source": [ "import re\n", "from bs4 import BeautifulSoup\n", "\n", "def strip_html_tags(text):\n", " soup = BeautifulSoup(text, \"html.parser\")\n", " [s.extract() for s in soup(['iframe', 'script'])]\n", " stripped_text = soup.get_text()\n", " stripped_text = re.sub(r'[\\r|\\n|\\r\\n]+', '\\n', stripped_text)\n", " return stripped_text\n", "\n", "clean_content = strip_html_tags(content)\n", "print(clean_content[1163:1957])" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "*** START OF THE PROJECT GUTENBERG EBOOK, THE BIBLE, KING JAMES, BOOK 1***\n", "This eBook was produced by David Widger\n", "with the help of Derek Andrew's text from January 1992\n", "and the work of Bryan Taylor in November 2002.\n", "Book 01 Genesis\n", "01:001:001 In the beginning God created the heaven and the earth.\n", "01:001:002 And the earth was without form, and void; and darkness was\n", " upon the face of the deep. And the Spirit of God moved upon\n", " the face of the waters.\n", "01:001:003 And God said, Let there be light: and there was light.\n", "01:001:004 And God saw the light, that it was good: and God divided the\n", "           light from the darkness.\n", "01:001:005 And God called the light Day, and the darkness he called\n", "           Night. And the evening and the morning were the first day.\n", "\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "9fJi5YyKJFXc" }, "source": [ "# Removing Accented Characters" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "Ps9wmhv9JFXd", "colab": {} }, "source": [ "import unicodedata\n", "\n", "def remove_accented_chars(text):\n", " text = unicodedata.normalize('NFKD', text).encode('ascii', 'ignore').decode('utf-8', 'ignore')\n", " return text" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "Mc7JR8CQJFXh", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "392b46f9-4945-47e0-b942-9d50de2fb3ff" }, "source": [ "s = 'Sómě Áccěntěd těxt'\n", "s" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Sómě Áccěntěd těxt'" ] }, "metadata": { "tags": [] }, "execution_count": 15 } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "I6a-e-mVJFXm", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "9073d7d9-198f-4c5c-9de9-2539cda1adcd" }, "source": [ "remove_accented_chars(s)" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Some Accented text'" ] }, "metadata": { "tags": [] }, "execution_count": 16 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "gj8CyGmPJFXr" }, "source": [ "# Removing Special Characters, Numbers and Symbols" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "1dkc4ESDJFXs", "colab": {} }, "source": [ "import re\n", "\n", "def remove_special_characters(text, remove_digits=False):\n", " pattern = r'[^a-zA-Z0-9\\s]' if not remove_digits else r'[^a-zA-Z\\s]'\n", " text = re.sub(pattern, '', text)\n", " return text\n" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "XUwKvQ-1JFXx", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "deb1477f-9de1-44fc-801b-f80d0930624f" }, "source": [ "s = \"Well this was fun! See you at 7:30, What do you think!!? #$@@9318@ 🙂🙂🙂\"\n", "s" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Well this was fun! See you at 7:30, What do you think!!? #$@@9318@ 🙂🙂🙂'" ] }, "metadata": { "tags": [] }, "execution_count": 18 } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "Sy9x4XFyJFYL", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "01596e65-1935-4095-b91a-1cd14da1ef48" }, "source": [ "remove_special_characters(s, remove_digits=True)" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Well this was fun See you at What do you think '" ] }, "metadata": { "tags": [] }, "execution_count": 19 } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "s2vT0GK5JFYQ", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "e8763d3d-6f64-4308-f39d-8487a97ffcf2" }, "source": [ "remove_special_characters(s)" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Well this was fun See you at 730 What do you think 9318 '" ] }, "metadata": { "tags": [] }, "execution_count": 20 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "ho6h68QbJFYX" }, "source": [ "# Expanding Contractions" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "mgGTT1URJFYY", "colab": { "base_uri": "https://localhost:8080/", "height": 353 }, "outputId": "2aa9f59a-9d7a-4436-9f90-0ba12a1b6016" }, "source": [ "!pip install contractions\n", "!pip install textsearch" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "Collecting contractions\n", " Downloading https://files.pythonhosted.org/packages/85/41/c3dfd5feb91a8d587ed1a59f553f07c05f95ad4e5d00ab78702fbf8fe48a/contractions-0.0.24-py2.py3-none-any.whl\n", "Collecting textsearch\n", " Downloading https://files.pythonhosted.org/packages/42/a8/03407021f9555043de5492a2bd7a35c56cc03c2510092b5ec018cae1bbf1/textsearch-0.0.17-py2.py3-none-any.whl\n", "Collecting pyahocorasick\n", "\u001b[?25l Downloading https://files.pythonhosted.org/packages/f4/9f/f0d8e8850e12829eea2e778f1c90e3c53a9a799b7f412082a5d21cd19ae1/pyahocorasick-1.4.0.tar.gz (312kB)\n", "\u001b[K |████████████████████████████████| 317kB 3.8MB/s \n", "\u001b[?25hCollecting Unidecode\n", "\u001b[?25l Downloading https://files.pythonhosted.org/packages/d0/42/d9edfed04228bacea2d824904cae367ee9efd05e6cce7ceaaedd0b0ad964/Unidecode-1.1.1-py2.py3-none-any.whl (238kB)\n", "\u001b[K |████████████████████████████████| 245kB 15.6MB/s \n", "\u001b[?25hBuilding wheels for collected packages: pyahocorasick\n", " Building wheel for pyahocorasick (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for pyahocorasick: filename=pyahocorasick-1.4.0-cp36-cp36m-linux_x86_64.whl size=81699 sha256=b766a035314d66115551991c1220d24c4a42480cc3e1f4b7ae56a50ba5e9f62a\n", " Stored in directory: /root/.cache/pip/wheels/0a/90/61/87a55f5b459792fbb2b7ba6b31721b06ff5cf6bde541b40994\n", "Successfully built pyahocorasick\n", "Installing collected packages: pyahocorasick, Unidecode, textsearch, contractions\n", "Successfully installed Unidecode-1.1.1 contractions-0.0.24 pyahocorasick-1.4.0 textsearch-0.0.17\n", "Requirement already satisfied: textsearch in /usr/local/lib/python3.6/dist-packages (0.0.17)\n", "Requirement already satisfied: Unidecode in /usr/local/lib/python3.6/dist-packages (from textsearch) (1.1.1)\n", "Requirement already satisfied: pyahocorasick in /usr/local/lib/python3.6/dist-packages (from textsearch) (1.4.0)\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "5xWsgO-jJFYc", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "b02a9709-8e7e-4174-d46c-769d73aeadda" }, "source": [ "s = \"Y'all can't expand contractions I'd think! You wouldn't be able to. How'd you do it?\"\n", "s" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "\"Y'all can't expand contractions I'd think! You wouldn't be able to. How'd you do it?\"" ] }, "metadata": { "tags": [] }, "execution_count": 22 } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "S2QTF2HFJFYi", "colab": { "base_uri": "https://localhost:8080/", "height": 185 }, "outputId": "d01e87fb-cc90-4f97-e01c-f8a76d2e1c60" }, "source": [ "import contractions\n", "\n", "list(contractions.contractions_dict.items())[:10]" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[(\"ain't\", 'are not'),\n", " (\"aren't\", 'are not'),\n", " (\"can't\", 'can not'),\n", " (\"can't've\", 'can not have'),\n", " (\"'cause\", 'because'),\n", " (\"could've\", 'could have'),\n", " (\"couldn't\", 'could not'),\n", " (\"couldn't've\", 'could not have'),\n", " (\"didn't\", 'did not'),\n", " (\"doesn't\", 'does not')]" ] }, "metadata": { "tags": [] }, "execution_count": 23 } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "KoIGJXqCJFYo", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "1cae6a7b-d520-46eb-ce81-ec50089f7ab3" }, "source": [ "contractions.fix(s)" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'you all can not expand contractions I would think! You would not be able to. how did you do it?'" ] }, "metadata": { "tags": [] }, "execution_count": 24 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "EeUHPmhDJFZC" }, "source": [ "# Stemming" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "8ndJ4XOKJFZD", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "fb53058e-bbdf-489b-f57d-fbdb7e7395c7" }, "source": [ "# Porter Stemmer\n", "from nltk.stem import PorterStemmer\n", "ps = PorterStemmer()\n", "\n", "ps.stem('jumping'), ps.stem('jumps'), ps.stem('jumped')" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "('jump', 'jump', 'jump')" ] }, "metadata": { "tags": [] }, "execution_count": 25 } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "CmWLISH-JFZG", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "4e8dcb51-121a-4f3b-8522-a7db0cf81357" }, "source": [ "ps.stem('lying')" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'lie'" ] }, "metadata": { "tags": [] }, "execution_count": 26 } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "Q7KRj1jtJFZJ", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "a1944d27-3e35-4a4e-dc10-41f0b15384bc" }, "source": [ "ps.stem('strange')" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'strang'" ] }, "metadata": { "tags": [] }, "execution_count": 27 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "cQNUmpfLJFZu" }, "source": [ "# Lemmatization" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "16ygP7t1JFZv", "colab": {} }, "source": [ "from nltk.stem import WordNetLemmatizer\n", "wnl = WordNetLemmatizer()" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "AieUIjYaJFZ3", "colab": { "base_uri": "https://localhost:8080/", "height": 84 }, "outputId": "09dc4df1-9ff9-48d4-f11a-cf63cbf6f3d9" }, "source": [ "help(wnl.lemmatize)" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "Help on method lemmatize in module nltk.stem.wordnet:\n", "\n", "lemmatize(word, pos='n') method of nltk.stem.wordnet.WordNetLemmatizer instance\n", "\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "_ZPcwz44JFZ7", "colab": { "base_uri": "https://localhost:8080/", "height": 50 }, "outputId": "c0b18e8d-d034-4241-fac2-53ff4ef56d1c" }, "source": [ "# lemmatize nouns\n", "print(wnl.lemmatize('cars', 'n'))\n", "print(wnl.lemmatize('boxes', 'n'))" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "car\n", "box\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "KJN-uQ28JFZ_", "colab": { "base_uri": "https://localhost:8080/", "height": 50 }, "outputId": "49826ee7-990d-44ee-c0d2-09b58e10fdb4" }, "source": [ "# lemmatize verbs\n", "print(wnl.lemmatize('running', 'v'))\n", "print(wnl.lemmatize('ate', 'v'))" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "run\n", "eat\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "L0u5uZeoJFaF", "colab": { "base_uri": "https://localhost:8080/", "height": 50 }, "outputId": "f7cc036f-5b61-4b0d-9ed2-7af8391851b9" }, "source": [ "# lemmatize adjectives\n", "print(wnl.lemmatize('saddest', 'a'))\n", "print(wnl.lemmatize('fancier', 'a'))" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "sad\n", "fancy\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "NhKXkdckJFaN", "colab": { "base_uri": "https://localhost:8080/", "height": 67 }, "outputId": "cd37a4a9-c4b2-48a5-a486-869ad8b8fea1" }, "source": [ "# ineffective lemmatization\n", "print(wnl.lemmatize('ate', 'n'))\n", "print(wnl.lemmatize('fancier', 'v'))\n", "print(wnl.lemmatize('fancier'))" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "ate\n", "fancier\n", "fancier\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "Z4g85bOGJFaQ", "colab": {} }, "source": [ "s = 'The brown foxes are quick and they are jumping over the sleeping lazy dogs!'" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "NQ1S2ngz7B84", "colab_type": "text" }, "source": [ "### Tokenize" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "0l372SiEJFaU", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "cd2a469c-26ed-4d06-cce1-91b6e29650af" }, "source": [ "tokens = nltk.word_tokenize(s)\n", "print(tokens)" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "['The', 'brown', 'foxes', 'are', 'quick', 'and', 'they', 'are', 'jumping', 'over', 'the', 'sleeping', 'lazy', 'dogs', '!']\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "s1FHAghFJFaX", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "765ad93e-2a6a-4666-c649-3c529d8cdf95" }, "source": [ "lemmatized_text = ' '.join(wnl.lemmatize(token) for token in tokens)\n", "lemmatized_text" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'The brown fox are quick and they are jumping over the sleeping lazy dog !'" ] }, "metadata": { "tags": [] }, "execution_count": 44 } ] }, { "cell_type": "markdown", "metadata": { "id": "d0-fgmbi7E5_", "colab_type": "text" }, "source": [ "### POS Tagging" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "UDffFU3gJFaZ", "colab": { "base_uri": "https://localhost:8080/", "height": 54 }, "outputId": "fcfbd8a2-dc38-4578-c479-b4672b58cde3" }, "source": [ "tagged_tokens = nltk.pos_tag(tokens)\n", "print(tagged_tokens)" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "[('The', 'DT'), ('brown', 'JJ'), ('foxes', 'NNS'), ('are', 'VBP'), ('quick', 'JJ'), ('and', 'CC'), ('they', 'PRP'), ('are', 'VBP'), ('jumping', 'VBG'), ('over', 'IN'), ('the', 'DT'), ('sleeping', 'VBG'), ('lazy', 'JJ'), ('dogs', 'NNS'), ('!', '.')]\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "9STnRHVt7HRG", "colab_type": "text" }, "source": [ "### Tag conversion to WordNet Tags" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "2S9kS_xPJFaf", "colab": {} }, "source": [ "from nltk.corpus import wordnet\n", "\n", "def pos_tag_wordnet(tagged_tokens):\n", " tag_map = {'j': wordnet.ADJ, 'v': wordnet.VERB, 'n': wordnet.NOUN, 'r': wordnet.ADV}\n", " new_tagged_tokens = [(word, tag_map.get(tag[0].lower(), wordnet.NOUN))\n", " for word, tag in tagged_tokens]\n", " return new_tagged_tokens" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "TbijTK6YJFaj", "colab": { "base_uri": "https://localhost:8080/", "height": 54 }, "outputId": "c97e38b3-8b0f-43ee-848b-bbbf9449ebfb" }, "source": [ "wordnet_tokens = pos_tag_wordnet(tagged_tokens)\n", "print(wordnet_tokens)" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "[('The', 'n'), ('brown', 'a'), ('foxes', 'n'), ('are', 'v'), ('quick', 'a'), ('and', 'n'), ('they', 'n'), ('are', 'v'), ('jumping', 'v'), ('over', 'n'), ('the', 'n'), ('sleeping', 'v'), ('lazy', 'a'), ('dogs', 'n'), ('!', 'n')]\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "qKia_-ov7KLH", "colab_type": "text" }, "source": [ "### Effective Lemmatization" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "tNOpTLDTJFal", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "575e08b1-a48d-40a8-abde-363fd671fa93" }, "source": [ "lemmatized_text = ' '.join(wnl.lemmatize(word, tag) for word, tag in wordnet_tokens)\n", "lemmatized_text" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'The brown fox be quick and they be jump over the sleep lazy dog !'" ] }, "metadata": { "tags": [] }, "execution_count": 50 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "9zisZIs1JFan" }, "source": [ "### Your turn: Define a function such that you put all the above steps together so that it does the following\n", "\n", "- Function name is __`wordnet_lemmatize_text(...)`__\n", "- Input is a variable __`text`__ which should take in a document (bunch of words)\n", "- Call the earlier defined functions and utilize them\n", "- Return lemmatized text as the output (as a string)" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "I6LditBNJFao", "colab": {} }, "source": [ "wnl = WordNetLemmatizer()\n", "\n", "def wordnet_lemmatize_text(text):\n", " tagged_tokens = nltk.pos_tag(nltk.word_tokenize(text))\n", " wordnet_tokens = pos_tag_wordnet(tagged_tokens)\n", " lemmatized_text = ' '.join(wnl.lemmatize(word, tag) for word, tag in wordnet_tokens)\n", " return lemmatized_text" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "UWQeSVxGJFap" }, "source": [ "### Your Turn: Now call the function on the below sentence and test it" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "FUvJQk-eJFaq", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "41276cb8-3c6e-4382-9f25-2516899052e2" }, "source": [ "s" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'The brown foxes are quick and they are jumping over the sleeping lazy dogs!'" ] }, "metadata": { "tags": [] }, "execution_count": 52 } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "7xE0WwbJJFas", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "638b66b3-02b5-41ff-ec4e-6a2deff153d9" }, "source": [ "wordnet_lemmatize_text(s)" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'The brown fox be quick and they be jump over the sleep lazy dog !'" ] }, "metadata": { "tags": [] }, "execution_count": 53 } ] }, { "cell_type": "markdown", "metadata": { "id": "KgQJp2SH7OC_", "colab_type": "text" }, "source": [ "## Lemmatization with Spacy" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "3N2ExlFqJFaw", "colab": {} }, "source": [ "import spacy\n", "nlp = spacy.load('en', parse=False, tag=False, entity=False)\n", "\n", "def spacy_lemmatize_text(text):\n", " text = nlp(text)\n", " text = ' '.join([word.lemma_ if word.lemma_ != '-PRON-' else word.text for word in text])\n", " return text" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "ga-E47JKJFaz", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "76333567-ae35-46ee-fdf9-260b3926cc31" }, "source": [ "s" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'The brown foxes are quick and they are jumping over the sleeping lazy dogs!'" ] }, "metadata": { "tags": [] }, "execution_count": 55 } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "Bb-PrIeqJFa5", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "b5b00364-7d85-4240-cf55-21f8fef8fa5d" }, "source": [ "spacy_lemmatize_text(s)" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'the brown fox be quick and they be jump over the sleep lazy dog !'" ] }, "metadata": { "tags": [] }, "execution_count": 56 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "aQsKAXlvJFa7" }, "source": [ "# Stopword Removal" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "VkJLKKxrJFa7", "colab": {} }, "source": [ "def remove_stopwords(text, is_lower_case=False, stopwords=None):\n", " if not stopwords:\n", " stopwords = nltk.corpus.stopwords.words('english')\n", " tokens = nltk.word_tokenize(text)\n", " tokens = [token.strip() for token in tokens]\n", " \n", " if is_lower_case:\n", " filtered_tokens = [token for token in tokens if token not in stopwords]\n", " else:\n", " filtered_tokens = [token for token in tokens if token.lower() not in stopwords]\n", " \n", " filtered_text = ' '.join(filtered_tokens) \n", " return filtered_text" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "veJLEhzKJFa-", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "3272c105-0664-4349-8f42-637cadd56727" }, "source": [ "stop_words = nltk.corpus.stopwords.words('english')\n", "print(stop_words[:10])" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', \"you're\"]\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "ycusSsPBJFbA", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "186389a0-7131-45eb-a876-6dde01d91fe9" }, "source": [ "s" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'The brown foxes are quick and they are jumping over the sleeping lazy dogs!'" ] }, "metadata": { "tags": [] }, "execution_count": 59 } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "oWKjTPnzJFbD", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "16fe7d8e-8248-4415-9ec8-902bd718295c" }, "source": [ "remove_stopwords(s, is_lower_case=False)" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'brown foxes quick jumping sleeping lazy dogs !'" ] }, "metadata": { "tags": [] }, "execution_count": 60 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "4bcnWSnAJFbG" }, "source": [ "### Your turn: Remove the words 'the' and 'brown' from the stop_words list and call the function with this new list" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "rPAM2rNZJFbH", "colab": {} }, "source": [ "stop_words.remove('the')\n", "stop_words.append('brown')" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "qk2Y-nbZJFbJ", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "a433ccad-6e6a-4671-b4b7-9723f45153af" }, "source": [ "remove_stopwords(s, is_lower_case=False, stopwords=stop_words)" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'The foxes quick jumping the sleeping lazy dogs !'" ] }, "metadata": { "tags": [] }, "execution_count": 62 } ] } ] } ================================================ FILE: notebooks/02_Text_Representation_Statistical_Models.ipynb ================================================ { "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "02-Text Representation - Statistical Models.ipynb", "provenance": [], "collapsed_sections": [] }, "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.6" } }, "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "-dWlnZ1vRP6H" }, "source": [ "# Text Representation with Feature Engineering\n", "\n", "### Exploring Traditional Statistical Models\n", "\n", "Feature Engineering is often known as the secret sauce to creating superior and better performing machine learning models. Just one excellent feature could be your ticket to winning a Kaggle challenge! The importance of feature engineering is even more important for unstructured, textual data because we need to convert free flowing text into some numeric representations which can then be understood by machine learning algorithms. \n", "\n", "Here we will explore the following feature engineering techniques:\n", "\n", "- Bag of Words Model (TF)\n", "- Bag of N-grams Model\n", "- TF-IDF Model\n", "- Similarity Features" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "NYC_RfbeRP6J" }, "source": [ "# Prepare a Sample Corpus\n", "\n", "Let’s now take a sample corpus of documents on which we will run most of our analyses in this article. A corpus is typically a collection of text documents usually belonging to one or more subjects or domains." ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "6xn8eDqARP6J", "colab": { "base_uri": "https://localhost:8080/", "height": 284 }, "outputId": "8ee06c2b-ddf7-4e5a-a23c-9b8038d3d9b2" }, "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "pd.options.display.max_colwidth = 200\n", "\n", "corpus = ['The sky is blue and beautiful.',\n", " 'Love this blue and beautiful sky!',\n", " 'The quick brown fox jumps over the lazy dog.',\n", " \"A king's breakfast has sausages, ham, bacon, eggs, toast and beans\",\n", " 'I love green eggs, ham, sausages and bacon!',\n", " 'The brown fox is quick and the blue dog is lazy!',\n", " 'The sky is very blue and the sky is very beautiful today',\n", " 'The dog is lazy but the brown fox is quick!' \n", "]\n", "labels = ['weather', 'weather', 'animals', 'food', 'food', 'animals', 'weather', 'animals']\n", "\n", "corpus = np.array(corpus)\n", "corpus_df = pd.DataFrame({'Document': corpus, \n", " 'Category': labels})\n", "corpus_df = corpus_df[['Document', 'Category']]\n", "corpus_df" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "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", "
DocumentCategory
0The sky is blue and beautiful.weather
1Love this blue and beautiful sky!weather
2The quick brown fox jumps over the lazy dog.animals
3A king's breakfast has sausages, ham, bacon, eggs, toast and beansfood
4I love green eggs, ham, sausages and bacon!food
5The brown fox is quick and the blue dog is lazy!animals
6The sky is very blue and the sky is very beautiful todayweather
7The dog is lazy but the brown fox is quick!animals
\n", "
" ], "text/plain": [ " Document Category\n", "0 The sky is blue and beautiful. weather\n", "1 Love this blue and beautiful sky! weather\n", "2 The quick brown fox jumps over the lazy dog. animals\n", "3 A king's breakfast has sausages, ham, bacon, eggs, toast and beans food\n", "4 I love green eggs, ham, sausages and bacon! food\n", "5 The brown fox is quick and the blue dog is lazy! animals\n", "6 The sky is very blue and the sky is very beautiful today weather\n", "7 The dog is lazy but the brown fox is quick! animals" ] }, "metadata": { "tags": [] }, "execution_count": 1 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "QOMGTkvCRP6N" }, "source": [ "You can see that we have taken a few sample text documents belonging to different categories for our toy corpus. Before we talk about feature engineering, as always, we need to do some data pre-processing or wrangling to remove unnecessary characters, symbols and tokens." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "nAcT9NHkRP6O" }, "source": [ "# Simple Text Pre-processing\n", "\n", "Since the focus of this unit is on feature engineering, we will build a simple text pre-processor which focuses on removing special characters, extra whitespaces, digits, stopwords and lower casing the text corpus." ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "vSIxl_PUSCvj", "colab": { "base_uri": "https://localhost:8080/", "height": 101 }, "outputId": "6477f365-d714-461e-bac4-cf4bc17705f5" }, "source": [ "import nltk\n", "nltk.download('stopwords')\n", "nltk.download('punkt')" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "[nltk_data] Downloading package stopwords to /root/nltk_data...\n", "[nltk_data] Unzipping corpora/stopwords.zip.\n", "[nltk_data] Downloading package punkt to /root/nltk_data...\n", "[nltk_data] Unzipping tokenizers/punkt.zip.\n" ], "name": "stdout" }, { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 2 } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "OElcyG9WRP6P", "colab": { "base_uri": "https://localhost:8080/", "height": 118 }, "outputId": "fcfae9fa-349d-4e77-9f75-e5f241dfe5d9" }, "source": [ "import nltk\n", "import re\n", "\n", "stop_words = nltk.corpus.stopwords.words('english')\n", "\n", "def normalize_document(doc):\n", " # lower case and remove special characters\\whitespaces\n", " doc = re.sub(r'[^a-zA-Z\\s]', '', doc, re.I|re.A)\n", " doc = doc.lower()\n", " doc = doc.strip()\n", " # tokenize document\n", " tokens = nltk.word_tokenize(doc)\n", " # filter stopwords out of document\n", " filtered_tokens = [token for token in tokens if token not in stop_words]\n", " # re-create document from filtered tokens\n", " doc = ' '.join(filtered_tokens)\n", " return doc\n", "\n", "normalize_corpus = np.vectorize(normalize_document)\n", "\n", "norm_corpus = normalize_corpus(corpus)\n", "norm_corpus" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array(['sky blue beautiful', 'love blue beautiful sky',\n", " 'quick brown fox jumps lazy dog',\n", " 'kings breakfast sausages ham bacon eggs toast beans',\n", " 'love green eggs ham sausages bacon',\n", " 'brown fox quick blue dog lazy', 'sky blue sky beautiful today',\n", " 'dog lazy brown fox quick'], 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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
baconbeansbeautifulbluebreakfastbrowndogeggsfoxgreenhamjumpskingslazylovequicksausagesskytoasttoday
000110000000000000100
100110000000000100100
200000110100101010000
311001001001010001010
410000001011000101000
500010110100001010000
600110000000000000201
700000110100001010000
\n", "" ], "text/plain": [ " bacon beans beautiful blue breakfast ... quick sausages sky toast today\n", "0 0 0 1 1 0 ... 0 0 1 0 0\n", "1 0 0 1 1 0 ... 0 0 1 0 0\n", "2 0 0 0 0 0 ... 1 0 0 0 0\n", "3 1 1 0 0 1 ... 0 1 0 1 0\n", "4 1 0 0 0 0 ... 0 1 0 0 0\n", "5 0 0 0 1 0 ... 1 0 0 0 0\n", "6 0 0 1 1 0 ... 0 0 2 0 1\n", "7 0 0 0 0 0 ... 1 0 0 0 0\n", "\n", "[8 rows x 20 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 5 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "A_1k60ZERP6Z" }, "source": [ "This should make things more clearer! You can clearly see that each column or dimension in the feature vectors represents a word from the corpus and each row represents one of our documents. The value in any cell, represents the number of times that word (represented by column) occurs in the specific document (represented by row). Hence if a corpus of documents consists of N unique words across all the documents, we would have an N-dimensional vector for each of the documents." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "0DfPJwSsRP6Z" }, "source": [ "# Bag of N-Grams Model\n", "\n", "A word is just a single token, often known as a unigram or 1-gram. We already know that the Bag of Words model doesn’t consider order of words. But what if we also wanted to take into account phrases or collection of words which occur in a sequence? N-grams help us achieve that. An N-gram is basically a collection of word tokens from a text document such that these tokens are contiguous and occur in a sequence. Bi-grams indicate n-grams of order 2 (two words), Tri-grams indicate n-grams of order 3 (three words), and so on. The Bag of N-Grams model is hence just an extension of the Bag of Words model so we can also leverage N-gram based features. The following example depicts bi-gram based features in each document feature vector." ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "4n1N32JyRP6a", "colab": { "base_uri": "https://localhost:8080/", "height": 321 }, "outputId": "5c091718-a76c-448c-add9-311f32486230" }, "source": [ "# you can set the n-gram range to 1,2 to get unigrams as well as bigrams\n", "bv = CountVectorizer(ngram_range=(2,2))\n", "bv_matrix = bv.fit_transform(norm_corpus)\n", "\n", "bv_matrix = bv_matrix.toarray()\n", "vocab = bv.get_feature_names()\n", "pd.DataFrame(bv_matrix, columns=vocab)" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
bacon eggsbeautiful skybeautiful todayblue beautifulblue dogblue skybreakfast sausagesbrown foxdog lazyeggs hameggs toastfox jumpsfox quickgreen eggsham baconham sausagesjumps lazykings breakfastlazy brownlazy doglove bluelove greenquick bluequick brownsausages baconsausages hamsky beautifulsky bluetoast beans
000010000000000000000000000010
101010000000000000000100000000
200000001000100001001000100000
310000010001000100100000001001
400000000010001010000010010000
500001001100010000000001000000
600100100000000000000000000110
700000001100010000010000000000
\n", "
" ], "text/plain": [ " bacon eggs beautiful sky ... sky blue toast beans\n", "0 0 0 ... 1 0\n", "1 0 1 ... 0 0\n", "2 0 0 ... 0 0\n", "3 1 0 ... 0 1\n", "4 0 0 ... 0 0\n", "5 0 0 ... 0 0\n", "6 0 0 ... 1 0\n", "7 0 0 ... 0 0\n", "\n", "[8 rows x 29 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 6 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "bW6F4h8QRP6c" }, "source": [ "This gives us feature vectors for our documents, where each feature consists of a bi-gram representing a sequence of two words and values represent how many times the bi-gram was present for our documents." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "pPwKOnnkRP6d" }, "source": [ "# TF-IDF Model\n", "\n", "There are some potential problems which might arise with the Bag of Words model when it is used on large corpora. Since the feature vectors are based on absolute term frequencies, there might be some terms which occur frequently across all documents and these may tend to overshadow other terms in the feature set. The TF-IDF model tries to combat this issue by using a scaling or normalizing factor in its computation. ___TF-IDF___ stands for __Term Frequency-Inverse Document Frequency__, which uses a combination of two metrics in its computation, namely: ___term frequency (tf)___ and ___inverse document frequency (idf)___. This technique was developed for ranking results for queries in search engines and now it is an indispensable model in the world of information retrieval and NLP.\n", "\n", "Mathematically, we can define TF-IDF as ___tfidf = tf x idf___, which can be expanded further to be represented as follows.\n", "\n", "![](https://github.com/dipanjanS/nlp_workshop_odsc19/blob/master/Module04%20-%20Text%20Representation/tfidf_eq.png?raw=1)\n", "\n", "Here, ___tfidf(w, D)___ is the TF-IDF score for word __w__ in document __D__. \n", "- The term ___tf(w, D)___ represents the term frequency of the word __w__ in document __D__, which can be obtained from the Bag of Words model. \n", "- The term ___idf(w, D)___ is the inverse document frequency for the term __w__, which can be computed as the log transform of the total number of documents in the corpus __C__ divided by the document frequency of the word __w__, which is basically the frequency of documents in the corpus where the word __w__ occurs. \n", "\n", "There are multiple variants of this model but they all end up giving quite similar results. Let’s apply this on our corpus now!" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "sFp9zDbgRP6d", "colab": { "base_uri": "https://localhost:8080/", "height": 284 }, "outputId": "009f142f-1b99-4bfd-9e78-2bbf9db03807" }, "source": [ "from sklearn.feature_extraction.text import TfidfVectorizer\n", "\n", "tv = TfidfVectorizer(min_df=0., max_df=1., use_idf=True)\n", "tv_matrix = tv.fit_transform(norm_corpus)\n", "tv_matrix = tv_matrix.toarray()\n", "\n", "vocab = tv.get_feature_names()\n", "pd.DataFrame(np.round(tv_matrix, 2), columns=vocab)" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
baconbeansbeautifulbluebreakfastbrowndogeggsfoxgreenhamjumpskingslazylovequicksausagesskytoasttoday
00.000.000.600.530.000.000.000.000.000.000.000.000.000.000.000.000.000.600.000.0
10.000.000.490.430.000.000.000.000.000.000.000.000.000.000.570.000.000.490.000.0
20.000.000.000.000.000.380.380.000.380.000.000.530.000.380.000.380.000.000.000.0
30.320.380.000.000.380.000.000.320.000.000.320.000.380.000.000.000.320.000.380.0
40.390.000.000.000.000.000.000.390.000.470.390.000.000.000.390.000.390.000.000.0
50.000.000.000.370.000.420.420.000.420.000.000.000.000.420.000.420.000.000.000.0
60.000.000.360.320.000.000.000.000.000.000.000.000.000.000.000.000.000.720.000.5
70.000.000.000.000.000.450.450.000.450.000.000.000.000.450.000.450.000.000.000.0
\n", "
" ], "text/plain": [ " bacon beans beautiful blue ... sausages sky toast today\n", "0 0.00 0.00 0.60 0.53 ... 0.00 0.60 0.00 0.0\n", "1 0.00 0.00 0.49 0.43 ... 0.00 0.49 0.00 0.0\n", "2 0.00 0.00 0.00 0.00 ... 0.00 0.00 0.00 0.0\n", "3 0.32 0.38 0.00 0.00 ... 0.32 0.00 0.38 0.0\n", "4 0.39 0.00 0.00 0.00 ... 0.39 0.00 0.00 0.0\n", "5 0.00 0.00 0.00 0.37 ... 0.00 0.00 0.00 0.0\n", "6 0.00 0.00 0.36 0.32 ... 0.00 0.72 0.00 0.5\n", "7 0.00 0.00 0.00 0.00 ... 0.00 0.00 0.00 0.0\n", "\n", "[8 rows x 20 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 7 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "0jvKPWIwRP6g" }, "source": [ "The TF-IDF based feature vectors for each of our text documents show scaled and normalized values as compared to the raw Bag of Words model values. " ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "7xzqFWAkRP6h" }, "source": [ "# Document Similarity\n", "\n", "Document similarity is the process of using a distance or similarity based metric that can be used to identify how similar a text document is with any other document(s) based on features extracted from the documents like bag of words or tf-idf.\n", "\n", "Thus you can see that we can build on top of the tf-idf based features we engineered in the previous section and use them to generate new features which can be useful in domains like search engines, document clustering and information retrieval by leveraging these similarity based features.\n", "\n", "Pairwise document similarity in a corpus involves computing document similarity for each pair of documents in a corpus. Thus if you have C documents in a corpus, you would end up with a C x C matrix such that each row and column represents the similarity score for a pair of documents, which represent the indices at the row and column, respectively. There are several similarity and distance metrics that are used to compute document similarity. These include cosine distance/similarity, euclidean distance, manhattan distance, BM25 similarity, jaccard distance and so on. In our analysis, we will be using perhaps the most popular and widely used similarity metric,\n", "cosine similarity and compare pairwise document similarity based on their TF-IDF feature vectors." ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "zyToCxp9RP6i", "colab": { "base_uri": "https://localhost:8080/", "height": 284 }, "outputId": "f9fa6f92-dded-4155-86fe-e6ebc8a5e8a7" }, "source": [ "from sklearn.metrics.pairwise import cosine_similarity\n", "\n", "similarity_matrix = cosine_similarity(tv_matrix)\n", "similarity_df = pd.DataFrame(similarity_matrix)\n", "similarity_df" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "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", "
01234567
01.0000000.8205990.0000000.0000000.0000000.1923530.8172460.000000
10.8205991.0000000.0000000.0000000.2254890.1578450.6706310.000000
20.0000000.0000001.0000000.0000000.0000000.7918210.0000000.850516
30.0000000.0000000.0000001.0000000.5068660.0000000.0000000.000000
40.0000000.2254890.0000000.5068661.0000000.0000000.0000000.000000
50.1923530.1578450.7918210.0000000.0000001.0000000.1154880.930989
60.8172460.6706310.0000000.0000000.0000000.1154881.0000000.000000
70.0000000.0000000.8505160.0000000.0000000.9309890.0000001.000000
\n", "
" ], "text/plain": [ " 0 1 2 ... 5 6 7\n", "0 1.000000 0.820599 0.000000 ... 0.192353 0.817246 0.000000\n", "1 0.820599 1.000000 0.000000 ... 0.157845 0.670631 0.000000\n", "2 0.000000 0.000000 1.000000 ... 0.791821 0.000000 0.850516\n", "3 0.000000 0.000000 0.000000 ... 0.000000 0.000000 0.000000\n", "4 0.000000 0.225489 0.000000 ... 0.000000 0.000000 0.000000\n", "5 0.192353 0.157845 0.791821 ... 1.000000 0.115488 0.930989\n", "6 0.817246 0.670631 0.000000 ... 0.115488 1.000000 0.000000\n", "7 0.000000 0.000000 0.850516 ... 0.930989 0.000000 1.000000\n", "\n", "[8 rows x 8 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 8 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Uq737P7GRP6l" }, "source": [ "Cosine similarity basically gives us a metric representing the cosine of the angle between the feature vector representations of two text documents. Lower the angle between the documents, the closer and more similar they are as depicted in the following figure.\n", "\n", "![](https://github.com/dipanjanS/nlp_workshop_odsc19/blob/master/Module04%20-%20Text%20Representation/cosine_depiction.png?raw=1)\n", "\n", "Looking closely at the similarity matrix clearly tells us that documents (0, 1 and 6), (2, 5 and 7) are very similar to one another and documents 3 and 4 are slightly similar to each other but the magnitude is not very strong, however still stronger than the other documents. This must indicate these similar documents have some similar features. This is a perfect example of grouping or clustering that can be solved by unsupervised learning especially when you are dealing with huge corpora of millions of text documents." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "vh3zF8lKRP6n" }, "source": [ "# Bonus: Clustering using Document Similarity Features\n", "\n", "We will use a very popular partition based clustering method, K-means clustering to cluster or group these documents based on their similarity based feature representations. In K-means clustering, we have an input parameter k, which specifies the number of clusters it will output using the document features. This clustering method is a centroid based clustering method, where it tries to cluster these documents into clusters of equal variance. It tries to create these clusters by minimizing the within-cluster sum of squares measure, also known as inertia. " ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "l5ff9gMZRP6o", "colab": { "base_uri": "https://localhost:8080/", "height": 284 }, "outputId": "865209d2-1755-4cda-82f3-669c7e4e224d" }, "source": [ "from sklearn.cluster import KMeans\n", "\n", "km = KMeans(n_clusters=3, random_state=0)\n", "km.fit_transform(similarity_matrix)\n", "cluster_labels = km.labels_\n", "cluster_labels = pd.DataFrame(cluster_labels, columns=['ClusterLabel'])\n", "pd.concat([corpus_df, cluster_labels], axis=1)" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "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", "
DocumentCategoryClusterLabel
0The sky is blue and beautiful.weather2
1Love this blue and beautiful sky!weather2
2The quick brown fox jumps over the lazy dog.animals1
3A king's breakfast has sausages, ham, bacon, eggs, toast and beansfood0
4I love green eggs, ham, sausages and bacon!food0
5The brown fox is quick and the blue dog is lazy!animals1
6The sky is very blue and the sky is very beautiful todayweather2
7The dog is lazy but the brown fox is quick!animals1
\n", "
" ], "text/plain": [ " Document ... ClusterLabel\n", "0 The sky is blue and beautiful. ... 2\n", "1 Love this blue and beautiful sky! ... 2\n", "2 The quick brown fox jumps over the lazy dog. ... 1\n", "3 A king's breakfast has sausages, ham, bacon, eggs, toast and beans ... 0\n", "4 I love green eggs, ham, sausages and bacon! ... 0\n", "5 The brown fox is quick and the blue dog is lazy! ... 1\n", "6 The sky is very blue and the sky is very beautiful today ... 2\n", "7 The dog is lazy but the brown fox is quick! ... 1\n", "\n", "[8 rows x 3 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 9 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "S-NsBGloRP6q" }, "source": [ "We can see from the above output that our documents were correctly assigned to the right clusters!" ] } ] } ================================================ FILE: notebooks/03_Text_Representation_Embedding_Models.ipynb ================================================ { "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "03 -Text Representation - Embedding Models.ipynb", "provenance": [], "collapsed_sections": [] }, "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.6" } }, "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "JS9QXHRoRiqc" }, "source": [ "# Text Representation with Feature Engineering\n", "\n", "### Exploring Word Embeddings with New Deep Learning Models\n", "\n", "We have discussed in the previous sub-unit that Feature Engineering is the secret sauce to creating superior and better performing machine learning models. \n", "\n", "Traditional (count-based) feature engineering strategies for textual data involve models belonging to a family of models popularly known as the Bag of Words model. This includes term frequencies, TF-IDF (term frequency-inverse document frequency), N-grams and so on. While they are effective methods for extracting features from text, due to the inherent nature of the model being just a bag of unstructured words, we lose additional information like the semantics, structure, sequence and context around nearby words in each text document. \n", "\n", "This forms as enough motivation for us to explore more sophisticated models which can capture this information and give us features which are vector representation of words, popularly known as embeddings.\n", "\n", "Here we will explore the following feature engineering techniques:\n", "\n", "- Word2Vec\n", "- GloVe\n", "- FastText\n", "\n", "Predictive methods like Neural Network based language models try to predict words from its neighboring words looking at word sequences in the corpus and in the process it learns distributed representations giving us dense word embeddings. We will be focusing on these predictive methods in this article." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "wYUGSn3dRiqd" }, "source": [ "# Prepare a Sample Corpus\n", "\n", "Let’s now take a sample corpus of documents on which we will run most of our analyses in this article. A corpus is typically a collection of text documents usually belonging to one or more subjects or domains." ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "KH4ZwBgtRiqe", "colab": { "base_uri": "https://localhost:8080/", "height": 284 }, "outputId": "c285dcff-5578-4fa8-9ecf-63dba9b4f7ca" }, "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "pd.options.display.max_colwidth = 200\n", "\n", "corpus = ['The sky is blue and beautiful.',\n", " 'Love this blue and beautiful sky!',\n", " 'The quick brown fox jumps over the lazy dog.',\n", " \"A king's breakfast has sausages, ham, bacon, eggs, toast and beans\",\n", " 'I love green eggs, ham, sausages and bacon!',\n", " 'The brown fox is quick and the blue dog is lazy!',\n", " 'The sky is very blue and the sky is very beautiful today',\n", " 'The dog is lazy but the brown fox is quick!' \n", "]\n", "labels = ['weather', 'weather', 'animals', 'food', 'food', 'animals', 'weather', 'animals']\n", "\n", "corpus = np.array(corpus)\n", "corpus_df = pd.DataFrame({'Document': corpus, \n", " 'Category': labels})\n", "corpus_df = corpus_df[['Document', 'Category']]\n", "corpus_df" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "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", "
DocumentCategory
0The sky is blue and beautiful.weather
1Love this blue and beautiful sky!weather
2The quick brown fox jumps over the lazy dog.animals
3A king's breakfast has sausages, ham, bacon, eggs, toast and beansfood
4I love green eggs, ham, sausages and bacon!food
5The brown fox is quick and the blue dog is lazy!animals
6The sky is very blue and the sky is very beautiful todayweather
7The dog is lazy but the brown fox is quick!animals
\n", "
" ], "text/plain": [ " Document Category\n", "0 The sky is blue and beautiful. weather\n", "1 Love this blue and beautiful sky! weather\n", "2 The quick brown fox jumps over the lazy dog. animals\n", "3 A king's breakfast has sausages, ham, bacon, eggs, toast and beans food\n", "4 I love green eggs, ham, sausages and bacon! food\n", "5 The brown fox is quick and the blue dog is lazy! animals\n", "6 The sky is very blue and the sky is very beautiful today weather\n", "7 The dog is lazy but the brown fox is quick! animals" ] }, "metadata": { "tags": [] }, "execution_count": 1 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "FLojJzK0Riqi" }, "source": [ "Let's go ahead and pre-process our text data now" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "7KR8w3qbRiqi" }, "source": [ "# Simple Text Pre-processing\n", "\n", "Since the focus of this unit is on feature engineering, we will build a simple text pre-processor which focuses on removing special characters, extra whitespaces, digits, stopwords and lower casing the text corpus." ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "5YHHrL7VRiqj", "colab": { "base_uri": "https://localhost:8080/", "height": 185 }, "outputId": "114e4855-473e-45da-a614-88cc52a190a3" }, "source": [ "import nltk\n", "import re\n", "\n", "nltk.download('stopwords')\n", "nltk.download('punkt')\n", "\n", "stop_words = nltk.corpus.stopwords.words('english')\n", "\n", "def normalize_document(doc):\n", " # lower case and remove special characters\\whitespaces\n", " doc = re.sub(r'[^a-zA-Z\\s]', '', doc, re.I|re.A)\n", " doc = doc.lower()\n", " doc = doc.strip()\n", " # tokenize document\n", " tokens = nltk.word_tokenize(doc)\n", " # filter stopwords out of document\n", " filtered_tokens = [token for token in tokens if token not in stop_words]\n", " # re-create document from filtered tokens\n", " doc = ' '.join(filtered_tokens)\n", " return doc\n", "\n", "normalize_corpus = np.vectorize(normalize_document)\n", "\n", "norm_corpus = normalize_corpus(corpus)\n", "norm_corpus" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "[nltk_data] Downloading package stopwords to /root/nltk_data...\n", "[nltk_data] Unzipping corpora/stopwords.zip.\n", "[nltk_data] Downloading package punkt to /root/nltk_data...\n", "[nltk_data] Unzipping tokenizers/punkt.zip.\n" ], "name": "stdout" }, { "output_type": "execute_result", "data": { "text/plain": [ "array(['sky blue beautiful', 'love blue beautiful sky',\n", " 'quick brown fox jumps lazy dog',\n", " 'kings breakfast sausages ham bacon eggs toast beans',\n", " 'love green eggs ham sausages bacon',\n", " 'brown fox quick blue dog lazy', 'sky blue sky beautiful today',\n", " 'dog lazy brown fox quick'], dtype='" ] }, "metadata": { "tags": [] }, "execution_count": 3 } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "6NY4l-zaRiqo", "colab": { "base_uri": "https://localhost:8080/", "height": 378 }, "outputId": "bd4ef9b9-421e-494a-9a0f-8e3985fba0a4" }, "source": [ "import matplotlib.pyplot as plt\n", "\n", "%matplotlib inline\n", "\n", "# visualize embeddings\n", "from sklearn.manifold import TSNE\n", "\n", "words = w2v_model.wv.index2word\n", "wvs = w2v_model.wv[words]\n", "\n", "tsne = TSNE(n_components=2, random_state=42, n_iter=5000, perplexity=5)\n", "np.set_printoptions(suppress=True)\n", "T = tsne.fit_transform(wvs)\n", "labels = words\n", "\n", "plt.figure(figsize=(12, 6))\n", "plt.scatter(T[:, 0], T[:, 1], c='orange', edgecolors='r')\n", "for label, x, y in zip(labels, T[:, 0], T[:, 1]):\n", " plt.annotate(label, xy=(x+1, y+1), xytext=(0, 0), textcoords='offset points')" ], "execution_count": null, "outputs": [ { "output_type": "display_data", "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAtEAAAFpCAYAAABauHSCAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAIABJREFUeJzs3Xucz3X+///byyGSU0U60aivHOeA\nGdkcUiraRGIpbGHxK5FtN9tJrZS2Wvupj1JKIhubXUqlTZtiUaoZzDiEj7RDyRYbMg1leP3+eI/3\nDhGvDDOT2/Vymcu8X8f34/V+d+lyn6fn6/EKwjBEkiRJ0uErVdQFSJIkSSWNIVqSJEmKyBAtSZIk\nRWSIliRJkiIyREuSJEkRGaIlSZKkiAzRkiRJUkSGaEmSJCkiQ7QkSZIUkSFakiRJiqhMYZwkCIKq\nwLNAIyAE+gKrgalAApANdAvDcEsQBAHwv8DPgVygdxiGi3/o/NWqVQsTEhIKo1RJkiTpoBYtWrQ5\nDMPqh9qvUEI0sVA8KwzDrkEQnABUAO4C3g7D8KEgCO4A7gBuB64A6uT/XAA8lf/7oBISEsjIyCik\nUiVJkqQDC4Jg3eHsd8TTOYIgqAK0BsYDhGH4XRiGW4FOwPP5uz0PXJ3/uhMwKYx5H6gaBMEZR1qH\nJEmSdKwUxpzo2sAmYEIQBEuCIHg2CIKTgBphGG7M3+ffQI3812cBnxY4/rP8dZIkS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+DJIAhKB0FQGhgDXAE0AK7L31fST8TR6mQwfPjweB/mglatWkVKSgqNGzdm7dq1\nP3yS/aahTPwLfP5s+R89DWXKlClkZmayatUq7rzzzh91jp+0g8xBP1BP8e3bt3PGGWewa9cuJk+e\nXATF5jtIzevXr4/fGDtlypT4SHG1atXIyclh2rRpQKw149lnn82MGTMA+Pbbb8nNzT1o60dJJdcR\nhegwDP8RhuHeP5nfB87Of90JeDEMw2/DMPwX8DHQLP/n4zAMPwnD8Dvgxfx9JR1Hdu/eXWjnmjFj\nBl27dmXJkiWcd955P7xzj54w6hmYfg70CZj4djk+H3xXyRhFL4l+YA76/u6//34uuOACWrRoEW9/\nWCQOUnPdE8owZswY6tevz5YtW7jpppvo378/jRo1ol27dvv0Fv/zn//M6NGjSUpK4sILL+Tf//43\nnTt3PmDrR0klV1BYdwEHQfAaMDUMwxeCIHgCeD8Mwxfyt40H3sjftX0Yhv3y1/8SuCAMw0EHON8A\nYABArVq1mu59MIOk4i07O5v27dvTtGlTFi9eTMOGDZk0aRINGjSge/fuvPXWW/zud78jLS2Nm2++\nmU2bNlGhQgXGjRtHvXr1eO2113jggQf47rvvOPXUU5k8eTI1atRg+PDhVKxYkdtuu41x48bx0ksv\nMWjQIH71q19RunRpzj//fObMmcPVV1/Np59+ys6dOxkyZAgDBgxg9+7d/OpXvyIjI4MgCOjbty81\na9akd+/enHXWWZx44oksXLiQE088sag/vp+W0qVio7kF777JA/oEsWlAxVFJrFlSoQqCYFEYhqmH\n2u+QNxYGQTAbONCfy3eHYfhK/j53E/vfTKH9G1wYhs8AzwCkpqba70cqQVavXs348eNp0aIFffv2\n5cknnwTg1FNPjT+RsG3btowdO5Y6derwwQcfMHDgQN555x1atmzJ+++/TxAEPPvsszzyyCP86U9/\nip/7iSee4K233mLGjNjc2htvvDEergGee+45TjnlFHbs2EFaWhpdunQhOzubDRs2xG8K3Lp1K1Wr\nVuWJJ55g1KhRpKYe8v+V+jHq1ILV62KjuXsV91aIJbFmSUXikCE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x+HJmZiYAa9euJTExkdtvv520tDRDtPQT4r8rSZK+r2DnFYCG0GDATh6YUJHLL7+cPXv2\nULZsWcaMGcNdd91Fs2bNOOWUU6hXrx5VqlQBYjfX9erVi5EjR9K+ffv4+rlz5/LHP/6RsmXLUrFi\nxcMfid6vpmUVYOjuHZTq24eyick89dRTzJgxg0aNGnH66aeTlpYWP/S2226jW7duPPPMM1x55ZXx\n9X/961/585//TNmyZTn99NO56667OOmkkxg7diz169enbt268S4vZ5111kGvdfTo0dx8880kJSWR\nl5dH69atGTt2LI899hhz5syhVKlSNGzYkCuuuOIIvhRJxYndOSRJ3xeh80pOTg4VK1YkLy+Pzp07\n07dvXzp37kxubi4nnngiQRDw4osv8pe//CXeyvBo13S0HOxaJf10HG53DkeiJUnfV6cWrF7335Fo\nOGjnleHDhzN79mx27tzJ5ZdfztVXXw3AokWLGDRoEGEYUrVqVZ577rljVtPRcrBrlXT8cSRakvR9\ne+cf98mFusTC6oQKRXvjaHGsqRDl5eXZvUMqBhyJliT9eHtD6Yi7Yc362GjvqJFFG1aLY00R3H//\n/bzwwgtUr16dmjVr0rRpU2bOnElKSgoLFizguuuu4/rrrz/gQ1u++eYbBg8ezPLly9m1axfDhw+n\nU6dOTJw4kVdffZXc3FzWrl1L586deeSRR4r4SqXjgyFaknRgPXoWv4BaHGs6DHsfwpKVlcWuXbto\n0qQJTZs2BeC7775j77+29ujRg1tvvZWWLVuyfv162rVrx8qVKxk5ciSXXHIJzz33HFu3bqVZs2Zc\neumlQKwTyJIlSyhXrhx169Zl8ODB1KxZs8iuVTpeGKIlSTrK3n33XTp16kT58uUpX748V111VXxb\n9+7d469nz57NRx99FF/e+9CWf/zjH7z66quMGjUKiPW63jta3bZt23iXkAYNGrBu3TpDtHQMGKIl\nSSpCJ510Uvz13oe2lC9ffp99wjBk+vTp1K1bd5/1H3zwAeXKlYsvly5dmry8vKNbsCTAh61IknR0\nFHi6YotHH+a1Sc+zc+dOcnJymDlz5gEPOdhDW9q1a8fj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ZmZjXyItrMzMzMrEZeRJuZmZmZ1ciLaDMzMzOzGnkRbWZmZmZWIy+izczMzMxq\n5EW0mZmZmVmNvIg2MzMzM6vRvwEyPu4Vjj3QAAAAAElFTkSuQmCC\n", "text/plain": [ "
" ] }, "metadata": { "tags": [] } } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "V7h2GmytRiqq", "colab": { "base_uri": "https://localhost:8080/", "height": 85 }, "outputId": "1a4fac8b-d815-4444-da85-cfbde23d1482" }, "source": [ "w2v_model.wv['sky'], w2v_model.wv['sky'].shape" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(array([-0.8185685 , 0.3990876 , 0.02527559, -0.46214026, 0.20842107,\n", " 0.75595075, -0.6801817 , -0.65128547, 0.23382275, 0.45040986,\n", " 0.97311556, 1.0209339 , 1.1465698 , 0.6203446 , 1.0008206 ],\n", " dtype=float32), (15,))" ] }, "metadata": { "tags": [] }, "execution_count": 5 } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "3INpkJK4Riqs", "colab": { "base_uri": "https://localhost:8080/", "height": 669 }, "outputId": "bebb8557-724d-40c7-bf92-bbeb5e0bf737" }, "source": [ "vec_df = pd.DataFrame(wvs, index=words)\n", "vec_df" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
01234567891011121314
sky-0.8185690.3990880.025276-0.4621400.2084210.755951-0.680182-0.6512850.2338230.4504100.9731161.0209341.1465700.6203451.000821
blue-0.3477050.484645-0.1239740.0183030.0180640.105478-0.628725-0.6259350.3371930.1793940.5679420.6256560.7720770.3088620.901417
beautiful-0.6317150.489223-0.039816-0.5605390.0274710.600807-0.557207-0.4532790.3330050.1946620.7005851.1444481.1312760.6473050.927159
quick-0.3779690.8753230.5050430.503531-0.134367-0.698876-0.5831880.0533570.3807720.7800730.057065-0.1840940.876488-0.2644861.617072
brown-0.4265620.5525250.4240010.568211-0.203049-0.795935-0.452301-0.1618560.3443700.9997540.189382-0.3068230.981535-0.4551291.527002
fox-0.5447430.9373190.3759540.619939-0.003503-0.792852-0.5751070.0106810.4323670.7602730.179154-0.0375240.739188-0.4555031.631031
lazy-0.3232500.8079960.3870090.3918660.059279-0.911020-0.712572-0.3556190.5133441.1313280.164242-0.4315850.981779-0.3827201.228837
dog-0.4972150.9465470.3817370.588996-0.123029-0.604750-0.472004-0.0145170.4939090.6397420.004018-0.1080500.898489-0.5542021.574298
love-0.631920-0.1595860.422561-0.758301-0.1407330.286956-0.247963-0.1876840.3905430.2906380.5008500.8307100.0135550.3539550.172023
sausages-0.576832-0.4581221.026463-1.154116-0.650626-0.082031-0.1895360.3698720.9955660.4192340.6932590.424213-0.0447900.0796870.145574
ham-0.877566-0.4823930.929203-1.328698-0.354967-0.043935-0.0039590.1602861.060482-0.0058150.6005830.4414040.137963-0.2050290.152584
bacon-0.976038-0.4637930.790353-0.859319-0.761272-0.040525-0.3448470.3350681.1914780.1813900.4412300.578118-0.040014-0.2331150.004666
eggs-0.840217-0.6290781.017721-1.310373-0.434270-0.297177-0.3302150.2293460.7032440.3180450.6074170.403582-0.140879-0.1052370.229215
jumps-0.3413780.6709750.4378490.494568-0.176995-0.914049-0.521373-0.0262360.4753100.806331-0.020645-0.3409280.754797-0.4867471.407213
kings-0.893495-0.4693361.210493-1.258101-0.816817-0.789102-0.2318740.4289631.0991320.2188360.0672660.3148670.015383-0.1801450.182565
breakfast-0.772558-0.5583121.258790-0.937237-0.905406-0.658782-0.1750290.6246101.1189710.5018620.5349450.137323-0.297624-0.4150960.298166
toast-0.753567-0.5693721.248701-1.184956-0.870067-0.782575-0.1379690.5732661.2467480.4341660.1617410.153767-0.128329-0.1778720.331763
beans-0.924956-0.5033581.221505-1.280991-0.901741-0.725451-0.0424560.4668201.1982790.3217500.1843010.354623-0.019580-0.1724240.229652
green-0.496436-0.4830390.496584-1.251115-0.488516-0.087176-0.4779040.0810750.932781-0.0924160.3587100.6119740.1584560.3806350.238793
today-0.6822140.4544010.012207-0.301444-0.0041280.539051-0.560557-0.6694680.2366890.2979530.7855351.2063580.8644460.6356670.865603
\n", "
" ], "text/plain": [ " 0 1 2 ... 12 13 14\n", "sky -0.818569 0.399088 0.025276 ... 1.146570 0.620345 1.000821\n", "blue -0.347705 0.484645 -0.123974 ... 0.772077 0.308862 0.901417\n", "beautiful -0.631715 0.489223 -0.039816 ... 1.131276 0.647305 0.927159\n", "quick -0.377969 0.875323 0.505043 ... 0.876488 -0.264486 1.617072\n", "brown -0.426562 0.552525 0.424001 ... 0.981535 -0.455129 1.527002\n", "fox -0.544743 0.937319 0.375954 ... 0.739188 -0.455503 1.631031\n", "lazy -0.323250 0.807996 0.387009 ... 0.981779 -0.382720 1.228837\n", "dog -0.497215 0.946547 0.381737 ... 0.898489 -0.554202 1.574298\n", "love -0.631920 -0.159586 0.422561 ... 0.013555 0.353955 0.172023\n", "sausages -0.576832 -0.458122 1.026463 ... -0.044790 0.079687 0.145574\n", "ham -0.877566 -0.482393 0.929203 ... 0.137963 -0.205029 0.152584\n", "bacon -0.976038 -0.463793 0.790353 ... -0.040014 -0.233115 0.004666\n", "eggs -0.840217 -0.629078 1.017721 ... -0.140879 -0.105237 0.229215\n", "jumps -0.341378 0.670975 0.437849 ... 0.754797 -0.486747 1.407213\n", "kings -0.893495 -0.469336 1.210493 ... 0.015383 -0.180145 0.182565\n", "breakfast -0.772558 -0.558312 1.258790 ... -0.297624 -0.415096 0.298166\n", "toast -0.753567 -0.569372 1.248701 ... -0.128329 -0.177872 0.331763\n", "beans -0.924956 -0.503358 1.221505 ... -0.019580 -0.172424 0.229652\n", "green -0.496436 -0.483039 0.496584 ... 0.158456 0.380635 0.238793\n", "today -0.682214 0.454401 0.012207 ... 0.864446 0.635667 0.865603\n", "\n", "[20 rows x 15 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 6 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "gTCiDguVRiqu" }, "source": [ "### Looking at term semantic similarity" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "S63FJhi2Riqv", "colab": { "base_uri": "https://localhost:8080/", "height": 689 }, "outputId": "ca368c74-ffbc-4b7a-9d48-25c2b3aa9358" }, "source": [ "import numpy as np\n", "from sklearn.metrics.pairwise import cosine_similarity\n", "\n", "similarity_matrix = cosine_similarity(vec_df.values)\n", "similarity_df = pd.DataFrame(similarity_matrix, index=words, columns=words)\n", "similarity_df" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
skybluebeautifulquickbrownfoxlazydoglovesausageshambaconeggsjumpskingsbreakfasttoastbeansgreentoday
sky1.0000000.9163220.9788840.4311730.4052900.4325290.4062960.4197920.7230050.3481530.3750510.3231780.3334580.3079800.1514950.1018260.1055310.1637780.4739980.980911
blue0.9163221.0000000.9125990.6249230.5936100.6371910.6043660.6245080.5299500.2036640.2207180.2150740.1856980.5298820.0914090.0447720.0524840.0910670.3622480.929598
beautiful0.9788840.9125991.0000000.4154920.3648760.4101710.3614480.4082510.7269560.3656980.3982610.3502960.3343640.2858940.1973000.1120350.1406270.2069210.5338980.981693
quick0.4311730.6249230.4154921.0000000.9747760.9865620.9489870.9850030.0788480.1472200.0953420.1397360.1408570.9806320.2389640.2684260.2658570.2292780.0768540.411802
brown0.4052900.5936100.3648760.9747761.0000000.9661860.9668260.9641260.0594480.1505360.1035890.1450330.1563640.9832290.2399370.2900210.2744470.2367940.0504160.374005
fox0.4325290.6371910.4101710.9865620.9661861.0000000.9408850.9886800.0868270.1250540.0940650.1467980.1341350.9739220.2190310.2606230.2420120.2113700.0515430.421023
lazy0.4062960.6043660.3614480.9489870.9668260.9408851.0000000.9319630.0595540.1346830.0880380.1187330.1415150.9656760.2245700.2511420.2496510.2100590.0557520.368139
dog0.4197920.6245080.4082510.9850030.9641260.9886800.9319631.0000000.0522530.1082740.0956220.1458490.1048350.9729040.2150230.2427030.2338130.2076810.0419350.404676
love0.7230050.5299500.7269560.0788480.0594480.0868270.0595540.0522531.0000000.7994700.7929940.7676790.7965660.0054660.6250800.5769930.5828720.6442430.8414800.741229
sausages0.3481530.2036640.3656980.1472200.1505360.1250540.1346830.1082740.7994701.0000000.9446560.9404540.9624460.1472890.9107870.9244280.9194230.9308420.8979560.339160
ham0.3750510.2207180.3982610.0953420.1035890.0940650.0880380.0956220.7929940.9446561.0000000.9349620.9511320.1014500.8990740.8648120.8759900.9129170.9003960.355246
bacon0.3231780.2150740.3502960.1397360.1450330.1467980.1187330.1458490.7676790.9404540.9349621.0000000.9194700.1562600.9125290.9072250.8976330.9242670.8765850.331912
eggs0.3334580.1856980.3343640.1408570.1563640.1341350.1415150.1048350.7965660.9624460.9511320.9194701.0000000.1550020.9266070.9179200.9153590.9314240.8886420.315476
jumps0.3079800.5298820.2858940.9806320.9832290.9739220.9656760.9729040.0054660.1472890.1014500.1562600.1550021.0000000.2822320.3199140.3154540.2698760.0550420.286493
kings0.1514950.0914090.1973000.2389640.2399370.2190310.2245700.2150230.6250800.9107870.8990740.9125290.9266070.2822321.0000000.9514720.9858160.9945380.8398870.158965
breakfast0.1018260.0447720.1120350.2684260.2900210.2606230.2511420.2427030.5769930.9244280.8648120.9072250.9179200.3199140.9514721.0000000.9778870.9636280.7518280.097428
toast0.1055310.0524840.1406270.2658570.2744470.2420120.2496510.2338130.5828720.9194230.8759900.8976330.9153590.3154540.9858160.9778871.0000000.9903080.8052780.105260
beans0.1637780.0910670.2069210.2292780.2367940.2113700.2100590.2076810.6442430.9308420.9129170.9242670.9314240.2698760.9945380.9636280.9903081.0000000.8380450.171681
green0.4739980.3622480.5338980.0768540.0504160.0515430.0557520.0419350.8414800.8979560.9003960.8765850.8886420.0550420.8398870.7518280.8052780.8380451.0000000.484399
today0.9809110.9295980.9816930.4118020.3740050.4210230.3681390.4046760.7412290.3391600.3552460.3319120.3154760.2864930.1589650.0974280.1052600.1716810.4843991.000000
\n", "
" ], "text/plain": [ " sky blue beautiful ... beans green today\n", "sky 1.000000 0.916322 0.978884 ... 0.163778 0.473998 0.980911\n", "blue 0.916322 1.000000 0.912599 ... 0.091067 0.362248 0.929598\n", "beautiful 0.978884 0.912599 1.000000 ... 0.206921 0.533898 0.981693\n", "quick 0.431173 0.624923 0.415492 ... 0.229278 0.076854 0.411802\n", "brown 0.405290 0.593610 0.364876 ... 0.236794 0.050416 0.374005\n", "fox 0.432529 0.637191 0.410171 ... 0.211370 0.051543 0.421023\n", "lazy 0.406296 0.604366 0.361448 ... 0.210059 0.055752 0.368139\n", "dog 0.419792 0.624508 0.408251 ... 0.207681 0.041935 0.404676\n", "love 0.723005 0.529950 0.726956 ... 0.644243 0.841480 0.741229\n", "sausages 0.348153 0.203664 0.365698 ... 0.930842 0.897956 0.339160\n", "ham 0.375051 0.220718 0.398261 ... 0.912917 0.900396 0.355246\n", "bacon 0.323178 0.215074 0.350296 ... 0.924267 0.876585 0.331912\n", "eggs 0.333458 0.185698 0.334364 ... 0.931424 0.888642 0.315476\n", "jumps 0.307980 0.529882 0.285894 ... 0.269876 0.055042 0.286493\n", "kings 0.151495 0.091409 0.197300 ... 0.994538 0.839887 0.158965\n", "breakfast 0.101826 0.044772 0.112035 ... 0.963628 0.751828 0.097428\n", "toast 0.105531 0.052484 0.140627 ... 0.990308 0.805278 0.105260\n", "beans 0.163778 0.091067 0.206921 ... 1.000000 0.838045 0.171681\n", "green 0.473998 0.362248 0.533898 ... 0.838045 1.000000 0.484399\n", "today 0.980911 0.929598 0.981693 ... 0.171681 0.484399 1.000000\n", "\n", "[20 rows x 20 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 7 } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "ngIHnn__Riqx", "colab": { "base_uri": "https://localhost:8080/", "height": 374 }, "outputId": "d05dd28c-5955-4313-ce19-029b87b8dfba" }, "source": [ "feature_names = np.array(words)\n", "similarity_df.apply(lambda row: feature_names[np.argsort(-row.values)[1:4]], \n", " axis=1)" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "sky [today, beautiful, blue]\n", "blue [today, sky, beautiful]\n", "beautiful [today, sky, blue]\n", "quick [fox, dog, jumps]\n", "brown [jumps, quick, lazy]\n", "fox [dog, quick, jumps]\n", "lazy [brown, jumps, quick]\n", "dog [fox, quick, jumps]\n", "love [green, sausages, eggs]\n", "sausages [eggs, ham, bacon]\n", "ham [eggs, sausages, bacon]\n", "bacon [sausages, ham, beans]\n", "eggs [sausages, ham, beans]\n", "jumps [brown, quick, fox]\n", "kings [beans, toast, breakfast]\n", "breakfast [toast, beans, kings]\n", "toast [beans, kings, breakfast]\n", "beans [kings, toast, breakfast]\n", "green [ham, sausages, eggs]\n", "today [beautiful, sky, blue]\n", "dtype: object" ] }, "metadata": { "tags": [] }, "execution_count": 8 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "rTUAkYxQRiqz" }, "source": [ "# The GloVe Model\n", "\n", "The GloVe model stands for Global Vectors which is an unsupervised learning model which can be used to obtain dense word vectors similar to Word2Vec. However the technique is different and training is performed on an aggregated global word-word co-occurrence matrix, giving us a vector space with meaningful sub-structures. This method was invented in Stanford by Pennington et al. and I recommend you to read the original paper on GloVe, _[‘GloVe: Global Vectors for Word Representation’ by Pennington et al.](https://nlp.stanford.edu/pubs/glove.pdf)_ which is an excellent read to get some perspective on how this model works.\n", "\n", "The basic methodology of the GloVe model is to first create a huge word-context co-occurence matrix consisting of (word, context) pairs such that each element in this matrix represents how often a word occurs with the context (which can be a sequence of words). The idea then is to apply matrix factorization to approximate this matrix as depicted in the following figure.\n", "\n", "![](https://github.com/dipanjanS/nlp_workshop_odsc19/blob/master/Module04%20-%20Text%20Representation/glove_arch.png?raw=1)\n", "\n", "Considering the __Word-Context (WC)__ matrix, __Word-Feature (WF)__ matrix and __Feature-Context (FC)__ matrix, we try to factorize __WC = WF x FC__\n", "\n", "Such that we we aim to reconstruct __WC__ from __WF__ and __FC__ by multiplying them. For this, we typically initialize __WF__ and __FC__ with some random weights and attempt to multiply them to get __WC'__ (an approximation of __WC__) and measure how close it is to __WC__. We do this multiple times using Stochastic Gradient Descent (SGD) to minimize the error. Finally, the __Word-Feature matrix (WF)__ gives us the word embeddings for each word where __F__ can be preset to a specific number of dimensions" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "_UFyeFqfRiq0" }, "source": [ "# Robust Glove Model with SpaCy\n", "\n", "Let’s try and leverage GloVe based embeddings for our document clustering task. The very popular spacy framework comes with capabilities to leverage GloVe embeddings based on different language models. You can also get pre-trained word vectors and load them up as needed using gensim or spacy.\n", "\n", "If you have spacy installed, we will be using the __[`en_vectors_web_lg`](https://spacy.io/models/en#en_vectors_web_lg)__ model which consists of 300-dimensional word vectors trained on [Common Crawl](http://commoncrawl.org) with GloVe.\n", "\n", "__Install Instructions:__\n", "\n", "```\n", "# Use the following command to install spaCy\n", "> pip install -U spacy\n", "OR\n", "> conda install -c conda-forge spacy\n", "\n", "C:\\WINDOWS\\system32>python -m spacy download en_vectors_web_lg\n", "Collecting en_vectors_web_lg==2.0.0 from https://github.com/explosion/spacy-models/releases/download/en_vectors_web_lg-2.0.0/en_vectors_web_lg-2.0.0.tar.gz#egg=en_vectors_web_lg==2.0.0\n", " Downloading https://github.com/explosion/spacy-models/releases/download/en_vectors_web_lg-2.0.0/en_vectors_web_lg-2.0.0.tar.gz (661.8MB)\n", " 100% |████████████████████████████████| 661.8MB 392kB/s\n", "Installing collected packages: en-vectors-web-lg\n", " Running setup.py install for en-vectors-web-lg ... done\n", "Successfully installed en-vectors-web-lg-2.0.0\n", "You are using pip version 10.0.1, however version 18.0 is available.\n", "You should consider upgrading via the 'python -m pip install --upgrade pip' command.\n", "\n", " Linking successful\n", " C:\\Anaconda3\\lib\\site-packages\\en_vectors_web_lg -->\n", " C:\\Anaconda3\\lib\\site-packages\\spacy\\data\\en_vectors_web_lg\n", "\n", " You can now load the model via spacy.load('en_vectors_web_lg')\n", "```" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "geEhPWzgTA7K", "colab": { "base_uri": "https://localhost:8080/", "height": 156 }, "outputId": "925b33eb-8de8-417f-d596-da42af328273" }, "source": [ "!python -m spacy download en" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "Requirement already satisfied: en_core_web_sm==2.1.0 from https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-2.1.0/en_core_web_sm-2.1.0.tar.gz#egg=en_core_web_sm==2.1.0 in /usr/local/lib/python3.6/dist-packages (2.1.0)\n", "\u001b[38;5;2m✔ Download and installation successful\u001b[0m\n", "You can now load the model via spacy.load('en_core_web_sm')\n", "\u001b[38;5;2m✔ Linking successful\u001b[0m\n", "/usr/local/lib/python3.6/dist-packages/en_core_web_sm -->\n", "/usr/local/lib/python3.6/dist-packages/spacy/data/en\n", "You can now load the model via spacy.load('en')\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "sz4gggzKTkYx", "colab": { "base_uri": "https://localhost:8080/", "height": 88 }, "outputId": "1b8aa213-261e-46d5-96a6-3ab1d2ac32d2" }, "source": [ "!python -m spacy download en_vectors_web_lg" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "Requirement already satisfied: en_vectors_web_lg==2.1.0 from https://github.com/explosion/spacy-models/releases/download/en_vectors_web_lg-2.1.0/en_vectors_web_lg-2.1.0.tar.gz#egg=en_vectors_web_lg==2.1.0 in /usr/local/lib/python3.6/dist-packages (2.1.0)\n", "\u001b[38;5;2m✔ Download and installation successful\u001b[0m\n", "You can now load the model via spacy.load('en_vectors_web_lg')\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "xCfnNf5fRiq0", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "32218af6-78f9-42f1-fcb4-d46673e47c1d" }, "source": [ "import spacy\n", "\n", "nlp = spacy.load('en_vectors_web_lg')\n", "total_vectors = len(nlp.vocab.vectors)\n", "\n", "print('Total word vectors:', total_vectors)" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "Total word vectors: 1070971\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "5Gf4dcBuRiq2" }, "source": [ "This validates that everything is working and in order. Let’s get the GloVe embeddings for each of our words now in our toy corpus." ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "sd68_DV0Riq3", "colab": { "base_uri": "https://localhost:8080/", "height": 718 }, "outputId": "0beef2e4-3e0e-4290-deee-4163bb2681bc" }, "source": [ "unique_words = list(set([word for sublist in tokenized_corpus for word in sublist]))\n", "\n", "word_glove_vectors = np.array([nlp(word).vector for word in unique_words])\n", "vec_df = pd.DataFrame(word_glove_vectors, index=unique_words)\n", "vec_df" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
0123456789101112131415161718192021222324252627282930313233343536373839...260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
sausages-0.174290-0.064869-0.0469760.287420-0.1281500.6476300.056315-0.240440-0.0250940.5022200.0299360.871290-0.270070-0.8543800.241580-0.2787500.4791000.1326800.2847400.323430-0.1714200.3242200.137840-0.3573100.288720-0.357420-0.602930-0.186040-0.271860-0.8297500.2705500.2257600.352370-0.1967001.0511000.2108400.1150400.2831200.7006701.452400...0.145720-0.327310-0.5948700.6791100.2133801.053200-0.0324280.105010-0.622310-0.1515300.418720-0.3802600.401980-0.007394-0.3997100.896790-0.081613-0.0192130.3608400.0136790.437120-0.148730-0.078819-0.4352400.2499500.6634000.297820-0.332740-0.254690-0.0185950.3022400.195470-0.653980-0.291150-0.684290-0.2663700.304310-0.8068300.6195400.201200
lazy-0.353320-0.299710-0.176230-0.321940-0.3856400.5861100.411160-0.4186800.0730931.4865000.178510-0.364790-0.318890-0.186660-0.176970-0.185470-0.2877900.5796300.413430-0.142170-0.1222500.093341-0.117550-0.4362100.416610-0.1728100.080734-0.5342500.790970-0.175410-0.5038000.0929050.000162-0.6364400.0646890.2628300.2663000.183250-0.3974600.109490...-0.1853900.121400-0.077576-0.1889000.394630-0.383130-0.288160-0.3593800.6420200.084683-0.288520-0.443680-0.141780-0.2077100.006349-0.368950-0.082058-0.195290-0.0671790.0013030.468360-0.1757100.1987600.4677800.593930-0.335560-0.072169-0.3698400.1146300.4306700.402310-0.038554-0.288670-0.2441300.4609900.5141700.1362600.344190-0.845300-0.077383
beautiful0.1712000.534390-0.348540-0.0972340.101800-0.1708600.295650-0.041816-0.5165502.1172000.1794500.264130-0.231180-0.328040-0.219140-0.043589-0.4923801.6425000.243910-0.196320-0.2574000.071950-0.027151-0.065426-0.2246500.1590300.545230-0.178430-0.2332000.475470-0.0738680.261460-0.195230-0.0846600.128970-0.4605300.164740-0.450340-0.340450-0.214240...0.5279400.1222100.3678800.1425000.039286-0.4184300.020668-0.153280-0.8524500.400710-1.663700-0.472610-0.488350-0.506310-0.2057500.1008000.075323-0.0227210.207690-0.1126500.589970-0.206480-0.314750-0.220790-0.126200-0.028778-0.1768300.1809900.232540-0.199140-0.2855400.1046700.1263100.1200400.2543800.2474000.2076700.1725800.0638750.350990
blue0.1294500.0365180.032298-0.0600340.399840-0.103020-0.5078800.076630-0.4229200.815730-0.182540-0.072278-0.358110-0.4286600.1088000.1510300.2403602.115100-0.116390-1.058300-0.053679-0.340660-0.3879300.220610-0.345300-0.294830-0.3771700.0407850.1559600.378200-0.2301400.106140-0.083818-0.121110-0.498090-0.3112300.1684200.005281-0.281660-0.335470...0.1589200.486020-0.0871390.601080-0.000770-0.082692-0.1207900.113970-0.492130-0.134840-0.4936000.351470-0.019813-0.0755970.369570-0.309500-0.2186000.3353400.106540-0.123870-0.140150-0.312630-0.524710-0.285340-0.2776200.0804870.0600310.5390100.554740-0.147810-0.5012800.1690100.548250-0.319380-0.0728870.3829500.2374100.0522890.1820600.412640
fox-0.348680-0.0777200.177750-0.094953-0.4528900.2377900.2094400.0378860.0350640.899010-0.517950-0.547580-0.673110-0.131650-0.5854200.312030-0.0941210.1400500.028915-0.242290-0.0915250.1332100.114760-0.545550-0.046149-0.022279-0.9948700.240050-0.113700-0.117760-0.4784000.2789800.659960-0.0823580.075643-0.3083800.134520-0.122870-0.243410-0.010298...0.5207000.1528500.196420-0.1764300.433690-0.4920500.0446670.195000-0.4313900.167560-0.8964500.5510100.0042030.3283600.3435800.185590-0.1464400.156990-0.5077900.0710180.1568700.2301500.0878280.2700500.2664100.291140-0.132860-0.254160-0.095441-0.174040-0.2830500.270240-0.6548000.105300-0.068738-0.5347500.0617830.123610-0.553700-0.544790
jumps-0.3348400.215990-0.350440-0.2600200.4110700.154010-0.3861100.2063800.3867001.4605000.355940-0.057770-0.3100300.533790-0.0163810.3668000.3605800.362340-0.101930-0.365060-0.5466500.5633700.229860-0.7579400.3202400.1325300.343050-0.2408400.044138-0.535110-0.2579300.346190-0.071962-0.2134400.332550-0.4043500.107330-0.0276240.289630-0.225150...0.1349100.3433900.048773-0.5176800.2120400.0518970.1420100.2257100.3520200.398190-0.202120-0.097922-0.024712-0.1009500.000120-0.4435600.097503-0.1289800.123670-0.291170-0.302130-0.337530-0.3722900.2016400.4195700.020038-0.1711600.4335600.1797100.089282-0.107030-0.279480-0.186200-0.543140-0.479980-0.2846800.0360220.1902900.692290-0.071501
dog-0.0571200.0526850.003026-0.0485170.0070430.041856-0.024704-0.0397830.0096140.308416-0.0891300.041381-0.0956400.033153-0.0487140.0260330.0714080.1519690.020897-0.064305-0.059467-0.0227010.038028-0.0694760.051839-0.006171-0.034795-0.0593600.012666-0.036328-0.0791830.017406-0.0118750.0783300.0517650.0218390.079245-0.128954-0.0069800.054850...0.005369-0.053469-0.016607-0.095284-0.0065780.055140-0.004591-0.007719-0.0645240.027798-0.0418980.1208600.0147080.0137920.0161210.0561610.0083940.0312470.026793-0.022593-0.0146450.0471500.008740-0.0424360.0632810.0672890.037409-0.0262950.020831-0.0044800.003257-0.036864-0.0438780.000249-0.0269590.0778950.0443500.0351070.042551-0.010643
breakfast0.0733780.2276700.208420-0.456790-0.0782190.601960-0.024494-0.4679800.0546272.283700-0.681900-0.037457-0.3243900.4424100.197600-0.508460-0.1961101.3134000.190610-0.050723-0.4587900.169650-0.265080-0.4399400.294480-0.395270-0.476240-0.436840-0.561440-0.1918500.1831800.1571900.2812400.0331120.0645990.271460-0.337520-0.229610-0.5393400.577800...-0.199320-0.3444300.108090-0.078482-0.1408500.065754-0.3350800.058111-1.5304000.123050-0.637070-0.1524800.308960-0.317620-0.1011600.7912000.111700-0.4490600.3368300.9068600.409920-0.610050-0.2464300.0674080.3448400.2858700.5430300.127340-0.4869700.9681100.6477100.3738200.019931-0.033672-0.0731840.2968300.340420-0.599390-0.0611140.232200
today-0.1565700.594890-0.031445-0.0775860.278630-0.509210-0.066350-0.081890-0.0479862.803600-0.131820-0.6271000.253880-0.472910-0.4287500.143520-0.1181800.679740-0.006229-0.001527-0.163790-0.0340670.2710200.054245-0.0117520.324470-0.321400-0.003704-0.2053900.0570080.177460-0.1660300.505320-0.2152000.0105030.5983900.0213470.1036500.0233870.097183...0.5531600.100970-0.3866300.012178-0.181380-0.381290-0.200480-0.3474700.7027800.299910-0.1981500.041330-0.1699900.346500-0.1355200.162350-0.1451500.2897700.095377-0.1138400.372630-0.1161700.0915930.126660-0.0352430.2611400.235310-0.323920-0.3380500.134510-0.326580-0.4133800.367910-0.262630-0.203690-0.296560-0.014873-0.250060-0.1159400.083741
green-0.0723680.2332000.137260-0.1566300.2484400.349870-0.241700-0.091426-0.5301501.341300-0.867850-0.131830-0.596790-0.344150-0.161210-0.0009250.5326702.1329000.021933-0.5193300.365570-0.012978-0.2715400.004896-0.118490-0.383380-0.4894400.4914700.136640-0.096163-0.0284290.0039630.155420-0.296800-0.148950-0.0553110.3000300.163760-0.169410-0.101660...-0.0637010.2685500.1253700.6033300.340680-0.364250-0.353150-0.432980-0.4208600.157040-0.2555200.1689500.079552-0.3151300.085769-0.0790490.0004990.4155100.1306200.2186900.170560-0.236900-0.3907400.059123-0.0802290.1195700.3729400.3898000.427670-0.112340-0.4051700.2435700.437300-0.461520-0.3527100.3362500.069899-0.1115500.5329300.712680
kings0.259230-0.8546900.360010-0.6420000.568530-0.3214200.1732500.133030-0.0897201.528600-0.289030-0.6587400.179310-0.0774450.086629-0.1327700.1895900.2432700.1944700.2561900.1157500.441050-0.3283500.151940-0.8687900.175560-0.1905400.382940-0.1240400.7845000.1118300.642220-0.2682600.053329-0.0554950.750070-0.644130-0.276970-0.1087000.445280...0.7562500.262880-0.0399650.037196-0.183640-0.3605400.5156700.445350-0.2355700.452830-0.2977700.626260-0.544090-0.003886-0.076988-0.6236400.1026500.178460-0.097651-0.2901800.5301200.071785-0.056542-0.0114500.0530020.3237200.379260-0.3120000.007405-0.572380-0.4700900.063743-0.545210-0.192310-0.3010201.0685000.231160-0.1473300.662490-0.577420
sky0.312550-0.3030800.019587-0.3549400.100180-0.141530-0.5142700.886110-0.5305401.556600-0.001385-0.214260-0.5242800.104250-0.3398100.1864000.0229811.186600-0.180270-0.8919300.0846580.173110-0.3804100.311870-0.259610-0.097019-0.0149940.0512640.0681530.035770-0.1237900.201050-0.246020-0.070083-0.265050-0.012957-0.184630-0.1298900.084696-0.021024...0.2639000.1085300.030066-0.1751700.155300-0.880140-0.1957200.157680-0.5571300.342250-0.7223800.326730-0.0515120.0900800.3787300.013059-0.239590-0.3689000.010188-0.1581600.291280-0.962140-0.925610-0.122720-0.022322-0.1520500.5956700.3974700.487380-0.147080-0.6670500.2791100.500970-0.277580-0.1437200.3427100.2875800.5377400.3634900.496920
brown-0.374120-0.0762640.1092600.1866200.0299430.182700-0.6319800.133060-0.1289800.603430-0.680410-0.142220-0.133570-0.6594300.0524020.1674500.6392301.7680000.346190-0.624800-0.128670-0.197030-0.3745000.3306400.046832-0.653510-0.5613600.2273700.229230-0.415810-0.1676700.3354300.097227-0.467030-0.026857-0.067665-0.192110-0.1337400.016268-0.208270...0.1174500.0550160.2368200.898190-0.4096900.075218-0.110300-0.409910-0.9571800.527490-0.0426550.2661800.305260-0.519010-0.460440-0.0938340.1300600.0192680.0101980.0075830.2955100.231560-0.034880-0.116940-0.3273300.2049100.4750000.513100-0.145760-0.185080-0.0154040.392890-0.034826-0.720300-0.3653200.7405100.108390-0.365760-0.2881900.114630
bacon-0.430730-0.0160250.4846200.101390-0.2992000.761820-0.353130-0.3252900.1567300.873210-0.0782800.277670-0.5242100.103800-0.018345-0.3975000.2308100.9401400.2756500.3144900.1579100.219310-0.328710-0.187760-0.209180-0.528110-0.4254600.0202780.124110-0.553420-0.5665700.7687100.329540-0.1533100.6313600.108580-0.1261600.1025000.2918101.129400...0.011195-0.129620-0.5188300.0153170.2493400.594640-0.209520-0.364450-0.853380-0.1962300.425870-0.3434100.6228900.374680-0.7684400.6329300.026989-0.281060-0.0108660.1823100.437670-0.2784900.109020-0.1773700.0254080.4807500.5862300.424540-0.2477700.1656900.3042400.413440-0.540730-0.035930-0.429450-0.2465900.161490-1.065400-0.2449400.269540
eggs-0.417810-0.035192-0.126150-0.215930-0.6697400.513250-0.797090-0.0686110.6346601.256300-0.8091000.012227-0.2616900.2605600.260520-0.185840-0.2857301.359900-0.115380-0.019430-0.0330520.386340-0.520640-0.325880-0.255210-0.614430-0.3660000.1339000.049880-1.093600-0.436250-0.2798000.525020-0.4500400.1963300.6118600.004073-0.3819500.3578300.753130...-0.0166940.168550-0.1350300.072960-0.0653440.768680-0.194850-0.313310-0.552650-0.4884900.470120-0.3965500.688490-0.017080-1.0377000.081078-0.4143300.1886000.2165100.6036000.943960-0.288970-0.023022-0.616510-0.1801300.4050200.043301-0.438280-0.6842500.209900-0.232860-0.139740-0.681080-0.370920-0.5455100.0737280.111620-0.3247000.0597210.159160
beans-0.423290-0.2645000.2008700.0821870.0669441.027600-0.989140-0.2599500.1459600.766450-0.5810100.317560-0.043719-0.8473300.188070-0.7168800.0095221.1241000.4372200.371400-0.208100-0.380830-0.2825300.159310-0.096079-0.959680-0.2220800.2999100.163690-0.7104100.242250-0.0720450.802380-0.477040-0.3138300.3322200.2775700.2600400.2787500.600180...-0.157950-0.2334300.0098330.106710-0.3240400.290390-0.099283-0.294040-0.5645500.2308600.647260-0.2448200.223060-0.353680-0.6334400.286330-0.4485000.0026100.6879000.7073100.353040-0.4514200.177150-0.5991300.2764500.7769000.7761900.4492900.172610-0.1848700.0487600.351680-0.786260-0.368790-0.5286400.287650-0.273120-1.1140000.0643220.223620
toast0.130740-0.1937300.2532700.090102-0.272580-0.0305710.096945-0.1150600.4840000.848380-0.0766590.018191-0.2412200.283660-0.1048400.246110-0.1439100.665090-0.140930-0.278840-0.205080-0.419600-0.690090-0.053260-0.144340-0.0390000.298490-0.279020-0.355020-0.702110-0.2389800.4287900.3339400.0890140.0054720.1782700.014409-0.3172700.1340300.863840...-0.2848000.2279200.1396900.382060-0.3554800.238640-0.2014500.166890-0.6968600.6018100.053797-0.0243510.9172200.472390-0.5955900.690710-0.2472700.1631000.3353200.5247000.196020-0.3986600.4004700.066376-0.1836700.4836200.7015900.019826-0.1628800.9158700.1420800.4819100.0451670.057151-0.149520-0.495130-0.086677-0.569040-0.3592900.097443
love0.1394900.534530-0.252470-0.1256500.0487480.1524400.199060-0.0659700.1288302.055900-0.443390-0.1017400.271180-0.124290-0.4575300.055878-0.3039500.795430-0.2906000.5190800.169090-0.3274300.206060-0.0838850.1969600.3284700.087838-0.1278400.3097900.206920-0.0858550.547900-0.4234400.3457300.229210-0.2153800.091481-0.278010-0.341830-0.130670...0.0557320.319250-0.2629300.3068300.0944070.3444000.3032400.417650-0.1042700.418500-0.678930-0.160720-0.820230-0.014762-0.091018-0.008955-0.026122-0.1345500.1926300.331130-0.152970-0.368220-0.146490-0.3154700.1396300.2988500.6719800.220490-0.182790-0.248110-0.1243800.178440-0.0994690.0086820.089213-0.075513-0.049069-0.0152280.0884080.302170
ham-0.773320-0.2825400.5807600.8414800.2585400.585210-0.021890-0.4636800.1390700.658720-0.1686700.192260-0.398010-0.600910-0.241560-0.0887100.0990110.8386800.3971700.2880500.477210-0.160270-0.0115550.400100-0.178950-0.941120-0.5386500.2846900.204900-0.603190-0.7678800.3310200.508820-0.1660500.7299800.247200-0.409570-0.030922-0.1473501.449700...-0.083141-0.4127000.0571520.079565-0.3703700.430860-0.237530-0.541830-0.9269100.0898180.516110-0.1746900.4827300.580170-0.6930600.632340-0.079333-0.0087980.0583710.006332-0.053509-0.0748030.1396800.3919600.5846100.2755200.7553700.418950-0.1832800.4523200.4644700.481400-0.8292000.3549100.224530-0.4939200.456930-0.649100-0.1319300.372040
quick-0.4456300.191510-0.2492100.4659000.1619500.212780-0.0464800.0211700.4176601.686900-0.451860-0.0118070.072409-0.114820-0.534210-0.150700-0.3529601.711500-0.211920-0.400470-0.4204100.0760420.023444-0.7448100.0064990.4435100.484180-0.5410200.375070-0.5955500.0970290.119870-0.079122-0.053397-0.0288880.1575100.1641000.288250-0.065758-0.240000...-0.185420-0.0649130.242120-0.6749200.394260-0.3042800.1030000.3369200.3004300.3496900.3536600.017008-0.170840-0.286230-0.1069200.3134000.307010-0.1039200.2315900.0271260.1697100.010259-0.1713800.448950-0.293820-0.312280-0.0541890.069783-0.104720-0.124150-0.3294600.421860-0.0395430.1501800.3382200.0495540.149420-0.038789-0.0190690.348650
\n", "

20 rows × 300 columns

\n", "
" ], "text/plain": [ " 0 1 2 ... 297 298 299\n", "sausages -0.174290 -0.064869 -0.046976 ... -0.806830 0.619540 0.201200\n", "lazy -0.353320 -0.299710 -0.176230 ... 0.344190 -0.845300 -0.077383\n", "beautiful 0.171200 0.534390 -0.348540 ... 0.172580 0.063875 0.350990\n", "blue 0.129450 0.036518 0.032298 ... 0.052289 0.182060 0.412640\n", "fox -0.348680 -0.077720 0.177750 ... 0.123610 -0.553700 -0.544790\n", "jumps -0.334840 0.215990 -0.350440 ... 0.190290 0.692290 -0.071501\n", "dog -0.057120 0.052685 0.003026 ... 0.035107 0.042551 -0.010643\n", "breakfast 0.073378 0.227670 0.208420 ... -0.599390 -0.061114 0.232200\n", "today -0.156570 0.594890 -0.031445 ... -0.250060 -0.115940 0.083741\n", "green -0.072368 0.233200 0.137260 ... -0.111550 0.532930 0.712680\n", "kings 0.259230 -0.854690 0.360010 ... -0.147330 0.662490 -0.577420\n", "sky 0.312550 -0.303080 0.019587 ... 0.537740 0.363490 0.496920\n", "brown -0.374120 -0.076264 0.109260 ... -0.365760 -0.288190 0.114630\n", "bacon -0.430730 -0.016025 0.484620 ... -1.065400 -0.244940 0.269540\n", "eggs -0.417810 -0.035192 -0.126150 ... -0.324700 0.059721 0.159160\n", "beans -0.423290 -0.264500 0.200870 ... -1.114000 0.064322 0.223620\n", "toast 0.130740 -0.193730 0.253270 ... -0.569040 -0.359290 0.097443\n", "love 0.139490 0.534530 -0.252470 ... -0.015228 0.088408 0.302170\n", "ham -0.773320 -0.282540 0.580760 ... -0.649100 -0.131930 0.372040\n", "quick -0.445630 0.191510 -0.249210 ... -0.038789 -0.019069 0.348650\n", "\n", "[20 rows x 300 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 12 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "fSKGVbkeRiq5" }, "source": [ "We can now use t-SNE to visualize these embeddings similar to what we did using our Word2Vec embeddings." ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "j6aSesHGRiq5", "colab": { "base_uri": "https://localhost:8080/", "height": 378 }, "outputId": "50fb644e-cbd4-4937-ac07-25b83d2f331c" }, "source": [ "tsne = TSNE(n_components=2, random_state=42, n_iter=5000, perplexity=3)\n", "np.set_printoptions(suppress=True)\n", "T = tsne.fit_transform(word_glove_vectors)\n", "labels = unique_words\n", "\n", "plt.figure(figsize=(12, 6))\n", "plt.scatter(T[:, 0], T[:, 1], c='red', edgecolors='r')\n", "for label, x, y in zip(labels, T[:, 0], T[:, 1]):\n", " plt.annotate(label, xy=(x+1, y+1), xytext=(0, 0), textcoords='offset points')" ], "execution_count": null, "outputs": [ { "output_type": "display_data", "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAtEAAAFpCAYAAABauHSCAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAIABJREFUeJzs3XucznX+//HHZ2jT5FQpnTC0JcwJ\nM6hxikp9VUgljS1ZbAelvr+Uol1ba7f62q1VSlol7fRNUdrV2UbOZUYjilbaGZItdrGmoYz5/P6Y\ncX3Hobgy5hrjcb/d5nZdn/fncL3eo9HTe96f9ycIwxBJkiRJBy4u1gVIkiRJhxtDtCRJkhQlQ7Qk\nSZIUJUO0JEmSFCVDtCRJkhQlQ7QkSZIUJUO0JEmSFCVDtCRJkhQlQ7QkSZIUJUO0JEmSFKXqsS7g\nQNSrVy9MSEiIdRmSJEmq4nJycjaGYXji/o47LEJ0QkIC2dnZsS5DkiRJVVwQBPkHcpzTOSRJkqQo\nGaIlSZKkKBmiJUmSpCgZoiVJkqQoGaIlSZKkKBmiJUmSpCgZoiVJkiqhb775hu7du5OSkkJiYiJT\npkzhvvvuIz09ncTERAYPHkwYhgB07tw5shzwxo0b2fV8jY8//pg2bdqQmppKcnIyq1atAqBnz560\nbt2aFi1aMGHChMhnTpw4kbPOOos2bdowaNAghgwZAsCGDRvo3bs36enppKenM3/+fADee+89UlNT\nSU1NpWXLlmzdurWivj0xd1isEy1JknSkefPNN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BI9frYD25P8BngB2NB+YfYmuQN4jO6W8vVV9W+AJB+la7gmgO1VtXc4U9fL8CPgKrqH0J4D\nPghQVQeSfJruzR3gU1V1YDhT1AxfBU4HdrU7C7ur6iNV5TE5oqrqkBmNnEuA9wGPJvl1q90E3Eq3\n7HEj8ATwnrbvqOdazVmfBHYk+QzwEN1/mGh/fifJNHCArvEeGL+xUJIkSeqTyzkkSZKkPtlES5Ik\nSX2yiZYkSZL6ZBMtSZIk9ckmWpIkSeqTTbQkSZLUJ5toSZIkqU820ZIkSVKf/gPaPplGScU8bAAA\nAABJRU5ErkJggg==\n", "text/plain": [ "
" ] }, "metadata": { "tags": [] } } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Ce6BV5bFRiq7" }, "source": [ "### Looking at term semantic similarity" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "fyO3JzS2Riq8", "colab": { "base_uri": "https://localhost:8080/", "height": 689 }, "outputId": "9b88675d-eb0f-4693-b318-a43bddcaeaf2" }, "source": [ "import numpy as np\n", "from sklearn.metrics.pairwise import cosine_similarity\n", "\n", "similarity_matrix = cosine_similarity(vec_df.values)\n", "similarity_df = pd.DataFrame(similarity_matrix, index=unique_words, columns=unique_words)\n", "similarity_df" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sausageslazybeautifulbluefoxjumpsdogbreakfasttodaygreenkingsskybrownbaconeggsbeanstoastlovehamquick
sausages1.0000000.1682050.1145400.0974430.1333480.0833060.2475290.4152310.0948790.2331690.0700710.0617530.2896250.7291620.5483100.5466470.4980620.1469170.6227700.162966
lazy0.1682051.0000000.2943080.2305130.2672400.2221120.3016780.3199410.2868020.2309450.1905150.2593610.2568750.2643760.2367690.2456720.2313290.3392800.2225710.400060
beautiful0.1145400.2943081.0000000.4613660.2100500.1425540.2806580.3144720.3141840.3923920.1586150.4280810.3557000.1511570.2144370.1652680.1894820.5947380.1109360.289178
blue0.0974430.2305130.4613661.0000000.3711780.1872630.3140650.1971030.1800880.7640830.2076560.6278000.6830110.2429870.2975460.2613040.2322640.3649610.1854520.220331
fox0.1333480.2672400.2100500.3711781.0000000.2508340.4858550.1650640.1500720.3238000.2267350.3034590.4069120.2100820.2473140.1185370.1633710.2550500.2094540.192686
jumps0.0833060.2221120.1425540.1872630.2508341.0000000.3079610.1038650.1420990.1507520.0866590.2785950.1104000.0910210.1506220.0955440.1190990.1851140.0464320.301096
dog0.2475290.3016780.2806580.3140650.4858550.3079611.0000000.2959570.2237300.2726930.1566740.2190870.3412040.2951230.2912020.2307780.1829940.3587150.2625790.299892
breakfast0.4152310.3199410.3144720.1971030.1650640.1038650.2959571.0000000.2887880.2884370.1618410.2054290.2771580.4877370.4311080.3782150.5134360.2457350.3716880.358221
today0.0948790.2868020.3141840.1800880.1500720.1420990.2237300.2887881.0000000.2467540.2047430.2567700.1474180.1589490.1830480.1476520.1742570.3716500.1041150.370023
green0.2331690.2309450.3923920.7640830.3238000.1507520.2726930.2884370.2467541.0000000.1515190.4883850.6468500.3866640.3920720.4624980.2876080.3273020.2907610.287504
kings0.0700710.1905150.1586150.2076560.2267350.0866590.1566740.1618410.2047430.1515191.0000000.2637850.1934870.1467160.1188180.0927590.1257410.2638050.1276510.127679
sky0.0617530.2593610.4280810.6278000.3034590.2785950.2190870.2054290.2567700.4883850.2637851.0000000.4088440.1729640.2235850.1600640.2163720.3510840.1744960.192515
brown0.2896250.2568750.3557000.6830110.4069120.1104000.3412040.2771580.1474180.6468500.1934870.4088441.0000000.4328910.4669030.4534270.3475780.3319430.3351110.217009
bacon0.7291620.2643760.1511570.2429870.2100820.0910210.2951230.4877370.1589490.3866640.1467160.1729640.4328911.0000000.6205390.6179090.6227010.2922680.7388160.265370
eggs0.5483100.2367690.2144370.2975460.2473140.1506220.2912020.4311080.1830480.3920720.1188180.2235850.4669030.6205391.0000000.5850530.4959350.2541770.4891160.245487
beans0.5466470.2456720.1652680.2613040.1185370.0955440.2307780.3782150.1476520.4624980.0927590.1600640.4534270.6179090.5850531.0000000.4492840.2498910.4957730.251534
toast0.4980620.2313290.1894820.2322640.1633710.1190990.1829940.5134360.1742570.2876080.1257410.2163720.3475780.6227010.4959350.4492841.0000000.2742460.5005860.292624
love0.1469170.3392800.5947380.3649610.2550500.1851140.3587150.2457350.3716500.3273020.2638050.3510840.3319430.2922680.2541770.2498910.2742461.0000000.2181280.292446
ham0.6227700.2225710.1109360.1854520.2094540.0464320.2625790.3716880.1041150.2907610.1276510.1744960.3351110.7388160.4891160.4957730.5005860.2181281.0000000.191665
quick0.1629660.4000600.2891780.2203310.1926860.3010960.2998920.3582210.3700230.2875040.1276790.1925150.2170090.2653700.2454870.2515340.2926240.2924460.1916651.000000
\n", "
" ], "text/plain": [ " sausages lazy beautiful ... love ham quick\n", "sausages 1.000000 0.168205 0.114540 ... 0.146917 0.622770 0.162966\n", "lazy 0.168205 1.000000 0.294308 ... 0.339280 0.222571 0.400060\n", "beautiful 0.114540 0.294308 1.000000 ... 0.594738 0.110936 0.289178\n", "blue 0.097443 0.230513 0.461366 ... 0.364961 0.185452 0.220331\n", "fox 0.133348 0.267240 0.210050 ... 0.255050 0.209454 0.192686\n", "jumps 0.083306 0.222112 0.142554 ... 0.185114 0.046432 0.301096\n", "dog 0.247529 0.301678 0.280658 ... 0.358715 0.262579 0.299892\n", "breakfast 0.415231 0.319941 0.314472 ... 0.245735 0.371688 0.358221\n", "today 0.094879 0.286802 0.314184 ... 0.371650 0.104115 0.370023\n", "green 0.233169 0.230945 0.392392 ... 0.327302 0.290761 0.287504\n", "kings 0.070071 0.190515 0.158615 ... 0.263805 0.127651 0.127679\n", "sky 0.061753 0.259361 0.428081 ... 0.351084 0.174496 0.192515\n", "brown 0.289625 0.256875 0.355700 ... 0.331943 0.335111 0.217009\n", "bacon 0.729162 0.264376 0.151157 ... 0.292268 0.738816 0.265370\n", "eggs 0.548310 0.236769 0.214437 ... 0.254177 0.489116 0.245487\n", "beans 0.546647 0.245672 0.165268 ... 0.249891 0.495773 0.251534\n", "toast 0.498062 0.231329 0.189482 ... 0.274246 0.500586 0.292624\n", "love 0.146917 0.339280 0.594738 ... 1.000000 0.218128 0.292446\n", "ham 0.622770 0.222571 0.110936 ... 0.218128 1.000000 0.191665\n", "quick 0.162966 0.400060 0.289178 ... 0.292446 0.191665 1.000000\n", "\n", "[20 rows x 20 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 14 } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "JC-aQ1IERiq9", "colab": { "base_uri": "https://localhost:8080/", "height": 374 }, "outputId": "caead4fe-3923-423f-9b0f-fbe71bcc6097" }, "source": [ "feature_names = np.array(unique_words)\n", "similarity_df.apply(lambda row: feature_names[np.argsort(-row.values)[1:4]], \n", " axis=1)" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "sausages [bacon, ham, eggs]\n", "lazy [quick, love, breakfast]\n", "beautiful [love, blue, sky]\n", "blue [green, brown, sky]\n", "fox [dog, brown, blue]\n", "jumps [dog, quick, sky]\n", "dog [fox, love, brown]\n", "breakfast [toast, bacon, eggs]\n", "today [love, quick, beautiful]\n", "green [blue, brown, sky]\n", "kings [love, sky, fox]\n", "sky [blue, green, beautiful]\n", "brown [blue, green, eggs]\n", "bacon [ham, sausages, toast]\n", "eggs [bacon, beans, sausages]\n", "beans [bacon, eggs, sausages]\n", "toast [bacon, breakfast, ham]\n", "love [beautiful, today, blue]\n", "ham [bacon, sausages, toast]\n", "quick [lazy, today, breakfast]\n", "dtype: object" ] }, "metadata": { "tags": [] }, "execution_count": 15 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "pU1BAEUWRiq_" }, "source": [ "# The FastText Model\n", "\n", "The FastText model was first introduced by Facebook in 2016 as an extension and supposedly improvement of the vanilla Word2Vec model. Based on the original paper titled _[‘Enriching Word Vectors with Subword Information’](https://arxiv.org/pdf/1607.04606.pdf)_ by Mikolov et al. which is an excellent read to gain an in-depth understanding of how this model works. Overall, FastText is a framework for learning word representations and also performing robust, fast and accurate text classification. The framework is open-sourced by Facebook on [GitHub](https://github.com/facebookresearch/fastText) and claims to have the following.\n", "\n", "- Recent state-of-the-art English word vectors.\n", "- Word vectors for 157 languages trained on Wikipedia and Crawl.\n", "- Models for language identification and various supervised tasks.\n", "\n", "Though I haven't implemented this model from scratch, based on the research paper, following is what I learnt about how the model works. In general, predictive models like the Word2Vec model typically considers each word as a distinct entity (e.g. where) and generates a dense embedding for the word. However this poses to be a serious limitation with languages having massive vocabularies and many rare words which may not occur a lot in different corpora. \n", "\n", "The Word2Vec model typically ignores the morphological structure of each word and considers a word as a single entity. The FastText model ___considers each word as a Bag of Character n-grams___. This is also called as a ___subword model___ in the paper.\n", "\n", "We add special boundary symbols __<__ and __>__ at the beginning and end of words. This enables us to distinguish prefixes and suffixes from other character sequences. We also include the word __w__ itself in the set of its n-grams, to learn a representation for each word (in addition to its character n-grams). \n", "\n", "Taking the word where and __n=3 (tri-grams)__ as an example, it will be represented by the __character n-grams__: ____ and the special sequence __< where >__ representing the whole word. Note that the sequence , corresponding to the word __< her >__ is different from the tri-gram __her__ from the word __where__.\n", "\n", "In practice, the paper recommends in extracting all the n-grams for __n ≥ 3__ and __n ≤ 6__. This is a very simple approach, and different sets of n-grams could be considered, for example taking all prefixes and suffixes. We typically associate a vector representation (embedding) to each n-gram for a word. \n", "\n", "Thus, we can represent a word by the sum of the vector representations of its n-grams or the average of the embedding of these n-grams. Thus, due to this effect of leveraging n-grams from individual words based on their characters, there is a higher chance for rare words to get a good representation since their character based n-grams should occur across other words of the corpus." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "ZTpq6CyxRiq_" }, "source": [ "# Robust FastText Model with Gensim\n", "\n", "The __`gensim`__ package has nice wrappers providing us interfaces to leverage the FastText model available under the `gensim.models.fasttext` module. Let’s apply this once again on our toy corpus." ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "qzrr2AiJRirA", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "953c4f62-98e4-42fc-f68d-dda2bc6c5989" }, "source": [ "from gensim.models.fasttext import FastText\n", "\n", "# Set values for various parameters\n", "feature_size = 15 # Word vector dimensionality \n", "window_context = 20 # Context window size \n", "min_word_count = 1 # Minimum word count \n", "sample = 1e-3 # Downsample setting for frequent words\n", "sg = 1 # skip-gram model\n", "\n", "ft_model = FastText(tokenized_corpus, size=feature_size, \n", " window=window_context, min_count = min_word_count,\n", " sg=sg, sample=sample, iter=5000)\n", "ft_model" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "" ] }, "metadata": { "tags": [] }, "execution_count": 16 } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "iR6sYaASRirB", "colab": { "base_uri": "https://localhost:8080/", "height": 378 }, "outputId": "233613d4-3851-4b02-dc33-1ff52f081948" }, "source": [ "# visualize embeddings\n", "from sklearn.manifold import TSNE\n", "\n", "words = ft_model.wv.index2word\n", "wvs = ft_model.wv[words]\n", "\n", "tsne = TSNE(n_components=2, random_state=42, n_iter=5000, perplexity=5)\n", "np.set_printoptions(suppress=True)\n", "T = tsne.fit_transform(wvs)\n", "labels = words\n", "\n", "plt.figure(figsize=(12, 6))\n", "plt.scatter(T[:, 0], T[:, 1], c='green', edgecolors='k')\n", "for label, x, y in zip(labels, T[:, 0], T[:, 1]):\n", " plt.annotate(label, xy=(x+1, y+1), xytext=(0, 0), textcoords='offset points')" ], "execution_count": null, "outputs": [ { "output_type": "display_data", "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAuAAAAFpCAYAAADdiZ2EAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAIABJREFUeJzs3XlclWX+//H3BZaGe7lkKRw0N+QA\nIqi5oaaZk4pLi4rj9lMnzdFxyq8WzkQWrc7YWJpLjU6FX/2mZWVlM06SaFaAHfcsF6DMKS1MCTWB\n6/cHeEZNywXuw/J6Ph48POc697nvz33P6cybm2sx1loBA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Dx/2dRnlTtLSJI2bMk6ZHTJl\n460yO2Rq3JRx3hA6YMCA8w7ElaRbb71VH3zwgXr06KGrr75aUuF87yEhIYqMjFRoaKh+97vfXVag\nPfeXgqPHjuqtt97SiRMnlJOTo9WrV5/3fbfeequeffZZ73OPxyOp8BeDZ599trDvq6RPP/30kmsq\nKef7Bejrr79WQECAhg0bpqlTp3oHCZ5vhiGXy6X09HRJOqt93759aty4sSZNmqTY2Fht3bpVP/zw\ng2rXrq2AgAB99tln+uijjyQVTsv3wQcfKDs7W3l5eVq5cqV3Pxe6pnv37pXb7da0adMUHR192QG8\nrHwPnW9hIJfLpWnTpikyMlKvvvqq9u7dq9tuu01t2rRR586dvdfkrbfeUrt27dS6dWv16NHDOx3i\nmRYtWqTevXufHtxcX9J4Y8w6STLGrDLGpBtjdhhjxhW1+Rtjlhhjthtjthljphhj7pAUJSnJGOMx\nxlxzwRMq6T4uxfVDH3AA5dErSa/YoKZB1vgZG9Q0qMQGYF6poKAg+o+XgNLY//ZCg4MHDBxgmzZt\najt16mQHDhxoFy5ceFb/Z2sL+0Dfdddd1u1225YtW9rf/e531trC/s7jxo2zoaGhNiQkxN5+++3W\n2sJBmKf7Oltr7e23327XrVvn83P9n2n/Y91utw0PD7dRUVE2NTXVxsfH28aNG9sOHTrYkSNH2oce\neshaa+2uXbus2+22ERERNj4+3tsH/PHHH7chISE2PDzc9urVy3733Xf2xIkT9rbbbrMtWrSwsbGx\nNiYmxnu+CxYssDfddJNt27atHT58uH3wwQettRe+phMnTrStWrWybrfbDh482J44ceKKrkNp/h7a\nv3+/leTt8z5q1Cj79NNP26CgIPvkk096t+vevbt3sOtHH31ku3XrZq219vvvv/cOml20aJH94x//\naK219qGHHrJPP/20ffbZZ22/fv2811DS15Lut6cHTErXFv17jaTtkq6T1EbSv87YplbRv8mSouyv\n5FpTtHGpFxUVZdPS0nxdBgCUez/++KPuuusuffXVV8rPz9ef/vQnTZs2TWlpaapataoGDhyogQMH\n6ssvv9S1116rP/zh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RMQgTAEqx\nC03D1rVrV82aNUtRUSU6TghwTGkYfFccXnnlFc2ZM0c//fST2rVrp3nz5mnJkiV68sknVatWLYWH\nh6ty5cp67rnntHfvXsXFxenHH39UbGysnnnmGeXk5OjgwYO6++67dfToUeXl5en5559X586dfX1q\nFQaDMAEAQIVQHqaf27Vrl5YvX66NGzfK4/HI399fSUlJeuSRR/TRRx9p48aNZ915nzx5siZPnqxt\n27apYcOG3valS5eqV69e8ng82rJliyIiInxxOmc536JIaWlpmjRpko8qKtsI4ABQyp1vHuQzVatW\nzft4xYoVGjlypCTp0KFDGjRokKKjoxUdHa2NGzc6WTZwSUrTsvCX69///rfS09MVHR2tiIgI/fvf\n/9Zf//pXxcTE6Nprr9VVV12lO++807v9pk2bvM+HDh3qbY+OjtbixYuVkJCgbdu2qXr1X13Z3Cei\noqI0Z84cX5dRJhHAAaCU2717tyZMmKBdu3apRo0a3lUof83kyZM1ZcoUpaamauXKlRozZkwJVwpc\nvvIwINhaqxEjRsjj8cjj8Wj37t1KSEi45P106dJF69ev14033qiRI0fqpZdeKv5ir8C+ffvUunVr\nPf300+rTp4+kwuXeR48era5du6px48ZnBfNHHnlEzZs3V6dOnTRkyBDNmjVLkjRnzhyFhIQoLCxM\ngwcP9sm5+AqDMAGglDt3HuSLveO0du1a7dy50/v86NGjysnJOeuOOVBalIcBwbfccotiY2M1ZcoU\n1atXT99//71at26tP/zhD8rOzlb16tW1cuVKud1uSVL79u21cuVK3X333Vq2bJl3P5mZmWrYsKHG\njh2rkydPavPmzRo+fLivTussu3fv1uDBg7VkyRJlZ2frgw8+8L722Wefad26dTp27JiaN2+u8ePH\ny+PxaOXKldqyZYtOnTqlyMhItWnTRpL0xBNPaP/+/apcubKOHDniq1PyCe6AA0Apd755kC/0/Mz5\nkAsKCvTRRx9578YdOHCA8I1SrSxNP3fulIlJS5MUEhKiRx99VLfeeqvCwsLUs2dPHTx4UA8++KDa\ntm2rjh07yuVyqWbNmpKkZ555Rn/9618VFhamPXv2e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7q9wjwnCR1\nFG4nMgAfzcE6hXeoMZ2/Efi10WaO5b+48Y/kbnwMfC3prKTX67lp2/P1+Ddgup/QJtqwHKZ2R5O+\ndAzqNP3jwGlSq2OxKKeQWm1N0ipJ54EF4BvgCnDd9u3apJm3v3Nar98AHuo24pWVAfgyJH0r6ccl\nXi9Spu22UKZS5oH3ew024t+2236CMt18QNIzzYsujz/KI5BGkByOTfrSMZC0FvgCeNP2H81rqdV2\nlshpanUEtu/Y3gZsoswQbO05pF5N9R3Avcz28/+lnaQPgRP141Xg0cblTfVc3F1yNya2r9b3BUnH\nKJ3dNUkbbM/XKeeFXoOcTMNymNptyfa1wXH60nYkraYMFD+xfbSeTq2OYKmcplbHw/Z1SaeApyhL\noKbqXe5m3gY5nZM0BawHfu8l4BWSO+AtLVoz9xIw2NU/C+ypO3g3Uza6fN91fBPqDPBY3RV9P2VT\ny2zPMU0cSWskrRscA7so9TkL7KvN9gHH+4lwog3L4Szwan3CxJPAjcb0fywjfelo6rrYj4DLtj9o\nXEqttjQsp6nV9iQ9IunBevwAsJOytv4UsLs2W1yng/rdDXzn/9kf1+QOeHvvSdpGmdb7GXgDwPZF\nSTPAJcpO6gO27/QW5QSxfVvSQeArYBVwyPbFnsOaRNPAsbpfZQr41PaXks4AM5JeA34BXu4xxnue\npM+AHcDDkuaAt4F3WTqHJ4EXKJuv/gT2dx7wBBiS0x3pS0fyNLAXuFDX1wK8RWp1FMNy+kpqtbUN\nwOH6dJj7gBnbJyRdAj6X9A5wjvLDh/r+saSfKBu39/QR9ErKP2FGRERERHQoS1AiIiIiIjqUAXhE\nRERERIcyAI+IiIiI6FAG4BERERERHcoAPCIiIiKiQxmAR0RERER0KAPwiIiIiIgOZQAeEREREdGh\nvwBoT//nYxH0QgAAAABJRU5ErkJggg==\n", "text/plain": [ "
" ] }, "metadata": { "tags": [] } } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "tzqhdlSpbY8s" }, "source": [ "## Embedding Operations" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "_LplYuHBRirE", "colab": { "base_uri": "https://localhost:8080/", "height": 85 }, "outputId": "00a2d159-159b-4e65-c398-464ffdd3f53c" }, "source": [ "ft_model.wv['sky'], ft_model.wv['sky'].shape" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(array([-1.1745292 , 1.3008002 , -0.62582475, 0.14681922, -0.46031192,\n", " -0.65553826, -0.1245426 , -0.09819014, -0.34571466, 0.23251319,\n", " 0.03760778, 0.8506686 , -0.5574059 , 0.03467001, -0.45169955],\n", " dtype=float32), (15,))" ] }, "metadata": { "tags": [] }, "execution_count": 18 } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "40z0JRH5RirG", "colab": { "base_uri": "https://localhost:8080/", "height": 105 }, "outputId": "4a3f3b54-7f35-41c5-f7b2-5738421a029b" }, "source": [ "print(ft_model.wv.similarity(w1='ham', w2='sky'))\n", "print(ft_model.wv.similarity(w1='ham', w2='sausages'))" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "0.38875625\n", "0.96545506\n" ], "name": "stdout" }, { "output_type": "stream", "text": [ "/usr/local/lib/python3.6/dist-packages/gensim/matutils.py:737: FutureWarning: Conversion of the second argument of issubdtype from `int` to `np.signedinteger` is deprecated. In future, it will be treated as `np.int64 == np.dtype(int).type`.\n", " if np.issubdtype(vec.dtype, np.int):\n" ], "name": "stderr" } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "7iwDNr-lRirI", "colab": { "base_uri": "https://localhost:8080/", "height": 139 }, "outputId": "c8988302-e591-4264-d174-5086d703b0ac" }, "source": [ "st1 = \"dog fox ham\"\n", "print('Odd one out for [',st1, ']:', \n", " ft_model.wv.doesnt_match(st1.split()))\n", "\n", "st2 = \"bacon ham sky sausages\"\n", "print('Odd one out for [',st2, ']:', \n", " ft_model.wv.doesnt_match(st2.split()))" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "Odd one out for [ dog fox ham ]: ham\n", "Odd one out for [ bacon ham sky sausages ]: sky\n" ], "name": "stdout" }, { "output_type": "stream", "text": [ "/usr/local/lib/python3.6/dist-packages/gensim/models/keyedvectors.py:895: FutureWarning: arrays to stack must be passed as a \"sequence\" type such as list or tuple. Support for non-sequence iterables such as generators is deprecated as of NumPy 1.16 and will raise an error in the future.\n", " vectors = vstack(self.word_vec(word, use_norm=True) for word in used_words).astype(REAL)\n", "/usr/local/lib/python3.6/dist-packages/gensim/matutils.py:737: FutureWarning: Conversion of the second argument of issubdtype from `int` to `np.signedinteger` is deprecated. In future, it will be treated as `np.int64 == np.dtype(int).type`.\n", " if np.issubdtype(vec.dtype, np.int):\n" ], "name": "stderr" } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "QxZeE7w8RirK" }, "source": [ "### Getting document level embeddings\n", "\n", "Now suppose we wanted to cluster the eight documents from our toy corpus, we would need to get the document level embeddings from each of the words present in each document. One strategy would be to average out the word embeddings for each word in a document. This is an extremely useful strategy and you can adopt the same for your own problems. Let’s apply this now on our corpus to get features for each document." ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "q8ETS-PxRirK", "colab": {} }, "source": [ "def average_word_vectors(words, model, vocabulary, num_features):\n", " \n", " feature_vector = np.zeros((num_features,),dtype=\"float64\")\n", " nwords = 0.\n", " \n", " for word in words:\n", " if word in vocabulary: \n", " nwords = nwords + 1.\n", " feature_vector = np.add(feature_vector, model.wv[word])\n", " \n", " if nwords:\n", " feature_vector = np.divide(feature_vector, nwords)\n", " \n", " return feature_vector\n", "\n", "\n", "def averaged_word_vectorizer(corpus, model, num_features):\n", " vocabulary = set(model.wv.index2word)\n", " features = [average_word_vectors(tokenized_sentence, model, vocabulary, num_features)\n", " for tokenized_sentence in corpus]\n", " return np.array(features)" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "MPFoB8y5RirM", "colab": { "base_uri": "https://localhost:8080/", "height": 297 }, "outputId": "c6073b7d-b71e-4496-e325-805d3ccacad4" }, "source": [ "# get document level embeddings\n", "ft_doc_features = averaged_word_vectorizer(corpus=tokenized_corpus, model=ft_model,\n", " num_features=feature_size)\n", "pd.DataFrame(ft_doc_features)" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "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", "
01234567891011121314
0-1.0178341.219471-0.4714040.100632-0.485411-0.580870-0.183611-0.032766-0.2665650.2894600.0746200.748789-0.5179580.038283-0.297274
1-1.0513890.996752-0.4613330.187398-0.399002-0.535357-0.133190-0.102741-0.2351520.2557510.0703970.575714-0.3767310.007694-0.241070
2-0.0806851.3947141.0154250.091486-1.111383-0.360878-0.1660060.346778-0.3758670.0034220.676978-0.001734-0.3803490.5632450.395930
3-1.4434670.329564-0.0083771.037905-0.312026-0.2330520.184552-0.440906-0.320137-0.3715660.666171-0.8953220.647324-0.199162-0.239623
4-1.3074740.324607-0.1404980.826300-0.173477-0.2651900.072099-0.486643-0.283610-0.1578700.412292-0.5081610.427139-0.166446-0.225815
5-0.1895421.3525780.8030240.068530-1.020865-0.378753-0.1932950.301214-0.3501970.0577260.5848050.126631-0.4093610.4845190.307555
6-1.0853961.229123-0.5360720.100034-0.476581-0.611018-0.181043-0.026112-0.2598820.2830330.0464870.772775-0.5327930.030967-0.318839
7-0.0880251.4158531.0036390.080209-1.119665-0.375660-0.1761120.350506-0.3779000.0021040.6625230.023472-0.3981870.5614280.385074
\n", "
" ], "text/plain": [ " 0 1 2 ... 12 13 14\n", "0 -1.017834 1.219471 -0.471404 ... -0.517958 0.038283 -0.297274\n", "1 -1.051389 0.996752 -0.461333 ... -0.376731 0.007694 -0.241070\n", "2 -0.080685 1.394714 1.015425 ... -0.380349 0.563245 0.395930\n", "3 -1.443467 0.329564 -0.008377 ... 0.647324 -0.199162 -0.239623\n", "4 -1.307474 0.324607 -0.140498 ... 0.427139 -0.166446 -0.225815\n", "5 -0.189542 1.352578 0.803024 ... -0.409361 0.484519 0.307555\n", "6 -1.085396 1.229123 -0.536072 ... -0.532793 0.030967 -0.318839\n", "7 -0.088025 1.415853 1.003639 ... -0.398187 0.561428 0.385074\n", "\n", "[8 rows x 15 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 22 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "mgrKl_vqRirN" }, "source": [ "### Trying out document clustering\n", "\n", "Now that we have our features for each document, let’s cluster these documents using the Affinity Propagation algorithm, which is a clustering algorithm based on the concept of “message passing” between data points and does not need the number of clusters as an explicit input which is often required by partition-based clustering algorithms." ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "n9C0aRLwRirO", "colab": { "base_uri": "https://localhost:8080/", "height": 297 }, "outputId": "cfe31134-897f-4f74-cae9-1f9529c09786" }, "source": [ "from sklearn.cluster import AffinityPropagation\n", "\n", "ap = AffinityPropagation()\n", "ap.fit(ft_doc_features)\n", "\n", "cluster_labels = ap.labels_\n", "cluster_labels = pd.DataFrame(cluster_labels, \n", " columns=['ClusterLabel'])\n", "\n", "pd.concat([corpus_df, cluster_labels], axis=1)" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "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", "
DocumentCategoryClusterLabel
0The sky is blue and beautiful.weather0
1Love this blue and beautiful sky!weather0
2The quick brown fox jumps over the lazy dog.animals2
3A king's breakfast has sausages, ham, bacon, eggs, toast and beansfood1
4I love green eggs, ham, sausages and bacon!food1
5The brown fox is quick and the blue dog is lazy!animals2
6The sky is very blue and the sky is very beautiful todayweather0
7The dog is lazy but the brown fox is quick!animals2
\n", "
" ], "text/plain": [ " Document ... ClusterLabel\n", "0 The sky is blue and beautiful. ... 0\n", "1 Love this blue and beautiful sky! ... 0\n", "2 The quick brown fox jumps over the lazy dog. ... 2\n", "3 A king's breakfast has sausages, ham, bacon, eggs, toast and beans ... 1\n", "4 I love green eggs, ham, sausages and bacon! ... 1\n", "5 The brown fox is quick and the blue dog is lazy! ... 2\n", "6 The sky is very blue and the sky is very beautiful today ... 0\n", "7 The dog is lazy but the brown fox is quick! ... 2\n", "\n", "[8 rows x 3 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 23 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "bB6pQQTPRirQ" }, "source": [ "We can see that our algorithm has clustered each document into the right group based on our Word2Vec features. Pretty neat! We can also visualize how each document in positioned in each cluster by using [_Principal Component Analysis (PCA)_](https://en.wikipedia.org/wiki/Principal_component_analysis) to reduce the feature dimensions to 2-D and then visualizing the same (by color coding each cluster)." ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "3VA6oJnIRirR", "colab": { "base_uri": "https://localhost:8080/", "height": 378 }, "outputId": "05242d98-b335-44c6-f234-d3c067a16bfa" }, "source": [ "from sklearn.decomposition import PCA\n", "\n", "pca = PCA(n_components=2, random_state=42)\n", "pcs = pca.fit_transform(ft_doc_features)\n", "labels = ap.labels_\n", "categories = list(corpus_df['Category'])\n", "plt.figure(figsize=(8, 6))\n", "\n", "for i in range(len(labels)):\n", " label = labels[i]\n", " color = 'orange' if label == 0 else 'blue' if label == 1 else 'green'\n", " annotation_label = categories[i]\n", " x, y = pcs[i]\n", " plt.scatter(x, y, c=color, edgecolors='k')\n", " plt.annotate(annotation_label, xy=(x+1e-2, y+1e-2), xytext=(0, 0), \n", " textcoords='offset points')" ], "execution_count": null, "outputs": [ { "output_type": "display_data", "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAe8AAAFpCAYAAAC1YKAIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAIABJREFUeJzt3X9w1NX97/HXmwiRWCrwDV8L1bDY\nL2iaHyaw0gQJQ0NRqA4g6lRMx1qxadqrt2WmTp0JKDDG+v2WtnfCqBm0NvhtoNwb7JV6oQIlKFh/\nsEkBwy9BmgTBWggKEogl9dw/soRIEwhks5uTPB8zGfZz9rPnvPdkySufz579xJxzAgAA/ugT6wIA\nAMDFIbwBAPAM4Q0AgGcIbwAAPEN4AwDgGcIbAADPEN4AAHiG8AYAwDOENwAAni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Q3AACeIbwBAPAM4Q0A\ngGcIbwAAPEN4AwDgGcIbAADPEN4AAHjm/wPdx/JV3GcdSgAAAABJRU5ErkJggg==\n", "text/plain": [ "
" ] }, "metadata": { "tags": [] } } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "PvFiGdkMRirS" }, "source": [ "Everything looks to be in order as documents in each cluster are closer to each other and far apart from other clusters." ] } ] } ================================================ FILE: notebooks/04_NLP_Applications_Text_Similarity_Content_Recommenders.ipynb ================================================ { "nbformat": 4, "nbformat_minor": 0, "metadata": { "accelerator": "GPU", "colab": { "name": "04-NLP Applications - Text Similarity Content Recommenders.ipynb", "provenance": [], "collapsed_sections": [] }, "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.6" } }, "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "wGdpBKaHXuoc" }, "source": [ "# Movie Recommendations with Document Similarity\n", "\n", "Recommender systems are one of the popular and most adopted applications of machine learning. They are typically used to recommend entities to users and these entites can be anything like products, movies, services and so on. \n", "\n", "Popular examples of recommendations include,\n", "- Amazon suggesting products on its website\n", "- Amazon Prime, Netflix, Hotstar recommending movies\\shows\n", "- YouTube recommending videos to watch\n", "\n", "Typically recommender systems can be implemented in three ways:\n", "\n", "- Simple Rule-based Recommenders: Typically based on specific global metrics and thresholds like movie popularity, global ratings etc.\n", "- Content-based Recommenders: This is based on providing similar entities based on a specific entity of interest. Content metadata can be used here like movie descriptions, genre, cast, director and so on\n", "- Collaborative filtering Recommenders: Here we don't need metadata but we try to predict recommendations and ratings based on past ratings of different users and specific items.\n", "\n", "We will be building a movie recommendation system here where based on data\\metadata pertaining to different movies, we try and recommend similar movies of interest!\n", "\n", "![](https://i.imgur.com/c7Go7d3.png)\n", "\n", "Since our focus in not really recommendation engines but NLP, we will be leveraging the text-based metadata for each movie to try and recommend similar movies based on specific movies of interest. This falls under content-based recommenders. " ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "S2lYDs1RjvRn" }, "source": [ "# Install Dependencies" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "-U-mW8t5YDFH", "colab": { "base_uri": "https://localhost:8080/", "height": 470 }, "outputId": "bf0849c5-079d-44cf-a71a-de310210787a" }, "source": [ "!pip install textsearch\n", "!pip install contractions\n", "import nltk\n", "nltk.download('punkt')\n", "nltk.download('stopwords')" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "Collecting textsearch\n", " Downloading https://files.pythonhosted.org/packages/42/a8/03407021f9555043de5492a2bd7a35c56cc03c2510092b5ec018cae1bbf1/textsearch-0.0.17-py2.py3-none-any.whl\n", "Collecting Unidecode\n", "\u001b[?25l Downloading https://files.pythonhosted.org/packages/d0/42/d9edfed04228bacea2d824904cae367ee9efd05e6cce7ceaaedd0b0ad964/Unidecode-1.1.1-py2.py3-none-any.whl (238kB)\n", "\u001b[K |████████████████████████████████| 245kB 7.9MB/s \n", "\u001b[?25hCollecting pyahocorasick\n", "\u001b[?25l Downloading https://files.pythonhosted.org/packages/f4/9f/f0d8e8850e12829eea2e778f1c90e3c53a9a799b7f412082a5d21cd19ae1/pyahocorasick-1.4.0.tar.gz (312kB)\n", "\u001b[K |████████████████████████████████| 317kB 14.2MB/s \n", "\u001b[?25hBuilding wheels for collected packages: pyahocorasick\n", " Building wheel for pyahocorasick (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for pyahocorasick: filename=pyahocorasick-1.4.0-cp36-cp36m-linux_x86_64.whl size=81702 sha256=806fc3ab02550f97e873a75ee295eb4acc7e8b73d1ad761dcd80d0de3dd91064\n", " Stored in directory: /root/.cache/pip/wheels/0a/90/61/87a55f5b459792fbb2b7ba6b31721b06ff5cf6bde541b40994\n", "Successfully built pyahocorasick\n", "Installing collected packages: Unidecode, pyahocorasick, textsearch\n", "Successfully installed Unidecode-1.1.1 pyahocorasick-1.4.0 textsearch-0.0.17\n", "Collecting contractions\n", " Downloading https://files.pythonhosted.org/packages/85/41/c3dfd5feb91a8d587ed1a59f553f07c05f95ad4e5d00ab78702fbf8fe48a/contractions-0.0.24-py2.py3-none-any.whl\n", "Requirement already satisfied: textsearch in /usr/local/lib/python3.6/dist-packages (from contractions) (0.0.17)\n", "Requirement already satisfied: pyahocorasick in /usr/local/lib/python3.6/dist-packages (from textsearch->contractions) (1.4.0)\n", "Requirement already satisfied: Unidecode in /usr/local/lib/python3.6/dist-packages (from textsearch->contractions) (1.1.1)\n", "Installing collected packages: contractions\n", "Successfully installed contractions-0.0.24\n", "[nltk_data] Downloading package punkt to /root/nltk_data...\n", "[nltk_data] Unzipping tokenizers/punkt.zip.\n", "[nltk_data] Downloading package stopwords to /root/nltk_data...\n", "[nltk_data] Unzipping corpora/stopwords.zip.\n" ], "name": "stdout" }, { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 1 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "7_de4c8pjvRr" }, "source": [ "# Load and View Data" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "ctP-qx30YUyC", "colab": { "base_uri": "https://localhost:8080/", "height": 470 }, "outputId": "ba2083d4-0322-43bc-b7bb-ee8ee28c7575" }, "source": [ "import pandas as pd\n", "\n", "df = pd.read_csv('https://github.com/dipanjanS/nlp_workshop_dhs18/raw/master/Unit%2010%20-%20Project%208%20-%20Movie%20Recommendations%20with%20Document%20Similarity/tmdb_5000_movies.csv.gz', compression='gzip')\n", "df.info()" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "\n", "RangeIndex: 4803 entries, 0 to 4802\n", "Data columns (total 20 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 budget 4803 non-null int64 \n", " 1 genres 4803 non-null object \n", " 2 homepage 1712 non-null object \n", " 3 id 4803 non-null int64 \n", " 4 keywords 4803 non-null object \n", " 5 original_language 4803 non-null object \n", " 6 original_title 4803 non-null object \n", " 7 overview 4800 non-null object \n", " 8 popularity 4803 non-null float64\n", " 9 production_companies 4803 non-null object \n", " 10 production_countries 4803 non-null object \n", " 11 release_date 4802 non-null object \n", " 12 revenue 4803 non-null int64 \n", " 13 runtime 4801 non-null float64\n", " 14 spoken_languages 4803 non-null object \n", " 15 status 4803 non-null object \n", " 16 tagline 3959 non-null object \n", " 17 title 4803 non-null object \n", " 18 vote_average 4803 non-null float64\n", " 19 vote_count 4803 non-null int64 \n", "dtypes: float64(3), int64(4), object(13)\n", "memory usage: 750.6+ KB\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "frQbM_zrZC2D", "colab": { "base_uri": "https://localhost:8080/", "height": 702 }, "outputId": "8b46b999-021c-4374-e4e8-255c6a1d73c1" }, "source": [ "df.head()" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "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", "
budgetgenreshomepageidkeywordsoriginal_languageoriginal_titleoverviewpopularityproduction_companiesproduction_countriesrelease_daterevenueruntimespoken_languagesstatustaglinetitlevote_averagevote_count
0237000000[{\"id\": 28, \"name\": \"Action\"}, {\"id\": 12, \"nam...http://www.avatarmovie.com/19995[{\"id\": 1463, \"name\": \"culture clash\"}, {\"id\":...enAvatarIn the 22nd century, a paraplegic Marine is di...150.437577[{\"name\": \"Ingenious Film Partners\", \"id\": 289...[{\"iso_3166_1\": \"US\", \"name\": \"United States o...2009-12-102787965087162.0[{\"iso_639_1\": \"en\", \"name\": \"English\"}, {\"iso...ReleasedEnter the World of Pandora.Avatar7.211800
1300000000[{\"id\": 12, \"name\": \"Adventure\"}, {\"id\": 14, \"...http://disney.go.com/disneypictures/pirates/285[{\"id\": 270, \"name\": \"ocean\"}, {\"id\": 726, \"na...enPirates of the Caribbean: At World's EndCaptain Barbossa, long believed to be dead, ha...139.082615[{\"name\": \"Walt Disney Pictures\", \"id\": 2}, {\"...[{\"iso_3166_1\": \"US\", \"name\": \"United States o...2007-05-19961000000169.0[{\"iso_639_1\": \"en\", \"name\": \"English\"}]ReleasedAt the end of the world, the adventure begins.Pirates of the Caribbean: At World's End6.94500
2245000000[{\"id\": 28, \"name\": \"Action\"}, {\"id\": 12, \"nam...http://www.sonypictures.com/movies/spectre/206647[{\"id\": 470, \"name\": \"spy\"}, {\"id\": 818, \"name...enSpectreA cryptic message from Bond’s past sends him o...107.376788[{\"name\": \"Columbia Pictures\", \"id\": 5}, {\"nam...[{\"iso_3166_1\": \"GB\", \"name\": \"United Kingdom\"...2015-10-26880674609148.0[{\"iso_639_1\": \"fr\", \"name\": \"Fran\\u00e7ais\"},...ReleasedA Plan No One EscapesSpectre6.34466
3250000000[{\"id\": 28, \"name\": \"Action\"}, {\"id\": 80, \"nam...http://www.thedarkknightrises.com/49026[{\"id\": 849, \"name\": \"dc comics\"}, {\"id\": 853,...enThe Dark Knight RisesFollowing the death of District Attorney Harve...112.312950[{\"name\": \"Legendary Pictures\", \"id\": 923}, {\"...[{\"iso_3166_1\": \"US\", \"name\": \"United States o...2012-07-161084939099165.0[{\"iso_639_1\": \"en\", \"name\": \"English\"}]ReleasedThe Legend EndsThe Dark Knight Rises7.69106
4260000000[{\"id\": 28, \"name\": \"Action\"}, {\"id\": 12, \"nam...http://movies.disney.com/john-carter49529[{\"id\": 818, \"name\": \"based on novel\"}, {\"id\":...enJohn CarterJohn Carter is a war-weary, former military ca...43.926995[{\"name\": \"Walt Disney Pictures\", \"id\": 2}][{\"iso_3166_1\": \"US\", \"name\": \"United States o...2012-03-07284139100132.0[{\"iso_639_1\": \"en\", \"name\": \"English\"}]ReleasedLost in our world, found in another.John Carter6.12124
\n", "
" ], "text/plain": [ " budget ... vote_count\n", "0 237000000 ... 11800\n", "1 300000000 ... 4500\n", "2 245000000 ... 4466\n", "3 250000000 ... 9106\n", "4 260000000 ... 2124\n", "\n", "[5 rows x 20 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 3 } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "2lURVWdjZGHL", "colab": { "base_uri": "https://localhost:8080/", "height": 218 }, "outputId": "341937af-3a75-4fc9-b8f5-051bd5d48115" }, "source": [ "df = df[['title', 'tagline', 'overview', 'popularity']]\n", "df.tagline.fillna('', inplace=True)\n", "df['description'] = df['tagline'].map(str) + ' ' + df['overview']\n", "df.dropna(inplace=True)\n", "df = df.sort_values(by=['popularity'], ascending=False)\n", "df.info()" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "\n", "Int64Index: 4800 entries, 546 to 4553\n", "Data columns (total 5 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 title 4800 non-null object \n", " 1 tagline 4800 non-null object \n", " 2 overview 4800 non-null object \n", " 3 popularity 4800 non-null float64\n", " 4 description 4800 non-null object \n", "dtypes: float64(1), object(4)\n", "memory usage: 225.0+ KB\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "hfwvf3UgZJK6", "colab": { "base_uri": "https://localhost:8080/", "height": 195 }, "outputId": "224b2622-664a-4ca2-8bea-2d017d990b74" }, "source": [ "df.head()" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "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", "
titletaglineoverviewpopularitydescription
546MinionsBefore Gru, they had a history of bad bossesMinions Stuart, Kevin and Bob are recruited by...875.581305Before Gru, they had a history of bad bosses M...
95InterstellarMankind was born on Earth. It was never meant ...Interstellar chronicles the adventures of a gr...724.247784Mankind was born on Earth. It was never meant ...
788DeadpoolWitness the beginning of a happy endingDeadpool tells the origin story of former Spec...514.569956Witness the beginning of a happy ending Deadpo...
94Guardians of the GalaxyAll heroes start somewhere.Light years from Earth, 26 years after being a...481.098624All heroes start somewhere. Light years from E...
127Mad Max: Fury RoadWhat a Lovely Day.An apocalyptic story set in the furthest reach...434.278564What a Lovely Day. An apocalyptic story set in...
\n", "
" ], "text/plain": [ " title ... description\n", "546 Minions ... Before Gru, they had a history of bad bosses M...\n", "95 Interstellar ... Mankind was born on Earth. It was never meant ...\n", "788 Deadpool ... Witness the beginning of a happy ending Deadpo...\n", "94 Guardians of the Galaxy ... All heroes start somewhere. Light years from E...\n", "127 Mad Max: Fury Road ... What a Lovely Day. An apocalyptic story set in...\n", "\n", "[5 rows x 5 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 5 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "3wDHsF6qZWNv" }, "source": [ "# Build a Movie Recommender System\n", "\n", "Here you will build your own movie recommender system. We will use the following pipeline:\n", "- Text pre-processing\n", "- Feature Engineering\n", "- Document Similarity Computation\n", "- Find top similar movies\n", "- Build a movie recommendation function\n", "\n", "\n", "## Document Similarity\n", "\n", "Recommendations are about understanding the underlying features which make us favour one choice over the other. Similarity between items(in this case movies) is one way to understanding why we choose one movie over another. There are different ways to calculate similarity between two items. One of the most widely used measures is __cosine similarity__ which we have already used in the previous unit.\n", "\n", "### Cosine Similarity\n", "\n", "Cosine Similarity is used to calculate a numeric score to denote the similarity between two text documents. Mathematically, it is defined as follows:\n", "\n", "$$ cosine(x,y) = \\frac{x. y^\\intercal}{||x||.||y||} $$" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "BjLTJDE9ZXSj", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "2643ed1b-92c6-466e-b9ae-81eeba4be39e" }, "source": [ "import nltk\n", "import re\n", "import numpy as np\n", "import contractions\n", "\n", "stop_words = nltk.corpus.stopwords.words('english')\n", "\n", "def normalize_document(doc):\n", " # lower case and remove special characters\\whitespaces\n", " doc = re.sub(r'[^a-zA-Z0-9\\s]', '', doc, re.I|re.A)\n", " doc = doc.lower()\n", " doc = doc.strip()\n", " doc = contractions.fix(doc)\n", " # tokenize document\n", " tokens = nltk.word_tokenize(doc)\n", " #filter stopwords out of document\n", " filtered_tokens = [token for token in tokens if token not in stop_words]\n", " # re-create document from filtered tokens\n", " doc = ' '.join(filtered_tokens)\n", " return doc\n", "\n", "normalize_corpus = np.vectorize(normalize_document)\n", "\n", "norm_corpus = normalize_corpus(list(df['description']))\n", "len(norm_corpus)" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "4800" ] }, "metadata": { "tags": [] }, "execution_count": 6 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "uNiwar7saOuj" }, "source": [ "## Extract TF-IDF Features" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "NZkWolSnaRkd", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "c26d1f42-92c3-408e-ec49-b1096d2f505c" }, "source": [ "from sklearn.feature_extraction.text import TfidfVectorizer\n", "\n", "tf = TfidfVectorizer(ngram_range=(1, 2), min_df=2)\n", "tfidf_matrix = tf.fit_transform(norm_corpus)\n", "tfidf_matrix.shape" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(4800, 20468)" ] }, "metadata": { "tags": [] }, "execution_count": 7 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "PIyXn448aXnt" }, "source": [ "## Compute Pairwise Document Similarity" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "dKYgrUc4aUHm", "colab": { "base_uri": "https://localhost:8080/", "height": 244 }, "outputId": "4efb9bc1-135e-4824-e7db-073251006c46" }, "source": [ "from sklearn.metrics.pairwise import cosine_similarity\n", "\n", "doc_sim = cosine_similarity(tfidf_matrix)\n", "doc_sim_df = pd.DataFrame(doc_sim)\n", "doc_sim_df.head()" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
0123456789101112131415161718192021222324252627282930313233343536373839...4760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799
01.000000.0000000.0000000.0000000.0060700.0080670.00.0000000.00.0000000.0255310.0085540.0181110.0000000.0000000.0000000.0000000.0074390.0104540.0000000.0000000.0081900.0083650.0100350.0000000.0000000.0509760.0065020.00.0107280.00.0069080.0000000.1675730.0000000.0000000.0000000.0000000.0091910.053475...0.0000000.0097110.0065080.0000000.00.0000000.00.0284090.00.0000000.0000000.0088700.0000000.0000000.0000000.00.0000000.0000000.0000000.0000000.0000000.0000000.00.0332460.00.0000000.0000000.0000000.0000000.0340920.0187540.0000000.0379240.00.0000000.0000000.0000000.0000000.0000000.009646
10.000001.0000000.0000000.0178390.0079670.0000000.00.0125010.00.0148400.0000000.0000000.0000000.0000000.0128140.0000000.0000000.0241440.0000000.0000000.0000000.0000000.0000000.0000000.0081010.0000000.0000000.0168980.00.0177890.00.0088850.0094320.0000000.0000000.0149470.0000000.0000000.0000000.022738...0.0000000.0000000.0197830.0000000.00.0000000.00.0000000.00.0000000.0114070.0114090.0000000.0116320.0000000.00.0000000.0000000.0000000.0000000.0000000.0153290.00.0083670.00.0000000.0000000.0215960.0000000.0000000.0000000.0000000.0175610.00.0191520.0000000.0000000.0000000.0000000.007963
20.000000.0000001.0000000.0000000.0171760.0000000.00.0000000.00.0243260.0054710.0180380.0000000.0000000.0000000.0050990.0049850.0047050.0000000.0048430.0000000.0170260.0000000.0036720.0039840.0309980.0069590.0000000.00.0061170.00.0195480.0203650.0092130.0284670.0105150.0041980.0063110.0151540.012698...0.0205970.0047230.0000000.0166730.00.0000000.00.0000000.00.0066250.0000000.0000000.0069720.0000000.0105740.00.0082220.0086040.0127820.0153530.0062590.0000000.00.0000000.00.0105550.0000000.0000000.0000000.0000000.0000000.0069030.0050230.00.0128930.0000000.0259750.0000000.0271260.009340
30.000000.0178390.0000001.0000000.0000000.0224140.00.0000000.00.0372070.0000000.0000000.0000000.0000000.0279580.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0122320.0178930.0436390.0000000.0000000.0000000.00.0231270.00.0000000.0000000.0000000.0000000.0093240.0229950.0000000.0000000.044476...0.0000000.0000000.0140290.0305610.00.0000000.00.0000000.00.0000000.0000000.0000000.0000000.0000000.0567610.00.0000000.0000000.0000000.0000000.0000000.0000000.00.0000000.00.0000000.0000000.0184860.0000000.0000000.0000000.0608460.0250350.00.0362370.0305160.0226050.0000000.0000000.000000
40.006070.0079670.0171760.0000001.0000000.0046720.00.0645720.00.0000000.0000000.0366110.0238790.0144210.0123160.0198340.0081700.0043080.0254590.0000000.0089070.0237360.0113410.0030090.0000000.0269280.0000000.0100260.00.0195570.00.0285210.0160180.0113110.0169120.0229740.0541420.0137670.0273970.005416...0.0050880.0150970.0307590.0136630.00.0126240.00.0000000.00.0000000.0270180.0120250.0057140.0000000.0172300.00.0223270.0070510.0380140.0117830.0186120.0214680.00.0037670.00.0086500.0094990.0000000.0127490.0000000.0220560.0196590.0368500.00.0158240.0000000.0760220.0045150.0434690.011464
\n", "

5 rows × 4800 columns

\n", "
" ], "text/plain": [ " 0 1 2 ... 4797 4798 4799\n", "0 1.00000 0.000000 0.000000 ... 0.000000 0.000000 0.009646\n", "1 0.00000 1.000000 0.000000 ... 0.000000 0.000000 0.007963\n", "2 0.00000 0.000000 1.000000 ... 0.000000 0.027126 0.009340\n", "3 0.00000 0.017839 0.000000 ... 0.000000 0.000000 0.000000\n", "4 0.00607 0.007967 0.017176 ... 0.004515 0.043469 0.011464\n", "\n", "[5 rows x 4800 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 8 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "h08MqvY9adgt" }, "source": [ "## Get List of Movie Titles" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "rWr6lgqTaZsJ", "colab": { "base_uri": "https://localhost:8080/", "height": 50 }, "outputId": "e71ed347-2667-431b-bd3c-9c343b704da5" }, "source": [ "movies_list = df['title'].values\n", "movies_list, movies_list.shape" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(array(['Minions', 'Interstellar', 'Deadpool', ..., 'Penitentiary',\n", " 'Alien Zone', 'America Is Still the Place'], dtype=object), (4800,))" ] }, "metadata": { "tags": [] }, "execution_count": 9 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "69SKx3ZTaftu" }, "source": [ "## Find Top Similar Movies for a Sample Movie\n", "\n", "Let's take __Minions__ the most popular movie the the dataframe above and try and find the most similar movies which can be recommended" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "JT3tw7Wka6B0" }, "source": [ "#### Find movie ID" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "MmAUpmxWa-Kv", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "ebfad8e3-77f1-4004-ca8c-ce1be801cb69" }, "source": [ "movie_idx = np.where(movies_list == 'Minions')[0][0]\n", "movie_idx" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "0" ] }, "metadata": { "tags": [] }, "execution_count": 10 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "2IO3VhLka_vi" }, "source": [ "#### Get movie similarities" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "Shbop-mDbDSS", "colab": { "base_uri": "https://localhost:8080/", "height": 50 }, "outputId": "7bc72ee0-3cd6-4595-8b4c-2e4232e1ee6e" }, "source": [ "movie_similarities = doc_sim_df.iloc[movie_idx].values\n", "movie_similarities" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([1. , 0. , 0. , ..., 0. , 0. ,\n", " 0.00964634])" ] }, "metadata": { "tags": [] }, "execution_count": 11 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "cjMx8FqtbE4O" }, "source": [ "#### Get top 5 similar movie IDs" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "AgmQgy8QbHDF", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "c00ee209-a607-4e0b-9348-30bb77681bd3" }, "source": [ "similar_movie_idxs = np.argsort(-movie_similarities)[1:6]\n", "similar_movie_idxs" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([ 33, 60, 737, 490, 298])" ] }, "metadata": { "tags": [] }, "execution_count": 12 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "n7Fzp3qcbJv3" }, "source": [ "#### Get top 5 similar movies" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "h4o7WpNlbNlT", "colab": { "base_uri": "https://localhost:8080/", "height": 67 }, "outputId": "8ca9f814-165d-4fb9-ff72-31fcc096cc56" }, "source": [ "similar_movies = movies_list[similar_movie_idxs]\n", "similar_movies" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array(['Despicable Me 2', 'Despicable Me',\n", " 'Teenage Mutant Ninja Turtles: Out of the Shadows', 'Superman',\n", " 'Rise of the Guardians'], dtype=object)" ] }, "metadata": { "tags": [] }, "execution_count": 13 } ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "f5lpIbnMcnOx" }, "source": [ "### Build a movie recommender function to recommend top 5 similar movies for any movie \n", "\n", "The movie title, movie title list and document similarity matrix dataframe will be given as inputs to the function" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "OOL8nLbacpXq", "colab": {} }, "source": [ "def movie_recommender(movie_title, movies=movies_list, doc_sims=doc_sim_df):\n", " # find movie id\n", " movie_idx = np.where(movies == movie_title)[0][0]\n", " # get movie similarities\n", " movie_similarities = doc_sims.iloc[movie_idx].values\n", " # get top 5 similar movie IDs\n", " similar_movie_idxs = np.argsort(-movie_similarities)[1:6]\n", " # get top 5 movies\n", " similar_movies = movies[similar_movie_idxs]\n", " # return the top 5 movies\n", " return similar_movies" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "U4t4wBWwceFA" }, "source": [ "# Get popular Movie Recommendations" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "5V4EpMgAbPYY", "colab": {} }, "source": [ "popular_movies = ['Minions', 'Interstellar', 'Deadpool', 'Jurassic World', 'Pirates of the Caribbean: The Curse of the Black Pearl',\n", " 'Dawn of the Planet of the Apes', 'The Hunger Games: Mockingjay - Part 1', 'Terminator Genisys', \n", " 'Captain America: Civil War', 'The Dark Knight', 'The Martian', 'Batman v Superman: Dawn of Justice', \n", " 'Pulp Fiction', 'The Godfather', 'The Shawshank Redemption', 'The Lord of the Rings: The Fellowship of the Ring', \n", " 'Harry Potter and the Chamber of Secrets', 'Star Wars', 'The Hobbit: The Battle of the Five Armies',\n", " 'Iron Man']" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "N1ky8g-Ichp6", "colab": { "base_uri": "https://localhost:8080/", "height": 1000 }, "outputId": "17575641-1b5f-4ae8-800a-5b65be5808e9" }, "source": [ "for movie in popular_movies:\n", " print('Movie:', movie)\n", " print('Top 5 recommended Movies:', movie_recommender(movie_title=movie, movies=movies_list, doc_sims=doc_sim_df))\n", " print()" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "Movie: Minions\n", "Top 5 recommended Movies: ['Despicable Me 2' 'Despicable Me'\n", " 'Teenage Mutant Ninja Turtles: Out of the Shadows' 'Superman'\n", " 'Rise of the Guardians']\n", "\n", "Movie: Interstellar\n", "Top 5 recommended Movies: ['Gattaca' 'Space Pirate Captain Harlock' 'Space Cowboys'\n", " 'Starship Troopers' 'Final Destination 2']\n", "\n", "Movie: Deadpool\n", "Top 5 recommended Movies: ['Silent Trigger' 'Underworld: Evolution' 'Bronson' 'Shaft' 'Don Jon']\n", "\n", "Movie: Jurassic World\n", "Top 5 recommended Movies: ['Jurassic Park' 'The Lost World: Jurassic Park'\n", " \"National Lampoon's Vacation\" 'The Nut Job' 'Vacation']\n", "\n", "Movie: Pirates of the Caribbean: The Curse of the Black Pearl\n", "Top 5 recommended Movies: [\"Pirates of the Caribbean: Dead Man's Chest\"\n", " 'Pirates of the Caribbean: On Stranger Tides' 'The Pirate'\n", " 'The Pirates! In an Adventure with Scientists!' 'Joyful Noise']\n", "\n", "Movie: Dawn of the Planet of the Apes\n", "Top 5 recommended Movies: ['Battle for the Planet of the Apes' 'Groove' 'The Other End of the Line'\n", " 'Chicago Overcoat' 'Definitely, Maybe']\n", "\n", "Movie: The Hunger Games: Mockingjay - Part 1\n", "Top 5 recommended Movies: ['The Hunger Games: Catching Fire' 'The Hunger Games: Mockingjay - Part 2'\n", " 'John Carter' 'For Greater Glory - The True Story of Cristiada'\n", " 'The Proposition']\n", "\n", "Movie: Terminator Genisys\n", "Top 5 recommended Movies: ['Terminator 2: Judgment Day' 'Terminator Salvation'\n", " 'Terminator 3: Rise of the Machines' 'Mad Max'\n", " 'X-Men: Days of Future Past']\n", "\n", "Movie: Captain America: Civil War\n", "Top 5 recommended Movies: ['Captain America: The Winter Soldier' 'This Means War'\n", " 'Avengers: Age of Ultron' 'Iron Man 2' 'Escape from Tomorrow']\n", "\n", "Movie: The Dark Knight\n", "Top 5 recommended Movies: ['The Dark Knight Rises' 'Batman Forever' 'Batman Returns'\n", " 'Batman: The Dark Knight Returns, Part 2' 'JFK']\n", "\n", "Movie: The Martian\n", "Top 5 recommended Movies: ['The Last Days on Mars' 'Swept Away' 'Alive' 'All Is Lost' 'Red Planet']\n", "\n", "Movie: Batman v Superman: Dawn of Justice\n", "Top 5 recommended Movies: ['Batman Returns' 'The Punisher' 'Defendor'\n", " 'Batman: The Dark Knight Returns, Part 2' 'Nowhere to Run']\n", "\n", "Movie: Pulp Fiction\n", "Top 5 recommended Movies: ['Sliding Doors' 'You Kill Me' 'New York Stories' 'Timecrimes'\n", " 'All or Nothing']\n", "\n", "Movie: The Godfather\n", "Top 5 recommended Movies: ['The Godfather: Part II' 'Blood Ties' 'Made' 'Lords of London'\n", " 'Easy Money']\n", "\n", "Movie: The Shawshank Redemption\n", "Top 5 recommended Movies: ['Civil Brand' 'Les Misérables' 'The Chorus' 'Prison' 'Fortress']\n", "\n", "Movie: The Lord of the Rings: The Fellowship of the Ring\n", "Top 5 recommended Movies: ['The Lord of the Rings: The Two Towers'\n", " 'The Hobbit: The Desolation of Smaug'\n", " 'The Lord of the Rings: The Return of the King'\n", " \"What's the Worst That Could Happen?\" 'The Hobbit: An Unexpected Journey']\n", "\n", "Movie: Harry Potter and the Chamber of Secrets\n", "Top 5 recommended Movies: ['Harry Potter and the Prisoner of Azkaban'\n", " 'Harry Potter and the Goblet of Fire'\n", " 'Harry Potter and the Order of the Phoenix'\n", " 'Harry Potter and the Half-Blood Prince'\n", " \"Harry Potter and the Philosopher's Stone\"]\n", "\n", "Movie: Star Wars\n", "Top 5 recommended Movies: ['The Empire Strikes Back' 'Return of the Jedi' 'Shrek the Third'\n", " 'The Ice Pirates' 'The Tale of Despereaux']\n", "\n", "Movie: The Hobbit: The Battle of the Five Armies\n", "Top 5 recommended Movies: ['The Hobbit: The Desolation of Smaug' 'The Hobbit: An Unexpected Journey'\n", " \"Dragon Nest: Warriors' Dawn\"\n", " 'A Funny Thing Happened on the Way to the Forum' 'X-Men: Apocalypse']\n", "\n", "Movie: Iron Man\n", "Top 5 recommended Movies: ['Iron Man 2' 'Avengers: Age of Ultron' 'Hostage' 'Iron Man 3'\n", " 'Baahubali: The Beginning']\n", "\n" ], "name": "stdout" } ] } ] } ================================================ FILE: notebooks/05_NLP_Applications_Predicting_E_Commerce_Product_Recommendation_Ratings_from_Reviews_.ipynb ================================================ { "nbformat": 4, "nbformat_minor": 0, "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" }, "colab": { "name": "05 - NLP Applications - Predicting E-Commerce Product Recommendation Ratings from Reviews .ipynb", "provenance": [], "collapsed_sections": [] } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "8wgGEuy-9G1x", "colab_type": "text" }, "source": [ "# Predicting E-Commerce Product Recommendation Ratings from Reviews \n", "\n", "\n", "![](https://github.com/dipanjanS/feature_engineering_session_dhs18/blob/master/ecommerce_product_ratings_prediction/clothing_banner.jpg?raw=1)\n", "\n", "This is a classic NLP problem dealing with data from an e-commerce store focusing on women's clothing. Each record in the dataset is a customer review which consists of the review title, text description and a rating (ranging from 1 - 5) for a product amongst other features\n", "\n", "We convert this into a binary classification problem such that a customer recommends a product (label 1) is the rating is > 3 else they do not recommend the product (label 0)\n", "\n", "__Main Objective:__ Leverage the review text attributes to predict the recommendation rating (classification)\n", "\n", "\n", "_Author: Dipanjan (DJ) Sarkar_" ] }, { "cell_type": "markdown", "metadata": { "id": "NdpvzY1D9G1y", "colab_type": "text" }, "source": [ "# Load up basic dependencies" ] }, { "cell_type": "code", "metadata": { "id": "VXs4584w9G1y", "colab_type": "code", "colab": {} }, "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "from sklearn.metrics import confusion_matrix, classification_report" ], "execution_count": 25, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "PxMJ0XnP9G11", "colab_type": "text" }, "source": [ "# Load and View the Dataset\n", "\n", "The data is available at https://www.kaggle.com/nicapotato/womens-ecommerce-clothing-reviews from where you can download it.\n", "\n", "\n", "We recommend using the kaggle API and the following command via CLI to get it.\n", "\n", "__`kaggle datasets download -d nicapotato/womens-ecommerce-clothing-reviews`__" ] }, { "cell_type": "code", "metadata": { "scrolled": true, "id": "xmq8n6Sa9G12", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 246 }, "outputId": "77cb6a72-43ae-4df4-81bb-d26822e8c2f4" }, "source": [ "df = pd.read_csv('https://raw.githubusercontent.com/dipanjanS/feature_engineering_session_dhs18/master/ecommerce_product_ratings_prediction/Womens%20Clothing%20E-Commerce%20Reviews.csv', keep_default_na=False)\n", "df.head()" ], "execution_count": 26, "outputs": [ { "output_type": "execute_result", "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", "
Unnamed: 0Clothing IDAgeTitleReview TextRatingRecommended INDPositive Feedback CountDivision NameDepartment NameClass Name
0076733Absolutely wonderful - silky and sexy and comf...410InitmatesIntimateIntimates
11108034Love this dress! it's sooo pretty. i happene...514GeneralDressesDresses
22107760Some major design flawsI had such high hopes for this dress and reall...300GeneralDressesDresses
33104950My favorite buy!I love, love, love this jumpsuit. it's fun, fl...510General PetiteBottomsPants
4484747Flattering shirtThis shirt is very flattering to all due to th...516GeneralTopsBlouses
\n", "
" ], "text/plain": [ " Unnamed: 0 Clothing ID Age ... Division Name Department Name Class Name\n", "0 0 767 33 ... Initmates Intimate Intimates\n", "1 1 1080 34 ... General Dresses Dresses\n", "2 2 1077 60 ... General Dresses Dresses\n", "3 3 1049 50 ... General Petite Bottoms Pants\n", "4 4 847 47 ... General Tops Blouses\n", "\n", "[5 rows x 11 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 26 } ] }, { "cell_type": "markdown", "metadata": { "id": "HSWdX9p49G15", "colab_type": "text" }, "source": [ "# Basic Data Processing\n", "\n", "- Merge all review text attributes (title, text description) into one attribute\n", "- Convert the 5-star rating system into a binary recommendation rating of 1 or 0" ] }, { "cell_type": "code", "metadata": { "id": "wD3desQl9G16", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 195 }, "outputId": "5788af75-820a-4c13-b580-1ba106f5d172" }, "source": [ "df['Review'] = (df['Title'].map(str) +' '+ df['Review Text']).apply(lambda row: row.strip())\n", "df['Rating'] = [1 if rating > 3 else 0 for rating in df['Rating']]\n", "df = df[['Review', 'Rating']]\n", "df.head()" ], "execution_count": 27, "outputs": [ { "output_type": "execute_result", "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", "
ReviewRating
0Absolutely wonderful - silky and sexy and comf...1
1Love this dress! it's sooo pretty. i happene...1
2Some major design flaws I had such high hopes ...0
3My favorite buy! I love, love, love this jumps...1
4Flattering shirt This shirt is very flattering...1
\n", "
" ], "text/plain": [ " Review Rating\n", "0 Absolutely wonderful - silky and sexy and comf... 1\n", "1 Love this dress! it's sooo pretty. i happene... 1\n", "2 Some major design flaws I had such high hopes ... 0\n", "3 My favorite buy! I love, love, love this jumps... 1\n", "4 Flattering shirt This shirt is very flattering... 1" ] }, "metadata": { "tags": [] }, "execution_count": 27 } ] }, { "cell_type": "markdown", "metadata": { "id": "hWM1huGb9G19", "colab_type": "text" }, "source": [ "## Remove all records with no review text" ] }, { "cell_type": "code", "metadata": { "id": "VB2DhPLH9G19", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 168 }, "outputId": "0c8fcc37-0998-435e-a912-f6f60f9c30de" }, "source": [ "df = df[df['Review'] != '']\n", "df.info()" ], "execution_count": 28, "outputs": [ { "output_type": "stream", "text": [ "\n", "Int64Index: 22642 entries, 0 to 23485\n", "Data columns (total 2 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 Review 22642 non-null object\n", " 1 Rating 22642 non-null int64 \n", "dtypes: int64(1), object(1)\n", "memory usage: 530.7+ KB\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "Sa8SCfu59G1_", "colab_type": "text" }, "source": [ "## There is some imbalance in the data based on product ratings" ] }, { "cell_type": "code", "metadata": { "id": "LHRcOkxZ9G2A", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 67 }, "outputId": "fdeea29a-c6ab-4634-848d-83cd9a4df54e" }, "source": [ "df['Rating'].value_counts()" ], "execution_count": 29, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "1 17449\n", "0 5193\n", "Name: Rating, dtype: int64" ] }, "metadata": { "tags": [] }, "execution_count": 29 } ] }, { "cell_type": "markdown", "metadata": { "id": "j9HZhCnN9G2C", "colab_type": "text" }, "source": [ "# Build train and test datasets" ] }, { "cell_type": "code", "metadata": { "id": "jBsPt1rC9G2C", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "047e7192-a598-47c1-d0e2-25a38fd8ba14" }, "source": [ "from sklearn.model_selection import train_test_split\n", "\n", "X_train, X_test, y_train, y_test = train_test_split(df[['Review']], df['Rating'], random_state=42)\n", "X_train.shape, X_test.shape" ], "execution_count": 30, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "((16981, 1), (5661, 1))" ] }, "metadata": { "tags": [] }, "execution_count": 30 } ] }, { "cell_type": "code", "metadata": { "id": "_m6_wP6X9G2F", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "b703f0af-1d6c-4200-b494-e37be4a59411" }, "source": [ "from collections import Counter\n", "Counter(y_train), Counter(y_test)" ], "execution_count": 31, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(Counter({0: 3922, 1: 13059}), Counter({0: 1271, 1: 4390}))" ] }, "metadata": { "tags": [] }, "execution_count": 31 } ] }, { "cell_type": "markdown", "metadata": { "id": "ZiKBkmzA9G2H", "colab_type": "text" }, "source": [ "# Experiment 1: Basic NLP Count based Features\n", "\n", "A number of basic text based features can also be created which sometimes are helpful for improving text classification models. \n", "Some examples are:\n", "\n", "- __Word Count:__ total number of words in the documents\n", "- __Character Count:__ total number of characters in the documents\n", "- __Average Word Density:__ average length of the words used in the documents\n", "- __Puncutation Count:__ total number of punctuation marks in the documents\n", "- __Upper Case Count:__ total number of upper count words in the documents\n", "- __Title Word Count:__ total number of proper case (title) words in the documents\n", "\n", "Source: https://www.analyticsvidhya.com/blog/2018/04/a-comprehensive-guide-to-understand-and-implement-text-classification-in-python/" ] }, { "cell_type": "code", "metadata": { "id": "Rk6Kc62u9G2H", "colab_type": "code", "colab": {} }, "source": [ "import string\n", "\n", "X_train['char_count'] = X_train['Review'].apply(len)\n", "X_train['word_count'] = X_train['Review'].apply(lambda x: len(x.split()))\n", "X_train['word_density'] = X_train['char_count'] / (X_train['word_count']+1)\n", "X_train['punctuation_count'] = X_train['Review'].apply(lambda x: len(\"\".join(_ for _ in x if _ in string.punctuation))) \n", "X_train['title_word_count'] = X_train['Review'].apply(lambda x: len([wrd for wrd in x.split() if wrd.istitle()]))\n", "X_train['upper_case_word_count'] = X_train['Review'].apply(lambda x: len([wrd for wrd in x.split() if wrd.isupper()]))\n", "\n", "\n", "X_test['char_count'] = X_test['Review'].apply(len)\n", "X_test['word_count'] = X_test['Review'].apply(lambda x: len(x.split()))\n", "X_test['word_density'] = X_test['char_count'] / (X_test['word_count']+1)\n", "X_test['punctuation_count'] = X_test['Review'].apply(lambda x: len(\"\".join(_ for _ in x if _ in string.punctuation))) \n", "X_test['title_word_count'] = X_test['Review'].apply(lambda x: len([wrd for wrd in x.split() if wrd.istitle()]))\n", "X_test['upper_case_word_count'] = X_test['Review'].apply(lambda x: len([wrd for wrd in x.split() if wrd.isupper()]))" ], "execution_count": 32, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "iyJ87jSe9G2J", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 195 }, "outputId": "47cce8ab-5da8-49f3-929e-01d225864d79" }, "source": [ "X_train.head()" ], "execution_count": 33, "outputs": [ { "output_type": "execute_result", "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", "
Reviewchar_countword_countword_densitypunctuation_counttitle_word_countupper_case_word_count
12896Soooo soft! This is a delightfully soft and fl...268525.056604820
13183Had my eye on this, but dind't get I finally v...399844.6941182021
1496I wanted to like this... I wanted to like this...5251045.0000001922
5205Beautiful blouse Bought this for my daughter i...203355.6388891020
13366Boxy. large. Boxy, unflattering, and large.\\n\\...295515.6730772220
\n", "
" ], "text/plain": [ " Review ... upper_case_word_count\n", "12896 Soooo soft! This is a delightfully soft and fl... ... 0\n", "13183 Had my eye on this, but dind't get I finally v... ... 1\n", "1496 I wanted to like this... I wanted to like this... ... 2\n", "5205 Beautiful blouse Bought this for my daughter i... ... 0\n", "13366 Boxy. large. Boxy, unflattering, and large.\\n\\... ... 0\n", "\n", "[5 rows x 7 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 33 } ] }, { "cell_type": "markdown", "metadata": { "id": "1mHfxYqj9G2L", "colab_type": "text" }, "source": [ "## Training a Logistic Regression Model \n", "\n", "A logistic regression model is easy to train, interpret and works well on a wide variety of NLP problems" ] }, { "cell_type": "code", "metadata": { "id": "4xlxi2zO9G2L", "colab_type": "code", "colab": {} }, "source": [ "from sklearn.linear_model import LogisticRegression\n", "\n", "lr = LogisticRegression(C=1, random_state=42, solver='liblinear')" ], "execution_count": 34, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "yLi8rI2d9G2N", "colab_type": "text" }, "source": [ "## Model Evaluation Metrics - Quick Refresher\n", "\n", "Just accuracy is never enough in datasets with a rare class problem.\n", "\n", "- __Precision:__ The positive predictive power of a model. Out of all the predictions made by a model for a class, how many are actually correct\n", "- __Recall:__ The coverage or hit-rate of a model. Out of all the test data samples belonging to a class, how many was the model able to predict (hit or cover) correctly.\n", "- __F1-score:__ The harmonic mean of the precision and recall\n", "\n", "Do check out ROC Curve, AUC Score and PR Curve also" ] }, { "cell_type": "code", "metadata": { "id": "v7uniDrC9G2O", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 311 }, "outputId": "549e2638-58b9-4360-fe88-9f8e2b1a0904" }, "source": [ "lr.fit(X_train.drop(['Review'], axis=1), y_train)\n", "predictions = lr.predict(X_test.drop(['Review'], axis=1))\n", "\n", "print(classification_report(y_test, predictions))\n", "pd.DataFrame(confusion_matrix(y_test, predictions))" ], "execution_count": 35, "outputs": [ { "output_type": "stream", "text": [ " precision recall f1-score support\n", "\n", " 0 0.00 0.00 0.00 1271\n", " 1 0.78 1.00 0.87 4390\n", "\n", " accuracy 0.78 5661\n", " macro avg 0.39 0.50 0.44 5661\n", "weighted avg 0.60 0.78 0.68 5661\n", "\n" ], "name": "stdout" }, { "output_type": "stream", "text": [ "/usr/local/lib/python3.6/dist-packages/sklearn/metrics/_classification.py:1272: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.\n", " _warn_prf(average, modifier, msg_start, len(result))\n" ], "name": "stderr" }, { "output_type": "execute_result", "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", "
01
001271
104390
\n", "
" ], "text/plain": [ " 0 1\n", "0 0 1271\n", "1 0 4390" ] }, "metadata": { "tags": [] }, "execution_count": 35 } ] }, { "cell_type": "markdown", "metadata": { "id": "WB1WXnmS9G2Q", "colab_type": "text" }, "source": [ "Looks like our model was not able to predict a single product having a bad (no recommendation) rating, i.e. __Class 0__. \n", "\n", "This is as good as someone predicting a __1__ or __good__ for every product review. \n", "\n", "Can we do better?" ] }, { "cell_type": "markdown", "metadata": { "id": "Njk5U6ld9G2a", "colab_type": "text" }, "source": [ "# Leveraging Text Sentiment\n", "\n", "Reviews are pretty subjective, opinionated and people often express stong emotions, feelings. \n", "This makes it a classic case where the text documents here are a good candidate for extracting sentiment as a feature.\n", "\n", "The general expectation is that highly rated and recommended products (__label 1__) should have a __positive__ sentiment and products which are not recommended (__label 0__) should have a __negative__ sentiment.\n", "\n", "TextBlob is an excellent open-source library for performing NLP tasks with ease, including sentiment analysis. It also an a sentiment lexicon (in the form of an XML file) which it leverages to give both polarity and subjectivity scores. \n", "\n", "- The polarity score is a float within the range [-1.0, 1.0]. \n", "- The subjectivity is a float within the range [0.0, 1.0] where 0.0 is very objective and 1.0 is very subjective. \n", "\n", "Perhaps this could be used for getting some new features? Let's look at some basic examples.\n", "\n", "Source: https://towardsdatascience.com/a-practitioners-guide-to-natural-language-processing-part-i-processing-understanding-text-9f4abfd13e72" ] }, { "cell_type": "code", "metadata": { "id": "oG7_MSsR9G2a", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "20f440ec-31e1-4d41-e276-2824b4348725" }, "source": [ "import textblob\n", "\n", "textblob.TextBlob('This is an AMAZING pair of Jeans!').sentiment" ], "execution_count": 36, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "Sentiment(polarity=0.7500000000000001, subjectivity=0.9)" ] }, "metadata": { "tags": [] }, "execution_count": 36 } ] }, { "cell_type": "code", "metadata": { "id": "71olVhAp9G2d", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "64b444d4-5e38-4474-fb4f-64826107da49" }, "source": [ "textblob.TextBlob('I really hated this UGLY T-shirt!!').sentiment" ], "execution_count": 37, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "Sentiment(polarity=-0.95, subjectivity=0.85)" ] }, "metadata": { "tags": [] }, "execution_count": 37 } ] }, { "cell_type": "markdown", "metadata": { "id": "G9vlglD09G2f", "colab_type": "text" }, "source": [ "Looks like this should help us get features which can distinguish between good and bad products. Let's try it out on our dataset!" ] }, { "cell_type": "markdown", "metadata": { "id": "k1D5kDCM9G2f", "colab_type": "text" }, "source": [ "# Experiment 2: Features from Sentiment Analysis \n", "\n", "Remember this is unsupervised, lexicon-based sentiment analysis where we don't have any pre-labeled data saying which review migth have a positive or negative sentiment. We use the lexicon to determine this." ] }, { "cell_type": "code", "metadata": { "id": "nkYd9_8A9G2f", "colab_type": "code", "colab": {} }, "source": [ "x_train_snt_obj = X_train['Review'].apply(lambda row: textblob.TextBlob(row).sentiment)\n", "X_train['Polarity'] = [obj.polarity for obj in x_train_snt_obj.values]\n", "X_train['Subjectivity'] = [obj.subjectivity for obj in x_train_snt_obj.values]\n", "\n", "x_test_snt_obj = X_test['Review'].apply(lambda row: textblob.TextBlob(row).sentiment)\n", "X_test['Polarity'] = [obj.polarity for obj in x_test_snt_obj.values]\n", "X_test['Subjectivity'] = [obj.subjectivity for obj in x_test_snt_obj.values]" ], "execution_count": 38, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "5so7o_W89G2h", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 195 }, "outputId": "57bb8e33-8416-4d78-8b3c-734aaf106dae" }, "source": [ "X_train.head()" ], "execution_count": 39, "outputs": [ { "output_type": "execute_result", "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", "
Reviewchar_countword_countword_densitypunctuation_counttitle_word_countupper_case_word_countPolaritySubjectivity
12896Soooo soft! This is a delightfully soft and fl...268525.0566048200.1704550.490909
13183Had my eye on this, but dind't get I finally v...399844.69411820210.1019440.719537
1496I wanted to like this... I wanted to like this...5251045.00000019220.1865380.458761
5205Beautiful blouse Bought this for my daughter i...203355.63888910200.6250000.825000
13366Boxy. large. Boxy, unflattering, and large.\\n\\...295515.67307722200.3296130.510268
\n", "
" ], "text/plain": [ " Review ... Subjectivity\n", "12896 Soooo soft! This is a delightfully soft and fl... ... 0.490909\n", "13183 Had my eye on this, but dind't get I finally v... ... 0.719537\n", "1496 I wanted to like this... I wanted to like this... ... 0.458761\n", "5205 Beautiful blouse Bought this for my daughter i... ... 0.825000\n", "13366 Boxy. large. Boxy, unflattering, and large.\\n\\... ... 0.510268\n", "\n", "[5 rows x 9 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 39 } ] }, { "cell_type": "markdown", "metadata": { "id": "G7xf9V_59G2j", "colab_type": "text" }, "source": [ "## Model Training and Evaluation" ] }, { "cell_type": "code", "metadata": { "id": "_fiZszFw9G2j", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 258 }, "outputId": "7ddabd2f-c990-46a9-a332-6dca46f3e917" }, "source": [ "lr.fit(X_train.drop(['Review'], axis=1), y_train, )\n", "predictions = lr.predict(X_test.drop(['Review'], axis=1))\n", "\n", "print(classification_report(y_test, predictions))\n", "pd.DataFrame(confusion_matrix(y_test, predictions))" ], "execution_count": 40, "outputs": [ { "output_type": "stream", "text": [ " precision recall f1-score support\n", "\n", " 0 0.69 0.27 0.38 1271\n", " 1 0.82 0.97 0.89 4390\n", "\n", " accuracy 0.81 5661\n", " macro avg 0.75 0.62 0.64 5661\n", "weighted avg 0.79 0.81 0.77 5661\n", "\n" ], "name": "stdout" }, { "output_type": "execute_result", "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", "
01
0339932
11534237
\n", "
" ], "text/plain": [ " 0 1\n", "0 339 932\n", "1 153 4237" ] }, "metadata": { "tags": [] }, "execution_count": 40 } ] }, { "cell_type": "markdown", "metadata": { "id": "0mx9hX449G2l", "colab_type": "text" }, "source": [ "Interesting! Looks like we are now able to predict __27%__ of the total number of bad or negative rated products now! \n", "Precision is quite good at __69%__\n", "\n", "__F1-Score__ for bad reviews is now __40%__ and good reviews is __89%__\n", "\n", "This brings our overall __F1-Score__ to __77%__ which is quite good.\n", "\n", "Can we still improve on our model since the recall of bad reviews is still pretty low?" ] }, { "cell_type": "markdown", "metadata": { "id": "U09f6e829G2l", "colab_type": "text" }, "source": [ "# Text Pre-processing and Wrangling\n", "\n", "We want to extract some specific features based on standard NLP feature engineering models like the classic Bag of Words model.\n", "For this we need to clean and pre-process our text data. We will build a simple text pre-processor here since the main intent is to look at feature engineering strategies.\n", "\n", "We will focus on:\n", "- Text Lowercasing\n", "- Removal of contractions\n", "- Removing unnecessary characters, numbers and symbols\n", "- Stemming\n", "- Stopword removal\n", "\n", "Source: https://towardsdatascience.com/a-practitioners-guide-to-natural-language-processing-part-i-processing-understanding-text-9f4abfd13e72" ] }, { "cell_type": "code", "metadata": { "id": "RNNjrbiRAXpA", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 235 }, "outputId": "a1a4b6dd-5381-4023-e7f4-19352e329ddb" }, "source": [ "!pip install contractions\n", "!pip install textsearch\n", "!pip install tqdm\n", "import nltk\n", "nltk.download('punkt')\n", "nltk.download('stopwords')" ], "execution_count": 41, "outputs": [ { "output_type": "stream", "text": [ "Requirement already satisfied: contractions in /usr/local/lib/python3.6/dist-packages (0.0.25)\n", "Requirement already satisfied: textsearch in /usr/local/lib/python3.6/dist-packages (from contractions) (0.0.17)\n", "Requirement already satisfied: pyahocorasick in /usr/local/lib/python3.6/dist-packages (from textsearch->contractions) (1.4.0)\n", "Requirement already satisfied: Unidecode in /usr/local/lib/python3.6/dist-packages (from textsearch->contractions) (1.1.1)\n", "Requirement already satisfied: textsearch in /usr/local/lib/python3.6/dist-packages (0.0.17)\n", "Requirement already satisfied: pyahocorasick in /usr/local/lib/python3.6/dist-packages (from textsearch) (1.4.0)\n", "Requirement already satisfied: Unidecode in /usr/local/lib/python3.6/dist-packages (from textsearch) (1.1.1)\n", "Requirement already satisfied: tqdm in /usr/local/lib/python3.6/dist-packages (4.41.1)\n", "[nltk_data] Downloading package punkt to /root/nltk_data...\n", "[nltk_data] Package punkt is already up-to-date!\n", "[nltk_data] Downloading package stopwords to /root/nltk_data...\n", "[nltk_data] Package stopwords is already up-to-date!\n" ], "name": "stdout" }, { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 41 } ] }, { "cell_type": "code", "metadata": { "id": "s8PQYYWm9G2n", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "32ae4c28-d3c2-414f-9ffb-2bc21ec0d2b6" }, "source": [ "import contractions\n", "\n", "contractions.fix('I didn\\'t like this t-shirt')" ], "execution_count": 42, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'I did not like this t-shirt'" ] }, "metadata": { "tags": [] }, "execution_count": 42 } ] }, { "cell_type": "code", "metadata": { "id": "ZSkz7gQY9G2t", "colab_type": "code", "colab": {} }, "source": [ "import nltk\n", "import contractions\n", "import re\n", "\n", "# remove some stopwords to capture negation in n-grams if possible\n", "stop_words = nltk.corpus.stopwords.words('english')\n", "stop_words.remove('no')\n", "stop_words.remove('not')\n", "stop_words.remove('but')\n", "\n", "# load up a simple porter stemmer - nothing fancy\n", "ps = nltk.porter.PorterStemmer()\n", "\n", "def simple_text_preprocessor(document): \n", " # lower case\n", " document = str(document).lower()\n", " \n", " # expand contractions\n", " document = contractions.fix(document)\n", " \n", " # remove unnecessary characters\n", " document = re.sub(r'[^a-zA-Z]',r' ', document)\n", " document = re.sub(r'nbsp', r'', document)\n", " document = re.sub(' +', ' ', document)\n", " \n", " # simple porter stemming\n", " document = ' '.join([ps.stem(word) for word in document.split()])\n", " \n", " # stopwords removal\n", " document = ' '.join([word for word in document.split() if word not in stop_words])\n", " \n", " return document\n", "\n", "stp = np.vectorize(simple_text_preprocessor)" ], "execution_count": 43, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "u3bHOnew9G2v", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 296 }, "outputId": "2d374c5c-dc4b-4d61-ec31-1fd3bf24fb97" }, "source": [ "X_train['Clean Review'] = stp(X_train['Review'].values)\n", "X_test['Clean Review'] = stp(X_test['Review'].values)\n", "\n", "X_train.head()" ], "execution_count": 44, "outputs": [ { "output_type": "execute_result", "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", "
Reviewchar_countword_countword_densitypunctuation_counttitle_word_countupper_case_word_countPolaritySubjectivityClean Review
12896Soooo soft! This is a delightfully soft and fl...268525.0566048200.1704550.490909soooo soft thi delight soft fluffi sweater mig...
13183Had my eye on this, but dind't get I finally v...399844.69411820210.1019440.719537eye thi but dind get final visit store petit t...
1496I wanted to like this... I wanted to like this...5251045.00000019220.1865380.458761want like thi want like thi top badli badli fa...
5205Beautiful blouse Bought this for my daughter i...203355.63888910200.6250000.825000beauti blous bought thi daughter law birthday ...
13366Boxy. large. Boxy, unflattering, and large.\\n\\...295515.67307722200.3296130.510268boxi larg boxi unflatt larg I curvi pound thi ...
\n", "
" ], "text/plain": [ " Review ... Clean Review\n", "12896 Soooo soft! This is a delightfully soft and fl... ... soooo soft thi delight soft fluffi sweater mig...\n", "13183 Had my eye on this, but dind't get I finally v... ... eye thi but dind get final visit store petit t...\n", "1496 I wanted to like this... I wanted to like this... ... want like thi want like thi top badli badli fa...\n", "5205 Beautiful blouse Bought this for my daughter i... ... beauti blous bought thi daughter law birthday ...\n", "13366 Boxy. large. Boxy, unflattering, and large.\\n\\... ... boxi larg boxi unflatt larg I curvi pound thi ...\n", "\n", "[5 rows x 10 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 44 } ] }, { "cell_type": "markdown", "metadata": { "id": "-wYVCWIQ9G2y", "colab_type": "text" }, "source": [ "## Extracting out the structured features from previous experiments" ] }, { "cell_type": "code", "metadata": { "id": "C7jXiEiG9G2y", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 195 }, "outputId": "36aeccd5-106c-4fbe-c75c-82d24d57fec9" }, "source": [ "X_train_metadata = X_train.drop(['Review', 'Clean Review'], axis=1).reset_index(drop=True)\n", "X_test_metadata = X_test.drop(['Review', 'Clean Review'], axis=1).reset_index(drop=True)\n", "\n", "X_train_metadata.head()" ], "execution_count": 45, "outputs": [ { "output_type": "execute_result", "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", "
char_countword_countword_densitypunctuation_counttitle_word_countupper_case_word_countPolaritySubjectivity
0268525.0566048200.1704550.490909
1399844.69411820210.1019440.719537
25251045.00000019220.1865380.458761
3203355.63888910200.6250000.825000
4295515.67307722200.3296130.510268
\n", "
" ], "text/plain": [ " char_count word_count ... Polarity Subjectivity\n", "0 268 52 ... 0.170455 0.490909\n", "1 399 84 ... 0.101944 0.719537\n", "2 525 104 ... 0.186538 0.458761\n", "3 203 35 ... 0.625000 0.825000\n", "4 295 51 ... 0.329613 0.510268\n", "\n", "[5 rows x 8 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 45 } ] }, { "cell_type": "markdown", "metadata": { "id": "r7777OYn9G21", "colab_type": "text" }, "source": [ "# Experiment 3: Adding Bag of Words based Features - 1-grams\n", "\n", "This is perhaps the most simple vector space representational model for unstructured text. A vector space model is simply a mathematical model to represent unstructured text (or any other data) as numeric vectors, such that each dimension of the vector is a specific feature\\attribute. \n", "\n", "The bag of words model represents each text document as a numeric vector where each dimension is a specific word from the corpus and the value could be its frequency in the document, occurrence (denoted by 1 or 0) or even weighted values. \n", "\n", "The model’s name is such because each document is represented literally as a ‘bag’ of its own words, disregarding word orders, sequences and grammar.\n", "\n", "Source: https://towardsdatascience.com/understanding-feature-engineering-part-3-traditional-methods-for-text-data-f6f7d70acd41" ] }, { "cell_type": "code", "metadata": { "id": "SaX1fDmT9G23", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 244 }, "outputId": "49a8d0d7-fdf0-4391-c636-015281834d30" }, "source": [ "from sklearn.feature_extraction.text import CountVectorizer\n", "\n", "cv = CountVectorizer(min_df=0.0, max_df=1.0, ngram_range=(1, 1))\n", "X_traincv = cv.fit_transform(X_train['Clean Review']).toarray()\n", "X_traincv = pd.DataFrame(X_traincv, columns=cv.get_feature_names())\n", "\n", "X_testcv = cv.transform(X_test['Clean Review']).toarray()\n", "X_testcv = pd.DataFrame(X_testcv, columns=cv.get_feature_names())\n", "X_traincv.head()" ], "execution_count": 46, "outputs": [ { "output_type": "execute_result", "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
aaaaaaandidonaaaaannnnnndaaaahaaaahmazaaahababbeyabbiabckabdomenabdominabercrombiabilabjectablabnormaboabolutabouabovabroadabruptliabsoabsolutabsolutleyabsolutliabsorbabstractabsurdabtabundabusacacaciaaccentaccentuaccentuateaacceptacceptabl...yogayogiyoginiyokeyolkyooyoreyorkyoselfyoungyoungeryouthyouthfulyryuckyuckiyukyumyumiyummiyummiestyummysweatyupzagzarazermattzerozigzigzagzillionzingzipzipperzipperizippizonezoolandzoomzowizuma
00000000000000000000000000000000000000000...0000000000000000000000000000000000000000
10000000000000000000000000000000000000000...1000000000000000000000000000000000000000
20000000000000000000000000000000000000000...0000000000000000000000000000000000000000
30000000000000000000000000000000000000000...0000000000000000000000000000000000000000
40000000000000000000000000000000000000000...0000000000000000000000000000000000000000
\n", "

5 rows × 8559 columns

\n", "
" ], "text/plain": [ " aa aaaaandidon aaaaannnnnnd aaaah ... zooland zoom zowi zuma\n", "0 0 0 0 0 ... 0 0 0 0\n", "1 0 0 0 0 ... 0 0 0 0\n", "2 0 0 0 0 ... 0 0 0 0\n", "3 0 0 0 0 ... 0 0 0 0\n", "4 0 0 0 0 ... 0 0 0 0\n", "\n", "[5 rows x 8559 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 46 } ] }, { "cell_type": "code", "metadata": { "id": "AMNdg_cg9G27", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 244 }, "outputId": "dcfd5c6a-1293-4e42-fdc8-af4bcb9a7ebb" }, "source": [ "X_train_comb = pd.concat([X_train_metadata, X_traincv], axis=1)\n", "X_test_comb = pd.concat([X_test_metadata, X_testcv], axis=1)\n", "\n", "X_train_comb.head()" ], "execution_count": 47, "outputs": [ { "output_type": "execute_result", "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
char_countword_countword_densitypunctuation_counttitle_word_countupper_case_word_countPolaritySubjectivityaaaaaaandidonaaaaannnnnndaaaahaaaahmazaaahababbeyabbiabckabdomenabdominabercrombiabilabjectablabnormaboabolutabouabovabroadabruptliabsoabsolutabsolutleyabsolutliabsorbabstractabsurdabtabund...yogayogiyoginiyokeyolkyooyoreyorkyoselfyoungyoungeryouthyouthfulyryuckyuckiyukyumyumiyummiyummiestyummysweatyupzagzarazermattzerozigzigzagzillionzingzipzipperzipperizippizonezoolandzoomzowizuma
0268525.0566048200.1704550.49090900000000000000000000000000000000...0000000000000000000000000000000000000000
1399844.69411820210.1019440.71953700000000000000000000000000000000...1000000000000000000000000000000000000000
25251045.00000019220.1865380.45876100000000000000000000000000000000...0000000000000000000000000000000000000000
3203355.63888910200.6250000.82500000000000000000000000000000000000...0000000000000000000000000000000000000000
4295515.67307722200.3296130.51026800000000000000000000000000000000...0000000000000000000000000000000000000000
\n", "

5 rows × 8567 columns

\n", "
" ], "text/plain": [ " char_count word_count word_density ... zoom zowi zuma\n", "0 268 52 5.056604 ... 0 0 0\n", "1 399 84 4.694118 ... 0 0 0\n", "2 525 104 5.000000 ... 0 0 0\n", "3 203 35 5.638889 ... 0 0 0\n", "4 295 51 5.673077 ... 0 0 0\n", "\n", "[5 rows x 8567 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 47 } ] }, { "cell_type": "markdown", "metadata": { "id": "OE5qPGfz9G3A", "colab_type": "text" }, "source": [ "## Model Training and Evaluation" ] }, { "cell_type": "code", "metadata": { "id": "fWaTndc-9G3A", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 258 }, "outputId": "2e50b231-5f42-4713-cbba-601dfd7eccb0" }, "source": [ "lr.fit(X_train_comb, y_train)\n", "predictions = lr.predict(X_test_comb)\n", "\n", "print(classification_report(y_test, predictions))\n", "pd.DataFrame(confusion_matrix(y_test, predictions))" ], "execution_count": 48, "outputs": [ { "output_type": "stream", "text": [ " precision recall f1-score support\n", "\n", " 0 0.76 0.70 0.73 1271\n", " 1 0.92 0.93 0.92 4390\n", "\n", " accuracy 0.88 5661\n", " macro avg 0.84 0.82 0.83 5661\n", "weighted avg 0.88 0.88 0.88 5661\n", "\n" ], "name": "stdout" }, { "output_type": "execute_result", "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", "
01
0890381
12864104
\n", "
" ], "text/plain": [ " 0 1\n", "0 890 381\n", "1 286 4104" ] }, "metadata": { "tags": [] }, "execution_count": 48 } ] }, { "cell_type": "markdown", "metadata": { "id": "1ePwRpYI9G3E", "colab_type": "text" }, "source": [ "Wow! This looks promising.\n", "\n", "We are now able to predict __70%__ of the total number of bad or negative rated products now! \n", "Precision is quite good at __76%__\n", "\n", "__F1-Score__ for bad reviews is now __73%__ and good reviews is __92%__\n", "\n", "This brings our overall __F1-Score__ to __88%__ which is quite good." ] } ] }