SlideShare a Scribd company logo
1 of 36
Download to read offline
Statistical Learning for Text Classification
         with scikit-learn and NLTK

                Olivier Grisel
         http://twitter.com/ogrisel

              PyCon – 2011
Outline
●   Why text classification?
●   What is text classification?
●   How?
    ●   scikit-learn
    ●   NLTK
    ●   Google Prediction API
●   Some results
Applications of Text Classification
             Task                     Predicted outcome

Spam filtering                  Spam, Ham, Priority

Language guessing               English, Spanish, French, ...

Sentiment Analysis for Product
                               Positive, Neutral, Negative
Reviews
News Feed Topic                 Politics, Business, Technology,
Categorization                  Sports, ...
Pay-per-click optimal ads
                                Will yield clicks / Won't
placement
Recommender systems             Will I buy this book? / I won't
Supervised Learning Overview
●   Convert training data to a set of vectors of features
●   Build a model based on the statistical properties of
    features in the training set, e.g.
    ●   Naïve Bayesian Classifier
    ●   Logistic Regression
    ●   Support Vector Machines
●   For each new text document to classify
    ●   Extract features
    ●   Asked model to predict the most likely outcome
Summary
    Training          features
      Text            vectors
   Documents,
    Images,
    Sounds...
                                 Machine
                                 Learning
                                 Algorithm
     Labels




  New
  Text          features
Document,       vector           Predictive   Expected
 Image,                            Model       Label
 Sound
