-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_data.py
172 lines (145 loc) · 4.63 KB
/
load_data.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import random
# data manipulation
# data = [
# [anser[0], anser[1], ...]
# [same]
# [same]
# ...
# ]
data = [[]]
classes = []
dict_ques_title = {0: "ques about: ", 1: "ques detail: "}
dict_title = {0: "data: ", 1: "guess: ", 2: "chances: "}
dict_ques_detail = {1: "כלל לא מסכים", 2: "לא מסכים", 3: "באמצע", 4: "מסכים", 5: "מסכים מאוד", None: "None"}
# Collecting data
with open("data.csv", encoding="utf-8") as f:
for i , line in enumerate(f.readlines()):
if i == 0:
line = line.replace("\"", "").replace(".", "")
#print(line)
start = True
new = True
item = ""
for index, let in enumerate(line):
if start and let == ",":
start = False
elif not start and let == ",":
classes.append(item)
item = ""
new = True
elif not start:
try:
int(let)
continue
except:
if new:
new = False
continue
item += let
continue
if i != 1:
data.append([])
line = line.replace("\"", "")
flag = True
item = ""
num = 0
for let in line:
if num < 2 and let == "":
num += 1
elif flag and num == 2:
flag = False
continue
elif num >= 2:
if let == ",":
data[i - 1].append(item)
item = ""
else:
item += let
f.close()
dict_places = {"מושב": 0, "יישוב": 0, "עיר": 2, "קיבוץ": 1, "UN": -1, None: "None"}
# manipulation data
def data_encode(data_s):
data = [[]]
for i_lst, lst in enumerate(data_s):
if i_lst != 0:
data.append([])
for item in lst:
try:
data[i_lst].append(int(item))
except:
if dict_places.get(item) is None:
item = "UN"
data[i_lst].append(dict_places[item])
return data
data = data_encode(data)
def delte_nosie(data):
for lst in data:
if lst[0] == -1:
data.remove(lst)
return data
data = delte_nosie(data)
LABEL = 0
def find_options(data, label):
unique = set()
for lst in data:
for i, item in enumerate(lst):
if i == label:
unique.add(item)
break
return unique
def decode_options(label):
if label == 0:
# dict = {"יישוב": 0, "עיר": 2, "קיבוץ": 1, "UN": -1, None: "None"}
dict = {0: "יישוב/מושב", 2: "עיר", 1: "קיבוץ", -1: "UN", None: "None"}
else:
dict = dict_ques_detail
return dict
dict_guess = decode_options(LABEL)
def is_numeric(value):
"""Test if a value is numeric."""
return isinstance(value, int) or isinstance(value, float)
def split_data(data, label, test_size=0):
if test_size < 0 or test_size > 1:
raise SyntaxError
if not is_numeric(test_size):
raise SyntaxError
x_train, x_test, y_train, y_test = [[]], [[]], [], []
for lst in data:
if test_size == 0:
if len(x_train[-1]) != 0:
x_train.append([])
lst_x = x_train
lst_y = y_train
elif test_size == 1:
if len(x_test[-1]) != 0:
x_test.append([])
lst_x = x_test
lst_y = y_train
elif len(x_test) >= len(data) / (1 / test_size):
if len(x_train[-1]) != 0:
x_train.append([])
lst_x = x_train
lst_y = y_train
elif len(x_train) >= len(data) / (1 / (1 - test_size)):
if len(x_test[-1]) != 0:
x_test.append([])
lst_x = x_test
lst_y = y_test
else:
if random.choice(range(1, 11)) > test_size * 10:
if len(x_train[-1]) != 0:
x_train.append([])
lst_x = x_train
lst_y = y_train
else:
if len(x_test[-1]) != 0:
x_test.append([])
lst_x = x_test
lst_y = y_test
for i, item in enumerate(lst):
if i == label:
lst_y.append(item)
else:
lst_x[-1].append(item)
return x_train, x_test, y_train, y_test
x_train, x_test, y_train, y_test = split_data(data, LABEL)