-
Notifications
You must be signed in to change notification settings - Fork 1
/
loaddata.py
274 lines (211 loc) · 10 KB
/
loaddata.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import pickle
import os
import struct
import numpy as np
from numpy.random import randint
import math
def loaddata(data_dir, data):
"""
Load RAW DATA from DATA file
INPUT:
data_dir: directory of data file
data: name of the dataset
OUTPUT:
train_X, train_Y, test_X, test_Y
idx: half of the total classes.
(for transferring every dataset into binary classification)
"""
if data == 'cifar10':
idx = 5
train_X, train_Y, test_X, test_Y = cifar10(data_dir)
if data == 'mnist':
idx = 5
# total_C = 10
train_X, train_Y, test_X, test_Y = mnist(data_dir)
if data == 'arcene':
idx = 1
train_X, train_Y, test_X, test_Y = arcene(data_dir)
if data == 'gisette':
idx = 1
train_X, train_Y, test_X, test_Y = gisette(data_dir)
if data == 'hapt':
idx = 6
train_X, train_Y, test_X, test_Y = hapt(data_dir)
if data == 'drive_diagnostics':
idx = 6
train_X, train_Y, test_X, test_Y = drive_diagnostics(data_dir)
if data == 'BlogFeedback':
idx = 1
train_X, train_Y, test_X, test_Y = BlogFeedback(data_dir)
if data == 'housing':
idx = 20
train_X, train_Y, test_X, test_Y = housing(data_dir)
if data == 'forest_fire':
idx = 1
train_X, train_Y, test_X, test_Y = forest_fire(data_dir)
if data == 'power_plant':
idx = 450
train_X, train_Y, test_X, test_Y = power_plant(data_dir)
if data == 'covtype':
idx = 3
train_X, train_Y, test_X, test_Y = covtype(data_dir)
return train_X, train_Y, test_X, test_Y, idx
def unpickle(file):
with open(file, 'rb') as fo:
data = pickle.load(fo, encoding='latin-1')
return data
def unpickle_csv(file):
with open(file, 'rb') as fo:
data = np.loadtxt(fo, delimiter=",", skiprows=1)
return data
def cifar10(data_dir):
train_data = None
train_labels = []
for i in range(1, 6):
data_dic = unpickle(data_dir + "/cifar10/data_batch_{}".format(i))
if i == 1:
train_data = data_dic['data'].astype(np.float64)
else:
train_data = np.vstack((train_data, data_dic['data']))
train_labels += data_dic['labels']
test_data_dic = unpickle(data_dir + "/cifar10/test_batch")
test_data = test_data_dic['data'].astype(np.float64)
test_labels = test_data_dic['labels']
train_labels = np.array(train_labels)
test_labels = np.array(test_labels)
return train_data, train_labels, test_data, test_labels
def mnist(data_dir):
train_labels=os.path.abspath(data_dir + '/MNIST/train-labels-idx1-ubyte')
train_data=os.path.abspath(data_dir + '/MNIST/train-images-idx3-ubyte')
test_labels=os.path.abspath(data_dir + '/MNIST/t10k-labels-idx1-ubyte')
test_data=os.path.abspath(data_dir + '/MNIST/t10k-images-idx3-ubyte')
with open(train_labels, 'rb') as lbpath:
magic, n = struct.unpack('>II',lbpath.read(8))
train_labels = np.fromfile(lbpath,dtype=np.uint8)
with open(test_labels, 'rb') as lbpath:
magic, n = struct.unpack('>II',lbpath.read(8))
test_labels = np.fromfile(lbpath,dtype=np.uint8)
with open(train_data, 'rb') as imgpath:
magic, num, rows, cols = struct.unpack(">IIII",imgpath.read(16))
train_data = np.fromfile(imgpath,dtype=np.uint8).reshape(len(train_labels), 784)
with open(test_data, 'rb') as imgpath:
magic, num, rows, cols = struct.unpack(">IIII",imgpath.read(16))
test_data = np.fromfile(imgpath,dtype=np.uint8).reshape(len(test_labels), 784)
train_data = train_data.astype(np.float64)
test_data = test_data.astype(np.float64)
return train_data, train_labels, test_data, test_labels
# data_tf = transforms.Compose(
# [transforms.ToTensor(), transforms.Normalize([0.5], [0.5])])
# train_Set = datasets.MNIST(root='../Data', train=True,
# transform=data_tf, download=True)
# data_tf = transforms.Compose([transforms.ToTensor(), transforms.Normalize([0.5], [0.5])])
# train_dataset = datasets.MNIST(root='../Data', train=True,
# transform=data_tf, download=True)
# # train_loader = DataLoader(train_dataset, shuffle=True,
# # batch_size=128, drop_last=True)
# # index = np.random.choice(60000, 1000, replace = False)
# # train_X = train_X[index,:]
# # train_dataset.data = train_dataset.data[index,:]
# train_loader = DataLoader(train_dataset, shuffle=False,
# batch_size=1000, drop_last=True)
# # return train_loader
# # train_Set = datasets.MNIST(data_dir, train=True,
# # transform=transforms.ToTensor())
# # # train_loader = DataLoader(train_Set)
# test_Set = datasets.MNIST(data_dir, train=False,
# transform=transforms.ToTensor())
# return train_loader, test_Set
def arcene(data_dir):
train_data = np.loadtxt(data_dir + "/arcene/arcene_train.data")
train_data = np.asarray(train_data, order='f', dtype=np.float64)
train_labels = np.loadtxt(data_dir + "/arcene/arcene_train.labels")
test_data = np.loadtxt(data_dir + "/arcene/arcene_valid.data")
test_labels = np.loadtxt(data_dir + "/arcene/arcene_valid.labels")
test_data = np.asarray(test_data, dtype=np.float32)
return train_data, train_labels, test_data, test_labels
def gisette(data_dir):
train_data = np.loadtxt(data_dir + "/gisette/gisette_train.data")
train_data = np.asarray(train_data, order='f', dtype=np.float64)
train_labels = np.loadtxt(data_dir + "/gisette/gisette_train.labels")
test_data = np.loadtxt(data_dir + "/gisette/gisette_valid.data")
test_labels = np.loadtxt(data_dir + "/gisette/gisette_valid.labels")
test_data = np.asarray(test_data, dtype=np.float32)
return train_data, train_labels, test_data, test_labels
def hapt(data_dir):
train_data = np.loadtxt(data_dir + "/hapt/Train/X_train.txt")
train_labels = np.loadtxt(data_dir + "/hapt/Train/Y_train.txt")
test_data = np.loadtxt(data_dir + "/hapt/Test/x_test.txt")
test_labels = np.loadtxt(data_dir + "/hapt/Test/y_test.txt")
return train_data, train_labels-1, test_data, test_labels-1
def drive_diagnostics(data_dir):
train_data = np.loadtxt(data_dir + "/drive_diagnostics/train_mat.txt")
train_labels = np.loadtxt(data_dir + "/drive_diagnostics/train_vec.txt")
test_data = np.loadtxt(data_dir + "/drive_diagnostics/test_mat.txt")
test_labels = np.loadtxt(data_dir + "/drive_diagnostics/test_vec.txt")
return train_data, train_labels, test_data, test_labels
def covtype(data_dir):
raw_data = np.loadtxt(data_dir + "/CoveType/covtype.data", delimiter=',')
# raw_data = np.loadtxt(data_dir + "/CoveType/covtype.txt", delimiter=',')
train_raw = raw_data[:, 0:-1]
labels_raw = raw_data[:, -1] - 1
test_index = randint(len(labels_raw), size = math.ceil(len(labels_raw)/4))
train_index =np.setdiff1d(np.arange(len(labels_raw)),test_index)
train_data = train_raw[train_index, :]
train_labels = labels_raw[train_index]
# train_labels = train_labels[:,0:-1]
test_data = train_raw[test_index, :]
test_labels = labels_raw[test_index]
return train_data, train_labels, test_data, test_labels
def housing(data_dir):
raw_data = unpickle_csv(data_dir + "/housing/housing.csv")
train_raw = raw_data[:, 0:-1]
labels_raw = raw_data[:, -1]
test_index = randint(len(labels_raw), size = math.ceil(len(labels_raw)/10))
train_index =np.setdiff1d(np.arange(len(labels_raw)),test_index)
train_data = train_raw[train_index, :]
train_labels = labels_raw[train_index]
test_data = train_raw[test_index, :]
test_labels = labels_raw[test_index]
return train_data, train_labels, test_data, test_labels
def BlogFeedback(data_dir):
raw_data = unpickle_csv(data_dir + "/BlogFeedback/blogData_train.csv")
train_raw = raw_data[:, 0:-1]
labels_raw = raw_data[:, -1]
test_index = randint(len(labels_raw), size = math.ceil(len(labels_raw)/10))
train_index =np.setdiff1d(np.arange(len(labels_raw)),test_index)
train_data = train_raw[train_index, :]
train_labels = labels_raw[train_index]
test_data = train_raw[test_index, :]
test_labels = labels_raw[test_index]
return train_data, train_labels, test_data, test_labels
def forest_fire(data_dir):
raw_data = unpickle_csv(data_dir + "/forest_fire/forestfires.csv")
train_raw = raw_data[:, 0:-1]
labels_raw = raw_data[:, -1]
test_index = randint(len(labels_raw), size = math.ceil(len(labels_raw)/10))
train_index =np.setdiff1d(np.arange(len(labels_raw)),test_index)
train_data = train_raw[train_index, :]
train_labels = labels_raw[train_index]
test_data = train_raw[test_index, :]
test_labels = labels_raw[test_index]
return train_data, train_labels, test_data, test_labels
def power_plant(data_dir):
raw_data = unpickle_csv(data_dir + "/CCPP/power_plant.csv")
train_raw = raw_data[:, 0:-1]
labels_raw = raw_data[:, -1]
test_index = randint(len(labels_raw), size = math.ceil(len(labels_raw)/10))
train_index =np.setdiff1d(np.arange(len(labels_raw)),test_index)
train_data = train_raw[train_index, :]
train_labels = labels_raw[train_index]
test_data = train_raw[test_index, :]
test_labels = labels_raw[test_index]
return train_data, train_labels, test_data, test_labels
def main():
data_dir = 'Data'
X_train, y_train, X_test, y_test = covtype(data_dir)
print(X_train.shape,X_train.dtype) #(60000,784) uint8
print(y_train.shape,y_train.dtype) #(60000,) uint8
print(X_test.shape,X_test.dtype) #(10000,784) uint8
print(np.mean(y_test),y_test.dtype) #(10000,) uint8
if __name__ == '__main__':
main()