

.. _example_applications_plot_out_of_core_classification.py:


======================================================
Out-of-core classification of text documents
======================================================

This is an example showing how scikit-learn can be used for classification
using an out-of-core approach: learning from data that doesn't fit into main
memory. We make use of an online classifier, i.e., one that supports the
partial_fit method, that will be fed with batches of examples. To guarantee
that the features space remains the same over time we leverage a
HashingVectorizer that will project each example into the same feature space.
This is especially useful in the case of text classification where new
features (words) may appear in each batch.

The dataset used in this example is Reuters-21578 as provided by the UCI ML
repository. It will be automatically downloaded and uncompressed on first run.

The plot represents the learning curve of the classifier: the evolution
of classification accuracy over the course of the mini-batches. Accuracy is
measured on the first 1000 samples, held out as a validation set.

To limit the memory consumption, we queue examples up to a fixed amount before
feeding them to the learner.



.. rst-class:: horizontal


    *

      .. image:: images/plot_out_of_core_classification_001.png
            :scale: 47

    *

      .. image:: images/plot_out_of_core_classification_002.png
            :scale: 47

    *

      .. image:: images/plot_out_of_core_classification_003.png
            :scale: 47

    *

      .. image:: images/plot_out_of_core_classification_004.png
            :scale: 47


**Script output**::

  downloading dataset (once and for all) into /tmp/buildd/scikit-learn-0.15.2/build/scikit_learn_data/reuters
  untarring Reuters dataset...
  done.
  Test set is 878 documents (108 positive)
    Passive-Aggressive classifier : 	   962 train docs (   132 positive)    878 test docs (   108 positive) accuracy: 0.946 in 1.83s (  525 docs/s)
            Perceptron classifier : 	   962 train docs (   132 positive)    878 test docs (   108 positive) accuracy: 0.842 in 1.84s (  524 docs/s)
                   SGD classifier : 	   962 train docs (   132 positive)    878 test docs (   108 positive) accuracy: 0.885 in 1.84s (  522 docs/s)
        NB Multinomial classifier : 	   962 train docs (   132 positive)    878 test docs (   108 positive) accuracy: 0.877 in 1.90s (  507 docs/s)
  
  
    Passive-Aggressive classifier : 	  3911 train docs (   517 positive)    878 test docs (   108 positive) accuracy: 0.937 in 5.26s (  743 docs/s)
            Perceptron classifier : 	  3911 train docs (   517 positive)    878 test docs (   108 positive) accuracy: 0.908 in 5.27s (  742 docs/s)
                   SGD classifier : 	  3911 train docs (   517 positive)    878 test docs (   108 positive) accuracy: 0.929 in 5.27s (  741 docs/s)
        NB Multinomial classifier : 	  3911 train docs (   517 positive)    878 test docs (   108 positive) accuracy: 0.885 in 5.33s (  734 docs/s)
  
  
    Passive-Aggressive classifier : 	  6821 train docs (   891 positive)    878 test docs (   108 positive) accuracy: 0.956 in 8.65s (  788 docs/s)
            Perceptron classifier : 	  6821 train docs (   891 positive)    878 test docs (   108 positive) accuracy: 0.913 in 8.65s (  788 docs/s)
                   SGD classifier : 	  6821 train docs (   891 positive)    878 test docs (   108 positive) accuracy: 0.956 in 8.66s (  787 docs/s)
        NB Multinomial classifier : 	  6821 train docs (   891 positive)    878 test docs (   108 positive) accuracy: 0.899 in 8.71s (  782 docs/s)
  
  
    Passive-Aggressive classifier : 	  9759 train docs (  1276 positive)    878 test docs (   108 positive) accuracy: 0.959 in 12.08s (  807 docs/s)
            Perceptron classifier : 	  9759 train docs (  1276 positive)    878 test docs (   108 positive) accuracy: 0.950 in 12.09s (  807 docs/s)
                   SGD classifier : 	  9759 train docs (  1276 positive)    878 test docs (   108 positive) accuracy: 0.958 in 12.10s (  806 docs/s)
        NB Multinomial classifier : 	  9759 train docs (  1276 positive)    878 test docs (   108 positive) accuracy: 0.909 in 12.15s (  803 docs/s)
  
  
    Passive-Aggressive classifier : 	 11680 train docs (  1499 positive)    878 test docs (   108 positive) accuracy: 0.942 in 15.05s (  775 docs/s)
            Perceptron classifier : 	 11680 train docs (  1499 positive)    878 test docs (   108 positive) accuracy: 0.935 in 15.06s (  775 docs/s)
                   SGD classifier : 	 11680 train docs (  1499 positive)    878 test docs (   108 positive) accuracy: 0.935 in 15.06s (  775 docs/s)
        NB Multinomial classifier : 	 11680 train docs (  1499 positive)    878 test docs (   108 positive) accuracy: 0.916 in 15.12s (  772 docs/s)
  
  
    Passive-Aggressive classifier : 	 14625 train docs (  1865 positive)    878 test docs (   108 positive) accuracy: 0.964 in 18.56s (  788 docs/s)
            Perceptron classifier : 	 14625 train docs (  1865 positive)    878 test docs (   108 positive) accuracy: 0.960 in 18.56s (  787 docs/s)
                   SGD classifier : 	 14625 train docs (  1865 positive)    878 test docs (   108 positive) accuracy: 0.965 in 18.57s (  787 docs/s)
        NB Multinomial classifier : 	 14625 train docs (  1865 positive)    878 test docs (   108 positive) accuracy: 0.926 in 18.63s (  785 docs/s)
  
  
    Passive-Aggressive classifier : 	 17360 train docs (  2179 positive)    878 test docs (   108 positive) accuracy: 0.949 in 21.73s (  798 docs/s)
            Perceptron classifier : 	 17360 train docs (  2179 positive)    878 test docs (   108 positive) accuracy: 0.937 in 21.73s (  798 docs/s)
                   SGD classifier : 	 17360 train docs (  2179 positive)    878 test docs (   108 positive) accuracy: 0.958 in 21.74s (  798 docs/s)
        NB Multinomial classifier : 	 17360 train docs (  2179 positive)    878 test docs (   108 positive) accuracy: 0.932 in 21.79s (  796 docs/s)



**Python source code:** :download:`plot_out_of_core_classification.py <plot_out_of_core_classification.py>`

.. literalinclude:: plot_out_of_core_classification.py
    :lines: 25-

**Total running time of the example:**  55.92 seconds
( 0 minutes  55.92 seconds)
    