Notebook
Challenge: Analyzing Text about Data Science
In this notebook, we experiment with using different URL - wikipedia article on Machine Learning. You can see that, unlike Data Science, this article contains a lot of terms, this making the analysis more problematic. We need to come up with another way to clean up the data after doing keyword extraction, to get rid of some frequent, but not meaningful word combinations.
In this example, let's do a simple exercise that covers all steps of a traditional data science process. You do not have to write any code, you can just click on the cells below to execute them and observe the result. As a challenge, you are encouraged to try this code out with different data.
Goal
In this lesson, we have been discussing different concepts related to Data Science. Let's try to discover more related concepts by doing some text mining. We will start with a text about Data Science, extract keywords from it, and then try to visualize the result.
As a text, I will use the page on Data Science from Wikipedia:
Step 1: Getting the Data
First step in every data science process is getting the data. We will use requests library to do that:
<!DOCTYPE html>
<html class="client-nojs" lang="en" dir="ltr">
<head>
<meta charset="UTF-8"/>
<title>Machine learning - Wikipedia</title>
<script>document.documentElement.className="client-js";RLCONF={"wgBreakFrames":!1,"wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"dmy","wgMonthNames":["","January","February","March","April","May","June","July","August","September","October","November","December"],"wgRequestId":"77162785-16e9-4d7f-a175-7f3fcf502a66","wgCSPNonce":!1,"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":!1,"wgNamespaceNumber":0,"wgPageName":"Machine_learning","wgTitle":"Machine learning","wgCurRevisionId":1041247229,"wgRevisionId":1041247229,"wgArticleId":233488,"wgIsArticle":!0,"wgIsRedirect":!1,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":["CS1 errors: missing periodical","Harv and Sfn no-target errors","CS1 maint: uses authors parameter","Articles with short description","Short description is dif
Step 2: Transforming the Data
The next step is to convert the data into the form suitable for processing. In our case, we have downloaded HTML source code from the page, and we need to convert it into plain text.
There are many ways this can be done. We will use the simplest build-in HTMLParser object from Python. We need to subclass the HTMLParser class and define the code that will collect all text inside HTML tags, except <script> and <style> tags.
Machine learning - Wikipedia Machine learning From Wikipedia, the free encyclopedia Jump to navigation Jump to search Study of algorithms that improve automatically through experience For the journal, see Machine Learning (journal) . "Statistical learning" redirects here. For statistical learning in linguistics, see statistical learning in language acquisition . Part of a series on Artificial intelligence Major goals Artificial general intelligence Planning Computer vision General game playing Knowledge reasoning Machine learning Natural language processing Robotics Approaches Symbolic Deep learning Bayesian networks Evolutionary algorithms Philosophy Ethics Existential risk Turing test Chinese room Control problem Friendly AI History Timeline Progress AI winter Technology Applications Projects Programming languages Glossary Glossary v t e Part of a series on Machine learning and data mining Problems Classification Clustering Regression Anomaly detection Data Cleaning AutoML Associ
Step 3: Getting Insights
The most important step is to turn our data into some for from which we can draw insights. In our case, we want to extract keywords from the text, and see which keywords are more meaningful.
We will use Python library called RAKE for keyword extraction. First, let's install this library in case it is not present:
Requirement already satisfied: nlp_rake in c:\winapp\miniconda3\lib\site-packages (0.0.2) Requirement already satisfied: langdetect>=1.0.8 in c:\winapp\miniconda3\lib\site-packages (from nlp_rake) (1.0.9) Requirement already satisfied: pyrsistent>=0.14.2 in c:\winapp\miniconda3\lib\site-packages (from nlp_rake) (0.17.3) Requirement already satisfied: numpy>=1.14.4 in c:\winapp\miniconda3\lib\site-packages (from nlp_rake) (1.19.5) Requirement already satisfied: regex>=2018.6.6 in c:\winapp\miniconda3\lib\site-packages (from nlp_rake) (2021.8.3)
C:\winapp\Miniconda3\lib\site-packages\secretstorage\dhcrypto.py:16: CryptographyDeprecationWarning: int_from_bytes is deprecated, use int.from_bytes instead from cryptography.utils import int_from_bytes C:\winapp\Miniconda3\lib\site-packages\secretstorage\util.py:25: CryptographyDeprecationWarning: int_from_bytes is deprecated, use int.from_bytes instead from cryptography.utils import int_from_bytes WARNING: Ignoring invalid distribution -umpy (c:\winapp\miniconda3\lib\site-packages) WARNING: Ignoring invalid distribution -umpy (c:\winapp\miniconda3\lib\site-packages) WARNING: Ignoring invalid distribution -umpy (c:\winapp\miniconda3\lib\site-packages) WARNING: Ignoring invalid distribution -umpy (c:\winapp\miniconda3\lib\site-packages) WARNING: Ignoring invalid distribution -umpy (c:\winapp\miniconda3\lib\site-packages) WARNING: Ignoring invalid distribution -umpy (c:\winapp\miniconda3\lib\site-packages)
Requirement already satisfied: six in c:\winapp\miniconda3\lib\site-packages (from langdetect>=1.0.8->nlp_rake) (1.16.0)
The main functionality is available from Rake object, which we can customize using some parameters. In our case, we will set the minimum length of a keyword to 5 characters, minimum frequency of a keyword in the document to 3, and maximum number of words in a keyword - to 2. Feel free to play around with other values and observe the result.
[('data mining', 4.0),
, ('polynomial time', 4.0),
, ('dimensionality reduction', 4.0),
, ('anomaly detection', 4.0),
, ('data set', 4.0),
, ('bayesian networks', 4.0),
, ('language corpora', 4.0),
, ('mcgraw hill', 4.0),
, ('mit press', 4.0),
, ('retrieved 2018-08-20', 4.0),
, ('artificial neuron', 3.9642857142857144),
, ('statistical learning', 3.9470198675496686),
, ('feature learning', 3.9470198675496686),
, ('reinforcement learning', 3.9470198675496686),
, ('deep learning', 3.9470198675496686),
, ('main article', 3.9411764705882355),
, ('machine learning', 3.9144111718974948),
, ('pattern recognition', 3.9),
, ('neural networks', 3.875),
, ('artificial intelligence', 3.864285714285714),
, ('supervised learning', 3.835908756438558),
, ('speech recognition', 3.833333333333333),
, ('bayesian network', 3.833333333333333),
, ('explicitly programmed', 3.8),
, ('biological brain', 3.8),
, ('unsupervised learning', 3.780353200883002),
, ('outlier detection', 3.75),
, ('ieee transactions', 3.75),
, ('isbn 978-0-262-01243-0', 3.7391304347826084),
, ('training data', 3.7222222222222223),
, ('training set', 3.7222222222222223),
, ('artificial neurons', 3.7142857142857144),
, ('make predictions', 3.666666666666667),
, ('international conference', 3.666666666666667),
, ('computer vision', 3.645833333333333),
, ('mathematical model', 3.642857142857143),
, ('genetic algorithms', 3.6233766233766236),
, ('modern approach', 3.5555555555555554),
, ('knowledge discovery', 3.5128205128205128),
, ('genetic algorithm', 3.5090909090909093),
, ('information theory', 3.4272727272727272),
, ('training examples', 3.3222222222222224),
, ('machine', 1.9673913043478262),
, ('learning', 1.9470198675496688),
, ('computer', 1.8125),
, ('research', 1.7692307692307692),
, ('regression', 1.75),
, ('theory', 1.7272727272727273),
, ('training', 1.7222222222222223),
, ('algorithms', 1.7142857142857142),
, ('related', 1.7),
, ('information', 1.7),
, ('representation', 1.6666666666666667),
, ('discovery', 1.6666666666666667),
, ('predictions', 1.6666666666666667),
, ('model', 1.6428571428571428),
, ('based', 1.625),
, ('methods', 1.6111111111111112),
, ('algorithm', 1.6),
, ('examples', 1.6),
, ('systems', 1.5625),
, ('approach', 1.5555555555555556),
, ('biases', 1.5454545454545454),
, ('classification', 1.5333333333333334),
, ('application', 1.5),
, ('tasks', 1.5),
, ('models', 1.5),
, ('environment', 1.5),
, ('compute', 1.5),
, ('input', 1.4545454545454546),
, ('racist', 1.4285714285714286),
, ('perform', 1.4166666666666667),
, ('journal', 1.4),
, ('decisions', 1.4),
, ('computers', 1.4),
, ('improve', 1.4),
, ('researchers', 1.4),
, ('program', 1.4),
, ('represented', 1.4),
, ('accuracy', 1.4),
, ('typically', 1.4),
, ('system', 1.375),
, ('optimization', 1.375),
, ('field', 1.3529411764705883),
, ('databases', 1.3333333333333333),
, ('terms', 1.3333333333333333),
, ('learned', 1.3333333333333333),
, ('outputs', 1.3333333333333333),
, ('called', 1.3333333333333333),
, ('observations', 1.3333333333333333),
, ('applied', 1.3333333333333333),
, ('target', 1.3333333333333333),
, ('original', 1.3333333333333333),
, ('study', 1.2857142857142858),
, ('trained', 1.2857142857142858),
, ('approaches', 1.25),
, ('complexity', 1.25),
, ('signal', 1.25),
, ('inputs', 1.25),
, ('output', 1.25),
, ('process', 1.25),
, ('people', 1.25),
, ('peter', 1.25),
, ('christopher', 1.25),
, ('problem', 1.2),
, ('performance', 1.2),
, ('difference', 1.2),
, ('michael', 1.2),
, ('predict', 1.2),
, ('norvig', 1.2),
, ('learn', 1.1666666666666667),
, ('class', 1.1666666666666667),
, ('applications', 1.125),
, ('found', 1.125),
, ('features', 1.1111111111111112),
, ('introduction', 1.0909090909090908),
, ('statistics', 1.0714285714285714),
, ('experience', 1.0),
, ('series', 1.0),
, ('order', 1.0),
, ('medicine', 1.0),
, ('respect', 1.0),
, ('question', 1.0),
, ('subfield', 1.0),
, ('leading', 1.0),
, ('actions', 1.0),
, ('maximize', 1.0),
, ('evaluated', 1.0),
, ('instances', 1.0),
, ('generalization', 1.0),
, ('context', 1.0),
, ('hypothesis', 1.0),
, ('addition', 1.0),
, ('types', 1.0),
, ('members', 1.0),
, ('bioinformatics', 1.0),
, ('connection', 1.0),
, ('layers', 1.0),
, ('ethics', 1.0),
, ('david', 1.0),
, ('martin', 1.0),
, ('springer', 1.0),
, ('citeseerx 10', 1.0),
, ('github', 1.0),
, ('andrew', 1.0),
, ('cybernetics', 1.0),
, ('proceedings', 1.0),
, ('arxiv', 1.0),
, ('archived', 1.0),
, ('survey', 1.0),
, ('bibcode', 1.0),
, ('cambridge', 1.0)] We obtained a list terms together with associated degree of importance. As you can see, the most relevant disciplines, such as machine learning and big data, are present in the list at top positions.
Step 4: Visualizing the Result
People can interpret the data best in the visual form. Thus it often makes sense to visualize the data in order to draw some insights. We can use matplotlib library in Python to plot simple distribution of the keywords with their relevance:
There is, however, even better way to visualize word frequencies - using Word Cloud. We will need to install another library to plot the word cloud from our keyword list.
WordCloud object is responsible for taking in either original text, or pre-computed list of words with their frequencies, and returns and image, which can then be displayed using matplotlib:
We can also pass in the original text to WordCloud - let's see if we are able to get similar result:
<wordcloud.wordcloud.WordCloud at 0x224b99d76a0>
You can see that word cloud now looks more impressive, but it also contains a lot of noise (eg. unrelated words such as Retrieved on). Also, we get fewer keywords that consist of two words, such as data scientist, or computer science. This is because RAKE algorithm does much better job at selecting good keywords from text. This example illustrates the importance of data pre-processing and cleaning, because clear picture at the end will allow us to make better decisions.
In this exercise we have gone through a simple process of extracting some meaning from Wikipedia text, in the form of keywords and word cloud. This example is quite simple, but it demonstrates well all typical steps a data scientist will take when working with data, starting from data acquisition, up to visualization.
In our course we will discuss all those steps in detail.