/
iris.py
151 lines (126 loc) · 5.22 KB
/
iris.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
# ----------------------------------------------------------------------------
# Copyright (C) 2021-2022 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 data set contains 3 classes of 50 instances each, where each class refers to a type of iris plant.
The data set contains 3 classes of 50 instances each, where each class refers to a type of iris plant.
One class is linearly separable from the other 2; the latter are NOT linearly separable from each other.
References
----------
* Fisher, R.A. “The use of multiple measurements in taxonomic problems” Annual Eugenics, 7, Part II,
179-188 (1936); also in “Contributions to Mathematical Statistics” (John Wiley, NY, 1950).
* Duda, R.O., & Hart, P.E. (1973) Pattern Classification and Scene Analysis. (Q327.D83) John Wiley & Sons.
ISBN 0-471-22361-1. See page 218.
* And many more..
The typical ML task in this dataset is to build a model that classifies the type of flower.
Dataset Shape:
.. list-table:: Dataset Shape
:widths: 50 50
:header-rows: 1
* - Property
- Value
* - Samples Total
- 150
* - Dimensionality
- 4
* - Features
- real
* - Targets
- 3
* - Samples per class
- 50
Description:
.. list-table:: Dataset Description
:widths: 50 50 50
:header-rows: 1
* - Column name
- Column Role
- Description
* - sepal length (cm)
- Feature
- The length of the flower's sepal (in cm)
* - sepal width (cm)
- Feature
- The width of the flower's sepal (in cm)
* - petal length (cm)
- Feature
- The length of the flower's petal (in cm)
* - petal width (cm)
- Feature
- The width of the flower's petal (in cm)
* - target
- Label
- The class (Setosa,Versicolour,Virginica)
"""
import typing as t
from urllib.request import urlopen
import joblib
import pandas as pd
import sklearn
from sklearn.ensemble import RandomForestClassifier
from deepchecks.tabular.dataset import Dataset
__all__ = ['load_data', 'load_fitted_model']
_MODEL_URL = 'https://figshare.com/ndownloader/files/35122762'
_FULL_DATA_URL = 'https://figshare.com/ndownloader/files/32652977'
_TRAIN_DATA_URL = 'https://figshare.com/ndownloader/files/32653172'
_TEST_DATA_URL = 'https://figshare.com/ndownloader/files/32653130'
_MODEL_VERSION = '1.0.2'
_target = 'target'
_CAT_FEATURES = []
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 Iris dataset (classification).
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, test : 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=_target, cat_features=_CAT_FEATURES)
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=_target, cat_features=_CAT_FEATURES, label_type='classification_label')
test = Dataset(test, label=_target, cat_features=_CAT_FEATURES, label_type='classification_label')
return train, test
def load_fitted_model(pretrained=True):
"""Load and return a fitted classification model to predict the flower type in the iris dataset.
Returns
-------
model : Joblib
the model/pipeline that was trained on the iris 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 RandomForestClassifier(random_state=0)