ó
‡ˆ\c           @   sÒ  d  Z  d d l Z d d l m Z d d l m Z m Z m Z d d l m	 Z	 d d l
 Z d d l j Z d d l m Z d d l m Z d d	 l m Z d d
 l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z e d d d d d d ƒ e d d d d d d ƒ e d d d d d d ƒ e d d d d d d ƒ e d d  d d! d d" ƒ f Z e d d# d d$ d d% ƒ Z e j e  ƒ Z! d d& e# d e$ e$ d' „ Z% d( „  Z& d) „  Z' d S(*   sh   RCV1 dataset.

The dataset page is available at

    http://jmlr.csail.mit.edu/papers/volume5/lewis04a/
iÿÿÿÿN(   t   remove(   t   dirnamet   existst   join(   t   GzipFilei   (   t   get_data_home(   t   _pkl_filepath(   t   _fetch_remote(   t   RemoteFileMetadatai   (   t   makedirs(   t   _joblib(   t   load_svmlight_files(   t   shuffle(   t   Buncht   urls.   https://ndownloader.figshare.com/files/5976069t   checksumt@   ed40f7e418d10484091b059703eeb95ae3199fe042891dcec4be6696b9968374t   filenames    lyrl2004_vectors_test_pt0.dat.gzs.   https://ndownloader.figshare.com/files/5976066t@   87700668ae45d45d5ca1ef6ae9bd81ab0f5ec88cc95dcef9ae7838f727a13aa6s    lyrl2004_vectors_test_pt1.dat.gzs.   https://ndownloader.figshare.com/files/5976063t@   48143ac703cbe33299f7ae9f4995db49a258690f60e5debbff8995c34841c7f5s    lyrl2004_vectors_test_pt2.dat.gzs.   https://ndownloader.figshare.com/files/5976060t@   dfcb0d658311481523c6e6ca0c3f5a3e1d3d12cde5d7a8ce629a9006ec7dbb39s    lyrl2004_vectors_test_pt3.dat.gzs.   https://ndownloader.figshare.com/files/5976057t@   5468f656d0ba7a83afc7ad44841cf9a53048a5c083eedc005dcdb5cc768924aes   lyrl2004_vectors_train.dat.gzs.   https://ndownloader.figshare.com/files/5976048t@   2a98e5e5d8b770bded93afc8930d88299474317fe14181aee1466cc754d0d1c1s   rcv1v2.topics.qrels.gzt   allc   )      C   s¼  d } d } d } d }	 t  d |  ƒ }  t |  d ƒ }
 | rX t |
 ƒ sX t |
 ƒ qX n  t |
 d ƒ } t |
 d ƒ } t |
 d	 ƒ } t |
 d
 ƒ } | rìt | ƒ s´ t | ƒ rìg  } xJ t D]B } t j d | j ƒ t	 | d |
 ƒ} | j
 t d | ƒ ƒ qÁ Wt | d | ƒ} t j | d | d | d | d | d g ƒ j ƒ  } t j | d | d | d | d | d f ƒ } | j t j ƒ } t j | | d d ƒt j | | d d ƒxF | D] } | j ƒ  t | j ƒ qÈWn t j | ƒ } t j | ƒ } | rEt | ƒ s*t | ƒ rEt j d t j ƒ t	 t d |
 ƒ} d } d } d } t j | | f d t j ƒ} t j | d t j ƒ} i  } t d | d d ƒ À } x¶ | D]® } | j d ƒ j d ƒ } t  | ƒ d k r½| \ } }  }! | | k r| d 7} | | | <n  t! |  ƒ }  |  | k rT|  } | d 7} |  | | <n  d | | | | f <q½q½WWd  QXt | ƒ t" | | ƒ }" | |" d  d  … f } t j# | d t$ ƒ}# x" | j% ƒ  D] }$ |$ |# | |$ <qÆWt j& |# ƒ }% |# |% }# t j' | d  d  … |% f ƒ } t j | | d d ƒt j |# | d d ƒn t j | ƒ } t j | ƒ }# | d! k rrn² | d" k rÃ| d  |	 … d  d  … f } | d  |	 … d  d  … f } | |	  } na | d# k r| |	 d  … d  d  … f } | |	 d  … d  d  … f } | |	 } n t( d$ | ƒ ‚ | rNt) | | | d% | ƒ\ } } } n  t* t+ ƒ }& t, t |& d& d' ƒ ƒ  }' |' j- ƒ  }( Wd  QX| r—| | f St. d( | d) | d* | d+ |# d, |( ƒ S(-   s‚
  Load the RCV1 multilabel dataset (classification).

    Download it if necessary.

    Version: RCV1-v2, vectors, full sets, topics multilabels.

    =================   =====================
    Classes                               103
    Samples total                      804414
    Dimensionality                      47236
    Features            real, between 0 and 1
    =================   =====================

    Read more in the :ref:`User Guide <rcv1_dataset>`.

    .. versionadded:: 0.17

    Parameters
    ----------
    data_home : string, optional
        Specify another download and cache folder for the datasets. By default
        all scikit-learn data is stored in '~/scikit_learn_data' subfolders.

    subset : string, 'train', 'test', or 'all', default='all'
        Select the dataset to load: 'train' for the training set
        (23149 samples), 'test' for the test set (781265 samples),
        'all' for both, with the training samples first if shuffle is False.
        This follows the official LYRL2004 chronological split.

    download_if_missing : boolean, default=True
        If False, raise a IOError if the data is not locally available
        instead of trying to download the data from the source site.

    random_state : int, RandomState instance or None (default)
        Determines random number generation for dataset shuffling. Pass an int
        for reproducible output across multiple function calls.
        See :term:`Glossary <random_state>`.

    shuffle : bool, default=False
        Whether to shuffle dataset.

    return_X_y : boolean, default=False.
        If True, returns ``(dataset.data, dataset.target)`` instead of a Bunch
        object. See below for more information about the `dataset.data` and
        `dataset.target` object.

        .. versionadded:: 0.20

    Returns
    -------
    dataset : dict-like object with the following attributes:

    dataset.data : scipy csr array, dtype np.float64, shape (804414, 47236)
        The array has 0.16% of non zero values.

    dataset.target : scipy csr array, dtype np.uint8, shape (804414, 103)
        Each sample has a value of 1 in its categories, and 0 in others.
        The array has 3.15% of non zero values.

    dataset.sample_id : numpy array, dtype np.uint32, shape (804414,)
        Identification number of each sample, as ordered in dataset.data.

    dataset.target_names : numpy array, dtype object, length (103)
        Names of each target (RCV1 topics), as ordered in dataset.target.

    dataset.DESCR : string
        Description of the RCV1 dataset.

    (data, target) : tuple if ``return_X_y`` is True

        .. versionadded:: 0.20
    i>F i„¸  ig   imZ  t	   data_homet   RCV1s   samples.pkls   sample_id.pkls   sample_topics.pkls   topics_names.pkls   Downloading %sR   R   t
   n_featuresi   i    i   i   i   i	   i   i   i   i   t   compressiÿÿÿÿt   dtypet   modet   rbt   asciiu    NR   t   traint   testsL   Unknown subset parameter. Got '%s' instead of one of ('all', 'train', test')t   random_statet   descrs   rcv1.rstt   datat   targett	   sample_idt   target_namest   DESCR(/   R   R   R   R	   R   t   XY_METADATAt   loggert   infoR   R   t   appendR   R   t   spt   vstackt   tocsrt   npt   hstackt   astypet   uint32R
   t   dumpt   closeR    t   namet   loadt   TOPICS_METADATAt   zerost   uint8t   int32t   decodet   splitt   lent   intt   _find_permutationt   emptyt   objectt   keyst   argsortt
   csr_matrixt
   ValueErrort   shuffle_R   t   __file__t   opent   readR   ()   R   t   subsett   download_if_missingR"   R   t
   return_X_yt	   N_SAMPLESt
   N_FEATURESt   N_CATEGORIESt   N_TRAINt   rcv1_dirt   samples_patht   sample_id_patht   sample_topics_patht   topics_patht   filest   eacht	   file_patht   Xyt   XR&   t   ft   topics_archive_patht   n_catt   n_doct   doc_previoust   yt   sample_id_bist   category_namest   linet   line_componentst   catt   doct   _t   permutationt
   categoriest   kt   ordert   module_patht   rst_filet   fdescr(    (    s4   lib/python2.7/site-packages/sklearn/datasets/rcv1.pyt
   fetch_rcv1O   s´    J82
		

!


$
c         C   sP   |  j  } t j | d t j ƒ} t j | d t j ƒ} t j | |  | ƒ | S(   s   inverse permutation pR   (   t   sizeR0   R9   R;   t   aranget   put(   t   pt   nt   st   i(    (    s4   lib/python2.7/site-packages/sklearn/datasets/rcv1.pyt   _inverse_permutation  s
    	c         C   s2   t  j |  ƒ } t  j | ƒ } t | ƒ } | | S(   s    find the permutation from a to b(   R0   RD   Rx   (   t   at   bt   tt   ut   u_(    (    s4   lib/python2.7/site-packages/sklearn/datasets/rcv1.pyR@     s    ((   t   __doc__t   loggingt   osR    t   os.pathR   R   R   t   gzipR   t   numpyR0   t   scipy.sparset   sparseR-   t   baseR   R   R   R   t   utils.fixesR	   t   utilsR
   t   svmlight_formatR   R   RG   R   R)   R8   t	   getLoggert   __name__R*   t   Nonet   Truet   FalseRp   Rx   R@   (    (    (    s4   lib/python2.7/site-packages/sklearn/datasets/rcv1.pyt   <module>   sX   		Æ		