.. _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**::

  Test set is 966 documents (88 positive)
    Passive-Aggressive classifier : 	   467 train docs (    42 positive)    966 test docs (    88 positive) accuracy: 0.909 in 1.87s (  250 docs/s)
            Perceptron classifier : 	   467 train docs (    42 positive)    966 test docs (    88 positive) accuracy: 0.910 in 1.87s (  249 docs/s)
                   SGD classifier : 	   467 train docs (    42 positive)    966 test docs (    88 positive) accuracy: 0.909 in 1.88s (  248 docs/s)
        NB Multinomial classifier : 	   467 train docs (    42 positive)    966 test docs (    88 positive) accuracy: 0.909 in 1.92s (  243 docs/s)
  
  
    Passive-Aggressive classifier : 	  3304 train docs (   322 positive)    966 test docs (    88 positive) accuracy: 0.962 in 5.37s (  614 docs/s)
            Perceptron classifier : 	  3304 train docs (   322 positive)    966 test docs (    88 positive) accuracy: 0.944 in 5.38s (  614 docs/s)
                   SGD classifier : 	  3304 train docs (   322 positive)    966 test docs (    88 positive) accuracy: 0.945 in 5.38s (  613 docs/s)
        NB Multinomial classifier : 	  3304 train docs (   322 positive)    966 test docs (    88 positive) accuracy: 0.909 in 5.42s (  609 docs/s)
  
  
    Passive-Aggressive classifier : 	  6246 train docs (   672 positive)    966 test docs (    88 positive) accuracy: 0.964 in 9.03s (  691 docs/s)
            Perceptron classifier : 	  6246 train docs (   672 positive)    966 test docs (    88 positive) accuracy: 0.955 in 9.03s (  691 docs/s)
                   SGD classifier : 	  6246 train docs (   672 positive)    966 test docs (    88 positive) accuracy: 0.963 in 9.04s (  691 docs/s)
        NB Multinomial classifier : 	  6246 train docs (   672 positive)    966 test docs (    88 positive) accuracy: 0.917 in 9.08s (  688 docs/s)
  
  
    Passive-Aggressive classifier : 	  9113 train docs (  1112 positive)    966 test docs (    88 positive) accuracy: 0.959 in 12.82s (  710 docs/s)
            Perceptron classifier : 	  9113 train docs (  1112 positive)    966 test docs (    88 positive) accuracy: 0.959 in 12.83s (  710 docs/s)
                   SGD classifier : 	  9113 train docs (  1112 positive)    966 test docs (    88 positive) accuracy: 0.960 in 12.83s (  710 docs/s)
        NB Multinomial classifier : 	  9113 train docs (  1112 positive)    966 test docs (    88 positive) accuracy: 0.931 in 12.87s (  707 docs/s)
  
  
    Passive-Aggressive classifier : 	 11904 train docs (  1508 positive)    966 test docs (    88 positive) accuracy: 0.968 in 16.39s (  726 docs/s)
            Perceptron classifier : 	 11904 train docs (  1508 positive)    966 test docs (    88 positive) accuracy: 0.953 in 16.40s (  725 docs/s)
                   SGD classifier : 	 11904 train docs (  1508 positive)    966 test docs (    88 positive) accuracy: 0.967 in 16.40s (  725 docs/s)
        NB Multinomial classifier : 	 11904 train docs (  1508 positive)    966 test docs (    88 positive) accuracy: 0.943 in 16.44s (  723 docs/s)
  
  
    Passive-Aggressive classifier : 	 14820 train docs (  1884 positive)    966 test docs (    88 positive) accuracy: 0.967 in 20.10s (  737 docs/s)
            Perceptron classifier : 	 14820 train docs (  1884 positive)    966 test docs (    88 positive) accuracy: 0.965 in 20.11s (  737 docs/s)
                   SGD classifier : 	 14820 train docs (  1884 positive)    966 test docs (    88 positive) accuracy: 0.966 in 20.11s (  736 docs/s)
        NB Multinomial classifier : 	 14820 train docs (  1884 positive)    966 test docs (    88 positive) accuracy: 0.944 in 20.15s (  735 docs/s)
  
  
    Passive-Aggressive classifier : 	 17625 train docs (  2216 positive)    966 test docs (    88 positive) accuracy: 0.964 in 23.71s (  743 docs/s)
            Perceptron classifier : 	 17625 train docs (  2216 positive)    966 test docs (    88 positive) accuracy: 0.960 in 23.72s (  743 docs/s)
                   SGD classifier : 	 17625 train docs (  2216 positive)    966 test docs (    88 positive) accuracy: 0.967 in 23.72s (  743 docs/s)
        NB Multinomial classifier : 	 17625 train docs (  2216 positive)    966 test docs (    88 positive) accuracy: 0.943 in 23.76s (  741 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:**  25.76 seconds
( 0 minutes  25.76 seconds)