/
datasets.py
executable file
·175 lines (117 loc) · 4.94 KB
/
datasets.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
def telco_churn(quantile=.5):
'''Returns dataset in format x, [y1, y2]. This dataset
is useful for demonstrating multi-output model or for
experimenting with reduction strategy creation.
The data is from hyperparameter optimization experiment with
Kaggle telco churn dataset.
x: features
y1: val_loss
y2: val_f1score
quantile is for transforming the otherwise continuous y variables into
labels so that higher value is stronger. If set to 0 then original
continuous will be returned.'''
import wrangle
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/autonomio/examples/master/telco_churn/telco_churn_for_sensitivity.csv')
df = df.drop(['val_acc', 'loss', 'f1score', 'acc', 'round_epochs'], 1)
for col in df.iloc[:, 2:].columns:
df = wrangle.col_to_multilabel(df, col)
df = wrangle.df_rename_cols(df)
if quantile > 0:
y1 = (df.C0 < df.C0.quantile(quantile)).astype(int).values
y2 = (df.C1 > df.C1.quantile(quantile)).astype(int).values
else:
y1 = df.C0.values
y2 = df.C1.values
x = df.drop(['C0', 'C1'], 1).values
return x, [y1, y2]
def icu_mortality(samples=None):
import pandas as pd
base = 'https://raw.githubusercontent.com/autonomio/datasets/master/autonomio-datasets/'
df = pd.read_csv(base + 'icu_mortality.csv')
df = df.dropna(thresh=3580, axis=1)
df = df.dropna()
df = df.sample(frac=1).head(samples)
y = df['hospitalmortality'].astype(int).values
x = df.drop('hospitalmortality', axis=1).values
return x, y
def titanic():
import pandas as pd
base = 'https://raw.githubusercontent.com/autonomio/datasets/master/autonomio-datasets/'
df = pd.read_csv(base + 'titanic.csv')
y = df.survived.values
x = df[['age', 'sibsp', 'parch']]
cols = ['class', 'embark_town', 'who', 'deck', 'sex']
for col in cols:
x = pd.merge(x,
pd.get_dummies(df[col]),
left_index=True,
right_index=True)
x = x.values
print('BE CAREFUL, this dataset has nan values.')
return x, y
def iris():
import pandas as pd
from keras.utils import to_categorical
base = 'https://raw.githubusercontent.com/autonomio/datasets/master/autonomio-datasets/'
df = pd.read_csv(base + 'iris.csv')
df['species'] = df['species'].factorize()[0]
df = df.sample(len(df))
y = to_categorical(df['species'])
x = df.iloc[:, :-1].values
y = to_categorical(df['species'])
x = df.iloc[:, :-1].values
return x, y
def cervical_cancer():
import pandas as pd
from numpy import nan
base = 'https://raw.githubusercontent.com/autonomio/datasets/master/autonomio-datasets/'
df = pd.read_csv(base + 'cervical_cancer.csv')
df = df.replace('?', nan)
df = df.drop(['citology', 'hinselmann', 'biopsy'], axis=1)
df = df.drop(['since_first_diagnosis',
'since_last_diagnosis'], axis=1).dropna()
df = df.astype(float)
y = df.schiller.values
x = df.drop('schiller', axis=1).values
return x, y
def breast_cancer():
import pandas as pd
base = 'https://raw.githubusercontent.com/autonomio/datasets/master/autonomio-datasets/'
df = pd.read_csv(base + 'breast_cancer.csv')
# then some minimal data cleanup
df.drop("Unnamed: 32", axis=1, inplace=True)
df.drop("id", axis=1, inplace=True)
# separate to x and y
y = df.diagnosis.values
x = df.drop('diagnosis', axis=1).values
# convert the string labels to binary
y = (y == 'M').astype(int)
return x, y
def mnist():
'''Note that this dataset, unlike other Talos datasets,returns:
x_train, y_train, x_val, y_val'''
import keras
import numpy as np
# the data, split between train and test sets
(x_train, y_train), (x_val, y_val) = keras.datasets.mnist.load_data()
# input image dimensions
img_rows, img_cols = 28, 28
if keras.backend.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_val = x_val.reshape(x_val.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_val = x_val.reshape(x_val.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_val = x_val.astype('float32')
x_train /= 255
x_val /= 255
classes = len(np.unique(y_train))
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, classes)
y_val = keras.utils.to_categorical(y_val, classes)
print("Use input_shape %s" % str(input_shape))
return x_train, y_train, x_val, y_val