Statistical Computing With Python
Statistical Computing With Python
with Python
Jason Anastasopoulos, Ph.D.
Upcoming Seminar:
October 22-24, 2020, Remote Seminar
Statistical Computing in
Python
Semi-Structured Data and Databases
HTML and Markup Languages
- Tree-structured (hierarchical) format
- Elements surrounded by opening & closing tags.
- Values embedded in open tags <tag-name attr-name=“attribute”> data
</tag-name>
Web Scraping Basics
- Use HTML and JSON data structures to build databases
- JSON is used to extract:
- Most data from APIs
- Data exchange systems like databases (SQL and MongoDB)
Getting data
Easiest: JSON from APIs
HTML - Very difficult, last resort if not available in APIs (rare now)
Other options:
- Write a bot.
- Pretend to be a browser (Selenium)
HTML
Hyper Text Markup Language
Uses tags
Example
<html>
<head>
<title>Page title here</title>
</head>
<body>
This is sample text...
<!--We use this syntax to write comments -->
<p>This is text within a paragraph.</p>
<em>I <strong>really</strong> mean that</em>
<img src="smileyface.jpg" alt="Smiley face" >
</body>
</html>
Webpage example
view-source:https://anastasopoulos.io/research
urllib package
Request package to retrieve file from ftp server
“Lightweight” format
- Data representations
- Easy for users to read
- Easy for parsers to translate
Main Structures
Object
- Uses {}, identical to a dictionary structure with key names and values separated
by comma.
Array
- List structure
- Uses []
- Contains values.
Value
- Lowest level.
- Values such as strings, numbers etc.
Simple JSON Sample
Accessing JSON Data
Data accessed via APIs formatted in JSON
Order is generally not taken into account, just word and term frequencies.
There are ways to parse documents into ngrams or words but we’ll stick with
words for now.
Tokenization
Tokenized tweet (1 gram): [“I”, “don’t”, “think”,
“you’re”, “the”....]
They tend to be words that are very common “the”, “and”, “is” etc.
These common words can cause problems for machine learning algorithms
and search engines because they add noise.
BEWARE Each package defines different lists of stop words and sometimes
removal can decrease performance of supervised mechine learning classifiers.
Sentiment Analysis
- Sentiment analysis is a type of supervised machine learning that is used to
predict the sentiment of texts.
- Without going into too much detail, we will use what is known as a pretrained
sentiment analysis algorithm.