-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataCleaning.py
136 lines (113 loc) · 4.79 KB
/
DataCleaning.py
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
import pandas as pd
import numpy as np
import random
def setRandom(seed):
random.seed(seed)
def clean_survey(data):
df = data.fillna(value=3)
df = (df.loc[(df!=0).all(axis=1)]-3)
return df
def clean_data(data):
#remove unwanted columns
data.pop("age")
data.pop("gender")
data.pop("accuracy")
data.pop("country")
data.pop("source")
data.pop("elapsed")
data.pop("P10")
#replace zeros with three, questions not answered replaced with average value
df = (data.loc[(data!=0).all(axis=1)]-3)
for i in range(-2, 3):
df = df.loc[~(df==i).all(axis=1)]
return df
def split_strat(data, num_sets = 3, keys = []):
# general idea is to split the data by question type letter(A), and then randomly select 1/3 of each letter to form
# three mixed data sets
# creating a dict of data frames for each letter in ASCII
for i in range(1, num_sets - len(keys)+1):
keys.append(f'set{str(i)}')
columnsList = list(data)
letterDict = {}
for i in range (65, 81):
letterDict[i] = []
for i in range(65,81):
for question in columnsList:
if chr(i) in question:
letterDict[i].append(question)
for i in range (65, 81):
random.shuffle(letterDict[i])
# creating a dict containing three different data frames for eat data "set"
setsDict = {}
for k in keys:
setsDict[k] = []
# test code: works and prints the data set containing all A questions/answers
#print (framesDict[65])
# shuffles each dataframe so it's randomized when columns are later selected
# go through each data frame, resetting a and b each time
remainders = []
for i in range (65, 81):
cols = len(letterDict[i])
increment = int((cols - cols%num_sets) /num_sets)
remainders += letterDict[i][(cols//num_sets) * num_sets:]
a = 0
b = increment
for s in keys:
# print(setsDict[s]) # go through each of the three sets
# print(framesDict[i].iloc[:,a:b])
setsDict[s] += letterDict[i][a:b] # and add on values from range a-b in the current letter/question dataframe
# print(setsDict[s])
a += increment # ^^^^ this line is probably the problem
b += increment # then increment a and b before moving on to next set
cols = len(remainders)
increment = int((cols - cols%num_sets) /num_sets)
a = 0
b= increment
for s in keys:
# print(setsDict[s]) # go through each of the three sets
# print(framesDict[i].iloc[:,a:b])
setsDict[s] += letterDict[i][a:b] # and add on values from range a-b in the current letter/question dataframe
# print(setsDict[s])
a += increment # ^^^^ this line is probably the problem
b += increment # then increment a and b before moving on to next set
return setsDict
def split_n_strat(data, num_qa = 20):
# general idea is to split the data by question type letter(A), and then randomly select 1/3 of each letter to form
# three mixed data sets
# creating a dict of data frames for each letter in ASCII
columnsList = list(data)
letterDict = {}
for i in range (65, 81):
letterDict[i] = []
for i in range(65,81):
for question in columnsList:
if chr(i) in question:
letterDict[i].append(question)
for i in range (65, 81):
random.shuffle(letterDict[i])
# creating a dict containing three different data frames for eat data "set"
setQs = []
while len(setQs) < num_qa:
for letter in letterDict:
if letterDict[letter]:
setQs.append(letterDict[letter].pop())
if len(setQs) >= num_qa:
break
return setQs
def split(data, split_labels = None):
if not split_labels:
labels = list(data)
random.shuffle(labels)
split_labels = {"set1": labels[:50], "set2": labels[50:100], "set3": labels[100:150]}
return {k: data[s] for k, s in split_labels.items()}
def make_train_test(data, split):
train_data = {k: data[k][:int(data[k].shape[0]*split)] for k in data}
test_data = {k: data[k][int(data[k].shape[0]*split):] for k in data}
return train_data, test_data
def get_input_dims(train_data):
return {k: (np.array(train_data[k]).astype(float))[0].shape[0] for k in train_data}
def preprocessing(data, num_sets, split, keys):
final_data = clean_data(data)
final_data = split_strat(final_data,num_sets)
train, test = make_train_test(final_data, 0.8)
return train, test