-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.py
More file actions
76 lines (47 loc) · 2.17 KB
/
Copy pathsample.py
File metadata and controls
76 lines (47 loc) · 2.17 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
import copy
from typing import List, Union
from datapoint import DataPoint
class Sample:
datapoints: List[DataPoint] = None
associated_attributes: List[str] or None = None
classlabel: str or None = None
def __init__(self, datapoints: List[DataPoint], associated_attributes: List[str] = None, classlabel: str = None):
self.datapoints = datapoints
self.associated_attributes = associated_attributes
self.classlabel = classlabel
@classmethod
def from_values(cls, values: List[float], associated_attributes: List[str], classlabel: str) -> 'Sample':
datapoints: List[DataPoint] = list()
for i in range(0, len(values)):
datapoints.append(DataPoint(values[i], associated_attributes[i], classlabel))
return cls(datapoints, associated_attributes=associated_attributes, classlabel=classlabel)
def length(self) -> int:
return len(self.datapoints)
def get(self, index: int) -> DataPoint:
return self.datapoints[index]
def get_datapoints(self, indices: List[int]) -> List[DataPoint]:
datapoints: List[DataPoint] = list()
for index in indices:
datapoints.append(copy.deepcopy(self.datapoints[index]))
return datapoints
def get_values(self, append_classlabel: bool = False) -> List[float] or List[Union[float, str]]:
values: List[float] or List[Union[float, str]] = list()
for datapoint in self.datapoints:
values.append(datapoint.value)
if append_classlabel:
values.append(self.classlabel)
return values
def set(self, datapoint: DataPoint, index: int):
self.datapoints[index] = datapoint
def set_value(self, value: float, index: int):
self.datapoints[index].value = value
def reset_attributes(self, new_attributes: List[str]):
self.associated_attributes = copy.deepcopy(new_attributes)
for i in range(0, len(new_attributes)):
self.datapoints[i].associated_attribute = new_attributes[i]
def subsample(self, origin: int = 0, bound: int = None):
if bound is None:
bound = self.length()
datapoints: List[DataPoint] = copy.deepcopy(self.datapoints[origin:bound])
attributes: List[str] = copy.deepcopy(self.associated_attributes[origin:bound])
return Sample(datapoints, associated_attributes=attributes, classlabel=self.classlabel)