o
    QeŒ  ã                   @   sd   d dl mZ d dlZd dlZd dlZd dlmZ d dlm	Z	 g Z
dZdZg d¢ZG dd	„ d	eƒZdS )
é    )Úprint_functionN)ÚDataset)Ú_check_exists_and_downloadz:http://paddlemodels.bj.bcebos.com/uci_housing/housing.dataZ d4accdce7a25600298819f8e28e8d593)ZCRIMZZNZINDUSZCHASZNOXZRMZAGEZDISZRADZTAXZPTRATIOÚBZLSTATc                   @   s4   e Zd ZdZddd„Zdd	d
„Zdd„ Zdd„ ZdS )Ú
UCIHousinga  
    Implementation of `UCI housing <https://archive.ics.uci.edu/ml/datasets/Housing>`_
    dataset

    Args:
        data_file(str): path to data file, can be set None if
            :attr:`download` is True. Default None
        mode(str): 'train' or 'test' mode. Default 'train'.
        download(bool): whether to download dataset automatically if
            :attr:`data_file` is not set. Default True

    Returns:
        Dataset: instance of UCI housing dataset.

    Examples:
        
        .. code-block:: python

            import paddle
            from paddle.text.datasets import UCIHousing

            class SimpleNet(paddle.nn.Layer):
                def __init__(self):
                    super(SimpleNet, self).__init__()

                def forward(self, feature, target):
                    return paddle.sum(feature), target

            paddle.disable_static()

            uci_housing = UCIHousing(mode='train')

            for i in range(10):
                feature, target = uci_housing[i]
                feature = paddle.to_tensor(feature)
                target = paddle.to_tensor(target)

                model = SimpleNet()
                feature, target = model(feature, target)
                print(feature.numpy().shape, target.numpy())

    NÚtrainTc                 C   sh   |  ¡ dv sJ d |¡ƒ‚|  ¡ | _|| _| jd u r)|s J dƒ‚t|ttd|ƒ| _|  ¡  t 	¡ | _
d S )N)r   Útestz,mode should be 'train' or 'test', but got {}z>data_file is not set and downloading automatically is disabledZuci_housing)ÚlowerÚformatÚmodeÚ	data_filer   ÚURLÚMD5Ú
_load_dataÚpaddleZget_default_dtypeÚdtype)Úselfr   r   Údownload© r   úPD:\Projects\ConvertPro\env\Lib\site-packages\paddle/text/datasets/uci_housing.pyÚ__init__N   s   ÿ

ÿzUCIHousing.__init__é   çš™™™™™é?c           	      C   sð   t j| jdd�}| |jd | |¡}|jdd�|jdd�|jdd�|jd  }}}tj	 
|d ¡D ]}|d d …|f ||  || ||   |d d …|f< q4t|jd | ƒ}| jdkrh|d |… | _d S | jdkrv||d … | _d S d S )Nú )Úsepr   )Zaxisé   r   r   )ÚnpÚfromfiler   ZreshapeÚshapeÚmaxÚminÚsumÚsixÚmovesÚrangeÚintr   Údata)	r   Zfeature_numÚratior&   ÚmaximumsÚminimumsZavgsÚiÚoffsetr   r   r   r   ^   s   ÿ
ÿ6

ÿzUCIHousing._load_datac                 C   s>   | j | }t |d d… ¡ | j¡t |dd … ¡ | j¡fS )Néÿÿÿÿ)r&   r   ÚarrayZastyper   )r   Úidxr&   r   r   r   Ú__getitem__k   s   
ÿzUCIHousing.__getitem__c                 C   s
   t | jƒS )N)Úlenr&   )r   r   r   r   Ú__len__p   s   
zUCIHousing.__len__)Nr   T)r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r/   r1   r   r   r   r   r   "   s    
+
r   )Ú
__future__r   r"   Únumpyr   r   Z	paddle.ior   Zpaddle.dataset.commonr   Ú__all__r   r   Zfeature_namesr   r   r   r   r   Ú<module>   s   