using scikit-learn for sentiment analysis
View the Project on GitHub UCSF-DSOS/Document-Classification
This workshop provides an introduction Python and the scikit-learn module. For a working example, we will train a machine learning model to assess the sentiment (positive or negative) of a movie review.
This material is designed for programmers, data scientists, and researchers who would like to get started with machine learning for natural language processing and analysis.
Scikit-learn is a widely used Python based machine learning and natural language processing module. Scikit-learn provides methods for supervised and unsupervised learning, implementations of different machine learning algorithms, and tools for common data preparation tasks involved in machine learning programming projects.
This workshop will focus on programming with scikit-learn, rather than the theory behind ML algorithms. The material here is designed for a 2-3 hour workshop and consists of two workbooks. The first workbook introduces machine learning, document categorization, and sentiment analysis using a very small dataset. The second workbook applies these techniques to a larger dataset using a scikit-learn pipeline.
Through these two workbooks, participants will use scikit-learn to:
This tutorial is intended for participants who have basic programming experience with Python, including loops, conditionals, and lists. No previous experience with scikit-learn or machine learning is expected. Some familiarity with pandas will help, but we will cover these concepts in the context of populating a scikit-learn model.
Geoffrey Boushey is a computational and research data science specialist at UCSF and a member of the Library’s Data Science and Open Scholarship team. Geoff teaches courses and provides consultations on foundational programming, data analysis, and AI techniques including Python, SQL, Unix, and Version Control.
This workshop uses data from the Cornell Movie Review dataset:
http://www.cs.cornell.edu/people/pabo/movie-review-data/
This workshop will use the polarity dataset v1.1:
http://www.cs.cornell.edu/people/pabo/movie-review-data/mix20_rand700_tokens_0211.tar.gz
This repository contains a series of notebooks that introduce document preparation, vectorization, classification, clustering, model evaluation, and application of these techniques to larger text collections.
Introduces document classification using a very small set of movie reviews. The notebook creates a bag-of-words representation with CountVectorizer, trains a Random Forest classifier, examines feature importance, and uses the model to predict the sentiment of new reviews.
Demonstrates the differences between stemming and lemmatization using NLTK’s PorterStemmer and WordNetLemmatizer, including examples of how each approach handles different forms of the same word.
Walks through preparing raw text for document analysis. Starting with a sample document, the notebook demonstrates vectorization, removal of non-alphabetic characters, normalization to lowercase, stop-word removal, stemming, and lemmatization.
Expands on Sentiment-Example.ipynb by combining document vectorization, TF-IDF transformation, and Random Forest classification into a scikit-learn Pipeline. The notebook also examines feature importance, prediction probabilities, precision, recall, and accuracy.
Applies the classification pipeline to the larger movie-review dataset. The notebook splits the data into training and testing sets, builds a TF-IDF and Random Forest pipeline, examines important features, generates predictions and probabilities, and evaluates the resulting classifier using precision, recall, accuracy, F-score, and cross-validation.
Prepares the Cornell movie-review data used by the classification examples. The notebook reads positive and negative review files, assigns sentiment labels, removes non-alphabetic characters and stop words, lemmatizes the text, and creates a dataframe suitable for subsequent analysis.
Demonstrates unsupervised document clustering using TF-IDF vectors and scikit-learn’s KMeans. The notebook clusters a small set of movie-review examples and examines the terms associated with each cluster.
Applies document classification to a dataset of computer-generated transcripts categorized as advertising or legal content. The notebook builds a Random Forest text-classification pipeline, examines feature importance, generates class probabilities, adjusts classification thresholds, and evaluates the model using precision, recall, accuracy, F-score, and cross-validation.
Provides a short example of loading and inspecting a Washington Post opioid collection dataset, including its available fields and OCR text.
These workbooks barely scratch the surface of machine learning and scikit-learn. Fortunately, the documentation for scikit-learn is extensive, including a programming guide for using the library, discussion on general use, mathematical background, theory and algorithms, and examples. Once you’ve gotten familiar with the examples provided in this repository, one good way to get more familiar with machine learning is work through the scikit-learn documentation.
As an entry point to scikit-learn, you may want to read more about the libraries and methods used in this workshop.