Bags of Words
●   Tokenize document: list of uni-grams
      ['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
●   Binary occurrences / counts:
      {'the': True, 'quick': True...}
●   Frequencies:
       {'the': 0.22, 'quick': 0.11, 'brown': 0.11, 'fox': 0.11…}
●   TF-IDF
      {'the': 0.001, 'quick': 0.05, 'brown': 0.06, 'fox': 0.24…}
Better than frequencies: TF-IDF
●   Term Frequency



●   Inverse Document Frequency




●   Non informative words such as “the” are scaled done
Even better features
●   bi-grams of words:
    ●   “New York”, “very bad”, “not good”
●   n-grams of chars:
    ●   “the”, “ed ”, “ a ” (useful for language guessing)
●   Combine with:
    ●   Binary occurrences
    ●   Frequencies
    ●   TF-IDF
scikit-learn
scikit-learn
●   BSD
●   numpy / scipy / cython / c++ wrappers
●   Many state of the art implementations
●   A new release every 3 months
●   17 contributors on release 0.7
●   Not just for text classification
Features Extraction in scikit-learn
from scikits.learn.features.text import WordNGramAnalyzer
text = (u"J'ai mangxe9 du kangourou ce midi,"
       u" c'xe9tait pas trxeas bon.")


WordNGramAnalyzer(min_n=1, max_n=2).analyze(text)
[u'ai', u'mange', u'du', u'kangourou', u'ce', u'midi', u'etait',
u'pas', u'tres', u'bon', u'ai mange', u'mange du', u'du
kangourou', u'kangourou ce', u'ce midi', u'midi etait', u'etait
pas', u'pas tres', u'tres bon']
Features Extraction in scikit-learn
from scikits.learn.features.text import CharNGramAnalyzer


analyzer = CharNGramAnalyzer(min_n=3, max_n=6)
char_ngrams = analyzer.analyze(text)


print char_ngrams[:5] + char_ngrams[-5:]
[u"j'a", u"'ai", u'ai ', u'i m', u' ma', u's tres', u' tres ', u'tres b',
u'res bo', u'es bon']
TF-IDF features & SVMs
from scikits.learn.features.text.sparse import Vectorizer
from scikits.learn.sparse.svm.sparse import LinearSVC



vec = Vectorizer(analyzer=analyzer)

features = vec.fit_transform(list_of_documents)

clf = LinearSVC(C=100).fit(features, labels)



clf2 = pickle.loads(pickle.dumps(clf))

predicted_labels = clf2.predict(features_of_new_docs)
)
                                    cs
                                (do
                        form
                      ns
                  .tra
    Training                 features
      Text       c
   Documents, ve             vectors
     Images,
     Sounds...
                                             )   Machine
                                          X,y
                                     fit(
                                                 Learning
                                   .
                               clf               Algorithm
      Labels

                                       w)
                                 _   ne                                                w)
                               cs                                                  _n
                                                                                      e
                           (do                                                   X
                         m                                                   ct(
                     sfor                                                ed
                                                                           i
  New             an                                              .   pr
  Text       c.t
                 r                                            clf
           e              features
Document, v               vector                 Predictive                     Expected
 Image,                                            Model                         Label
 Sound
NLTK
●   Code: ASL 2.0 & Book: CC-BY-NC-ND
●   Tokenizers, Stemmers, Parsers, Classifiers,
    Clusterers, Corpus Readers
NLTK Corpus Downloader
>>> import nltk
>>> nltk.download()
Using a NLTK corpus
>>> from nltk.corpus import movie_reviews as reviews


>>> pos_ids = reviews.fileids('pos')
>>> neg_ids = reviews.fileids('neg')
>>> len(pos_ids), len(neg_ids)
1000, 1000


>>> reviews.words(pos_ids[0])
['films', 'adapted', 'from', 'comic', 'books', 'have', ...]
Common data cleanup operations
●   Lower case & remove accentuated chars:
import unicodedata
s = ''.join(c for c in unicodedata.normalize('NFD', s.lower())
             if unicodedata.category(c) != 'Mn')
●   Extract only word tokens of at least 2 chars
    ●   Using NLTK tokenizers & stemmers
    ●   Using a simple regexp:
        re.compile(r"bww+b", re.U).findall(s)
Feature Extraction with NLTK
                   Unigram features




def word_features(words):
   return dict((word, True) for word in words)
Feature Extraction with NLTK
                     Bigram Collocations
from nltk.collocations import BigramCollocationFinder
from nltk.metrics import BigramAssocMeasures as BAM
from itertools import chain


def bigram_features(words, score_fn=BAM.chi_sq):
   bg_finder = BigramCollocationFinder.from_words(words)
   bigrams = bg_finder.nbest(score_fn, 100000)
   return dict((bg, True) for bg in chain(words, bigrams))
The NLTK Naïve Bayes Classifier
from nltk.classify import NaiveBayesClassifier


neg_examples = [(features(reviews.words(i)), 'neg') for i in neg_ids]
pos_examples = [(features(reviews.words(i)), 'pos') for i in pos_ids]
train_set = pos_examples + neg_examples


classifier = NaiveBayesClassifier.train(train_set)
Most informative features
>>> classifier.show_most_informative_features()
     magnificent = True         pos : neg          =     15.0 : 1.0
     outstanding = True         pos : neg          =      13.6 : 1.0
      insulting = True        neg : pos     =          13.0 : 1.0
      vulnerable = True        pos : neg           =     12.3 : 1.0
      ludicrous = True         neg : pos       =        11.8 : 1.0
        avoids = True         pos : neg        =       11.7 : 1.0
     uninvolving = True         neg : pos          =     11.7 : 1.0
      astounding = True         pos : neg          =      10.3 : 1.0
     fascination = True        pos : neg       =         10.3 : 1.0
       idiotic = True        neg : pos     =           9.8 : 1.0
Training NLTK classifiers
●   Try nltk-trainer

●   python train_classifier.py --instances paras 
    --classifier NaiveBayes –bigrams 
    --min_score 3      movie_reviews
REST services
NLTK – Online demos
NLTK – REST APIs
% curl -d "text=Inception is the best movie ever" 
            http://text-processing.com/api/sentiment/


{
    "probability": {
         "neg": 0.36647424288117808,
         "pos": 0.63352575711882186
    },
    "label": "pos"
}
Google Prediction API
Typical performance results:
                   movie reviews
●   nltk:
    ● unigram occurrences
    ● Naïve Bayesian Classifier          ~ 70%
●   Google Prediction API                ~ 83%
●   scikit-learn:
    ●  TF-IDF unigram features
    ● LinearSVC                          ~ 87%
●   nltk:
    ●   Collocation features selection
    ●   Naïve Bayesian Classifier        ~ 97%
Typical results:
    newsgroups topics classification

●   20 newsgroups dataset
    ●   ~ 19K short text documents
    ●   20 categories
    ●   By date train / test split

●   Bigram TF-IDF + LinearSVC:       ~ 87%
Confusion Matrix (20 newsgroups)
00 alt.atheism
01 comp.graphics
02 comp.os.ms-windows.misc
03 comp.sys.ibm.pc.hardware
04 comp.sys.mac.hardware
05 comp.windows.x
06 misc.forsale
07 rec.autos
08 rec.motorcycles
09 rec.sport.baseball
10 rec.sport.hockey
11 sci.crypt
12 sci.electronics
13 sci.med
14 sci.space
15 soc.religion.christian
16 talk.politics.guns
17 talk.politics.mideast
18 talk.politics.misc
19 talk.religion.misc
Typical results:
           Language Identification


●   15 Wikipedia articles
●   [p.text_content() for p in html_tree.findall('//p')]
●   CharNGramAnalyzer(min_n=1, max_n=3)
●   TF-IDF
●   LinearSVC
Typical results:
Language Identification
Scaling to many possible outcomes
●   Example: possible outcomes are all the
    categories of Wikipedia (565,108)
●   From Document Categorization
                           to Information Retrieval
●   Fulltext index for TF-IDF similarity queries
●   Smart way to find the top 30 search keywords
●   Use Apache Lucene / Solr MoreLikeThisQuery
Some pointers
●   http://scikit-learn.sf.net               doc & examples
    http://github.com/scikit-learn                     code
●   http://www.nltk.org              code & doc & PDF book
●   http://streamhacker.com/
    ●   Jacob Perkins' blog on NLTK & APIs
●   https://github.com/japerk/nltk-trainer
●   http://www.slideshare.net/ogrisel             these slides
●   http://twitter.com/ogrisel / http://github.com/ogrisel

                             Questions?

More Related Content

What's hot

NAIVE BAYES CLASSIFIER
NAIVE BAYES CLASSIFIERNAIVE BAYES CLASSIFIER
NAIVE BAYES CLASSIFIERKnoldus Inc.
 
literature review slides.pptx
literature review slides.pptxliterature review slides.pptx
literature review slides.pptxBELLO ALHASSAN
 
Ensemble methods in machine learning
Ensemble methods in machine learningEnsemble methods in machine learning
Ensemble methods in machine learningSANTHOSH RAJA M G
 
Python NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | EdurekaPython NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | EdurekaEdureka!
 
Overview of Data Cleaning.pdf
Overview of Data Cleaning.pdfOverview of Data Cleaning.pdf
Overview of Data Cleaning.pdfSheetalDandge
 
Scikit Learn intro
Scikit Learn introScikit Learn intro
Scikit Learn intro9xdot
 
Decision Tree - C4.5&CART
Decision Tree - C4.5&CARTDecision Tree - C4.5&CART
Decision Tree - C4.5&CARTXueping Peng
 
Material Algoritmos e Estruturas de Dados - 1º Bimestre
Material Algoritmos e Estruturas de Dados - 1º BimestreMaterial Algoritmos e Estruturas de Dados - 1º Bimestre
Material Algoritmos e Estruturas de Dados - 1º BimestreElaine Cecília Gatto
 
String Matching (Naive,Rabin-Karp,KMP)
String Matching (Naive,Rabin-Karp,KMP)String Matching (Naive,Rabin-Karp,KMP)
String Matching (Naive,Rabin-Karp,KMP)Aditya pratap Singh
 
Machine Learning 3 - Decision Tree Learning
Machine Learning 3 - Decision Tree LearningMachine Learning 3 - Decision Tree Learning
Machine Learning 3 - Decision Tree Learningbutest
 
Handling Imbalanced Data: SMOTE vs. Random Undersampling
Handling Imbalanced Data: SMOTE vs. Random UndersamplingHandling Imbalanced Data: SMOTE vs. Random Undersampling
Handling Imbalanced Data: SMOTE vs. Random UndersamplingIRJET Journal
 
Understanding your Data - Data Analytics Lifecycle and Machine Learning
Understanding your Data - Data Analytics Lifecycle and Machine LearningUnderstanding your Data - Data Analytics Lifecycle and Machine Learning
Understanding your Data - Data Analytics Lifecycle and Machine LearningAbzetdin Adamov
 

What's hot (20)

NAIVE BAYES CLASSIFIER
NAIVE BAYES CLASSIFIERNAIVE BAYES CLASSIFIER
NAIVE BAYES CLASSIFIER
 
literature review slides.pptx
literature review slides.pptxliterature review slides.pptx
literature review slides.pptx
 
Ensemble methods in machine learning
Ensemble methods in machine learningEnsemble methods in machine learning
Ensemble methods in machine learning
 
Die Weisheit der Dakota neu
Die Weisheit der Dakota neuDie Weisheit der Dakota neu
Die Weisheit der Dakota neu
 
Linguagem C - Funções
Linguagem C - FunçõesLinguagem C - Funções
Linguagem C - Funções
 
Python NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | EdurekaPython NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | Edureka
 
Overview of Data Cleaning.pdf
Overview of Data Cleaning.pdfOverview of Data Cleaning.pdf
Overview of Data Cleaning.pdf
 
Python Scipy Numpy
Python Scipy NumpyPython Scipy Numpy
Python Scipy Numpy
 
Scikit Learn intro
Scikit Learn introScikit Learn intro
Scikit Learn intro
 
Numpy Talk at SIAM
Numpy Talk at SIAMNumpy Talk at SIAM
Numpy Talk at SIAM
 
Intro to Jupyter Notebooks
Intro to Jupyter NotebooksIntro to Jupyter Notebooks
Intro to Jupyter Notebooks
 
Decision Tree - C4.5&CART
Decision Tree - C4.5&CARTDecision Tree - C4.5&CART
Decision Tree - C4.5&CART
 
Material Algoritmos e Estruturas de Dados - 1º Bimestre
Material Algoritmos e Estruturas de Dados - 1º BimestreMaterial Algoritmos e Estruturas de Dados - 1º Bimestre
Material Algoritmos e Estruturas de Dados - 1º Bimestre
 
Learning With Complete Data
Learning With Complete DataLearning With Complete Data
Learning With Complete Data
 
String Matching (Naive,Rabin-Karp,KMP)
String Matching (Naive,Rabin-Karp,KMP)String Matching (Naive,Rabin-Karp,KMP)
String Matching (Naive,Rabin-Karp,KMP)
 
Machine Learning 3 - Decision Tree Learning
Machine Learning 3 - Decision Tree LearningMachine Learning 3 - Decision Tree Learning
Machine Learning 3 - Decision Tree Learning
 
Handling Imbalanced Data: SMOTE vs. Random Undersampling
Handling Imbalanced Data: SMOTE vs. Random UndersamplingHandling Imbalanced Data: SMOTE vs. Random Undersampling
Handling Imbalanced Data: SMOTE vs. Random Undersampling
 
Python
PythonPython
Python
 
Understanding your Data - Data Analytics Lifecycle and Machine Learning
Understanding your Data - Data Analytics Lifecycle and Machine LearningUnderstanding your Data - Data Analytics Lifecycle and Machine Learning
Understanding your Data - Data Analytics Lifecycle and Machine Learning
 
Introdução a python
Introdução a pythonIntrodução a python
Introdução a python
 

Similar to Statistical Machine Learning for Text Classification with scikit-learn and NLTK

Statistical Learning and Text Classification with NLTK and scikit-learn
Statistical Learning and Text Classification with NLTK and scikit-learnStatistical Learning and Text Classification with NLTK and scikit-learn
Statistical Learning and Text Classification with NLTK and scikit-learnOlivier Grisel
 
Simple APIs and innovative documentation
Simple APIs and innovative documentationSimple APIs and innovative documentation
Simple APIs and innovative documentationPyDataParis
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimizationg3_nittala
 
Authorship attribution pydata london
Authorship attribution   pydata londonAuthorship attribution   pydata london
Authorship attribution pydata londonkperi
 
Statistical inference for (Python) Data Analysis. An introduction.
Statistical inference for (Python) Data Analysis. An introduction.Statistical inference for (Python) Data Analysis. An introduction.
Statistical inference for (Python) Data Analysis. An introduction.Piotr Milanowski
 
Authorship Attribution and Forensic Linguistics with Python/Scikit-Learn/Pand...
Authorship Attribution and Forensic Linguistics with Python/Scikit-Learn/Pand...Authorship Attribution and Forensic Linguistics with Python/Scikit-Learn/Pand...
Authorship Attribution and Forensic Linguistics with Python/Scikit-Learn/Pand...PyData
 
TransmogrifAI - Automate Machine Learning Workflow with the power of Scala an...
TransmogrifAI - Automate Machine Learning Workflow with the power of Scala an...TransmogrifAI - Automate Machine Learning Workflow with the power of Scala an...
TransmogrifAI - Automate Machine Learning Workflow with the power of Scala an...Chetan Khatri
 
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...Positive Hack Days
 
Learning with classification and clustering, neural networks
Learning with classification and clustering, neural networksLearning with classification and clustering, neural networks
Learning with classification and clustering, neural networksShaun D'Souza
 
Software tookits for machine learning and graphical models
Software tookits for machine learning and graphical modelsSoftware tookits for machine learning and graphical models
Software tookits for machine learning and graphical modelsbutest
 
Data Summer Conf 2018, “How we build Computer vision as a service (ENG)” — Ro...
Data Summer Conf 2018, “How we build Computer vision as a service (ENG)” — Ro...Data Summer Conf 2018, “How we build Computer vision as a service (ENG)” — Ro...
Data Summer Conf 2018, “How we build Computer vision as a service (ENG)” — Ro...Provectus
 
Automate ml workflow_transmogrif_ai-_chetan_khatri_berlin-scala
Automate ml workflow_transmogrif_ai-_chetan_khatri_berlin-scalaAutomate ml workflow_transmogrif_ai-_chetan_khatri_berlin-scala
Automate ml workflow_transmogrif_ai-_chetan_khatri_berlin-scalaChetan Khatri
 
TensorFlow in Your Browser
TensorFlow in Your BrowserTensorFlow in Your Browser
TensorFlow in Your BrowserOswald Campesato
 
Machine Learning on Code - SF meetup
Machine Learning on Code - SF meetupMachine Learning on Code - SF meetup
Machine Learning on Code - SF meetupsource{d}
 
200612_BioPackathon_ss
200612_BioPackathon_ss200612_BioPackathon_ss
200612_BioPackathon_ssSatoshi Kume
 
Tesseract. Recognizing Errors in Recognition Software
Tesseract. Recognizing Errors in Recognition SoftwareTesseract. Recognizing Errors in Recognition Software
Tesseract. Recognizing Errors in Recognition SoftwareAndrey Karpov
 
Standardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for PythonStandardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for PythonRalf Gommers
 
Python + Tensorflow: how to earn money in the Stock Exchange with Deep Learni...
Python + Tensorflow: how to earn money in the Stock Exchange with Deep Learni...Python + Tensorflow: how to earn money in the Stock Exchange with Deep Learni...
Python + Tensorflow: how to earn money in the Stock Exchange with Deep Learni...ETS Asset Management Factory
 
R Programming - part 1.pdf
R Programming - part 1.pdfR Programming - part 1.pdf
R Programming - part 1.pdfRohanBorgalli
 

Similar to Statistical Machine Learning for Text Classification with scikit-learn and NLTK (20)

Statistical Learning and Text Classification with NLTK and scikit-learn
Statistical Learning and Text Classification with NLTK and scikit-learnStatistical Learning and Text Classification with NLTK and scikit-learn
Statistical Learning and Text Classification with NLTK and scikit-learn
 
Simple APIs and innovative documentation
Simple APIs and innovative documentationSimple APIs and innovative documentation
Simple APIs and innovative documentation
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimization
 
Authorship attribution pydata london
Authorship attribution   pydata londonAuthorship attribution   pydata london
Authorship attribution pydata london
 
Statistical inference for (Python) Data Analysis. An introduction.
Statistical inference for (Python) Data Analysis. An introduction.Statistical inference for (Python) Data Analysis. An introduction.
Statistical inference for (Python) Data Analysis. An introduction.
 
Authorship Attribution and Forensic Linguistics with Python/Scikit-Learn/Pand...
Authorship Attribution and Forensic Linguistics with Python/Scikit-Learn/Pand...Authorship Attribution and Forensic Linguistics with Python/Scikit-Learn/Pand...
Authorship Attribution and Forensic Linguistics with Python/Scikit-Learn/Pand...
 
Text analysis using python
Text analysis using pythonText analysis using python
Text analysis using python
 
TransmogrifAI - Automate Machine Learning Workflow with the power of Scala an...
TransmogrifAI - Automate Machine Learning Workflow with the power of Scala an...TransmogrifAI - Automate Machine Learning Workflow with the power of Scala an...
TransmogrifAI - Automate Machine Learning Workflow with the power of Scala an...
 
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
 
Learning with classification and clustering, neural networks
Learning with classification and clustering, neural networksLearning with classification and clustering, neural networks
Learning with classification and clustering, neural networks
 
Software tookits for machine learning and graphical models
Software tookits for machine learning and graphical modelsSoftware tookits for machine learning and graphical models
Software tookits for machine learning and graphical models
 
Data Summer Conf 2018, “How we build Computer vision as a service (ENG)” — Ro...
Data Summer Conf 2018, “How we build Computer vision as a service (ENG)” — Ro...Data Summer Conf 2018, “How we build Computer vision as a service (ENG)” — Ro...
Data Summer Conf 2018, “How we build Computer vision as a service (ENG)” — Ro...
 
Automate ml workflow_transmogrif_ai-_chetan_khatri_berlin-scala
Automate ml workflow_transmogrif_ai-_chetan_khatri_berlin-scalaAutomate ml workflow_transmogrif_ai-_chetan_khatri_berlin-scala
Automate ml workflow_transmogrif_ai-_chetan_khatri_berlin-scala
 
TensorFlow in Your Browser
TensorFlow in Your BrowserTensorFlow in Your Browser
TensorFlow in Your Browser
 
Machine Learning on Code - SF meetup
Machine Learning on Code - SF meetupMachine Learning on Code - SF meetup
Machine Learning on Code - SF meetup
 
200612_BioPackathon_ss
200612_BioPackathon_ss200612_BioPackathon_ss
200612_BioPackathon_ss
 
Tesseract. Recognizing Errors in Recognition Software
Tesseract. Recognizing Errors in Recognition SoftwareTesseract. Recognizing Errors in Recognition Software
Tesseract. Recognizing Errors in Recognition Software
 
Standardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for PythonStandardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for Python
 
Python + Tensorflow: how to earn money in the Stock Exchange with Deep Learni...
Python + Tensorflow: how to earn money in the Stock Exchange with Deep Learni...Python + Tensorflow: how to earn money in the Stock Exchange with Deep Learni...
Python + Tensorflow: how to earn money in the Stock Exchange with Deep Learni...
 
R Programming - part 1.pdf
R Programming - part 1.pdfR Programming - part 1.pdf
R Programming - part 1.pdf
 

More from Olivier Grisel

Strategies and Tools for Parallel Machine Learning in Python
Strategies and Tools for Parallel Machine Learning in PythonStrategies and Tools for Parallel Machine Learning in Python
Strategies and Tools for Parallel Machine Learning in PythonOlivier Grisel
 
Universal Topic Classification - Named Entity Disambiguation (IKS Workshop Pa...
Universal Topic Classification - Named Entity Disambiguation (IKS Workshop Pa...Universal Topic Classification - Named Entity Disambiguation (IKS Workshop Pa...
Universal Topic Classification - Named Entity Disambiguation (IKS Workshop Pa...Olivier Grisel
 
Nuxeo 5.3 and Semantic R&D
Nuxeo 5.3 and Semantic R&DNuxeo 5.3 and Semantic R&D
Nuxeo 5.3 and Semantic R&DOlivier Grisel
 
Hadoop MapReduce - OSDC FR 2009
Hadoop MapReduce - OSDC FR 2009Hadoop MapReduce - OSDC FR 2009
Hadoop MapReduce - OSDC FR 2009Olivier Grisel
 

More from Olivier Grisel (6)

Strategies and Tools for Parallel Machine Learning in Python
Strategies and Tools for Parallel Machine Learning in PythonStrategies and Tools for Parallel Machine Learning in Python
Strategies and Tools for Parallel Machine Learning in Python
 
Universal Topic Classification - Named Entity Disambiguation (IKS Workshop Pa...
Universal Topic Classification - Named Entity Disambiguation (IKS Workshop Pa...Universal Topic Classification - Named Entity Disambiguation (IKS Workshop Pa...
Universal Topic Classification - Named Entity Disambiguation (IKS Workshop Pa...
 
Nuxeo Iks 2009 11 13
Nuxeo Iks 2009 11 13Nuxeo Iks 2009 11 13
Nuxeo Iks 2009 11 13
 
Nuxeo 5.3 and Semantic R&D
Nuxeo 5.3 and Semantic R&DNuxeo 5.3 and Semantic R&D
Nuxeo 5.3 and Semantic R&D
 
Hadoop MapReduce - OSDC FR 2009
Hadoop MapReduce - OSDC FR 2009Hadoop MapReduce - OSDC FR 2009
Hadoop MapReduce - OSDC FR 2009
 
Programming the PS3
Programming the PS3Programming the PS3
Programming the PS3
 

Recently uploaded

83770-87607 ۞Call Girls In Near The Park Hotel (Cp) Delhi
83770-87607 ۞Call Girls In Near The Park Hotel (Cp) Delhi83770-87607 ۞Call Girls In Near The Park Hotel (Cp) Delhi
83770-87607 ۞Call Girls In Near The Park Hotel (Cp) Delhidollysharma2066
 
Namrata 7 Plumeria Drive Pimpri Chinchwad Pune Brochure.pdf
Namrata 7 Plumeria Drive Pimpri Chinchwad Pune Brochure.pdfNamrata 7 Plumeria Drive Pimpri Chinchwad Pune Brochure.pdf
Namrata 7 Plumeria Drive Pimpri Chinchwad Pune Brochure.pdfPrachiRudram
 
Affordable and Quality Construction SVTN's Signature Blend of Cost-Efficiency...
Affordable and Quality Construction SVTN's Signature Blend of Cost-Efficiency...Affordable and Quality Construction SVTN's Signature Blend of Cost-Efficiency...
Affordable and Quality Construction SVTN's Signature Blend of Cost-Efficiency...AditiAlishetty
 
Rustomjee The Panorama At Pali Hill, Bandra West, Mumbai - Brochure.pdf
Rustomjee The Panorama At Pali Hill, Bandra West, Mumbai - Brochure.pdfRustomjee The Panorama At Pali Hill, Bandra West, Mumbai - Brochure.pdf
Rustomjee The Panorama At Pali Hill, Bandra West, Mumbai - Brochure.pdfmonikasharma630
 
Low Rate Call Girls in Lajpat Nagar Delhi Call 9990771857
Low Rate Call Girls in Lajpat Nagar Delhi Call 9990771857Low Rate Call Girls in Lajpat Nagar Delhi Call 9990771857
Low Rate Call Girls in Lajpat Nagar Delhi Call 9990771857delhimodel235
 
Call Girls in Nehru Place Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Nehru Place Delhi 💯Call Us 🔝8264348440🔝Call Girls in Nehru Place Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Nehru Place Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Ryan Mahoney - How Property Technology Is Altering the Real Estate Market
Ryan Mahoney - How Property Technology Is Altering the Real Estate MarketRyan Mahoney - How Property Technology Is Altering the Real Estate Market
Ryan Mahoney - How Property Technology Is Altering the Real Estate MarketRyan Mahoney
 
Call Girls in Sarai Kale Khan Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Sarai Kale Khan Delhi 💯Call Us 🔝8264348440🔝Call Girls in Sarai Kale Khan Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Sarai Kale Khan Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Prestige Sector 94 at Noida E Brochure.pdf
Prestige Sector 94 at Noida E Brochure.pdfPrestige Sector 94 at Noida E Brochure.pdf
Prestige Sector 94 at Noida E Brochure.pdfsarak0han45400
 
Prestige Rainbow Waters Raidurgam, Gachibowli Hyderabad E- Brochure.pdf
Prestige Rainbow Waters Raidurgam, Gachibowli Hyderabad E- Brochure.pdfPrestige Rainbow Waters Raidurgam, Gachibowli Hyderabad E- Brochure.pdf
Prestige Rainbow Waters Raidurgam, Gachibowli Hyderabad E- Brochure.pdffaheemali990101
 
The Omaxe State Dwarka Delhi-broucher.pdf.pdf
The Omaxe State Dwarka Delhi-broucher.pdf.pdfThe Omaxe State Dwarka Delhi-broucher.pdf.pdf
The Omaxe State Dwarka Delhi-broucher.pdf.pdfkratirudram
 
Eco-Efficient Living: Redefining Sustainability through Leech's Green Design ...
Eco-Efficient Living: Redefining Sustainability through Leech's Green Design ...Eco-Efficient Living: Redefining Sustainability through Leech's Green Design ...
Eco-Efficient Living: Redefining Sustainability through Leech's Green Design ...Newman George Leech
 
Brigade Neopolis Kokapet, Hyderabad E- Brochure
Brigade Neopolis Kokapet, Hyderabad E- BrochureBrigade Neopolis Kokapet, Hyderabad E- Brochure
Brigade Neopolis Kokapet, Hyderabad E- Brochurefaheemali990101
 
Dynamic Netsoft A leader In Property management Software
Dynamic Netsoft A leader In Property management SoftwareDynamic Netsoft A leader In Property management Software
Dynamic Netsoft A leader In Property management SoftwareDynamic Netsoft
 
Call Girls In Sahibabad Ghaziabad ❤️8860477959 Low Rate Escorts Service In 24...
Call Girls In Sahibabad Ghaziabad ❤️8860477959 Low Rate Escorts Service In 24...Call Girls In Sahibabad Ghaziabad ❤️8860477959 Low Rate Escorts Service In 24...
Call Girls In Sahibabad Ghaziabad ❤️8860477959 Low Rate Escorts Service In 24...lizamodels9
 
A Brief History of Intangibles in Ad Valorem Taxation.pdf
A Brief History of Intangibles in Ad Valorem Taxation.pdfA Brief History of Intangibles in Ad Valorem Taxation.pdf
A Brief History of Intangibles in Ad Valorem Taxation.pdfTim Wilmath
 

Recently uploaded (20)

83770-87607 ۞Call Girls In Near The Park Hotel (Cp) Delhi
83770-87607 ۞Call Girls In Near The Park Hotel (Cp) Delhi83770-87607 ۞Call Girls In Near The Park Hotel (Cp) Delhi
83770-87607 ۞Call Girls In Near The Park Hotel (Cp) Delhi
 
Namrata 7 Plumeria Drive Pimpri Chinchwad Pune Brochure.pdf
Namrata 7 Plumeria Drive Pimpri Chinchwad Pune Brochure.pdfNamrata 7 Plumeria Drive Pimpri Chinchwad Pune Brochure.pdf
Namrata 7 Plumeria Drive Pimpri Chinchwad Pune Brochure.pdf
 
Hot call girls in Moti Bagh🔝 9953056974 🔝 escort Service
Hot call girls in Moti Bagh🔝 9953056974 🔝 escort ServiceHot call girls in Moti Bagh🔝 9953056974 🔝 escort Service
Hot call girls in Moti Bagh🔝 9953056974 🔝 escort Service
 
Affordable and Quality Construction SVTN's Signature Blend of Cost-Efficiency...
Affordable and Quality Construction SVTN's Signature Blend of Cost-Efficiency...Affordable and Quality Construction SVTN's Signature Blend of Cost-Efficiency...
Affordable and Quality Construction SVTN's Signature Blend of Cost-Efficiency...
 
Rustomjee The Panorama At Pali Hill, Bandra West, Mumbai - Brochure.pdf
Rustomjee The Panorama At Pali Hill, Bandra West, Mumbai - Brochure.pdfRustomjee The Panorama At Pali Hill, Bandra West, Mumbai - Brochure.pdf
Rustomjee The Panorama At Pali Hill, Bandra West, Mumbai - Brochure.pdf
 
Call Girls in Mahavir Nagar whatsaap call US +919953056974
Call Girls in Mahavir Nagar  whatsaap call US  +919953056974Call Girls in Mahavir Nagar  whatsaap call US  +919953056974
Call Girls in Mahavir Nagar whatsaap call US +919953056974
 
Low Rate Call Girls in Lajpat Nagar Delhi Call 9990771857
Low Rate Call Girls in Lajpat Nagar Delhi Call 9990771857Low Rate Call Girls in Lajpat Nagar Delhi Call 9990771857
Low Rate Call Girls in Lajpat Nagar Delhi Call 9990771857
 
Call Girls in Nehru Place Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Nehru Place Delhi 💯Call Us 🔝8264348440🔝Call Girls in Nehru Place Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Nehru Place Delhi 💯Call Us 🔝8264348440🔝
 
Low Rate Call Girls in Triveni Complex Delhi Call 9873940964
Low Rate Call Girls in Triveni Complex Delhi Call 9873940964Low Rate Call Girls in Triveni Complex Delhi Call 9873940964
Low Rate Call Girls in Triveni Complex Delhi Call 9873940964
 
Ryan Mahoney - How Property Technology Is Altering the Real Estate Market
Ryan Mahoney - How Property Technology Is Altering the Real Estate MarketRyan Mahoney - How Property Technology Is Altering the Real Estate Market
Ryan Mahoney - How Property Technology Is Altering the Real Estate Market
 
Call Girls in Sarai Kale Khan Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Sarai Kale Khan Delhi 💯Call Us 🔝8264348440🔝Call Girls in Sarai Kale Khan Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Sarai Kale Khan Delhi 💯Call Us 🔝8264348440🔝
 
Prestige Sector 94 at Noida E Brochure.pdf
Prestige Sector 94 at Noida E Brochure.pdfPrestige Sector 94 at Noida E Brochure.pdf
Prestige Sector 94 at Noida E Brochure.pdf
 
Prestige Rainbow Waters Raidurgam, Gachibowli Hyderabad E- Brochure.pdf
Prestige Rainbow Waters Raidurgam, Gachibowli Hyderabad E- Brochure.pdfPrestige Rainbow Waters Raidurgam, Gachibowli Hyderabad E- Brochure.pdf
Prestige Rainbow Waters Raidurgam, Gachibowli Hyderabad E- Brochure.pdf
 
The Omaxe State Dwarka Delhi-broucher.pdf.pdf
The Omaxe State Dwarka Delhi-broucher.pdf.pdfThe Omaxe State Dwarka Delhi-broucher.pdf.pdf
The Omaxe State Dwarka Delhi-broucher.pdf.pdf
 
Eco-Efficient Living: Redefining Sustainability through Leech's Green Design ...
Eco-Efficient Living: Redefining Sustainability through Leech's Green Design ...Eco-Efficient Living: Redefining Sustainability through Leech's Green Design ...
Eco-Efficient Living: Redefining Sustainability through Leech's Green Design ...
 
Brigade Neopolis Kokapet, Hyderabad E- Brochure
Brigade Neopolis Kokapet, Hyderabad E- BrochureBrigade Neopolis Kokapet, Hyderabad E- Brochure
Brigade Neopolis Kokapet, Hyderabad E- Brochure
 
young call girls in Lajpat Nagar,🔝 9953056974 🔝 escort Service
young call girls in Lajpat Nagar,🔝 9953056974 🔝 escort Serviceyoung call girls in Lajpat Nagar,🔝 9953056974 🔝 escort Service
young call girls in Lajpat Nagar,🔝 9953056974 🔝 escort Service
 
Dynamic Netsoft A leader In Property management Software
Dynamic Netsoft A leader In Property management SoftwareDynamic Netsoft A leader In Property management Software
Dynamic Netsoft A leader In Property management Software
 
Call Girls In Sahibabad Ghaziabad ❤️8860477959 Low Rate Escorts Service In 24...
Call Girls In Sahibabad Ghaziabad ❤️8860477959 Low Rate Escorts Service In 24...Call Girls In Sahibabad Ghaziabad ❤️8860477959 Low Rate Escorts Service In 24...
Call Girls In Sahibabad Ghaziabad ❤️8860477959 Low Rate Escorts Service In 24...
 
A Brief History of Intangibles in Ad Valorem Taxation.pdf
A Brief History of Intangibles in Ad Valorem Taxation.pdfA Brief History of Intangibles in Ad Valorem Taxation.pdf
A Brief History of Intangibles in Ad Valorem Taxation.pdf
 

Statistical Machine Learning for Text Classification with scikit-learn and NLTK

  • 1. Statistical Learning for Text Classification with scikit-learn and NLTK Olivier Grisel http://twitter.com/ogrisel PyCon – 2011
  • 2. Outline ● Why text classification? ● What is text classification? ● How? ● scikit-learn ● NLTK ● Google Prediction API ● Some results
  • 3. Applications of Text Classification Task Predicted outcome Spam filtering Spam, Ham, Priority Language guessing English, Spanish, French, ... Sentiment Analysis for Product Positive, Neutral, Negative Reviews News Feed Topic Politics, Business, Technology, Categorization Sports, ... Pay-per-click optimal ads Will yield clicks / Won't placement Recommender systems Will I buy this book? / I won't
  • 4. Supervised Learning Overview ● Convert training data to a set of vectors of features ● Build a model based on the statistical properties of features in the training set, e.g. ● Naïve Bayesian Classifier ● Logistic Regression ● Support Vector Machines ● For each new text document to classify ● Extract features ● Asked model to predict the most likely outcome
  • 5. Summary Training features Text vectors Documents, Images, Sounds... Machine Learning Algorithm Labels New Text features Document, vector Predictive Expected Image, Model Label Sound
  • 6. Bags of Words ● Tokenize document: list of uni-grams ['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog'] ● Binary occurrences / counts: {'the': True, 'quick': True...} ● Frequencies: {'the': 0.22, 'quick': 0.11, 'brown': 0.11, 'fox': 0.11…} ● TF-IDF {'the': 0.001, 'quick': 0.05, 'brown': 0.06, 'fox': 0.24…}
  • 7. Better than frequencies: TF-IDF ● Term Frequency ● Inverse Document Frequency ● Non informative words such as “the” are scaled done
  • 8. Even better features ● bi-grams of words: ● “New York”, “very bad”, “not good” ● n-grams of chars: ● “the”, “ed ”, “ a ” (useful for language guessing) ● Combine with: ● Binary occurrences ● Frequencies ● TF-IDF
  • 10. scikit-learn ● BSD ● numpy / scipy / cython / c++ wrappers ● Many state of the art implementations ● A new release every 3 months ● 17 contributors on release 0.7 ● Not just for text classification
  • 11. Features Extraction in scikit-learn from scikits.learn.features.text import WordNGramAnalyzer text = (u"J'ai mangxe9 du kangourou ce midi," u" c'xe9tait pas trxeas bon.") WordNGramAnalyzer(min_n=1, max_n=2).analyze(text) [u'ai', u'mange', u'du', u'kangourou', u'ce', u'midi', u'etait', u'pas', u'tres', u'bon', u'ai mange', u'mange du', u'du kangourou', u'kangourou ce', u'ce midi', u'midi etait', u'etait pas', u'pas tres', u'tres bon']
  • 12. Features Extraction in scikit-learn from scikits.learn.features.text import CharNGramAnalyzer analyzer = CharNGramAnalyzer(min_n=3, max_n=6) char_ngrams = analyzer.analyze(text) print char_ngrams[:5] + char_ngrams[-5:] [u"j'a", u"'ai", u'ai ', u'i m', u' ma', u's tres', u' tres ', u'tres b', u'res bo', u'es bon']
  • 13. TF-IDF features & SVMs from scikits.learn.features.text.sparse import Vectorizer from scikits.learn.sparse.svm.sparse import LinearSVC vec = Vectorizer(analyzer=analyzer) features = vec.fit_transform(list_of_documents) clf = LinearSVC(C=100).fit(features, labels) clf2 = pickle.loads(pickle.dumps(clf)) predicted_labels = clf2.predict(features_of_new_docs)
  • 14. ) cs (do form ns .tra Training features Text c Documents, ve vectors Images, Sounds... ) Machine X,y fit( Learning . clf Algorithm Labels w) _ ne w) cs _n e (do X m ct( sfor ed i New an . pr Text c.t r clf e features Document, v vector Predictive Expected Image, Model Label Sound
  • 15. NLTK ● Code: ASL 2.0 & Book: CC-BY-NC-ND ● Tokenizers, Stemmers, Parsers, Classifiers, Clusterers, Corpus Readers
  • 16. NLTK Corpus Downloader >>> import nltk >>> nltk.download()
  • 17. Using a NLTK corpus >>> from nltk.corpus import movie_reviews as reviews >>> pos_ids = reviews.fileids('pos') >>> neg_ids = reviews.fileids('neg') >>> len(pos_ids), len(neg_ids) 1000, 1000 >>> reviews.words(pos_ids[0]) ['films', 'adapted', 'from', 'comic', 'books', 'have', ...]
  • 18. Common data cleanup operations ● Lower case & remove accentuated chars: import unicodedata s = ''.join(c for c in unicodedata.normalize('NFD', s.lower()) if unicodedata.category(c) != 'Mn') ● Extract only word tokens of at least 2 chars ● Using NLTK tokenizers & stemmers ● Using a simple regexp: re.compile(r"bww+b", re.U).findall(s)
  • 19. Feature Extraction with NLTK Unigram features def word_features(words): return dict((word, True) for word in words)
  • 20. Feature Extraction with NLTK Bigram Collocations from nltk.collocations import BigramCollocationFinder from nltk.metrics import BigramAssocMeasures as BAM from itertools import chain def bigram_features(words, score_fn=BAM.chi_sq): bg_finder = BigramCollocationFinder.from_words(words) bigrams = bg_finder.nbest(score_fn, 100000) return dict((bg, True) for bg in chain(words, bigrams))
  • 21. The NLTK Naïve Bayes Classifier from nltk.classify import NaiveBayesClassifier neg_examples = [(features(reviews.words(i)), 'neg') for i in neg_ids] pos_examples = [(features(reviews.words(i)), 'pos') for i in pos_ids] train_set = pos_examples + neg_examples classifier = NaiveBayesClassifier.train(train_set)
  • 22. Most informative features >>> classifier.show_most_informative_features() magnificent = True pos : neg = 15.0 : 1.0 outstanding = True pos : neg = 13.6 : 1.0 insulting = True neg : pos = 13.0 : 1.0 vulnerable = True pos : neg = 12.3 : 1.0 ludicrous = True neg : pos = 11.8 : 1.0 avoids = True pos : neg = 11.7 : 1.0 uninvolving = True neg : pos = 11.7 : 1.0 astounding = True pos : neg = 10.3 : 1.0 fascination = True pos : neg = 10.3 : 1.0 idiotic = True neg : pos = 9.8 : 1.0
  • 23. Training NLTK classifiers ● Try nltk-trainer ● python train_classifier.py --instances paras --classifier NaiveBayes –bigrams --min_score 3 movie_reviews
  • 26. NLTK – REST APIs % curl -d "text=Inception is the best movie ever" http://text-processing.com/api/sentiment/ { "probability": { "neg": 0.36647424288117808, "pos": 0.63352575711882186 }, "label": "pos" }
  • 28.
  • 29.
  • 30. Typical performance results: movie reviews ● nltk: ● unigram occurrences ● Naïve Bayesian Classifier ~ 70% ● Google Prediction API ~ 83% ● scikit-learn: ● TF-IDF unigram features ● LinearSVC ~ 87% ● nltk: ● Collocation features selection ● Naïve Bayesian Classifier ~ 97%
  • 31. Typical results: newsgroups topics classification ● 20 newsgroups dataset ● ~ 19K short text documents ● 20 categories ● By date train / test split ● Bigram TF-IDF + LinearSVC: ~ 87%
  • 32. Confusion Matrix (20 newsgroups) 00 alt.atheism 01 comp.graphics 02 comp.os.ms-windows.misc 03 comp.sys.ibm.pc.hardware 04 comp.sys.mac.hardware 05 comp.windows.x 06 misc.forsale 07 rec.autos 08 rec.motorcycles 09 rec.sport.baseball 10 rec.sport.hockey 11 sci.crypt 12 sci.electronics 13 sci.med 14 sci.space 15 soc.religion.christian 16 talk.politics.guns 17 talk.politics.mideast 18 talk.politics.misc 19 talk.religion.misc
  • 33. Typical results: Language Identification ● 15 Wikipedia articles ● [p.text_content() for p in html_tree.findall('//p')] ● CharNGramAnalyzer(min_n=1, max_n=3) ● TF-IDF ● LinearSVC
  • 35. Scaling to many possible outcomes ● Example: possible outcomes are all the categories of Wikipedia (565,108) ● From Document Categorization to Information Retrieval ● Fulltext index for TF-IDF similarity queries ● Smart way to find the top 30 search keywords ● Use Apache Lucene / Solr MoreLikeThisQuery
  • 36. Some pointers ● http://scikit-learn.sf.net doc & examples http://github.com/scikit-learn code ● http://www.nltk.org code & doc & PDF book ● http://streamhacker.com/ ● Jacob Perkins' blog on NLTK & APIs ● https://github.com/japerk/nltk-trainer ● http://www.slideshare.net/ogrisel these slides ● http://twitter.com/ogrisel / http://github.com/ogrisel Questions?