Run in Google Colab
|
View on GitHub
|
Homework 10 - Sentiment Analysis with TF-IDF Vectorization¶
In this assignment, we will apply NLP concepts from lecture and use TF-IDF Vectorization. We will need to use the sentiment dataset linked to from the canvas assignment page. Make sure to have this downloaded for using this guide. As always, we'll first need a few libraries for this assignment.
Complete the missing parts in this guide.
Step 1: Load Data¶
You can load the data from the provided TSV file using pandas.
Step 2: Preprocess¶
- Clean the data by removing stop-words, punctuations, emoticons etc..
Step 3: Train and test a model to predict the sentiment of each sentence¶
- Train and test the model using TfidfVectorizer, Pipeline, Logistic regression with this data.
- Print the best_params_, best_score_, score.
Step 4: Repeat for all the datasets¶
- 'amazon_cells_labelled.tsv'
- 'yelp_labelled.tsv'
- 'imdb_labelled.tsv'
Dataset Overview¶
The dataset obtained originally from https://archive.ics.uci.edu/dataset/331/sentiment+labelled+sentences contains sentences labeled with sentiment. Each sentence is associated with a sentiment label (positive or negative). The dataset is split into three parts, each containing sentences from different sources: Amazon, Yelp, and IMDb. Score is either 1 (for positive) or 0 (for negative)
Submission Guidelines¶
- Submit your completed notebook as a HTML export, or a PDF file.
To export to HTML, if you are on Jupyter, select File > Export Notebook As > HTML.
If you are on VSCode, you can use the Jupyter: Export to HTML command.
- Open the command palette (Ctrl+Shift+P or Cmd+Shift+P on Mac).
- Search for
Jupyter: Export to HTML. - Save the HTML file to your computer and submit it via Canvas.
- Search for
import pandas as pd
import numpy as np
import string
import matplotlib.pyplot as plt
from sklearn.feature_extraction.text import TfidfVectorizer
from nltk.corpus import stopwords
import nltk
import re
%matplotlib inline
The datasets have two columns: sentence and score. The sentence column contains the text of the sentence, and the score column contains the sentiment label (1 for positive, 0 for negative).
df = pd.read_csv("../../Datasets/yelp_labelled.tsv", sep="\t")
df.head()
| sentence | score | |
|---|---|---|
| 0 | Wow... Loved this place. | 1 |
| 1 | Crust is not good. | 0 |
| 2 | Not tasty and the texture was just nasty. | 0 |
| 3 | Stopped by during the late May bank holiday of... | 1 |
| 4 | The selection on the menu was great and so wer... | 1 |
Great! Now we need to clean up the dataframe by removing non words like stop-test, and punctuation. Fill in the code for the remove_punctuation() and remove_stopwords() functions as described in lecture.
Note: In addition to the 'remove_punctuation' and 'remove_stopwords', you can also try to check for lower case and upper case and convert to lower case accordingly. You can also tokenize the text, stemming the tokens and then join the stemmed tokens back into a string.
nltk.download('stopwords')
stop = stopwords.words('english')
def remove_punctuation(text):
# Your Code Here
def remove_stopwords(text):
# Your Code Here
df['sentence'] = df['sentence'].apply(remove_punctuation).apply(remove_stopwords)
df.head()
Split the cleaned dataset using train test split
# Your code here
Define a pipeline with the asked models(tfifd and Logistic Regression) in our case
Next we can call the TfidfVectorizer() function, passing it 'english' as a parameter.
pipe = Pipeline((), () ) # Your code here
Next, you can define a parameter grid for finding the best hyperparameters, then use GridsearchCV and pass the pipeline to find the best parameters and then fit the model using the best hyperparameters
param_grid = { } # Your code here
# Your code for GridsearchCV
# Your code to fit the model
Write a code to print the best_params_, best_score_, score.
# Your code here
Run in Google Colab
View on GitHub