-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreprocessor.py
More file actions
70 lines (54 loc) · 1.72 KB
/
Preprocessor.py
File metadata and controls
70 lines (54 loc) · 1.72 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
import os
import numpy as np
import pandas as pd
class Preprocessor:
'''
Preprocessor is a class containing various abstracted methods of converting raw data into the desired data structures to feed into MachineLearningModel class (e.g. X and Y data points).
Args:
datapath (str) : file path to the data as a string, MUST be relative to this file!
Attributes:
self.datapath : the current working directory joined with <datapath>
'''
def __init__(self, datapath):
self.datapath = os.path.join(os.getcwd(), datapath)
print("Initializing Preprocessor...")
print(f"Loaded data from: {self.datapath}")
def load_from_txt(self, delimiter=","):
'''
A method of Preprocessor
loads data into numpy array from .txt file
Args:
delimiter (str) : the delimiter as a string, default is ","
Returns:
self
'''
self.data = np.loadtxt(self.datapath, delimiter)
return self
def load_from_csv(self, header=None):
'''
A method of Preprocessor
loads data into numpy array from .csv file
Args:
header (bool) : boolean True, False or None, default is None
Returns:
self
'''
if header == None: dataframe = pd.read_csv(self.datapath)
else: dataframe = pd.read_csv(self.datapath, header)
self.data = np.array(dataframe)
return self
def get_XY(self):
'''
A method of Preprocessor
splits self.data into X and Y
Returns:
self.X, self.Y
'''
self.X = self.data[:, :-1]
self.Y = self.data[:, -1]
return self
if __name__ == "__main__":
datapath = os.path.join("data", "for_perceptron", "train.csv")
data = Preprocessor(datapath).load_from_csv().get_XY()
print(f"X: {data.X}")
print(f"Y: {data.Y}")