-
Notifications
You must be signed in to change notification settings - Fork 247
/
avocado.py
184 lines (160 loc) · 6.4 KB
/
avocado.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# ----------------------------------------------------------------------------
# Copyright (C) 2021-2023 Deepchecks (https://www.deepchecks.com)
#
# This file is part of Deepchecks.
# Deepchecks is distributed under the terms of the GNU Affero General
# Public License (version 3 or later).
# You should have received a copy of the GNU Affero General Public License
# along with Deepchecks. If not, see <http://www.gnu.org/licenses/>.
# ----------------------------------------------------------------------------
#
"""The avocado dataset contains historical data on avocado prices and sales volume in multiple US markets.
The avocado dataset contains historical data on avocado prices and sales volume in multiple US markets
https://www.kaggle.com/neuromusic/avocado-prices.
This dataset is licensed under the Open Data Commons Open Database License (ODbL) v1.0
(https://opendatacommons.org/licenses/odbl/1-0/).
The typical ML task in this dataset is to build a model that predicts the average price of Avocados.
Dataset Shape:
.. list-table:: Dataset Shape
:widths: 50 50
:header-rows: 1
* - Property
- Value
* - Samples Total
- 18.2K
* - Dimensionality
- 14
* - Features
- real, string
* - Targets
- real 0.44 - 3.25
Description:
.. list-table:: Dataset Description
:widths: 50 50 50
:header-rows: 1
* - Column name
- Column Role
- Description
* - Date
- Datetime
- The date of the observation
* - Total Volume
- Feature
- Total number of avocados sold
* - 4046
- Feature
- Total number of avocados with PLU 4046 (small avocados) sold
* - 4225
- Feature
- Total number of avocados with PLU 4225 (large avocados) sold
* - 4770
- Feature
- Total number of avocados with PLU 4770 (xlarge avocados) sold
* - Total Bags
- Feature
-
* - Small Bags
- Feature
-
* - Large Bags
- Feature
-
* - XLarge Bags
- Feature
-
* - type
- Feature
- Conventional or organic
* - year
- Feature
-
* - region
- Feature
- The city or region of the observation
* - AveragePrice
- Label
- The average price of a single avocado
"""
import typing as t
from urllib.request import urlopen
import joblib
import pandas as pd
import sklearn
from category_encoders import OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.ensemble import RandomForestRegressor
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from deepchecks.tabular.dataset import Dataset
__all__ = ['load_data', 'load_fitted_model']
_MODEL_URL = 'https://figshare.com/ndownloader/files/35259829'
_FULL_DATA_URL = 'https://figshare.com/ndownloader/files/35259799'
_TRAIN_DATA_URL = 'https://figshare.com/ndownloader/files/35259769'
_TEST_DATA_URL = 'https://figshare.com/ndownloader/files/35259814'
_MODEL_VERSION = '1.0.2'
_target = 'AveragePrice'
_CAT_FEATURES = ['region', 'type']
_NUM_FEATURES = ['Total Volume', '4046', '4225', 'Total Bags', 'Small Bags', 'Large Bags', 'XLarge Bags']
def load_data(data_format: str = 'Dataset', as_train_test: bool = True) -> \
t.Union[t.Tuple, t.Union[Dataset, pd.DataFrame]]:
"""Load and returns the Avocado dataset (regression).
Parameters
----------
data_format : str , default: Dataset
Represent the format of the returned value. Can be 'Dataset'|'Dataframe'
'Dataset' will return the data as a Dataset object
'Dataframe' will return the data as a pandas Dataframe object
as_train_test : bool , default: True
If True, the returned data is splitted into train and test exactly like the toy model
was trained. The first return value is the train data and the second is the test data.
In order to get this model, call the load_fitted_model() function.
Otherwise, returns a single object.
Returns
-------
dataset : Union[deepchecks.Dataset, pd.DataFrame]
the data object, corresponding to the data_format attribute.
train_data, test_data : Tuple[Union[deepchecks.Dataset, pd.DataFrame],Union[deepchecks.Dataset, pd.DataFrame]
tuple if as_train_test = True. Tuple of two objects represents the dataset splitted to train and test sets.
"""
if not as_train_test:
dataset = pd.read_csv(_FULL_DATA_URL)
if data_format == 'Dataset':
dataset = Dataset(dataset, label='AveragePrice', cat_features=_CAT_FEATURES, datetime_name='Date')
return dataset
else:
train = pd.read_csv(_TRAIN_DATA_URL)
test = pd.read_csv(_TEST_DATA_URL)
if data_format == 'Dataset':
train = Dataset(train, label='AveragePrice', cat_features=_CAT_FEATURES, datetime_name='Date')
test = Dataset(test, label='AveragePrice', cat_features=_CAT_FEATURES, datetime_name='Date')
return train, test
def load_fitted_model(pretrained=True):
"""Load and return a fitted regression model to predict the AveragePrice in the avocado dataset.
Returns
-------
model : Joblib
the model/pipeline that was trained on the Avocado dataset.
"""
if sklearn.__version__ == _MODEL_VERSION and pretrained:
with urlopen(_MODEL_URL) as f:
model = joblib.load(f)
else:
model = _build_model()
train, _ = load_data()
model.fit(train.data[train.features], train.data[train.label_name])
return model
def _build_model():
"""Build the model to fit."""
return Pipeline(steps=[
('preprocessor',
ColumnTransformer(transformers=[('num',
Pipeline(steps=[('imputer',
SimpleImputer(strategy='median')),
('scaler',
StandardScaler())]),
_NUM_FEATURES),
('cat', OneHotEncoder(),
_CAT_FEATURES)])),
('classifier', RandomForestRegressor(random_state=0, max_depth=7, n_estimators=30))
])