Repository navigation
Expand file tree
/
Copy pathload_data.py
More file actions
178 lines (159 loc) · 6.56 KB
/
Copy pathload_data.py
File metadata and controls
178 lines (159 loc) · 6.56 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
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
173
174
175
176
177
178
import numpy as np
import torch
import math
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# device = torch.cuda.set_device(2)
device = torch.cuda.set_device(0)
class DATA(object):
def __init__(self, n_question, seqlen, separate_char, name="data"):
# In the ASSISTments2009 dataset:
# param: n_queation = 110
# seqlen = 200
self.separate_char = separate_char
self.n_question = n_question
self.seqlen = seqlen
# data format
# id, true_student_id
# 1,1,1,1,7,7,9,10,10,10,10,11,11,45,54
# 0,1,1,1,1,1,0,0,1,1,1,1,1,0,0
def load_data(self, path):
f_data = open(path, 'r')
q_data = []
qa_data = []
idx_data = []
for lineID, line in enumerate(f_data):
line = line.strip()
# lineID starts from 0
if lineID % 3 == 0:
student_id = lineID // 3
if lineID % 3 == 1:
Q = line.split(self.separate_char)
if len(Q[len(Q) - 1]) == 0:
Q = Q[:-1]
# print(len(Q))
elif lineID % 3 == 2:
A = line.split(self.separate_char)
if len(A[len(A) - 1]) == 0:
A = A[:-1]
# print(len(A),A)
# start split the data
n_split = 1
# print('len(Q):',len(Q))
if len(Q) > self.seqlen:
n_split = math.floor(len(Q) / self.seqlen)
if len(Q) % self.seqlen:
n_split = n_split + 1
# print('n_split:',n_split)
for k in range(n_split):
question_sequence = []
answer_sequence = []
if k == n_split - 1:
endINdex = len(A)
else:
endINdex = (k + 1) * self.seqlen
for i in range(k * self.seqlen, endINdex):
if len(Q[i]) > 0:
# int(A[i]) is in {0,1}
Xindex = int(Q[i]) + int(A[i]) * self.n_question
question_sequence.append(int(Q[i]))
answer_sequence.append(Xindex)
else:
print(Q[i])
# print('instance:-->', len(instance),instance)
q_data.append(question_sequence)
qa_data.append(answer_sequence)
idx_data.append(student_id)
f_data.close()
# data: [[],[],[],...] <-- set_max_seqlen is used
# convert data into ndarrays for better speed during training
q_dataArray = np.zeros((len(q_data), self.seqlen))
# 这个切片操作取出了数组中第 j 行的前 len(dat) 列,并用 dat 填充
for j in range(len(q_data)):
dat = q_data[j]
q_dataArray[j, :len(dat)] = dat
qa_dataArray = np.zeros((len(qa_data), self.seqlen))
for j in range(len(qa_data)):
dat = qa_data[j]
qa_dataArray[j, :len(dat)] = dat
# dataArray: [ array([[],[],..])] Shape: (3633, 200)
return q_dataArray, qa_dataArray, np.asarray(idx_data)
class PID_DATA(object):
def __init__(self, n_question, seqlen, separate_char, name="data"):
# In the ASSISTments2009 dataset:
# param: n_queation = 110
# seqlen = 200
self.separate_char = separate_char
self.seqlen = seqlen
self.n_question = n_question
# data format
# id, true_student_id
# pid1, pid2, ...
# 1,1,1,1,7,7,9,10,10,10,10,11,11,45,54
# 0,1,1,1,1,1,0,0,1,1,1,1,1,0,0
def load_data(self, path):
f_data = open(path, 'r')
q_data = []
qa_data = []
p_data = []
for lineID, line in enumerate(f_data):
line = line.strip()
# lineID starts from 0
if lineID % 4 == 0:
student_id = lineID // 4
if lineID % 4 == 2:
Q = line.split(self.separate_char)
if len(Q[len(Q) - 1]) == 0:
Q = Q[:-1]
# print(len(Q))
if lineID % 4 == 1:
P = line.split(self.separate_char)
if len(P[len(P) - 1]) == 0:
P = P[:-1]
elif lineID % 4 == 3:
A = line.split(self.separate_char)
if len(A[len(A) - 1]) == 0:
A = A[:-1]
# print(len(A),A)
# start split the data
n_split = 1
# print('len(Q):',len(Q))
if len(Q) > self.seqlen:
n_split = math.floor(len(Q) / self.seqlen)
if len(Q) % self.seqlen:
n_split = n_split + 1
# print('n_split:',n_split)
for k in range(n_split):
question_sequence = []
problem_sequence = []
answer_sequence = []
if k == n_split - 1:
endINdex = len(A)
else:
endINdex = (k + 1) * self.seqlen
for i in range(k * self.seqlen, endINdex):
if len(Q[i]) > 0: # 做对的加110,做错的加0(不做处理),规范每一行的长度为200
Xindex = int(Q[i]) + int(A[i]) * self.n_question
question_sequence.append(int(Q[i]))
problem_sequence.append(int(P[i]))
answer_sequence.append(Xindex)
else:
print(Q[i])
q_data.append(question_sequence)
qa_data.append(answer_sequence)
p_data.append(problem_sequence)
f_data.close()
### data: [[],[],[],...] <-- set_max_seqlen is used
# convert data into ndarrays for better speed during training
q_dataArray = np.zeros((len(q_data), self.seqlen))
for j in range(len(q_data)):
dat = q_data[j]
q_dataArray[j, :len(dat)] = dat
qa_dataArray = np.zeros((len(qa_data), self.seqlen))
for j in range(len(qa_data)):
dat = qa_data[j]
qa_dataArray[j, :len(dat)] = dat
p_dataArray = np.zeros((len(p_data), self.seqlen))
for j in range(len(p_data)):
dat = p_data[j]
p_dataArray[j, :len(dat)] = dat
return q_dataArray, qa_dataArray, p_dataArray