-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathANN_aggregated_Halstead.py
More file actions
248 lines (193 loc) · 6.48 KB
/
ANN_aggregated_Halstead.py
File metadata and controls
248 lines (193 loc) · 6.48 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
# %%
# The purpose of this code is to create a ANN
# The ANN is applied on aggregated features from early.csv and also Halstead features
# which can be applied on the CSEDM data to predict final exam grade on test data
# %%
# import files
import pandas as pd
import numpy as np
import random
from sklearn.preprocessing import MinMaxScaler
from keras import optimizers
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.callbacks import EarlyStopping
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
import tensorflow as tf
# %%
# 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 = pd.DataFrame(dfSubject['SubjectID'])
print(newDF)
# %%
# create y datalist for training
Y_train = dfSubject['X-Grade']
Y_train = Y_train/100
print(Y_train)
# %%
# extract features for training
# number of problem attempted
newDF['problemAttempted'] = 0
# number of problems gotten correct eventually
newDF['NumCorrectEventually'] = 0
# total attempts
newDF['totalAttempts'] = 0
# number of problems gotten correct on first try
newDF['NumCorrectFirstTry'] = 0
for i in range(len(newDF)):
#for i in range(1):
student = newDF['SubjectID'].iloc[i]
studentRows = dfEarly[dfEarly['SubjectID']==student]
#print(len(studentRows))
newDF['problemAttempted'].iloc[i] = studentRows.shape[0]
newDF['NumCorrectEventually'].iloc[i] = np.sum(studentRows['CorrectEventually'])
newDF['totalAttempts'].iloc[i] = np.sum(studentRows['Attempts'])
newDF['NumCorrectFirstTry'].iloc[i] = np.sum(studentRows['Attempts'] == 1)
print(newDF)
# %%
newDF.to_csv('data.csv')
# %%
# reading halstead for train data
halsteadDF = pd.read_csv("D:\Courses\Spring2022\CSC591 Educational data mining\Project\Dataset\F19_Release_Train_06-28-21\Fall_Train_Subject_With_Halstead.csv")
print(halsteadDF.shape)
# %%
halsteadDF = halsteadDF.drop('X-Grade',1)
print(halsteadDF.shape)
# %%
newDF = pd.merge(newDF, halsteadDF, on='SubjectID', how='left')
print(newDF.shape)
print(newDF.head(1))
# %%
# normalize the dataset
scaler = MinMaxScaler()
X_train = newDF
for i in range(1,17):
X_train.iloc[:,i:i+1] = scaler.fit_transform(X_train.iloc[:,i:i+1])
print(X_train)
# %%
# remove subjectID from data
X_train = X_train.drop('SubjectID',axis=1)
print(X_train)
# %%
X_train.to_csv("trainData.csv")
# %%
# %%
# reshape X_train for LSTM
X_train = X_train.to_numpy()
#X_train=X_train.reshape(X_train.shape[0],X_train.shape[1],1)
#print(X_train.shape)
# %%
#x_train, x_val, y_train, y_val = train_test_split(X_train, Y_train, test_size=0.2)
#print(x_train.shape)
#print(x_val.shape)
# %%
# create ANN model
model = Sequential()
# Add the first layer
model.add(Dense(32, activation='relu', input_shape=(16,)))
model.add(Dense(64, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
ad = optimizers.Adam(learning_rate=0.00001)
model.compile(loss='mean_squared_error', optimizer=ad)
#es = EarlyStopping(monitor='val_loss', mode='min', verbose=1)
model.fit(X_train, Y_train, epochs=100, batch_size=1, verbose=2)
# %%
# make prediction on train data
import math
trainPredict = model.predict(X_train)
trainScore = mean_squared_error(Y_train*100, trainPredict[:,0]*100)
print('Train Score: %.2f MSE' % (trainScore))
# 222.59 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)
# %%
# reading halstead for test data
halsteadDFTest = pd.read_csv("D:\Courses\Spring2022\CSC591 Educational data mining\Project\Dataset\F19_Release_Train_06-28-21\Fall_Test_Subject_With_Halstead.csv")
print(halsteadDFTest.shape)
# %%
# create new dataframe for testing
newDFTest = pd.DataFrame(dfSubjectTest['SubjectID'])
print(newDFTest.shape)
print(newDFTest)
# %%
# extract features for testing
# number of problem attempted
newDFTest['problemAttempted'] = 0
# number of problems gotten correct eventually
newDFTest['NumCorrectEventually'] = 0
# total attempts
newDFTest['totalAttempts'] = 0
# number of problems gotten correct on first try
newDFTest['NumCorrectFirstTry'] = 0
for i in range(len(newDFTest)):
#for i in range(1):
student = newDFTest['SubjectID'].iloc[i]
studentRows = dfEarlyTest[dfEarlyTest['SubjectID']==student]
#print(len(studentRows))
newDFTest['problemAttempted'].iloc[i] = studentRows.shape[0]
newDFTest['NumCorrectEventually'].iloc[i] = np.sum(studentRows['CorrectEventually'])
newDFTest['totalAttempts'].iloc[i] = np.sum(studentRows['Attempts'])
newDFTest['NumCorrectFirstTry'].iloc[i] = np.sum(studentRows['Attempts'] == 1)
print(newDFTest)
# %%
# merge the halstead data with other features
newDFTest = pd.merge(newDFTest, halsteadDFTest, on='SubjectID', how='left')
print(newDFTest.shape)
print(newDFTest.head(1))
# %%
# normalize the dataset
scaler = MinMaxScaler()
X_test = newDFTest
for i in range(1,17):
X_test.iloc[:,i:i+1] = scaler.fit_transform(X_test.iloc[:,i:i+1])
print(X_test)
# %%
# remove subjectID from data
X_test = X_test.drop('SubjectID',axis=1)
print(X_test)
# %%
X_test.to_csv("testData.csv")
# %%
# reshape X_test for ANN
X_test = X_test.to_numpy()
#X_test = X_test.reshape(X_test.shape[0],X_test.shape[1],1)
#print(X_test.shape)
# %%
# make prediction on test data
testPredict = model.predict(X_test)
testPredict = testPredict*100
print(testPredict)
# %%
dfSubjectTest['X-Grade'] = testPredict
print(dfSubjectTest)
# %%
dfSubjectTest.to_csv('output.csv')