uci_housing.py 4.8 KB
Newer Older
D
dangqingqing 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Y
Yu Yang 已提交
14 15 16
"""
UCI Housing dataset.

G
gongweibao 已提交
17
This module will download dataset from
Q
qijun 已提交
18
https://archive.ics.uci.edu/ml/machine-learning-databases/housing/ and
Q
qijun 已提交
19
parse training set and test set into paddle reader creators.
Y
Yu Yang 已提交
20
"""
D
dangqingqing 已提交
21

22
from __future__ import print_function
T
tangwei12 已提交
23

D
dangqingqing 已提交
24
import numpy as np
M
minqiyang 已提交
25
import six
T
tangwei12 已提交
26 27
import tempfile
import tarfile
D
dangqingqing 已提交
28
import os
29
import paddle.dataset.common
30
import paddle.utils.deprecated as deprecated
D
dangqingqing 已提交
31

Y
Yancey1989 已提交
32
URL = 'http://paddlemodels.bj.bcebos.com/uci_housing/housing.data'
D
dangqingqing 已提交
33 34 35
MD5 = 'd4accdce7a25600298819f8e28e8d593'
feature_names = [
    'CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX',
36
    'PTRATIO', 'B', 'LSTAT'
D
dangqingqing 已提交
37 38 39 40
]

UCI_TRAIN_DATA = None
UCI_TEST_DATA = None
T
tangwei12 已提交
41 42 43

FLUID_URL_MODEL = 'https://github.com/PaddlePaddle/book/raw/develop/01.fit_a_line/fluid/fit_a_line.fluid.tar'
FLUID_MD5_MODEL = '6e6dd637ccd5993961f68bfbde46090b'
D
dangqingqing 已提交
44

45

D
dangqingqing 已提交
46 47 48 49 50 51
def feature_range(maximums, minimums):
    import matplotlib
    matplotlib.use('Agg')
    import matplotlib.pyplot as plt
    fig, ax = plt.subplots()
    feature_num = len(maximums)
52 53 54 55
    ax.bar(list(range(feature_num)),
           maximums - minimums,
           color='r',
           align='center')
D
dangqingqing 已提交
56
    ax.set_title('feature scale')
57
    plt.xticks(list(range(feature_num)), feature_names)
D
dangqingqing 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    plt.xlim([-1, feature_num])
    fig.set_figheight(6)
    fig.set_figwidth(10)
    if not os.path.exists('./image'):
        os.makedirs('./image')
    fig.savefig('image/ranges.png', dpi=48)
    plt.close(fig)


def load_data(filename, feature_num=14, ratio=0.8):
    global UCI_TRAIN_DATA, UCI_TEST_DATA
    if UCI_TRAIN_DATA is not None and UCI_TEST_DATA is not None:
        return

    data = np.fromfile(filename, sep=' ')
M
minqiyang 已提交
73
    data = data.reshape(data.shape[0] // feature_num, feature_num)
D
dangqingqing 已提交
74 75
    maximums, minimums, avgs = data.max(axis=0), data.min(axis=0), data.sum(
        axis=0) / data.shape[0]
76 77
    # if you want to print the distribution of input data, you could use function of feature_range
    #feature_range(maximums[:-1], minimums[:-1])
M
minqiyang 已提交
78
    for i in six.moves.range(feature_num - 1):
D
dangqingqing 已提交
79 80 81 82 83 84
        data[:, i] = (data[:, i] - avgs[i]) / (maximums[i] - minimums[i])
    offset = int(data.shape[0] * ratio)
    UCI_TRAIN_DATA = data[:offset]
    UCI_TEST_DATA = data[offset:]


85 86 87 88
@deprecated(
    since="2.0.0",
    update_to="paddle.text.datasets.UCIHousing",
    reason="Please use new dataset API which supports paddle.io.DataLoader")
D
dangqingqing 已提交
89
def train():
Q
qijun 已提交
90
    """
Q
qijun 已提交
91
    UCI_HOUSING training set creator.
Q
qijun 已提交
92

Q
qijun 已提交
93 94
    It returns a reader creator, each sample in the reader is features after
    normalization and price number.
Q
qijun 已提交
95

Q
qijun 已提交
96
    :return: Training reader creator
Q
qijun 已提交
97 98
    :rtype: callable
    """
D
dangqingqing 已提交
99
    global UCI_TRAIN_DATA
100
    load_data(paddle.dataset.common.download(URL, 'uci_housing', MD5))
D
dangqingqing 已提交
101 102 103 104 105 106 107 108

    def reader():
        for d in UCI_TRAIN_DATA:
            yield d[:-1], d[-1:]

    return reader


109 110 111 112
@deprecated(
    since="2.0.0",
    update_to="paddle.text.datasets.UCIHousing",
    reason="Please use new dataset API which supports paddle.io.DataLoader")
D
dangqingqing 已提交
113
def test():
Q
qijun 已提交
114 115 116
    """
    UCI_HOUSING test set creator.

Q
qijun 已提交
117 118
    It returns a reader creator, each sample in the reader is features after
    normalization and price number.
Q
qijun 已提交
119 120 121 122

    :return: Test reader creator
    :rtype: callable
    """
D
dangqingqing 已提交
123
    global UCI_TEST_DATA
124
    load_data(paddle.dataset.common.download(URL, 'uci_housing', MD5))
D
dangqingqing 已提交
125 126 127 128 129 130

    def reader():
        for d in UCI_TEST_DATA:
            yield d[:-1], d[-1:]

    return reader
Y
Yancey1989 已提交
131

T
tangwei12 已提交
132

T
tangwei12 已提交
133
def fluid_model():
T
tangwei12 已提交
134 135
    parameter_tar = paddle.dataset.common.download(
        FLUID_URL_MODEL, 'uci_housing', FLUID_MD5_MODEL, 'fit_a_line.fluid.tar')
T
tangwei12 已提交
136 137 138 139 140 141 142

    tar = tarfile.TarFile(parameter_tar, mode='r')
    dirpath = tempfile.mkdtemp()
    tar.extractall(path=dirpath)

    return dirpath

T
tangwei12 已提交
143

144 145 146 147
@deprecated(
    since="2.0.0",
    update_to="paddle.text.datasets.UCIHousing",
    reason="Please use new dataset API which supports paddle.io.DataLoader")
T
tangwei12 已提交
148 149
def predict_reader():
    """
150
    It returns just one tuple data to do inference.
T
tangwei12 已提交
151

152
    :return: one tuple data
M
minqiyang 已提交
153
    :rtype: tuple
T
tangwei12 已提交
154 155 156
    """
    global UCI_TEST_DATA
    load_data(paddle.dataset.common.download(URL, 'uci_housing', MD5))
T
tangwei12 已提交
157
    return (UCI_TEST_DATA[0][:-1], )
Y
Yancey1989 已提交
158

T
tangwei12 已提交
159

160 161 162 163
@deprecated(
    since="2.0.0",
    update_to="paddle.text.datasets.UCIHousing",
    reason="Please use new dataset API which supports paddle.io.DataLoader")
164
def fetch():
165
    paddle.dataset.common.download(URL, 'uci_housing', MD5)