

.. _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.89s (  509 docs/s)
            Perceptron classifier : 	   962 train docs (   132 positive)    878 test docs (   108 positive) accuracy: 0.842 in 1.89s (  508 docs/s)
                   SGD classifier : 	   962 train docs (   132 positive)    878 test docs (   108 positive) accuracy: 0.885 in 1.90s (  506 docs/s)
        NB Multinomial classifier : 	   962 train docs (   132 positive)    878 test docs (   108 positive) accuracy: 0.877 in 1.95s (  493 docs/s)
  
  
    Passive-Aggressive classifier : 	  3911 train docs (   517 positive)    878 test docs (   108 positive) accuracy: 0.937 in 5.47s (  715 docs/s)
            Perceptron classifier : 	  3911 train docs (   517 positive)    878 test docs (   108 positive) accuracy: 0.908 in 5.47s (  714 docs/s)
                   SGD classifier : 	  3911 train docs (   517 positive)    878 test docs (   108 positive) accuracy: 0.929 in 5.48s (  713 docs/s)
        NB Multinomial classifier : 	  3911 train docs (   517 positive)    878 test docs (   108 positive) accuracy: 0.885 in 5.53s (  707 docs/s)
  
  
    Passive-Aggressive classifier : 	  6821 train docs (   891 positive)    878 test docs (   108 positive) accuracy: 0.956 in 8.99s (  759 docs/s)
            Perceptron classifier : 	  6821 train docs (   891 positive)    878 test docs (   108 positive) accuracy: 0.913 in 8.99s (  758 docs/s)
                   SGD classifier : 	  6821 train docs (   891 positive)    878 test docs (   108 positive) accuracy: 0.956 in 9.00s (  758 docs/s)
        NB Multinomial classifier : 	  6821 train docs (   891 positive)    878 test docs (   108 positive) accuracy: 0.899 in 9.04s (  754 docs/s)
  
  
    Passive-Aggressive classifier : 	  9759 train docs (  1276 positive)    878 test docs (   108 positive) accuracy: 0.959 in 12.58s (  775 docs/s)
            Perceptron classifier : 	  9759 train docs (  1276 positive)    878 test docs (   108 positive) accuracy: 0.950 in 12.58s (  775 docs/s)
                   SGD classifier : 	  9759 train docs (  1276 positive)    878 test docs (   108 positive) accuracy: 0.958 in 12.59s (  775 docs/s)
        NB Multinomial classifier : 	  9759 train docs (  1276 positive)    878 test docs (   108 positive) accuracy: 0.909 in 12.64s (  772 docs/s)
  
  
    Passive-Aggressive classifier : 	 11680 train docs (  1499 positive)    878 test docs (   108 positive) accuracy: 0.942 in 15.67s (  745 docs/s)
            Perceptron classifier : 	 11680 train docs (  1499 positive)    878 test docs (   108 positive) accuracy: 0.935 in 15.67s (  745 docs/s)
                   SGD classifier : 	 11680 train docs (  1499 positive)    878 test docs (   108 positive) accuracy: 0.935 in 15.68s (  744 docs/s)
        NB Multinomial classifier : 	 11680 train docs (  1499 positive)    878 test docs (   108 positive) accuracy: 0.916 in 15.73s (  742 docs/s)
  
  
    Passive-Aggressive classifier : 	 14625 train docs (  1865 positive)    878 test docs (   108 positive) accuracy: 0.964 in 19.35s (  755 docs/s)
            Perceptron classifier : 	 14625 train docs (  1865 positive)    878 test docs (   108 positive) accuracy: 0.960 in 19.35s (  755 docs/s)
                   SGD classifier : 	 14625 train docs (  1865 positive)    878 test docs (   108 positive) accuracy: 0.965 in 19.36s (  755 docs/s)
        NB Multinomial classifier : 	 14625 train docs (  1865 positive)    878 test docs (   108 positive) accuracy: 0.926 in 19.41s (  753 docs/s)
  
  
    Passive-Aggressive classifier : 	 17360 train docs (  2179 positive)    878 test docs (   108 positive) accuracy: 0.949 in 22.67s (  765 docs/s)
            Perceptron classifier : 	 17360 train docs (  2179 positive)    878 test docs (   108 positive) accuracy: 0.937 in 22.68s (  765 docs/s)
                   SGD classifier : 	 17360 train docs (  2179 positive)    878 test docs (   108 positive) accuracy: 0.958 in 22.68s (  765 docs/s)
        NB Multinomial classifier : 	 17360 train docs (  2179 positive)    878 test docs (   108 positive) accuracy: 0.932 in 22.73s (  763 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:**  50.08 seconds
( 0 minutes  50.08 seconds)
    