Module 18 · Natural language processing · Lesson 61 of 66
Project: one star or five?
Classify Yelp reviews as 1-star or 5-star from their text alone.
In this lesson · about 25 minutes
- Explore text data with Seaborn
- Train and compare naive Bayes models with and without TF-IDF
Can a model tell a 1-star review from a 5-star one just by reading it? Each question loads yelp for you.
Exercise
1. Review length
Add a column 'text length': the number of characters in each review. End with yelp['text length'].describe().
Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
yelp = pd.read_csv('yelp.csv')Exercise
2. Length by star rating
Make a FacetGrid with one column per stars value and map plt.hist of 'text length' onto it.
Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
yelp = pd.read_csv('yelp.csv')
yelp['text length'] = yelp['text'].apply(len)3. More views
Exercise
4. Votes by star rating
Make stars: the mean of cool, useful, funny and text length for each star rating, using groupby. Then draw a heat map of stars.corr() with annot=True and the 'coolwarm' color map.
Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
yelp = pd.read_csv('yelp.csv')
yelp['text length'] = yelp['text'].apply(len)Exercise
5. Only 1 and 5 stars
Make yelp_class: only the 1-star and 5-star reviews. Set X to its text and y to its stars, then replace X with the bag-of-words matrix from CountVectorizer().fit_transform(X). End with X.shape.
Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
yelp = pd.read_csv('yelp.csv')Exercise
6. Train naive Bayes
The bag of words is split for you (test_size=0.3, random_state=101). Fit a MultinomialNB called nb, save its predictions as predictions, and print the confusion matrix and classification report.
Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
yelp = pd.read_csv('yelp.csv')
yelp_class = yelp[(yelp.stars == 1) | (yelp.stars == 5)]
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
X = CountVectorizer().fit_transform(yelp_class['text'])
y = yelp_class['stars']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101)Exercise
7. Add TF-IDF with a pipeline
Build a Pipeline called pipeline with three steps: ('bow', CountVectorizer()), ('tfidf', TfidfTransformer()) and ('classifier', MultinomialNB()). Split the raw text of yelp_class again (test_size=0.3, random_state=101), fit the pipeline, and save its predictions as predictions.
Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
yelp = pd.read_csv('yelp.csv')
yelp_class = yelp[(yelp.stars == 1) | (yelp.stars == 5)]
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_splitWhich did better?
Exercises solved: 0 of 6
Noviq Academy's Data Science course is free and for learning. Python runs in your browser with Pyodide, which downloads about 20 MB the first time, plus a few more for scikit-learn. Your code and progress stay on this device. The salaries data is a sample of San Francisco's public pay records; the purchases, customers, housing, advertising and classified data are made up. The Titanic, tips, flights and iris data sets come from Seaborn's examples; the maps use public 2011 to 2014 US and world figures. The 911 data is a 20,000-call sample of Montgomery County, Pennsylvania's public 911 records; the movie ratings are the MovieLens 100K data from GroupLens Research; the reviews are a 3,000-review sample from the Yelp Dataset Challenge; the loans are public LendingClub data; the colleges are the College data from An Introduction to Statistical Learning; the bank notes, kyphosis and breast cancer data come from the UCI Machine Learning Repository. Spark and TensorFlow can't run in a browser, so those lessons use stand-ins that run here, and show the code for the real tools.