-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
70 lines (47 loc) · 1.48 KB
/
parser.py
File metadata and controls
70 lines (47 loc) · 1.48 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
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import StandardScaler
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--path","-p")
parser.add_argument("--target", "-t")
parsed = parser.parse_args()
le = LabelEncoder()
scaler = StandardScaler()
path = parsed.path
target_name = parsed.target
def read_dataset(path):
return pd.read_csv(path)
def inspect_column(df):
columns = list(df.columns)
columns.remove(target_name)
columns_to_drop = []
columns_to_encode = []
for column in columns:
if df[column].unique() == 1:
columns_to_drop.append(column)
elif (df[column].unique <= 5 ) and (df[column].unique >= 1):
columns_to_encode.append(column)
else:
df[column] = le.fit_transform(df[column])
print("columns_to_encode" , columns_to_encode)
print("columns_to_drop" , columns_to_drop)
df.drop(labels = columns , axis = 1 , inplace = True)
df = pd.get_dummies(df , columns = columns_to_encode ,prefix_sep="__" , drop_first=True)
return df
def do_scale(df):
columns = list(df.columns)
columns.remove(target_name)
for column in columns:
df[column] = scaler.fit_transform(df[[column]])
return df
def save_df(df):
df.to_csv("output.csv")
print("saved")
def main():
df = read_dataset(path)
df = inspect_column(df)
df = do_scale(df)
save_df(df)
if __name__ == "__main__":
main()