-
Notifications
You must be signed in to change notification settings - Fork 0
/
FeatureCreationPart1_.py
233 lines (212 loc) · 8.56 KB
/
FeatureCreationPart1_.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
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
import pandas as pd
import numpy as np
data=pd.read_csv('Filtered_skill_builders_16_17.csv')
print("original data lenth:",len(data.index))
unique_Actions=list(data['action_name'])
unique_Actions=set(unique_Actions)
print("unique 1",unique_Actions)
print(len(data.index))
data=data.loc[~data['action_name'].isin(['start','comment','end','work','resume'])]
print(len(data.index))
#data.to_csv('Filtered_skill_builders_16_17.csv')
#data=data.loc[data['action_name'] not in ('start','comment','end','work','resume')]
data=data.loc[~data['action_name'].isin(['start','comment','end','work','resume'])]
data_orig=data.loc[~data['action_name'].isin(['start','comment','end','work','resume'])]
unique_Actions2=list(data['action_name'])
print(set(unique_Actions2))
print(data.columns)
print(data['action_time'])
data['next_assignment_wheel_spin']=-1
users=list(set(data['user_id']))
#print(users)
new_df=pd.DataFrame()
#Creating the feature 1 : Next_assignment_wheel_spin
#data=data[0:1000]
for i in users:
user_df=data.loc[data['user_id']==i]
user_df=user_df.sort_values(by=['action_time'])
assignments=user_df[['assignment_id','assignment_wheel_spin']]
#print(type(assignments))
#print(len(assignments.index))
assignments=assignments.drop_duplicates()
assignments_ids=list(assignments['assignment_id'])
assignment_wheel_spin=list(assignments['assignment_wheel_spin'])
#print(assignments_ids)
#print(assignment_wheel_spin)
user_df = user_df.reset_index(drop=True)
for index in range(0, len(user_df.index)):
current_assignment=user_df.at[index, 'assignment_id']
#print("Current assignment:",current_assignment)
pos=assignments_ids.index(current_assignment)
#print("pos=",pos)
if(pos==(len(assignments_ids)-1)):
#print("last assignment")
user_df.at[index, 'next_assignment_wheel_spin']=-1
else:
#print(assignments_ids[pos+1])
user_df.at[index, 'next_assignment_wheel_spin'] = assignment_wheel_spin[pos+1]
new_df=new_df.append(user_df)
#print("Done")
#print(user_df['next_assignment_wheel_spin'])
#print("New Df")
#print(len(data.index),len(new_df.index))
#print(new_df.columns)
#print("NEXT ASSIGNMENT STOPOUT")
data=new_df
data['next_assignment_stopout']=-1
#Creating the feature
new_df1=pd.DataFrame()
for i in users:
user_df=data.loc[data['user_id']==i]
user_df = user_df.sort_values(by=['action_time'])
assignments=user_df[['assignment_id','assignment_stopout']]
#print(type(assignments))
#print(len(assignments.index))
assignments=assignments.drop_duplicates()
assignments_ids=list(assignments['assignment_id'])
assignment_wheel_spin=list(assignments['assignment_stopout'])
#print(assignments_ids)
#print(assignment_wheel_spin)
user_df = user_df.reset_index(drop=True)
for index in range(0, len(user_df.index)):
current_assignment=user_df.at[index, 'assignment_id']
#print("Current assignment:",current_assignment)
pos=assignments_ids.index(current_assignment)
#print("pos=",pos)
if(pos==(len(assignments_ids)-1)):
#print("last assignment")
user_df.at[index, 'next_assignment_stopout']=-1
else:
#print(assignments_ids[pos+1])
user_df.at[index, 'next_assignment_stopout'] = assignment_wheel_spin[pos+1]
#print("User df:",len(user_df.index))
new_df1=new_df1.append(user_df)
#print("New df1 append:",len(new_df1.index))
#print("Done")
#print(user_df['next_assignment_stopout'])
print("New Df1")
print(len(data.index), len(new_df1.index))
print(new_df1.columns)
#Creating feature 3 one hot encoding of correct
#print(new_df1['correct'])
mapping = {1:'correct', 0:'incorrect',np.NaN:'non-attempt'}
new_df1=new_df1.replace({'correct':mapping})
new_df1=pd.get_dummies(new_df1,columns=['correct'])
#print(new_df1[0:5])
print(new_df1.columns)
# #Creating bottom out hint cumaltive
#
# #for each student grab the problem ids and for each problem id get all the rows and check if the row is for bottom out hint and update all the
# #rows after it
# data=new_df1
# new_df2=pd.DataFrame()
# for i in users:
# user_df=data.loc[data['user_id']==i]
# user_df = user_df.sort_values(by=['action_time'])
# #print(user_df['problem_id'])
# problem_ids=np.unique(user_df["problem_id"].values)
# #print("Problem_ids=",problem_ids)
#
# for j in range(0,len(problem_ids)):
# bottom_out_hint=0
# #print("problem:",problem_ids[j])
# problem_df=user_df.loc[user_df['problem_id']==problem_ids[j]]
# #print(problem_df['problem_id'])
# problem_df = problem_df.reset_index(drop=True)
# problem_df['used_bottom_out_hint']=0
#
# for index in range(0, len(problem_df.index)):
# if(bottom_out_hint==0):
# #print("Problem bottom hint:",)
# if(problem_df.at[index, 'problem_bottom_hint'] == 1):
# problem_df.at[index, 'used_bottom_out_hint'] = 1
# bottom_out_hint=1
# else:
# problem_df.at[index, 'used_bottom_out_hint'] = 1
#
# new_df2=new_df2.append(problem_df)
# #print(problem_df['used_bottom_out_hint'])
#
# print("New df 2")
# print(len(data_orig.index),len(new_df2.index))
# print(new_df2.columns)
#
#
#
# #Creating feature used_penultimate_hint
#
# data=new_df2
# new_df3=pd.DataFrame()
# for i in users:
# user_df=data.loc[data['user_id']==i]
# user_df = user_df.sort_values(by=['action_time'])
# #print(user_df['problem_id'])
# all_problems=np.unique(user_df["problem_id"].values)
# all_problems=list(all_problems)
# problem_hints = user_df[['problem_id', 'problem_hint_count','problem_total_hints']]
# problem_hints = problem_hints.drop_duplicates()
# problem_hints = problem_hints.dropna()
# problem_hints['hints_not_used']=problem_hints['problem_total_hints']-problem_hints['problem_hint_count']
# problem_ids=list(problem_hints.loc[problem_hints['hints_not_used']==1]['problem_id'])
# hint_counts=list(problem_hints.loc[problem_hints['hints_not_used']==1]['problem_total_hints'])
#
# #print(problem_ids)
# for j in range(0,len(all_problems)):
# #print("All problems:",all_problems[j])
# problem_df = user_df.loc[user_df['problem_id'] == all_problems[j]]
# if(all_problems[j] in problem_ids):
# problem_df=user_df.loc[user_df['problem_id']==all_problems[j]]
# problem_df['used_penultimate_hint'] = 0
# p=problem_ids.index(all_problems[j])
# count=hint_counts[p]
# curr_hint_count=0
# for index in range(0, len(problem_df.index)):
# if (problem_df.at[index, 'action_name'] == 'hint'):
# curr_hint_count=curr_hint_count+1
#
# if (curr_hint_count>=(count-1)):
# problem_df.at[index, 'used_penultimate_hint']=1
#
# new_df3=new_df3.append(problem_df)
# else:
# problem_df['used_penultimate_hint'] = 0
# new_df3 = new_df3.append(problem_df)
#
# print("New df 3")
# print(len(data_orig.index),len(new_df3.index))
# print(new_df3.columns)
#
#
#
# data=new_df3
# new_df4=pd.DataFrame()
# #Create previous 3 actions
# for i in users:
# user_df = data.loc[data['user_id'] == i]
# user_df = user_df.sort_values(by=['action_time'])
# user_df['previous_3_states'] = str(np.NaN)
# user_df = user_df.reset_index(drop=True)
# for index in range(0,len(user_df.index)):
# #print("index=",index)
#
# if(index==0):
# user_df.at[index, 'previous_3_states'] = ('null', 'null', 'null')
#
# elif(index==1):
# user_df.at[index, 'previous_3_states'] = ('null', 'null',user_df.at[0, 'action_name'])
#
# elif(index==2):
# user_df.at[index, 'previous_3_states'] = ('null', user_df.at[index-2, 'action_name'],user_df.at[index - 1, 'action_name'])
# else:
# user_df.at[index,'previous_3_states']=(user_df.at[index-3,'action_name'],user_df.at[index-2,'action_name'],user_df.at[index-1,'action_name'])
#
#
# new_df4=new_df4.append(user_df)
#
# print("FINAL DF")
# print(new_df4.columns)
# print("New df 4")
# print(len(data_orig.index),len(new_df4.index))
#new_df4.to_csv('features_created1.csv')
new_df1.to_csv('features_created1.csv')
print("DONE creating new file")