-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreprocess.py
More file actions
65 lines (59 loc) · 1.95 KB
/
preprocess.py
File metadata and controls
65 lines (59 loc) · 1.95 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
import sys, getopt
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from utilities import csv_to_dataframe, write_dataframe_to_file
def preprocess(dataframe):
'''
Method to preprocess the advanced Data Mining
Techniques dataset
Parameters
----------
dataframe : pandas dataframe
input dataframe to be preprocessed
Returns
-------
dataframe : pandas dataframe
preprocessed dataframe
Raises
------
None
'''
#drop this useless column
dataframe = dataframe.drop('Unnamed: 0', axis=1)
#put the time column to pandas Datetime
dataframe.time = pd.to_datetime(dataframe.time)
#Search for missing and null values
dataframe = dataframe.dropna(subset=['id', 'time', 'variable', 'value'], \
how='any')
#scale the inputs in range 0->1 for each variable
scaler = MinMaxScaler()
for x in dataframe.variable.unique():
if not x in ['call', 'sms', 'activity', 'mood']:
dataframe.value[dataframe.variable == x] = \
scaler.fit_transform(dataframe.value[dataframe.variable == x] \
.values.reshape(-1,1))
else:
continue
dataframe = dataframe.set_index(dataframe.time)
dataframe = dataframe.drop('time', axis=1)
return dataframe
def main(argv):
try:
opts, args = getopt.getopt(argv, "h:c:", ["csv_name="])
except getopt.GetoptError:
print('preprocess.py -c <csv_name>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('preprocess.py -c <csv_name>')
sys.exit()
elif opt in ("-c"):
csv_name = arg
print('')
dataframe = csv_to_dataframe(csv_name)
new_df = preprocess(dataframe)
write_dataframe_to_file('rnn_dataframes/{}_preprocessed.pkl'\
.format(csv_name), new_df)
if __name__ == "__main__":
main(sys.argv[1:])