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",
"
Document
\n",
"
Category
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
The sky is blue and beautiful.
\n",
"
weather
\n",
"
\n",
"
\n",
"
1
\n",
"
Love this blue and beautiful sky!
\n",
"
weather
\n",
"
\n",
"
\n",
"
2
\n",
"
The quick brown fox jumps over the lazy dog.
\n",
"
animals
\n",
"
\n",
"
\n",
"
3
\n",
"
A king's breakfast has sausages, ham, bacon, eggs, toast and beans
\n",
"
food
\n",
"
\n",
"
\n",
"
4
\n",
"
I love green eggs, ham, sausages and bacon!
\n",
"
food
\n",
"
\n",
"
\n",
"
5
\n",
"
The brown fox is quick and the blue dog is lazy!
\n",
"
animals
\n",
"
\n",
"
\n",
"
6
\n",
"
The sky is very blue and the sky is very beautiful today
\n",
"
weather
\n",
"
\n",
"
\n",
"
7
\n",
"
The dog is lazy but the brown fox is quick!
\n",
"
animals
\n",
"
\n",
" \n",
"
\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",
"
bacon
\n",
"
beans
\n",
"
beautiful
\n",
"
blue
\n",
"
breakfast
\n",
"
brown
\n",
"
dog
\n",
"
eggs
\n",
"
fox
\n",
"
green
\n",
"
ham
\n",
"
jumps
\n",
"
kings
\n",
"
lazy
\n",
"
love
\n",
"
quick
\n",
"
sausages
\n",
"
sky
\n",
"
toast
\n",
"
today
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
\n",
"
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
\n",
"
\n",
"
2
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
1
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
\n",
"
\n",
"
3
\n",
"
1
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
\n",
"
\n",
"
4
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
1
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
\n",
"
\n",
"
5
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
1
\n",
"
1
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
\n",
"
\n",
"
6
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
2
\n",
"
0
\n",
"
1
\n",
"
\n",
"
\n",
"
7
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
1
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
\n",
" \n",
"
\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",
"
bacon eggs
\n",
"
beautiful sky
\n",
"
beautiful today
\n",
"
blue beautiful
\n",
"
blue dog
\n",
"
blue sky
\n",
"
breakfast sausages
\n",
"
brown fox
\n",
"
dog lazy
\n",
"
eggs ham
\n",
"
eggs toast
\n",
"
fox jumps
\n",
"
fox quick
\n",
"
green eggs
\n",
"
ham bacon
\n",
"
ham sausages
\n",
"
jumps lazy
\n",
"
kings breakfast
\n",
"
lazy brown
\n",
"
lazy dog
\n",
"
love blue
\n",
"
love green
\n",
"
quick blue
\n",
"
quick brown
\n",
"
sausages bacon
\n",
"
sausages ham
\n",
"
sky beautiful
\n",
"
sky blue
\n",
"
toast beans
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
\n",
"
\n",
"
1
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
\n",
"
\n",
"
2
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
\n",
"
\n",
"
3
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
\n",
"
\n",
"
4
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
\n",
"
\n",
"
5
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
\n",
"
\n",
"
6
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
1
\n",
"
0
\n",
"
\n",
"
\n",
"
7
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
1
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
0
\n",
"
\n",
" \n",
"
\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",
"\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",
"
bacon
\n",
"
beans
\n",
"
beautiful
\n",
"
blue
\n",
"
breakfast
\n",
"
brown
\n",
"
dog
\n",
"
eggs
\n",
"
fox
\n",
"
green
\n",
"
ham
\n",
"
jumps
\n",
"
kings
\n",
"
lazy
\n",
"
love
\n",
"
quick
\n",
"
sausages
\n",
"
sky
\n",
"
toast
\n",
"
today
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
0.00
\n",
"
0.00
\n",
"
0.60
\n",
"
0.53
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.60
\n",
"
0.00
\n",
"
0.0
\n",
"
\n",
"
\n",
"
1
\n",
"
0.00
\n",
"
0.00
\n",
"
0.49
\n",
"
0.43
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.57
\n",
"
0.00
\n",
"
0.00
\n",
"
0.49
\n",
"
0.00
\n",
"
0.0
\n",
"
\n",
"
\n",
"
2
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.38
\n",
"
0.38
\n",
"
0.00
\n",
"
0.38
\n",
"
0.00
\n",
"
0.00
\n",
"
0.53
\n",
"
0.00
\n",
"
0.38
\n",
"
0.00
\n",
"
0.38
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.0
\n",
"
\n",
"
\n",
"
3
\n",
"
0.32
\n",
"
0.38
\n",
"
0.00
\n",
"
0.00
\n",
"
0.38
\n",
"
0.00
\n",
"
0.00
\n",
"
0.32
\n",
"
0.00
\n",
"
0.00
\n",
"
0.32
\n",
"
0.00
\n",
"
0.38
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.32
\n",
"
0.00
\n",
"
0.38
\n",
"
0.0
\n",
"
\n",
"
\n",
"
4
\n",
"
0.39
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.39
\n",
"
0.00
\n",
"
0.47
\n",
"
0.39
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.39
\n",
"
0.00
\n",
"
0.39
\n",
"
0.00
\n",
"
0.00
\n",
"
0.0
\n",
"
\n",
"
\n",
"
5
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.37
\n",
"
0.00
\n",
"
0.42
\n",
"
0.42
\n",
"
0.00
\n",
"
0.42
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.42
\n",
"
0.00
\n",
"
0.42
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.0
\n",
"
\n",
"
\n",
"
6
\n",
"
0.00
\n",
"
0.00
\n",
"
0.36
\n",
"
0.32
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.72
\n",
"
0.00
\n",
"
0.5
\n",
"
\n",
"
\n",
"
7
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.45
\n",
"
0.45
\n",
"
0.00
\n",
"
0.45
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.45
\n",
"
0.00
\n",
"
0.45
\n",
"
0.00
\n",
"
0.00
\n",
"
0.00
\n",
"
0.0
\n",
"
\n",
" \n",
"
\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",
"
0
\n",
"
1
\n",
"
2
\n",
"
3
\n",
"
4
\n",
"
5
\n",
"
6
\n",
"
7
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
1.000000
\n",
"
0.820599
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.192353
\n",
"
0.817246
\n",
"
0.000000
\n",
"
\n",
"
\n",
"
1
\n",
"
0.820599
\n",
"
1.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.225489
\n",
"
0.157845
\n",
"
0.670631
\n",
"
0.000000
\n",
"
\n",
"
\n",
"
2
\n",
"
0.000000
\n",
"
0.000000
\n",
"
1.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.791821
\n",
"
0.000000
\n",
"
0.850516
\n",
"
\n",
"
\n",
"
3
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
1.000000
\n",
"
0.506866
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
\n",
"
\n",
"
4
\n",
"
0.000000
\n",
"
0.225489
\n",
"
0.000000
\n",
"
0.506866
\n",
"
1.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
\n",
"
\n",
"
5
\n",
"
0.192353
\n",
"
0.157845
\n",
"
0.791821
\n",
"
0.000000
\n",
"
0.000000
\n",
"
1.000000
\n",
"
0.115488
\n",
"
0.930989
\n",
"
\n",
"
\n",
"
6
\n",
"
0.817246
\n",
"
0.670631
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.115488
\n",
"
1.000000
\n",
"
0.000000
\n",
"
\n",
"
\n",
"
7
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.850516
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.930989
\n",
"
0.000000
\n",
"
1.000000
\n",
"
\n",
" \n",
"
\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",
"\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",
"
Document
\n",
"
Category
\n",
"
ClusterLabel
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
The sky is blue and beautiful.
\n",
"
weather
\n",
"
2
\n",
"
\n",
"
\n",
"
1
\n",
"
Love this blue and beautiful sky!
\n",
"
weather
\n",
"
2
\n",
"
\n",
"
\n",
"
2
\n",
"
The quick brown fox jumps over the lazy dog.
\n",
"
animals
\n",
"
1
\n",
"
\n",
"
\n",
"
3
\n",
"
A king's breakfast has sausages, ham, bacon, eggs, toast and beans
\n",
"
food
\n",
"
0
\n",
"
\n",
"
\n",
"
4
\n",
"
I love green eggs, ham, sausages and bacon!
\n",
"
food
\n",
"
0
\n",
"
\n",
"
\n",
"
5
\n",
"
The brown fox is quick and the blue dog is lazy!
\n",
"
animals
\n",
"
1
\n",
"
\n",
"
\n",
"
6
\n",
"
The sky is very blue and the sky is very beautiful today
\n",
"
weather
\n",
"
2
\n",
"
\n",
"
\n",
"
7
\n",
"
The dog is lazy but the brown fox is quick!
\n",
"
animals
\n",
"
1
\n",
"
\n",
" \n",
"
\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",
"
Document
\n",
"
Category
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
The sky is blue and beautiful.
\n",
"
weather
\n",
"
\n",
"
\n",
"
1
\n",
"
Love this blue and beautiful sky!
\n",
"
weather
\n",
"
\n",
"
\n",
"
2
\n",
"
The quick brown fox jumps over the lazy dog.
\n",
"
animals
\n",
"
\n",
"
\n",
"
3
\n",
"
A king's breakfast has sausages, ham, bacon, eggs, toast and beans
\n",
"
food
\n",
"
\n",
"
\n",
"
4
\n",
"
I love green eggs, ham, sausages and bacon!
\n",
"
food
\n",
"
\n",
"
\n",
"
5
\n",
"
The brown fox is quick and the blue dog is lazy!
\n",
"
animals
\n",
"
\n",
"
\n",
"
6
\n",
"
The sky is very blue and the sky is very beautiful today
\n",
"
weather
\n",
"
\n",
"
\n",
"
7
\n",
"
The dog is lazy but the brown fox is quick!
\n",
"
animals
\n",
"
\n",
" \n",
"
\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\nUiVJkqQDC4Jg3eHsd8TTOYIgqAK0BsYDhG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f8/OnTvj++zdv1SpUvsc\nW6pUqUPOTy+smoMg2GeXIAgYOHAg06ZNY9myZfTv33+fmqOIOudeUvFwpNM53gIahWGYBPwfcCdA\nEAQNgGuJ/WNpe+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/7iSee4K233m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ffv36xbulRK3p88vXUeH3t9KrVMDQ\noUPj56lWrRo5OTlMmzYtfmhCQgKLFi0C2Gf9J598wrnnnsstt9xCp06dWLp06UG7vKSlpR30Wi+/\n/HIef/zx+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\nSSpCJ510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sM3A18lXtwAR7HeVRp\nFLA8P0HoHGBVRKyXtN+RvKMAAABwSURBVBdYKekpYAfFHz/k17cldVA8sDu3ikG3mMdo4Cz8HwvN\nzMzMzGrk7RxmZmZmZjXyItrMzMzMrEZeRJuZmZmZ1ciLaDMzMzOzGnkRbWZmZmZWIy+izczMzMxq\n5EW0mZmZmVmNvIg2MzMzM6vRvwEyPu4Vjj3QAAAAAElFTkSuQmCC\n",
"text/plain": [
"
"
],
"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",
"\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": [
"
"
],
"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/J6Ph48POc697nvz33P6cybm2sx1loBAAAAcIafrwsAAAAAKhICOAAAAOAgAjgA\nAADgIAI4AAAA4CACOAAAAOAgAjgAAADgIAI4AAAA4CACOAAAAOAgAjgAAADgIAI4AAAA4KBKvi7g\nYtWpU8e6XC5flwEAAIByLD09/bC1tm5JHqPMBHCXy6W0tDRflwEAAIByzBiTWdLHoAsKAAAA4CAC\nOAAAAOAgAjgAAADgIAI4AAAolTIyMhQaGurrMoBiRwAHAAAAHEQABwAApVZ+fr7Gjh2rVq1a6dZb\nb9Xx48e1aNEiRUdHKzw8XIMGDVJubq4kaeTIkRo/frzat2+vxo0bKzk5WaNHj1bLli01cuRI354I\ncAYCOABUUCX95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vvqq9u7dq9tuu01t2rRR586dvdfkrbfeUrt27dS6dWv16NHDOx3i\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/zhD5Kk+Ph41atXT5MnT/bxGZQdfv5+svFW8j+jMV8yiUYF+QU+qcnVzKXMDplS\n8BmN+6VGGxopa0+WcnNz1aVLFy1cuFCRkZE+qbG4XOhcgz4MUsbnGY7WkpOTo2rVqikvL08DBgzQ\n6NGjNWDAAEdrKK0yMjLUpUsX77iB999/X3PmzJHH49EHH3ygoKAg5eTkqG7dumrevLn3fSdPntSu\nXbu0bds23XfffTp48KB++uknBQcHa82aNUpISNBrr72mRo0aadWqVbrqqqskScaYg5L+aq2dVfQ8\nQdLp/zFcknpJ2i0pTdI7kt5WYb/vAmNMsgrD+y+GVrqgAADOsmbNGt1www3asmWL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VaGio3G63li9fLklKTk5W165d9fprr6uKXxUNGTxE+3fvJ3z7WGnql1+SuAMO\nABXYld5tKigo8M7Be3qlP6C4ZGRkKDg4WBs2bFDHjh01evRohYSEaPTo0d5VI3/729/qrrvuUt++\nfdWuXTtNnz5dAwYM0IkTJ1RQUKB3331X8+fP15o1a3T48GFFR0fr448/1u7duxUbG6sdO3bohhtu\nUMeOHfX000//rG8xnMUdcABAuXcld5t27typm266SbfccgvhGyWmUaNG6tixoyRp2LBh2rBhg9at\nW6d27drJ7Xbr/fff144dO3Ts2DEdOHDAO3VflSpVFBAQoA0bNmjIkCHy9/dX/fr1FRMTo9TUVElS\n27Zt1bBhQ/n5+SkiIkIZGRm+Ok0UqSj98iv5ugAAgO8ENglUZtY5d5suchaIkJAQ7du3r+SKA3T+\n6ecmTJigtLQ0NWrUSAkJCb+6nPuFVK5c2fvY39//slYNRfE63QUoPiFeWS8X9cufXf765XMHHAAq\nsIpytwllw6VMP3fusvDVq1dXw4YNtWrVKkmFi7Dk5uaqc+fOWr58ufLz83Xo0CGtX79ebdu29c0J\n4qKUpjEIJYU74ABQgVWUu00o/U4PCM7tnSsNljKzzp5+7nT/7/Hjxys7O1uhoaG6/vrrFR0d7d3H\nyy+/rN/97nf685//rKuuukqvvvqqBgwYoE2bNik8PFzGGD311FO6/vrr9dlnn/nwbFHRMQgTAEqx\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/9618VFhamPXv2eNuTk5MVHh6u1q1ba/ny5Zo8ebIvT9Xr0KFD\nio2NVVJSksLDw3/2+u23367KlSurTp06qlevnr755htt3LhRsbGxqlKliqpXr66+fft6tw8LC1Nc\nXJxeeeUVVapUse4JE8ABoJS70DzIp9WvX1+7du1SQUGBXn/9dW/7rbfeqmeffdb73OPxOFMwUM6d\nnjIxs0OmbLxVZodMjZsyTklLk3T33XfL4/Fo69atSk9PV/v27TV06FB98cUX2rhxo77//nvv7EU3\n3nijPvroI23dulWRkZHe9hEjRmj79u369NNPlZKSouDg4F8qxzE1a9ZUYGCgNmzYcN7XL3Vho7ff\nflv33nuvNm/erOjo6Aq1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a1wXH60nYkraYMFD+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",
"
0
\n",
"
1
\n",
"
2
\n",
"
3
\n",
"
4
\n",
"
5
\n",
"
6
\n",
"
7
\n",
"
8
\n",
"
9
\n",
"
10
\n",
"
11
\n",
"
12
\n",
"
13
\n",
"
14
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
-1.017834
\n",
"
1.219471
\n",
"
-0.471404
\n",
"
0.100632
\n",
"
-0.485411
\n",
"
-0.580870
\n",
"
-0.183611
\n",
"
-0.032766
\n",
"
-0.266565
\n",
"
0.289460
\n",
"
0.074620
\n",
"
0.748789
\n",
"
-0.517958
\n",
"
0.038283
\n",
"
-0.297274
\n",
"
\n",
"
\n",
"
1
\n",
"
-1.051389
\n",
"
0.996752
\n",
"
-0.461333
\n",
"
0.187398
\n",
"
-0.399002
\n",
"
-0.535357
\n",
"
-0.133190
\n",
"
-0.102741
\n",
"
-0.235152
\n",
"
0.255751
\n",
"
0.070397
\n",
"
0.575714
\n",
"
-0.376731
\n",
"
0.007694
\n",
"
-0.241070
\n",
"
\n",
"
\n",
"
2
\n",
"
-0.080685
\n",
"
1.394714
\n",
"
1.015425
\n",
"
0.091486
\n",
"
-1.111383
\n",
"
-0.360878
\n",
"
-0.166006
\n",
"
0.346778
\n",
"
-0.375867
\n",
"
0.003422
\n",
"
0.676978
\n",
"
-0.001734
\n",
"
-0.380349
\n",
"
0.563245
\n",
"
0.395930
\n",
"
\n",
"
\n",
"
3
\n",
"
-1.443467
\n",
"
0.329564
\n",
"
-0.008377
\n",
"
1.037905
\n",
"
-0.312026
\n",
"
-0.233052
\n",
"
0.184552
\n",
"
-0.440906
\n",
"
-0.320137
\n",
"
-0.371566
\n",
"
0.666171
\n",
"
-0.895322
\n",
"
0.647324
\n",
"
-0.199162
\n",
"
-0.239623
\n",
"
\n",
"
\n",
"
4
\n",
"
-1.307474
\n",
"
0.324607
\n",
"
-0.140498
\n",
"
0.826300
\n",
"
-0.173477
\n",
"
-0.265190
\n",
"
0.072099
\n",
"
-0.486643
\n",
"
-0.283610
\n",
"
-0.157870
\n",
"
0.412292
\n",
"
-0.508161
\n",
"
0.427139
\n",
"
-0.166446
\n",
"
-0.225815
\n",
"
\n",
"
\n",
"
5
\n",
"
-0.189542
\n",
"
1.352578
\n",
"
0.803024
\n",
"
0.068530
\n",
"
-1.020865
\n",
"
-0.378753
\n",
"
-0.193295
\n",
"
0.301214
\n",
"
-0.350197
\n",
"
0.057726
\n",
"
0.584805
\n",
"
0.126631
\n",
"
-0.409361
\n",
"
0.484519
\n",
"
0.307555
\n",
"
\n",
"
\n",
"
6
\n",
"
-1.085396
\n",
"
1.229123
\n",
"
-0.536072
\n",
"
0.100034
\n",
"
-0.476581
\n",
"
-0.611018
\n",
"
-0.181043
\n",
"
-0.026112
\n",
"
-0.259882
\n",
"
0.283033
\n",
"
0.046487
\n",
"
0.772775
\n",
"
-0.532793
\n",
"
0.030967
\n",
"
-0.318839
\n",
"
\n",
"
\n",
"
7
\n",
"
-0.088025
\n",
"
1.415853
\n",
"
1.003639
\n",
"
0.080209
\n",
"
-1.119665
\n",
"
-0.375660
\n",
"
-0.176112
\n",
"
0.350506
\n",
"
-0.377900
\n",
"
0.002104
\n",
"
0.662523
\n",
"
0.023472
\n",
"
-0.398187
\n",
"
0.561428
\n",
"
0.385074
\n",
"
\n",
" \n",
"
\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",
"
Document
\n",
"
Category
\n",
"
ClusterLabel
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
The sky is blue and beautiful.
\n",
"
weather
\n",
"
0
\n",
"
\n",
"
\n",
"
1
\n",
"
Love this blue and beautiful sky!
\n",
"
weather
\n",
"
0
\n",
"
\n",
"
\n",
"
2
\n",
"
The quick brown fox jumps over the lazy dog.
\n",
"
animals
\n",
"
2
\n",
"
\n",
"
\n",
"
3
\n",
"
A king's breakfast has sausages, ham, bacon, eggs, toast and beans
\n",
"
food
\n",
"
1
\n",
"
\n",
"
\n",
"
4
\n",
"
I love green eggs, ham, sausages and bacon!
\n",
"
food
\n",
"
1
\n",
"
\n",
"
\n",
"
5
\n",
"
The brown fox is quick and the blue dog is lazy!
\n",
"
animals
\n",
"
2
\n",
"
\n",
"
\n",
"
6
\n",
"
The sky is very blue and the sky is very beautiful today
\n",
"
weather
\n",
"
0
\n",
"
\n",
"
\n",
"
7
\n",
"
The dog is lazy but the brown fox is quick!
\n",
"
animals
\n",
"
2
\n",
"
\n",
" \n",
"
\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\nAMDFIbwBAPAM4Q0AgGcIbwAAPEN4AwDgGcIbAADPEN4AAHiG8AYAwDOENwAAniG8AQDwzGWxLqA9\niYmJLhAIxLoMAACiprKy8ohzbsiF9uu24R0IBBQKhWJdBgAAUWNmtR3Zj9PmAAB4hvAGAMAzhDcA\nAJ4hvAEA8AzhDQCAZwhvAAA8Q3gD8F5paakOHTrUsh0IBHTkyJEYVgR0LcIbgPfODe/OaGpqikg/\nQFcivAFE3c9//nMVFxdLkubMmaPc3FxJ0oYNG5SXl6e1a9cqOztbo0eP1l133aUTJ05IkhYuXKgb\nb7xRqampys/Pl3NO5eXlCoVCysvLU0ZGhk6dOiVJWrx4sUaPHq20tDTt3r1bktTQ0KD7779fY8eO\nVWZmpl566SVJzeE/bdo05ebmatKkSdGeDuCiEd4Aoi4nJ0ebNm2SJIVCIZ04cUKnT5/Wpk2blJ6e\nrscff1zr169XVVWVgsGgfvnLX0qSHnzwQW3ZskXV1dU6deqUXn75Zd15550KBoMqKyvT1q1b1b9/\nf0lSYmKiqqqq9IMf/ECLFi2SJBUVFSk3N1dvv/22Kioq9PDDD6uhoUGSVFVVp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N+f2+njzOvi/1mtlFSpqSeEN4d+T6f2ed9M7tM0pWS6qNTXtRccB6cc62f83OS/isKdXVXPebn\nQ2c55463ur3azJ42s0TnXHe55vnn9MjT5mZ2hZkNOHNb0s1qXtzVG22RNNLMRphZP0l3S+pRq6zD\nVkn6Tvj2dyT9y1kJMxtkZvHh24mSbpK0M2oVdq2OfJ9bz9Gdkja4nnehhwvOwznv6U6TtCuK9XU3\nqyTdG151niXpWKu3n3oVM/vSmTUgZjZWzfnYfX+5dc559SXpdjW/L/OppA8lvRJuHyZpdfj2tZK2\nhb92qPkUc8xrj8VchLe/KeldNR9h9tS5+Dc1rzLfK2m9pMHh9qCk58K3x0l6J/y6eEfS7FjXHeE5\n+Jfvs6SFkqaFb18u6f9I2ifpbUnXxrrmGM3Dz8I/F7ZJqpB0faxr7sK5WC7pA0mnwz8rZksqkFQQ\nvt/UvDr/vfD/iWCsa47hXDzY6nXxpqRxsa75fF9cYQ0AAM/0yNPmAAD0ZIQ3AACeIbwBAPAM4Q0A\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",
"\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": [
"
"
],
"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",
"
0
\n",
"
1
\n",
"
2
\n",
"
3
\n",
"
4
\n",
"
5
\n",
"
6
\n",
"
7
\n",
"
8
\n",
"
9
\n",
"
10
\n",
"
11
\n",
"
12
\n",
"
13
\n",
"
14
\n",
"
15
\n",
"
16
\n",
"
17
\n",
"
18
\n",
"
19
\n",
"
20
\n",
"
21
\n",
"
22
\n",
"
23
\n",
"
24
\n",
"
25
\n",
"
26
\n",
"
27
\n",
"
28
\n",
"
29
\n",
"
30
\n",
"
31
\n",
"
32
\n",
"
33
\n",
"
34
\n",
"
35
\n",
"
36
\n",
"
37
\n",
"
38
\n",
"
39
\n",
"
...
\n",
"
4760
\n",
"
4761
\n",
"
4762
\n",
"
4763
\n",
"
4764
\n",
"
4765
\n",
"
4766
\n",
"
4767
\n",
"
4768
\n",
"
4769
\n",
"
4770
\n",
"
4771
\n",
"
4772
\n",
"
4773
\n",
"
4774
\n",
"
4775
\n",
"
4776
\n",
"
4777
\n",
"
4778
\n",
"
4779
\n",
"
4780
\n",
"
4781
\n",
"
4782
\n",
"
4783
\n",
"
4784
\n",
"
4785
\n",
"
4786
\n",
"
4787
\n",
"
4788
\n",
"
4789
\n",
"
4790
\n",
"
4791
\n",
"
4792
\n",
"
4793
\n",
"
4794
\n",
"
4795
\n",
"
4796
\n",
"
4797
\n",
"
4798
\n",
"
4799
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
1.00000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.006070
\n",
"
0.008067
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.025531
\n",
"
0.008554
\n",
"
0.018111
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.007439
\n",
"
0.010454
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.008190
\n",
"
0.008365
\n",
"
0.010035
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.050976
\n",
"
0.006502
\n",
"
0.0
\n",
"
0.010728
\n",
"
0.0
\n",
"
0.006908
\n",
"
0.000000
\n",
"
0.167573
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.009191
\n",
"
0.053475
\n",
"
...
\n",
"
0.000000
\n",
"
0.009711
\n",
"
0.006508
\n",
"
0.000000
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.0
\n",
"
0.028409
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.008870
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.0
\n",
"
0.033246
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.034092
\n",
"
0.018754
\n",
"
0.000000
\n",
"
0.037924
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.009646
\n",
"
\n",
"
\n",
"
1
\n",
"
0.00000
\n",
"
1.000000
\n",
"
0.000000
\n",
"
0.017839
\n",
"
0.007967
\n",
"
0.000000
\n",
"
0.0
\n",
"
0.012501
\n",
"
0.0
\n",
"
0.014840
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.012814
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.024144
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.008101
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.016898
\n",
"
0.0
\n",
"
0.017789
\n",
"
0.0
\n",
"
0.008885
\n",
"
0.009432
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.014947
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.022738
\n",
"
...
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.019783
\n",
"
0.000000
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.011407
\n",
"
0.011409
\n",
"
0.000000
\n",
"
0.011632
\n",
"
0.000000
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.015329
\n",
"
0.0
\n",
"
0.008367
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.021596
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.017561
\n",
"
0.0
\n",
"
0.019152
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.007963
\n",
"
\n",
"
\n",
"
2
\n",
"
0.00000
\n",
"
0.000000
\n",
"
1.000000
\n",
"
0.000000
\n",
"
0.017176
\n",
"
0.000000
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.0
\n",
"
0.024326
\n",
"
0.005471
\n",
"
0.018038
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.005099
\n",
"
0.004985
\n",
"
0.004705
\n",
"
0.000000
\n",
"
0.004843
\n",
"
0.000000
\n",
"
0.017026
\n",
"
0.000000
\n",
"
0.003672
\n",
"
0.003984
\n",
"
0.030998
\n",
"
0.006959
\n",
"
0.000000
\n",
"
0.0
\n",
"
0.006117
\n",
"
0.0
\n",
"
0.019548
\n",
"
0.020365
\n",
"
0.009213
\n",
"
0.028467
\n",
"
0.010515
\n",
"
0.004198
\n",
"
0.006311
\n",
"
0.015154
\n",
"
0.012698
\n",
"
...
\n",
"
0.020597
\n",
"
0.004723
\n",
"
0.000000
\n",
"
0.016673
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.0
\n",
"
0.006625
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.006972
\n",
"
0.000000
\n",
"
0.010574
\n",
"
0.0
\n",
"
0.008222
\n",
"
0.008604
\n",
"
0.012782
\n",
"
0.015353
\n",
"
0.006259
\n",
"
0.000000
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.0
\n",
"
0.010555
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.006903
\n",
"
0.005023
\n",
"
0.0
\n",
"
0.012893
\n",
"
0.000000
\n",
"
0.025975
\n",
"
0.000000
\n",
"
0.027126
\n",
"
0.009340
\n",
"
\n",
"
\n",
"
3
\n",
"
0.00000
\n",
"
0.017839
\n",
"
0.000000
\n",
"
1.000000
\n",
"
0.000000
\n",
"
0.022414
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.0
\n",
"
0.037207
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.027958
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.012232
\n",
"
0.017893
\n",
"
0.043639
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.0
\n",
"
0.023127
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.009324
\n",
"
0.022995
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.044476
\n",
"
...
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.014029
\n",
"
0.030561
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.056761
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.018486
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.060846
\n",
"
0.025035
\n",
"
0.0
\n",
"
0.036237
\n",
"
0.030516
\n",
"
0.022605
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.000000
\n",
"
\n",
"
\n",
"
4
\n",
"
0.00607
\n",
"
0.007967
\n",
"
0.017176
\n",
"
0.000000
\n",
"
1.000000
\n",
"
0.004672
\n",
"
0.0
\n",
"
0.064572
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.000000
\n",
"
0.036611
\n",
"
0.023879
\n",
"
0.014421
\n",
"
0.012316
\n",
"
0.019834
\n",
"
0.008170
\n",
"
0.004308
\n",
"
0.025459
\n",
"
0.000000
\n",
"
0.008907
\n",
"
0.023736
\n",
"
0.011341
\n",
"
0.003009
\n",
"
0.000000
\n",
"
0.026928
\n",
"
0.000000
\n",
"
0.010026
\n",
"
0.0
\n",
"
0.019557
\n",
"
0.0
\n",
"
0.028521
\n",
"
0.016018
\n",
"
0.011311
\n",
"
0.016912
\n",
"
0.022974
\n",
"
0.054142
\n",
"
0.013767
\n",
"
0.027397
\n",
"
0.005416
\n",
"
...
\n",
"
0.005088
\n",
"
0.015097
\n",
"
0.030759
\n",
"
0.013663
\n",
"
0.0
\n",
"
0.012624
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.0
\n",
"
0.000000
\n",
"
0.027018
\n",
"
0.012025
\n",
"
0.005714
\n",
"
0.000000
\n",
"
0.017230
\n",
"
0.0
\n",
"
0.022327
\n",
"
0.007051
\n",
"
0.038014
\n",
"
0.011783
\n",
"
0.018612
\n",
"
0.021468
\n",
"
0.0
\n",
"
0.003767
\n",
"
0.0
\n",
"
0.008650
\n",
"
0.009499
\n",
"
0.000000
\n",
"
0.012749
\n",
"
0.000000
\n",
"
0.022056
\n",
"
0.019659
\n",
"
0.036850
\n",
"
0.0
\n",
"
0.015824
\n",
"
0.000000
\n",
"
0.076022
\n",
"
0.004515
\n",
"
0.043469
\n",
"
0.011464
\n",
"
\n",
" \n",
"
\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",
"\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",
"
Unnamed: 0
\n",
"
Clothing ID
\n",
"
Age
\n",
"
Title
\n",
"
Review Text
\n",
"
Rating
\n",
"
Recommended IND
\n",
"
Positive Feedback Count
\n",
"
Division Name
\n",
"
Department Name
\n",
"
Class Name
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
0
\n",
"
767
\n",
"
33
\n",
"
\n",
"
Absolutely wonderful - silky and sexy and comf...
\n",
"
4
\n",
"
1
\n",
"
0
\n",
"
Initmates
\n",
"
Intimate
\n",
"
Intimates
\n",
"
\n",
"
\n",
"
1
\n",
"
1
\n",
"
1080
\n",
"
34
\n",
"
\n",
"
Love this dress! it's sooo pretty. i happene...
\n",
"
5
\n",
"
1
\n",
"
4
\n",
"
General
\n",
"
Dresses
\n",
"
Dresses
\n",
"
\n",
"
\n",
"
2
\n",
"
2
\n",
"
1077
\n",
"
60
\n",
"
Some major design flaws
\n",
"
I had such high hopes for this dress and reall...
\n",
"
3
\n",
"
0
\n",
"
0
\n",
"
General
\n",
"
Dresses
\n",
"
Dresses
\n",
"
\n",
"
\n",
"
3
\n",
"
3
\n",
"
1049
\n",
"
50
\n",
"
My favorite buy!
\n",
"
I love, love, love this jumpsuit. it's fun, fl...
\n",
"
5
\n",
"
1
\n",
"
0
\n",
"
General Petite
\n",
"
Bottoms
\n",
"
Pants
\n",
"
\n",
"
\n",
"
4
\n",
"
4
\n",
"
847
\n",
"
47
\n",
"
Flattering shirt
\n",
"
This shirt is very flattering to all due to th...
\n",
"
5
\n",
"
1
\n",
"
6
\n",
"
General
\n",
"
Tops
\n",
"
Blouses
\n",
"
\n",
" \n",
"
\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",
"
Review
\n",
"
Rating
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
Absolutely wonderful - silky and sexy and comf...
\n",
"
1
\n",
"
\n",
"
\n",
"
1
\n",
"
Love this dress! it's sooo pretty. i happene...
\n",
"
1
\n",
"
\n",
"
\n",
"
2
\n",
"
Some major design flaws I had such high hopes ...
\n",
"
0
\n",
"
\n",
"
\n",
"
3
\n",
"
My favorite buy! I love, love, love this jumps...
\n",
"
1
\n",
"
\n",
"
\n",
"
4
\n",
"
Flattering shirt This shirt is very flattering...
\n",
"
1
\n",
"
\n",
" \n",
"
\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",
"
Review
\n",
"
char_count
\n",
"
word_count
\n",
"
word_density
\n",
"
punctuation_count
\n",
"
title_word_count
\n",
"
upper_case_word_count
\n",
"
\n",
" \n",
" \n",
"
\n",
"
12896
\n",
"
Soooo soft! This is a delightfully soft and fl...
\n",
"
268
\n",
"
52
\n",
"
5.056604
\n",
"
8
\n",
"
2
\n",
"
0
\n",
"
\n",
"
\n",
"
13183
\n",
"
Had my eye on this, but dind't get I finally v...
\n",
"
399
\n",
"
84
\n",
"
4.694118
\n",
"
20
\n",
"
2
\n",
"
1
\n",
"
\n",
"
\n",
"
1496
\n",
"
I wanted to like this... I wanted to like this...
\n",
"
525
\n",
"
104
\n",
"
5.000000
\n",
"
19
\n",
"
2
\n",
"
2
\n",
"
\n",
"
\n",
"
5205
\n",
"
Beautiful blouse Bought this for my daughter i...
\n",
"
203
\n",
"
35
\n",
"
5.638889
\n",
"
10
\n",
"
2
\n",
"
0
\n",
"
\n",
"
\n",
"
13366
\n",
"
Boxy. large. Boxy, unflattering, and large.\\n\\...
\n",
"
295
\n",
"
51
\n",
"
5.673077
\n",
"
22
\n",
"
2
\n",
"
0
\n",
"
\n",
" \n",
"
\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",
"
0
\n",
"
1
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
0
\n",
"
1271
\n",
"
\n",
"
\n",
"
1
\n",
"
0
\n",
"
4390
\n",
"
\n",
" \n",
"
\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",
"
Review
\n",
"
char_count
\n",
"
word_count
\n",
"
word_density
\n",
"
punctuation_count
\n",
"
title_word_count
\n",
"
upper_case_word_count
\n",
"
Polarity
\n",
"
Subjectivity
\n",
"
\n",
" \n",
" \n",
"
\n",
"
12896
\n",
"
Soooo soft! This is a delightfully soft and fl...
\n",
"
268
\n",
"
52
\n",
"
5.056604
\n",
"
8
\n",
"
2
\n",
"
0
\n",
"
0.170455
\n",
"
0.490909
\n",
"
\n",
"
\n",
"
13183
\n",
"
Had my eye on this, but dind't get I finally v...
\n",
"
399
\n",
"
84
\n",
"
4.694118
\n",
"
20
\n",
"
2
\n",
"
1
\n",
"
0.101944
\n",
"
0.719537
\n",
"
\n",
"
\n",
"
1496
\n",
"
I wanted to like this... I wanted to like this...
\n",
"
525
\n",
"
104
\n",
"
5.000000
\n",
"
19
\n",
"
2
\n",
"
2
\n",
"
0.186538
\n",
"
0.458761
\n",
"
\n",
"
\n",
"
5205
\n",
"
Beautiful blouse Bought this for my daughter i...
\n",
"
203
\n",
"
35
\n",
"
5.638889
\n",
"
10
\n",
"
2
\n",
"
0
\n",
"
0.625000
\n",
"
0.825000
\n",
"
\n",
"
\n",
"
13366
\n",
"
Boxy. large. Boxy, unflattering, and large.\\n\\...
\n",
"
295
\n",
"
51
\n",
"
5.673077
\n",
"
22
\n",
"
2
\n",
"
0
\n",
"
0.329613
\n",
"
0.510268
\n",
"
\n",
" \n",
"
\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",
"
0
\n",
"
1
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
339
\n",
"
932
\n",
"
\n",
"
\n",
"
1
\n",
"
153
\n",
"
4237
\n",
"
\n",
" \n",
"
\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",
"
Review
\n",
"
char_count
\n",
"
word_count
\n",
"
word_density
\n",
"
punctuation_count
\n",
"
title_word_count
\n",
"
upper_case_word_count
\n",
"
Polarity
\n",
"
Subjectivity
\n",
"
Clean Review
\n",
"
\n",
" \n",
" \n",
"
\n",
"
12896
\n",
"
Soooo soft! This is a delightfully soft and fl...
\n",
"
268
\n",
"
52
\n",
"
5.056604
\n",
"
8
\n",
"
2
\n",
"
0
\n",
"
0.170455
\n",
"
0.490909
\n",
"
soooo soft thi delight soft fluffi sweater mig...
\n",
"
\n",
"
\n",
"
13183
\n",
"
Had my eye on this, but dind't get I finally v...
\n",
"
399
\n",
"
84
\n",
"
4.694118
\n",
"
20
\n",
"
2
\n",
"
1
\n",
"
0.101944
\n",
"
0.719537
\n",
"
eye thi but dind get final visit store petit t...
\n",
"
\n",
"
\n",
"
1496
\n",
"
I wanted to like this... I wanted to like this...
\n",
"
525
\n",
"
104
\n",
"
5.000000
\n",
"
19
\n",
"
2
\n",
"
2
\n",
"
0.186538
\n",
"
0.458761
\n",
"
want like thi want like thi top badli badli fa...
\n",
"
\n",
"
\n",
"
5205
\n",
"
Beautiful blouse Bought this for my daughter i...
\n",
"
203
\n",
"
35
\n",
"
5.638889
\n",
"
10
\n",
"
2
\n",
"
0
\n",
"
0.625000
\n",
"
0.825000
\n",
"
beauti blous bought thi daughter law birthday ...
\n",
"
\n",
"
\n",
"
13366
\n",
"
Boxy. large. Boxy, unflattering, and large.\\n\\...
\n",
"
295
\n",
"
51
\n",
"
5.673077
\n",
"
22
\n",
"
2
\n",
"
0
\n",
"
0.329613
\n",
"
0.510268
\n",
"
boxi larg boxi unflatt larg I curvi pound thi ...
\n",
"
\n",
" \n",
"
\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",
"
char_count
\n",
"
word_count
\n",
"
word_density
\n",
"
punctuation_count
\n",
"
title_word_count
\n",
"
upper_case_word_count
\n",
"
Polarity
\n",
"
Subjectivity
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
268
\n",
"
52
\n",
"
5.056604
\n",
"
8
\n",
"
2
\n",
"
0
\n",
"
0.170455
\n",
"
0.490909
\n",
"
\n",
"
\n",
"
1
\n",
"
399
\n",
"
84
\n",
"
4.694118
\n",
"
20
\n",
"
2
\n",
"
1
\n",
"
0.101944
\n",
"
0.719537
\n",
"
\n",
"
\n",
"
2
\n",
"
525
\n",
"
104
\n",
"
5.000000
\n",
"
19
\n",
"
2
\n",
"
2
\n",
"
0.186538
\n",
"
0.458761
\n",
"
\n",
"
\n",
"
3
\n",
"
203
\n",
"
35
\n",
"
5.638889
\n",
"
10
\n",
"
2
\n",
"
0
\n",
"
0.625000
\n",
"
0.825000
\n",
"
\n",
"
\n",
"
4
\n",
"
295
\n",
"
51
\n",
"
5.673077
\n",
"
22
\n",
"
2
\n",
"
0
\n",
"
0.329613
\n",
"
0.510268
\n",
"
\n",
" \n",
"
\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": [
"
"
],
"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."
]
}
]
}