/
classifier_estimator.py
393 lines (339 loc) · 16.6 KB
/
classifier_estimator.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
###############################################################################
# MIT License (MIT)
#
# Copyright (c)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
###############################################################################
from pandas import DataFrame
from skfuzzy import cmeans_predict
from numpy import zeros as np_zeros
from numpy import ones as np_ones
from numpy import unique as np_unique
from numpy import average as np_average
from numpy import array as np_array
from numpy import max as np_max
from numpy import min as np_min
from numpy import mean as np_mean
from numpy import log as np_log
from numpy import append as np_append
from numpy.linalg import norm as np_linalg_norm
from pandas import merge as pd_merge
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import AdaBoostClassifier
from sklearn.svm import SVC#, LinearSVC
from sklearn.calibration import CalibratedClassifierCV
from sklearn.metrics import recall_score
from sklearn.cross_validation import train_test_split
# from sklearn.naive_bayes import GaussianNB
# from sklearn.cluster import KMeans
from itertools import product as it_product
class AcademicFailureEstimator():
def __init__(self, academic_clusterer, **kwargs):
"""
"""
if kwargs == {}:
self._academic_clusterer = academic_clusterer
self._academic_clusterer.semesters_cluster()
self._academic_clusterer.students_cluster()
self._cntr_sf = self._academic_clusterer.cntr_sf
self._cntr_se = self._academic_clusterer.cntr_se
self._rates = self._academic_clusterer.rates
"""
"""
@property
def students_classifier_fn(self):
return self._students_clf
"""
"""
def init_students_classifier_fn(self, **kwargs):
if kwargs == {}:
m = self._academic_clusterer._m; error = 1.e-10; maxiter = 100
clf = lambda data: cmeans_predict( data.T, self._cntr_sf, m, error, maxiter )
else:
clf = lambda data: cmeans_predict( data.T, self._cntr_sf, kwargs )
self._students_clf = clf
"""
"""
@property
def semesters_classifier_fn(self):
return self._semesters_clf
"""
"""
def init_semesters_classifier_fn(self, **kwargs):
if kwargs == {}:
svc = SVC(kernel='linear', gamma=10e3, probability=True)
else:
svc = SVC(kwargs)
svc.fit( self._cntr_se, range( len( self._cntr_se ) ) )
svc_predict = svc.predict
svc_prob = svc.predict_proba
clf = lambda data: [svc_predict( data ), svc_prob(data)]
self._semesters_clf = clf
@staticmethod
def get_courses_as_bitarray(semester):
result = \
[ int( _course in semester ) for _course in AcademicFailureEstimator.COURSES ]
return result
# @staticmethod
def get_semester_f(self, semester):
abs_df = self._academic_clusterer.courses_features
alpha_total = abs_df[ abs_df['course'].isin(semester) ]['alpha'].sum()
# credit_total = abs_df[ abs_df['course'].isin(semester) ]['credits'].sum()
return [alpha_total]
# return [credit_total]
# return [alpha_total, credit_total]
# @staticmethod
def get_ss_features(self, row):
semester = row['taken_courses'].split(' ')
# student_features = [row['GPA'],row['performance_y']]
student_features = [row['GPA']]
semester_features = self.get_semester_f(semester)
return student_features + semester_features
"""KM_FEAT_ = 'factor1_measure', 'factor2_measure', 'factor3_measure',
'factor4_measure', 'factor5_measure', 'factor6_measure']
student_features = row[KM_FEAT_].values.tolist()
semester = row['taken_courses'].split(' ')
semester_features = [self.get_semester_f(semester)]#get_courses_as_bitarray( row['taken_courses'].split(' ') )
# print student_features
# print semester_features
return student_features + semester_features"""
"""
KM_FEAT_ = ['factor1_measure', 'factor2_measure', 'factor3_measure',
'factor4_measure', 'factor5_measure', 'factor6_measure']
student_features = row[KM_FEAT_].values.tolist()
semester = row['taken_courses'].split(' ')
semester_features = AcademicFailureEstimator.get_courses_as_bitarray( semester )
return student_features + semester_features"""
@property
def classifier_fn(self):
return self._clf
"""
"""
def init_classifier_fn(self, **kwargs):
cs_df = self._academic_clusterer.courses_features
AcademicFailureEstimator.COURSES = cs_df['course'].values
se_df = self._academic_clusterer.semesters_features
sf_df = self._academic_clusterer.students_features
gpa_df = self._academic_clusterer.ha_df.drop_duplicates(['student','GPA'])
ss_df = pd_merge( se_df, sf_df, on='student' )
ss_df = pd_merge( ss_df, gpa_df, on='student' )
ss_df = pd_merge( ss_df, cs_df, on='course' )
data = ss_df.apply( self.get_ss_features, axis=1 )
data = np_array( data.tolist() )
X = data
y = ss_df['ha_reprobado'].apply(lambda x: 0 if x else 1).values
# H = np_unique( X[:,0] )
# H = np_array( [ H, np_zeros( len(H) ) ] ).T
# l = np_ones( len( H ) )
# X = np_append( X, H, axis=0)
# y = np_append( y, l )
X_train, X_test, y_train, y_test = train_test_split(X,
y,
test_size=0.30,
random_state=7)
# logreg = LogisticRegression(random_state=7)
logreg = AdaBoostClassifier(random_state=10)
logreg = CalibratedClassifierCV( logreg, cv=2, method='sigmoid')
# logreg = GaussianNB()
logreg.fit(X, y)
logreg_prob = logreg.predict_proba
logreg_predict = logreg.predict
y_pred = logreg.predict(X_test)
recall = recall_score(y_test, y_pred)
def quality(data):
_z_ = logreg_predict(data)[0]
sample = X[ y==_z_ ]
sample_ = X[ y==(1-_z_) ]
d = np_linalg_norm( [data] - sample )
d_ = np_linalg_norm( [data] - sample_ )
r = np_max( d_ )/np_max( d )
# r = np_mean( d )/np_mean( d_ )
# r = np_min( d )/np_min( d_ )
# r = 0.5 * ( r + recall )
if r > 1:
r = abs( 1-r )
r = 0.5 * ( r + recall )
return str( r )
clf = lambda data: [ logreg_prob( data ), quality(data) ]
self._clf = clf
"""
FrEATURES = ['factor1_measure', 'factor2_measure', 'factor3_measure',
'factor4_measure', 'factor5_measure', 'factor6_measure',
'semester_feature']
# 'alpha_total', 'beta_total', 'skewness_total', 'year_n',
# 'courses_num']
AcademicFailureEstimator.COURSES = self._academic_clusterer.courses_features['course'].values
se_df = self._academic_clusterer.semesters_features
sf_df = self._academic_clusterer.students_features
ss_df = pd_merge( se_df, sf_df, on='student' )
########################################################################
# ss_df['_class'] = 0
# mask_c = {}
# mask_c[0] = ss_df['beta_total']<=-1.5
# mask_c[1] = (ss_df['beta_total']>-1.5) & (ss_df['beta_total']<-0.862)
# mask_c[2] = (ss_df['beta_total']>=-0.862) & (ss_df['beta_total']<0)
# mask_c[3] = ss_df['beta_total']>=0
# for i in mask_c.keys():
# ss_df.loc[ mask_c[i], '_class' ] = i
tmp = ss_df['taken_courses'].apply(lambda x: x.split())
Z = tmp.apply( AcademicFailureEstimator.get_courses_as_bitarray )
Z = np_array( Z.as_matrix().tolist() )
self._km = KMeans(n_clusters=8, random_state=7)
_z = self._km.fit_predict( Z )
ss_df['semester_feature'] = _z
########################################################################
KM_FEAT_ = ['factor1_measure', 'factor2_measure', 'factor3_measure',
'factor4_measure', 'factor5_measure', 'factor6_measure']
T = ss_df.drop_duplicates('student')[ KM_FEAT_ ].as_matrix()
T_ = ss_df[ KM_FEAT_ ].as_matrix()
self._fm = KMeans(n_clusters=4, random_state=7)
self._fm.fit( T )
_z = self._fm.predict( T_ )
ss_df['student_feature'] = _z
########################################################################
# X = ss_df[ FEATURES ].as_matrix()
X = ss_df[ ['semester_feature','student_feature'] ].as_matrix()
y = ss_df['ha_reprobado'].apply(lambda x: 0 if x else 1).values
# y = ss_df['_class'].values
X_train, X_test, y_train, y_test = train_test_split(X,
y,
test_size=0.30,
random_state=7)
if kwargs == {}:
# logreg = SVC(kernel='linear', tol=0.0001, max_iter=1000, probability=True)
# logreg = LinearSVC()
logreg = LogisticRegression(random_state=7)
# logreg = LogisticRegression(C=1e5)
# est = GaussianNB()
else:
logreg = LogisticRegression(kwargs)
# est = GaussianNB(kwargs)
# isotonic = CalibratedClassifierCV(est, cv=2, method='isotonic')
logreg.fit(X, y)
# isotonic.fit(X, y)
# logreg_predict = logreg.predict
# prob_pos = isotonic.predict_proba
logreg_prob = logreg.predict_proba
########################################################################
# prob_pos = logreg.decision_function(X_test)
# prob_min = prob_pos.min()
# prob_max = prob_pos.max()
# prob_den = prob_max - prob_min
########################################################################
# logreg_prob = logreg.decision_function
# y_pred = isotonic.predict(X_test)
y_pred = logreg.predict(X_test)
recall = recall_score(y_test, y_pred)
# clf = lambda data: [ logreg_prob( data ), logreg.score(X, y) ]
# clf = lambda data: [ (logreg_prob( data ) - prob_min)/prob_den,\
# logreg.score(X, y)]
clf = lambda data: [ logreg_prob( data ), recall ]
self._clf = clf
# data_ = [i for i in it_product(range(4),range(8))]
# self._clf.ratios = logreg_prob( data_ )
"""
"""
Use of the certainty value given by Ceratainty=1-Uncertainty
The forecasts are probabilistic, the observations are binary.
Sample baseline calculated from observations.
Brier Score (BS) = 0.24
Brier Score - Baseline = 0.2488
Skill Score = 0.03551
Reliability = 0.01094
Resolution = 0.01978
Uncertainty = 0.2488
"""
def predict(self, student_ID, semester):
semester_features, student_features = self.get_features( student_ID, semester )
if self._academic_clusterer.source == 'espol':
semester_type = self.semesters_classifier_fn(semester_features)
#U_, U0_, d_, Jm_, p_, fpc_
U_, _, _, _, _, fpc_ = self.students_classifier_fn(student_features)
student_membership = U_.T[0]
# print(semester_type)
set_mask = ( self._rates['km_cluster_ID'] == semester_type[0][0] )
possibilities = self._rates[ set_mask ]['ratio'].values
relative_sample_size = self._rates[ set_mask ]['tamanio_relativo'].values
risk = np_average(possibilities, weights=student_membership, axis=0)\
+ semester_type[1][0][semester_type[0][0]]**2
#certainty = 1. - 0.2488
quality = np_average(relative_sample_size, weights=student_membership, axis=0) #+ certainty**2
if quality > 1:
quality = 1.
if risk > 1:
risk = 1.
elif self._academic_clusterer.source == 'kuleuven':
# _semester_features = AcademicFailureEstimator.get_courses_as_bitarray( semester )
gpa_df = self._academic_clusterer.ha_df.drop_duplicates(['student','GPA'])
_semester_features = self.get_semester_f( semester )
tmp = gpa_df[ gpa_df['student']==student_ID ]
# _student_features = [ tmp['GPA'],tmp['performance_y'] ]
_student_features = [ tmp['GPA'] ]
# student_semester = student_features[0].tolist() + _semester_features
student_semester = _student_features + _semester_features
predict_proba, q = self.classifier_fn( student_semester )
risk = predict_proba[0][0]
quality = q
"""
####################################################################
# student_semester = list( semester_features ) + list( student_features[0] )
semester_features = AcademicFailureEstimator.get_courses_as_bitarray( semester )
####################################################################
# student_semester = student_features.tolist()[0].append( self._km.predict( semester_features ) )
# student_semester = [ self._fm.predict(student_features)[0], self._km.predict( semester_features )[0]]
# print student_semester
semester_type = self._km.predict( semester_features )[0]
U_, _, _, _, _, fpc_ = self.students_classifier_fn(student_features)
student_membership = U_.T[0]
risk = 0
for student_type in xrange(4):
predict_proba, q = self.classifier_fn( [student_type, semester_type] )
risk += student_membership[student_type]*predict_proba[0][1]
# predict_proba, q = self.classifier_fn( student_semester )
# risk = predict_proba[0][1]
# risk = predict_proba[0]
quality = q
"""
return risk, quality
def get_features(self, student_ID, semester):
"""
"""
abs_df = self._academic_clusterer.courses_features
tmp_df = abs_df[ abs_df[self._academic_clusterer.course_attr].isin(semester) ]
tse_df = self._academic_clusterer.semesters_features
tse_df = tse_df[ tse_df[self._academic_clusterer.studentId_attr]==student_ID ]
if tse_df.empty:
semester_lvl = 1
else:
semester_lvl = tse_df[self._academic_clusterer.SEMESTERS_F_LABELS[0]].values.max() + 1
alpha = tmp_df['alpha'].values.sum()
beta = tmp_df['beta'].values.sum()
skewness = tmp_df['skewness'].values.sum()
n_courses = len( semester )
semester_features = (semester_lvl, alpha, beta, skewness, n_courses)
print(semester_features)
cs_df = self._academic_clusterer.students_features
cs_df = cs_df[ cs_df[self._academic_clusterer.studentId_attr] == student_ID ]
if cs_df.empty:
student_features = np_zeros((1,5))
else:
student_features = cs_df[ self._academic_clusterer.STUDENTS_F_LABELS ].values
return semester_features, student_features