-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDataSet.py
More file actions
334 lines (290 loc) · 11.8 KB
/
DataSet.py
File metadata and controls
334 lines (290 loc) · 11.8 KB
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
from __future__ import annotations
from Classification.FeatureSelection.FeatureSubSet import FeatureSubSet
from Classification.InstanceList.InstanceList import InstanceList
from Classification.DataSet.DataDefinition import DataDefinition
from Classification.Attribute.AttributeType import AttributeType
from Classification.Instance.Instance import Instance
from Classification.Instance.CompositeInstance import CompositeInstance
from Classification.Attribute.ContinuousAttribute import ContinuousAttribute
from Classification.Attribute.DiscreteAttribute import DiscreteAttribute
from Classification.Attribute.BinaryAttribute import BinaryAttribute
from Classification.Attribute.DiscreteIndexedAttribute import DiscreteIndexedAttribute
from Classification.InstanceList.Partition import Partition
class DataSet(object):
__instances: InstanceList
__definition: DataDefinition
def __init__(self,
definition: DataDefinition = None,
separator: str = None,
fileName: str = None):
"""
Constructor for generating a new DataSet with given DataDefinition.
PARAMETERS
----------
definition : DataDefinition
Data definition of the data set.
separator : str
Separator character which separates the attribute values in the data file.
fileName : str
Name of the data set file.
"""
self.__definition = definition
if separator is None:
self.__instances = InstanceList()
else:
self.__instances = InstanceList(listOrDefinition=definition,
separator=separator,
fileName=fileName)
def initWithFile(self, fileName: str):
"""
Constructor for generating a new DataSet from given File.
PARAMETERS
----------
fileName : str
File to generate DataSet from.
"""
self.__instances = InstanceList()
self.__definition = DataDefinition()
input_file = open(fileName, 'r', encoding='utf8')
lines = input_file.readlines()
input_file.close()
i = 0
for line in lines:
attributes = line.split(",")
if i == 0:
for j in range(len(attributes) - 1):
try:
float(attributes[j])
self.__definition.addAttribute(AttributeType.CONTINUOUS)
except:
self.__definition.addAttribute(AttributeType.DISCRETE)
else:
if len(attributes) != self.__definition.attributeCount() + 1:
continue
if ";" not in attributes[len(attributes) - 1]:
instance = Instance(attributes[len(attributes) - 1])
else:
labels = attributes[len(attributes) - 1].split(";")
instance = CompositeInstance(labels[0], None, labels)
for j in range(len(attributes) - 1):
if self.__definition.getAttributeType(j) is AttributeType.CONTINUOUS:
instance.addAttribute(ContinuousAttribute(float(attributes[j])))
elif self.__definition.getAttributeType(j) is AttributeType.DISCRETE:
instance.addAttribute(DiscreteAttribute(attributes[j]))
if instance.attributeSize() == self.__definition.attributeCount():
self.__instances.add(instance)
i = i + 1
def __checkDefinition(self, instance: Instance) -> bool:
"""
Checks the correctness of the attribute type, for instance, if the attribute of given instance is a Binary
attribute, and the attribute type of the corresponding item of the data definition is also a Binary attribute,
it then returns true, and false otherwise.
PARAMETERS
----------
instance : Instance
Instance to checks the attribute type.
RETURNS
-------
bool
true if attribute types of given Instance and data definition matches.
"""
for i in range(instance.attributeSize()):
if isinstance(instance.getAttribute(i), BinaryAttribute):
if self.__definition.getAttributeType(i) is not AttributeType.BINARY:
return False
elif isinstance(instance.getAttribute(i), DiscreteIndexedAttribute):
if self.__definition.getAttributeType(i) is not AttributeType.DISCRETE_INDEXED:
return False
elif isinstance(instance.getAttribute(i), DiscreteAttribute):
if self.__definition.getAttributeType(i) is not AttributeType.DISCRETE:
return False
elif isinstance(instance.getAttribute(i), ContinuousAttribute):
if self.__definition.getAttributeType(i) is not AttributeType.CONTINUOUS:
return False
return True
def __setDefinition(self, instance: Instance):
"""
Adds the attribute types according to given Instance. For instance, if the attribute type of given Instance
is a Discrete type, it than adds a discrete attribute type to the list of attribute types.
PARAMETERS
----------
instance : Instance
Instance input.
"""
attribute_types = []
for i in range(instance.attributeSize()):
if isinstance(instance.getAttribute(i), BinaryAttribute):
attribute_types.append(AttributeType.BINARY)
elif isinstance(instance.getAttribute(i), DiscreteIndexedAttribute):
attribute_types.append(AttributeType.DISCRETE_INDEXED)
elif isinstance(instance.getAttribute(i), DiscreteAttribute):
attribute_types.append(AttributeType.DISCRETE)
elif isinstance(instance.getAttribute(i), ContinuousAttribute):
attribute_types.append(AttributeType.CONTINUOUS)
self.__definition = DataDefinition(attribute_types)
def sampleSize(self) -> int:
"""
Returns the size of the InstanceList.
RETURNS
-------
int
Size of the InstanceList.
"""
return self.__instances.size()
def classCount(self) -> int:
"""
Returns the size of the class label distribution of InstanceList.
RETURNS
-------
int
Size of the class label distribution of InstanceList.
"""
return len(self.__instances.classDistribution())
def attributeCount(self) -> int:
"""
Returns the number of attribute types at DataDefinition list.
RETURNS
-------
int
The number of attribute types at DataDefinition list.
"""
return self.__definition.attributeCount()
def discreteAttributeCount(self) -> int:
"""
Returns the number of discrete attribute types at DataDefinition list.
RETURNS
-------
int
The number of discrete attribute types at DataDefinition list.
"""
return self.__definition.discreteAttributeCount()
def continuousAttributeCount(self) -> int:
"""
Returns the number of continuous attribute types at DataDefinition list.
RETURNS
-------
int
The number of continuous attribute types at DataDefinition list.
"""
return self.__definition.continuousAttributeCount()
def getClasses(self) -> str:
"""
Returns the accumulated String of class labels of the InstanceList.
RETURNS
-------
str
The accumulated String of class labels of the InstanceList.
"""
class_labels = self.__instances.getDistinctClassLabels()
result = class_labels[0]
for i in range(1, len(class_labels)):
result = result + ";" + class_labels[i]
return result
def info(self, dataSetName: str) -> str:
"""
Returns the general information about the given data set such as the number of instances, distinct class labels,
attributes, discrete and continuous attributes.
PARAMETERS
----------
dataSetName : str
Data set name.
RETURNS
-------
str
General information about the given data set.
"""
result = "DATASET: " + dataSetName + "\n"
result = result + "Number of instances: " + self.sampleSize().__str__() + "\n"
result = result + "Number of distinct class labels: " + self.classCount().__str__() + "\n"
result = result + "Number of attributes: " + self.attributeCount().__str__() + "\n"
result = result + "Number of discrete attributes: " + self.discreteAttributeCount().__str__() + "\n"
result = result + "Number of continuous attributes: " + self.continuousAttributeCount().__str__() + "\n"
result = result + "Class labels: " + self.getClasses()
return result
def addInstance(self, current: Instance):
"""
Adds a new instance to the InstanceList.
PARAMETERS
----------
current : Instance
Instance to add.
"""
if self.__definition is None:
self.__setDefinition(current)
self.__instances.add(current)
elif self.__checkDefinition(current):
self.__instances.add(current)
def addInstanceList(self, instanceList: list):
"""
Adds all the instances of given instance list to the InstanceList.
PARAMETERS
----------
instanceList : list
InstanceList to add instances from.
"""
for instance in instanceList:
self.addInstance(instance)
def getInstances(self) -> list:
"""
Returns the instances of InstanceList.
RETURNS
-------
list
The instances of InstanceList.
"""
return self.__instances.getInstances()
def getClassInstances(self) -> list:
"""
Returns instances of the items at the list of instance lists from the partitions.
RETURNS
-------
list
Instances of the items at the list of instance lists from the partitions.
"""
return Partition(self.__instances).getLists()
def getInstanceList(self) -> InstanceList:
"""
Accessor for the InstanceList.
RETURNS
-------
InstanceList
The InstanceList.
"""
return self.__instances
def getDataDefinition(self) -> DataDefinition:
"""
Accessor for the data definition.
RETURNS
-------
DataDefinition
The data definition.
"""
return self.__definition
def getSubSetOfFeatures(self, featureSubSet: FeatureSubSet) -> DataSet:
"""
Return a subset generated via the given FeatureSubSet.
PARAMETERS
----------
featureSubSet : FeatureSubSet
FeatureSubSet input.
RETURNS
-------
FeatureSubSet
Subset generated via the given FeatureSubSet.
"""
result = DataSet(self.__definition.getSubSetOfFeatures(featureSubSet))
for i in range(self.__instances.size()):
result.addInstance(self.__instances.get(i).getSubSetOfFeatures(featureSubSet))
return result
def writeToFile(self, outFileName: str):
"""
Print out the instances of InstanceList as a String.
PARAMETERS
----------
outFileName : str
File name to write the output.
"""
out_file = open(outFileName, "w")
for i in range(self.__instances.size()):
out_file.write(self.__instances.get(i).__str__() + "\n")
out_file.close()