-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
540 lines (471 loc) · 14.1 KB
/
dataset.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
import random
import csv
import math
from itertools import groupby
class Dataset():
def __init__(self, data, target_index, columns_names, name):
"""Method create Dataset item
Args:
data (list): list of table rows
target_index (int): index of column with target attribute
columns_names (list): list with columns names
"""
# Check data
if data is None:
raise ValueError("Data in None!")
self._data = data
# Check target_index for valid index
if target_index >= self.get_columns_number():
raise ValueError(f"Target index out of range! Got {target_index}, columns number: {self.get_columns_number()}")
self._target_index = target_index
# Check is number of columns names same as columns in data
# If not raise exception
if len(columns_names) != self.get_columns_number():
raise ValueError(f"Wrong columns names number! Got {len(columns_names)}, columns number: {self.get_columns_number()}")
self._columns_names = columns_names
self._name = name
# TODO pathlib
@staticmethod
def get_iris(path=None):
"""Method return iris dataset
by default dataset must be available
by path "resources\\data\\iris\\iris.data"
link to dataset "http://archive.ics.uci.edu/ml/datasets/Iris/"
Args:
path (string): Path to file with data
Returns:
Dataset: Dataset object based on iris data set
"""
# If path not specify
if path is None:
path = "resources\\data\\iris\\iris.data"
# Open file as csv
csv_reader = csv.reader(open(path), delimiter=",")
# Save data to list
data = []
# For each row in file
for row in csv_reader:
temp = []
# Read first 4 attributes as float
for i in range(4):
temp.append(float(row[i]))
# Read last attribute as string
temp.append(row[4])
data.append(temp)
# Set attributes names
names = [
"Sepal length", "Sepal width",
"Petal length", "Petal width",
"Class"]
# Create Dataset
dataset = Dataset(data, 4, names, "Iris")
# return Dataset
return dataset
# TODO pathlib
@staticmethod
def get_tennis(path=None):
"""Method return tennis dataset
by default dataset must be available
by path "resources\\data\\tennis\\tennis.data"
link to dataset "http://archive.ics.uci.edu/ml/datasets/Iris/"
Args:
path (string): Path to file with data
Returns:
Dataset: Dataset object based on tennis data set
"""
# If path not specify
if path is None:
path = "resources\\data\\tennis\\tennis.data"
# Open file as csv
csv_reader = csv.reader(open(path), delimiter=",")
# Save data to list
data = [row for row in csv_reader]
# Set attributes names
names = [
"Outlook", "Temperature",
"Humidity", "Wind",
"PlayTennis"]
# Create Dataset
dataset = Dataset(data, 4, names, "Tennis")
# return Dataset
return dataset
def print(self, rows_number=None):
"""Method prints the dataset in console
Args:
rows_number (int): number of rows to print
"""
# If number of rows to print not specify
if rows_number is None:
# Set number of rows in dataset
rows_number = self.get_rows_number()
# Print dataset name
print(f"Name = {self._name}")
# Print name of target attributes
print(f"Target = {self._columns_names[self._target_index]}")
# Print column columns_names
for name in self._columns_names:
# Center align of text
print("{:^15}".format(name), end=" | ")
# Go to new line
print()
# Print data as table
for i in range(min([rows_number, self.get_rows_number()])):
for attribute in self.data[i]:
# Center align of text
print("{:^15}".format(attribute), end=" | ")
# Go to new line
print()
@property
def data(self):
return [row.copy() for row in self._data]
@property
def target(self):
return self._target_index
@target.setter
def target(self, new_target_index):
# If index is valid
if self.is_column_index_correct(new_target_index):
# Set new value
self._target_index = new_target_index
@property
def name(self):
return self._name
@name.setter
def name(self, new_name):
self._name = new_name
def is_column_index_correct(self, column_index):
"""Method check is column_index is valid
Args:
column_index (int): index to check
Retuns:
false: index is invalid
true: index is valid
"""
# Check is index valid
if column_index > self.get_columns_number() or column_index < 0:
# If not valid return false
return False
# Index is valid return true
return True
def get_row(self, index):
"""Method return row by index
Args:
index (int): index of row.
Must be large than 0 and less than rows number in dataset
Returns:
list: row
"""
# Check is index valid
if index > self.get_rows_number() or index < 0:
return None
# Return copy of the row
return data[index].copy()
def get_column(self, column_index):
"""Method returns values of required column
Args:
index (int): index of column to get values from
Returns:
list: values from requested column
None: if index is incorrect
"""
# Check is index invalid
if not self.is_column_index_correct(column_index):
return None
# Return column
return [row[column_index] for row in self._data]
def get_target_column(self):
"""Method returns list of values from column with target attributes
Returns:
list: values from target attributes
"""
# Return target column
return self.get_column(self._target_index)
def get_rows_number(self):
"""Method returns number of rows in dataset
Returns:
int: number of rows
"""
return len(self._data)
def get_columns_number(self):
"""Method returns number of columns in dataset
Returns:
int: number of columns
"""
return len(self._data[0])
def get_name(self, column_index):
"""Method return column name
Args:
column_index (int): index of column to get name from
Returns:
string: name of specific column
None: if column_index is incorrect
"""
# Check is index invalid
if not self.is_column_index_correct(column_index):
return None
# Return column name
return self._columns_names[column_index]
def remove_column(self, column_index):
"""Method remove specific column from dataset
Args:
column_indes (int): index of column to remove
Returns:
None: if column_index is incorrect
or if try to remove target column
"""
# Check is index invalid
if not self.is_column_index_correct(column_index):
return None
# Check to remove not target column
if column_index == self.target:
return None
# Move target column
if column_index < self.target:
self.target -= 1
# Delete column name
self._columns_names.pop(column_index)
# In each row remove specific attribute
for row in self._data:
row.pop(column_index)
def split_by_predicate(self, column_index, predicate):
"""Method split dataset by specific column and predicate
Args:
column_index (int): index of column by which split
predicate (predicate): function by which split
takes two arguments (row (list), column_index (int))
Returns:
(list, list): first list is list of datasets, second list values of column by which split
None: if column_index is incorrect
"""
# Check is index invalid
if not self.is_column_index_correct(column_index):
return None
# Unpacking predicate
unpacking_predicate = lambda row: predicate(row, column_index)
# Group data using this predicate
datas = groupby(sorted(self.data, key=unpacking_predicate), key=unpacking_predicate)
# Separate grouped datas on key value and data
splitted_data = []
keys = []
for key, value in datas:
# Save keys values
keys.append(key)
# Convert to list
splitted_data.append(list(value))
# Convert data to Datasets objects
datasets = [
Dataset(splitted_data[i], self._target_index, self._columns_names.copy(), self._name)
for i in range(len(splitted_data))]
# Return datasets and key values
return datasets, keys
def split_by_ratio(self, ratio):
"""Method split dataset into training and test parts with given ratio
Args:
ratio (float): percentage of training part in dataset. In range [0, 1]
Returns:
tuple: consists of training and test Dataset
"""
# Calculate length of first part of dataset
TRAIN_DATASET_LEN = round(len(self._data) * ratio)
# Create list of values for first dataset
first = [
self._data[i].copy()
for i in range(TRAIN_DATASET_LEN)]
# Create list of values for second dataset
second = [
self._data[i].copy()
for i in range(TRAIN_DATASET_LEN, self.get_rows_number())]
# Create two datasets and return them
return (Dataset(first, self._target_index, self._columns_names, self._name),
Dataset(second, self._target_index, self._columns_names, self._name))
def shuffle(self):
"""Method shuffle rows in dataset
Returns:
Dataset: Dataset with shuffled rows
"""
# Get data
data = self.data
# Shuffle data
random.shuffle(data)
# Create new dataset on shuffled data
return Dataset(data, self._target_index, self._columns_names, self._name)
# TODO different variants of normalization
# for date type and etc.
def normalize(self, column_index):
"""Method normalize the specific column
Args:
column_index (int): index of column to normalize
Returns:
None: if column_index is incorrect
"""
# Check is index invalid
if not self.is_column_index_correct(column_index):
return None
# Find minimum value in column
minimum = min(self.get_column(column_index))
# Find maximum value in column
maximum = max(self.get_column(column_index))
# For each value on column
for row in self._data:
# Calculate new value
row[column_index] = (row[column_index] - minimum) / (maximum - minimum)
def threshold(self, column_index, method=None):
"""Method thresholds specific column by specific method
thresholds means change values to 0 and 1
according to threshold value
0 - less then threshold value
1 - otherwise
Args:
column_index (int): index of column to threshold
method (string): method which used to find threshold value
can ve "median" or "gain"
Returns:
threshold (float): value by which thresholds
None: if column_index is incorrect
"""
# Check is index invalid
if not self.is_column_index_correct(column_index):
return None
threshold = None
# Finding threshold acording to the method
if method is None or method == "median":
threshold = self._find_threshold_median(column_index)
elif method == "gain":
threshold = self._find_threshold_gain(column_index)
# If got unknown method
else:
return None
# Use treshold to change column values
for row in self._data:
if row[column_index] < threshold:
row[column_index] = 0
else:
row[column_index] = 1
# Return threshold value
return threshold
def _find_threshold_median(self, column_index):
"""Method return median value of the specific column
Args:
column_index (int): index of column to find median
Returns:
median (float): median value of column
None: if column_index is incorrect
"""
# Check is index invalid
if not self.is_column_index_correct(column_index):
return None
# Get column
column = self.get_column(column_index)
# Sort
column.sort()
# Find median index
med = int(self.get_rows_number() / 2)
# Return median value
return column[med]
def _find_threshold_gain(self, column_index):
"""Method return threshold of column
find using information gain
Args:
column_index (int): index of column to find threshold
Returns:
threshold (float): threshold value of column
None: if column_index is incorrect
"""
# Check is index invalid
if not self.is_column_index_correct(column_index):
return None
column = self.get_column(column_index)
target = self.get_target_column()
# Concat column and target column
# Convert to list of turples
pairs = list(zip(column, target))
# Sort pairs
pairs.sort()
# Find thresholds
thresholds = [
# Find average
(pairs[i - 1][0] + pairs[i][0]) / 2
# For each element in pairs, starting from second
for i in range(1, len(column))
# If target value changed
if pairs[i - 1][1] != pairs[i][1]]
# Remove duplicates
thresholds = list(set(thresholds))
# Calculate gain for each threshold
gains = [
Dataset.gain(
self.get_column(column_index),
self.get_target_column(),
lambda x, y: x < threshold)
for threshold in thresholds]
# Find index of max gain
index = gains.index(max(gains))
# Return thresholds with max gain
return thresholds[index]
@staticmethod
def entropy(column):
"""Method calculate column entropy
Args:
column (List): list of values
Returns:
float: entropy of column. In the range [0, 1]
None: if column is None
"""
# If column is None
if column == None:
# Return None
return None
# Group column by identity
grouped = groupby(sorted(column))
# Count rows number
rows_number = len(column)
# Count number of clases
n = len(set(column))
# If have only one class, in this case entropy equals 0
if n == 1:
# Return 0
return 0
# Calculate entropy
output = 0
# For each group
for _, value in grouped:
# Get list of values
v = list(value)
# Calculate
output -= len(v) / rows_number * math.log(len(v)/rows_number, n)
# Return entropy
return output
@staticmethod
def gain(column, target_column, predicate=None):
"""Method calculates gain of specific column
Args:
index (int): index of column
predicate (function(x, y)): function that takes two arguments,
used to group column
Return:
float: gain of column
None: if index is wrong
"""
# Concat column and target column
pairs = zip(column, target_column)
# If predicate doesn't specified
if predicate == None:
unpacking_predicate = lambda x: x[0]
# Otherwise
else:
# Unpacking predicate
unpacking_predicate = lambda x: predicate(*x)
# Group by predicate
grouped = groupby(sorted(pairs, key=unpacking_predicate), unpacking_predicate)
# Count row number
rows_number = len(column)
# Count entropy of target column
output = Dataset.entropy(target_column)
# For each group
for _, value in grouped:
# Get list of values
v = list(value)
# Calculate
output -= len(v) / rows_number * Dataset.entropy(v)
# Return entropy
return output