Classifying fake
news using
supervised learning
with NLP
I N T R O D U C T I O N T O N AT U R A L L A N G U A G E P R O C E S S I N G I N P Y T H O N
Katharine Jarmul
Founder, kjamistan
What is supervised learning?
Form of machine learning
Problem has prede ned training data
This data has a label (or outcome) you want the model to
learn
Classi cation problem
Goal: Make good hypotheses about the species based on
geometric features
Sepal Sepal Petal Petal
Species
length width length width
5.1 3.5 1.4 0.2 I. setosa
7.0 3.2 4.77 1.4 [Link]
6.3 3.3 6.0 2.5 [Link]
INTRODUCTION TO NATURAL LANGUAGE PROCESSING IN PYTHON
Supervised learning with NLP
Need to use language instead of geometric features
scikit-learn : Powerful open-source library
How to create supervised learning data from text?
Use bag-of-words models or tf-idf as features
INTRODUCTION TO NATURAL LANGUAGE PROCESSING IN PYTHON
IMDB Movie Dataset
Sci-
Plot Action
Fi
In a post-apocalyptic world in human decay, a
1 0
...
Mohei is a wandering swordsman. He arrives
0 1
in ...
#137 is a SCI/FI thriller about a girl, Marla,... 1 0
Goal: Predict movie genre based on plot summary
Categorical features generated using preprocessing
INTRODUCTION TO NATURAL LANGUAGE PROCESSING IN PYTHON
Supervised learning steps
Collect and preprocess our data
Determine a label (Example: Movie genre)
Split data into training and test sets
Extract features from the text to help predict the label
Bag-of-words vector built into scikit-learn
Evaluate trained model using the test set
INTRODUCTION TO NATURAL LANGUAGE PROCESSING IN PYTHON
Let's practice!
I N T R O D U C T I O N T O N AT U R A L L A N G U A G E P R O C E S S I N G I N P Y T H O N
Building word count
vectors with scikit-
learn
I N T R O D U C T I O N T O N AT U R A L L A N G U A G E P R O C E S S I N G I N P Y T H O N
Katharine Jarmul
Founder, kjamistan
Predicting movie genre
Dataset consisting of movie plots and corresponding genre
Goal: Create bag-of-word vectors for the movie plots
Can we predict genre based on the words used in the plot
summary?
INTRODUCTION TO NATURAL LANGUAGE PROCESSING IN PYTHON
Count Vectorizer with Python
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
df = ... # Load data into DataFrame
y = df['Sci-Fi']
X_train, X_test, y_train, y_test = train_test_split(
df['plot'], y,
test_size=0.33,
random_state=53)
count_vectorizer = CountVectorizer(stop_words='english')
count_train = count_vectorizer.fit_transform(X_train.values)
count_test = count_vectorizer.transform(X_test.values)
INTRODUCTION TO NATURAL LANGUAGE PROCESSING IN PYTHON
Let's practice!
I N T R O D U C T I O N T O N AT U R A L L A N G U A G E P R O C E S S I N G I N P Y T H O N
Training and testing
a classification
model with scikit-
learn
I N T R O D U C T I O N T O N AT U R A L L A N G U A G E P R O C E S S I N G I N P Y T H O N
Katharine Jarmul
Founder, kjamistan
Naive Bayes classifier
Naive Bayes Model
Commonly used for testing NLP classi cation problems
Basis in probability
Given a particular piece of data, how likely is a particular
outcome?
Examples:
If the plot has a spaceship, how likely is it to be sci- ?
Given a spaceship and an alien, how likely now is it sci- ?
Each word from CountVectorizer acts as a feature
Naive Bayes: Simple and e ective
INTRODUCTION TO NATURAL LANGUAGE PROCESSING IN PYTHON
Naive Bayes with scikit-learn
from sklearn.naive_bayes import MultinomialNB
from sklearn import metrics
nb_classifier = MultinomialNB()
nb_classifier.fit(count_train, y_train)
pred = nb_classifier.predict(count_test)
metrics.accuracy_score(y_test, pred)
0.85841849389820424
INTRODUCTION TO NATURAL LANGUAGE PROCESSING IN PYTHON
Confusion matrix
metrics.confusion_matrix(y_test, pred, labels=[0,1])
array([[6410, 563],
[ 864, 2242]])
Action Sci-Fi
Action 6410 563
Sci-Fi 864 2242
INTRODUCTION TO NATURAL LANGUAGE PROCESSING IN PYTHON
Let's practice!
I N T R O D U C T I O N T O N AT U R A L L A N G U A G E P R O C E S S I N G I N P Y T H O N
Simple NLP, complex
problems
I N T R O D U C T I O N T O N AT U R A L L A N G U A G E P R O C E S S I N G I N P Y T H O N
Katharine Jarmul
Founder, kjamistan
Translation
source:
(h ps://twi [Link]/Lupintweets/status/865533182455685121)
INTRODUCTION TO NATURAL LANGUAGE PROCESSING IN PYTHON
Sentiment analysis
(source: h ps://[Link]/projects/socialsent/)
INTRODUCTION TO NATURAL LANGUAGE PROCESSING IN PYTHON
Language biases
(related talk: h ps://[Link]/watch?
v=j7FwpZB1hWc)
INTRODUCTION TO NATURAL LANGUAGE PROCESSING IN PYTHON
Let's practice!
I N T R O D U C T I O N T O N AT U R A L L A N G U A G E P R O C E S S I N G I N P Y T H O N