-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLSTM_test.py
More file actions
357 lines (273 loc) · 8.87 KB
/
LSTM_test.py
File metadata and controls
357 lines (273 loc) · 8.87 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# The purpose of this code is to create a LSTM neural network
# The LSTM is applied on features from early.csv
# which can be applied on the CSEDM data to predict final exam grade
# Fall and Spring
# %%
# import files
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from keras.callbacks import EarlyStopping
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
from keras import optimizers
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
import tensorflow as tf
import random
# %%
# fix random seed for reproducibility
random.seed(1)
np.random.seed(1)
tf.random.set_seed(1)
# %%
# read training data
dfSubject = pd.read_csv("D:\Courses\Spring2022\CSC591 Educational data mining\Project\Dataset\F19_Release_Train_06-28-21\Train\Data\LinkTables\Subject.csv")
print(dfSubject.shape)
# %%
# read training data
dfMainTable = pd.read_csv("D:\Courses\Spring2022\CSC591 Educational data mining\Project\Dataset\F19_Release_Train_06-28-21\Train\Data\MainTable.csv")
print(dfMainTable.shape)
# %%
# read training data
dfEarly = pd.read_csv("D:\Courses\Spring2022\CSC591 Educational data mining\Project\Dataset\F19_Release_Train_06-28-21\Train\early.csv")
print(dfEarly.shape)
# %%
# create new dataframe for training
newDF = dfEarly
newDF = newDF.drop(['AssignmentID','ProblemID'],1)
print(newDF.shape)
# %%
# converting the true/false to 0/1
for i in range(len(newDF)):
if(newDF['CorrectEventually'].iloc[i]==True):
newDF['CorrectEventually'].iloc[i]=1
elif(newDF['CorrectEventually'].iloc[i]==False):
newDF['CorrectEventually'].iloc[i]=0
if(newDF['Label'].iloc[i]==True):
newDF['Label'].iloc[i]=1
elif(newDF['Label'].iloc[i]==False):
newDF['Label'].iloc[i]=0
print(newDF)
# %%
# create y datalist for training
Y_train = dfSubject['X-Grade']
Y_train = Y_train/100
print(Y_train)
print(Y_train.shape)
# %%
# read training data for Spring
dfEarlySpring = pd.read_csv("D:/Courses/Spring2022/CSC591 Educational data mining/Project/Dataset/S19_Release_6_28_21.zip/Train/early.csv")
print(dfEarlySpring.shape)
# %%
print(len(dfEarlySpring['SubjectID'].unique()))
# %%
# create new dataframe for training
newDFSpring = dfEarlySpring
newDFSpring = newDFSpring.drop(['AssignmentID','ProblemID'],1)
print(newDFSpring.shape)
# %%
# converting the true/false to 0/1 for Spring
for i in range(len(newDFSpring)):
if(newDFSpring['CorrectEventually'].iloc[i]==True):
newDFSpring['CorrectEventually'].iloc[i]=1
elif(newDFSpring['CorrectEventually'].iloc[i]==False):
newDFSpring['CorrectEventually'].iloc[i]=0
if(newDFSpring['Label'].iloc[i]==True):
newDFSpring['Label'].iloc[i]=1
elif(newDFSpring['Label'].iloc[i]==False):
newDFSpring['Label'].iloc[i]=0
print(newDFSpring.shape)
print(newDFSpring)
# %%
# concat two data from Fall and Spring to create new training data
newDFAll = newDF.append(newDFSpring, ignore_index=True)
print(newDFAll.shape)
print(newDFAll.head(1))
# %%
newDFAll.to_csv('dataAll.csv')
# %%
# normalize the dataset between -1 and 1
#scaler = MinMaxScaler()
X_train = newDFAll
#for i in range(1,3):
# X_train.iloc[:,i:i+1] = scaler.fit_transform(X_train.iloc[:,i:i+1])
# %%
# read Spring training data
dfSubjectSpring = pd.read_csv("D:/Courses/Spring2022/CSC591 Educational data mining/Project/Dataset/S19_Release_6_28_21.zip/Train/Data/LinkTables/Subject.csv")
print(dfSubjectSpring.shape)
print(len(dfEarlySpring['SubjectID'].unique()))
# %%
duplicate = dfSubjectSpring[dfSubjectSpring['SubjectID'].duplicated()]
print(duplicate)
# %%
dfSubjectSpring = dfSubjectSpring.drop(75,0)
print(dfSubjectSpring.shape)
# %%
dfSubjectAll = dfSubject.append(dfSubjectSpring, ignore_index=True)
print(dfSubjectAll.shape)
# %%
# create studentList from subject.csv
studentList = dfSubjectAll['SubjectID']
print(len(studentList))
print(studentList)
studentList.to_csv("studentList.csv")
# %%
# check duplicate in studentlist
duplicate = studentList[studentList.duplicated()]
print(duplicate)
# %%
# group the per student data
data = [X_train[X_train['SubjectID'] == student] for student in studentList]
# %%
print(len(data))
print(data[367])
# %%
for i in range(len(data)):
data[i] = data[i].drop(['SubjectID'],1)
# %%
print(data[0])
print(len(data[0]))
# %%
for i in range(len(data)):
l = len(data[i])
print(l)
if(l<30):
needed = 30-l
print(needed)
for j in range(needed):
data[i] = data[i].append({'Attempts' : 0, 'CorrectEventually' : 0, 'Label': 0}, ignore_index = True)
#print(data[i])
# %%
for i in range(len(data)):
print(data[i].shape)
# %%
print(studentList.shape)
# %%
for i in range(len(data)):
data[i] = tf.convert_to_tensor(data[i], dtype=tf.float32)
data[i] = np.asarray(data[i])
#data[i] = data[i].reshape(1,data[i].shape[0],data[i].shape[1])
print(data[i].shape)
# %%
# create y datalist for training for Spring
Y_trainSpring = dfSubjectSpring['X-Grade']
#Y_trainSpring = Y_train/100
print(Y_trainSpring.shape)
# %%
# create new y datalist
Y_trainAll = Y_train.append(Y_trainSpring, ignore_index=True)
print(Y_trainAll.shape)
print(Y_trainAll.iloc[367])
# %%
# reshape X_train for LSTM
X_train = data
X_train = np.asarray(X_train)
#X_train=X_train.reshape(X_train.shape[0],X_train.shape[1],1)
#print(X_train.shape)
# %%
# divide X_train in train and val dataset
#x_train, x_val, y_train, y_val = train_test_split(X_train, Y_trainAll, test_size=0.2)
#print(x_train.shape)
#print(x_val.shape)
# %%
# create LSTM model
model = Sequential()
model.add(LSTM(20, input_shape=(30,3)))
model.add(Dense(1, activation='sigmoid'))
#model.add(Dropout(0.1))
ad = optimizers.Adam(learning_rate=0.0001)
model.compile(loss='mean_squared_error', optimizer=ad)
es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=2, restore_best_weights=True)
model.fit(X_train, Y_trainAll, epochs=100, batch_size=1, verbose=2)
# %%
#Adam:
# for learning rate 0.01, trainMSE = 199 , ValMSE = 421
# for learning rate 0.001, trainMSE = 36 , ValMSE = 390
# for learning rate 0.0001, trainMSE = 165, ValMSE = 271
# for learing rate 0.0005, trainMSE = 77, ValMSE = 335
# for learning rate 0.00001, trainMSE = 248 , ValMSE = 330
# SGD:
# for learning rate 0.0001, trainMSE = 281, ValMSE = 349
# LSTM layer 20:
# for learning rate 0.0001, trainMSE = 173, ValMSE = 271
# %%
# make prediction on train data
import math
trainPredict = model.predict(X_train)
trainScore = mean_squared_error(Y_trainAll*100, trainPredict[:,0]*100)
print('Train Score: %.2f MSE' % (trainScore))
# 197.05
# 184 MSE
# %%
# read test data
dfEarlyTest = pd.read_csv("D:\Courses\Spring2022\CSC591 Educational data mining\Project\Dataset\F19_Release_Test_06-28-21\Test\early.csv")
print(dfEarlyTest.shape)
print(dfEarlyTest)
# %%
# read test data
dfSubjectTest = pd.read_csv("D:\Courses\Spring2022\CSC591 Educational data mining\Project\Dataset\F19_Release_Test_06-28-21\Test\Data\LinkTables\Subject.csv")
print(dfSubjectTest.shape)
print(dfSubjectTest)
# %%
# create new dataframe for training
newDFTest = dfEarlyTest
newDFTest = newDFTest.drop(['AssignmentID','ProblemID'],1)
print(newDFTest)
# %%
# converting the true/false to 0/1
for i in range(len(newDFTest)):
if(newDFTest['CorrectEventually'].iloc[i]==True):
newDFTest['CorrectEventually'].iloc[i]=1
elif(newDFTest['CorrectEventually'].iloc[i]==False):
newDFTest['CorrectEventually'].iloc[i]=0
if(newDFTest['Label'].iloc[i]==True):
newDFTest['Label'].iloc[i]=1
elif(newDFTest['Label'].iloc[i]==False):
newDFTest['Label'].iloc[i]=0
print(newDFTest)
# %%
studentListTest = dfSubjectTest['SubjectID']
print(len(studentListTest))
# %%
# group the per student test data
testData = [newDFTest[newDFTest['SubjectID'] == student] for student in studentListTest]
# %%
print(studentListTest.shape)
# %%
for i in range(len(testData)):
testData[i] = testData[i].drop(['SubjectID'],1)
# %%
for i in range(len(testData)):
l = len(testData[i])
print(l)
if(l<30):
needed = 30-l
print(needed)
for j in range(needed):
testData[i] = testData[i].append({'Attempts' : 0, 'CorrectEventually' : 0, 'Label': 0}, ignore_index = True)
#print(data[i])
# %%
for i in range(len(testData)):
print(testData[i].shape)
# %%
for i in range(len(testData)):
testData[i] = tf.convert_to_tensor(testData[i], dtype=tf.float32)
testData[i] = np.asarray(testData[i])
#data[i] = data[i].reshape(1,data[i].shape[0],data[i].shape[1])
print(testData[i].shape)
# %%
X_test = testData
X_test = np.asarray(X_test)
# %%
# make prediction on train data
testPredict = model.predict(X_test)
testPredict = testPredict*100
print(testPredict)
# %%
dfSubjectTest['X-Grade'] = testPredict
print(dfSubjectTest)
# %%
dfSubjectTest.to_csv('output.csv')