def compute_classes(XAll, YAll, PERC_TRAIN):
n = XAll.shape[0]
m = XAll.shape[1]
indI = np.arange(n)
indTrain = np.random.permutation(n)[0:int(PERC_TRAIN*n)]
indTest = np.setdiff1d(indI, indTrain)
nTrain = indTrain.size
nTest = indTest.size
indJ = np.arange(m)
phat = np.mean(YAll[indTrain])
model = ConcreteModel()
model.indI = Set(initialize=indI.tolist())
model.indTrain = Set(initialize=indTrain.tolist())
model.indTest = Set(initialize=indTest.tolist())
model.indJ = Set(initialize=indJ.tolist())
model.X = Var( model.indI, within=Binary )
def obj_rule(model):
z = ( sum( YAll[i]*model.X[i] for i in model.indTrain )/nTrain ) + ( ( sum( (1-model.X[i]) * sum( ( XAll[i,j] - (sum(XAll[k,j]*model.X[k] for k in model.indI))/(sum(model.X[k] for k in model.indI)) )**2 for j in model.indJ ) for i in model.indI ) ) / sum( (1-model.X[i]) for i in model.indI) ) - ( ( sum( model.X[i] * sum( ( XAll[i,j] - (sum(XAll[k,j]*model.X[k] for k in model.indI))/(sum(model.X[k] for k in model.indI)) )**2 for j in model.indJ ) for i in model.indI ) ) / sum(model.X[i] for i in model.indI) )
return z
def const_rule0(model):
z = ( ( sum( (1-model.X[i]) * sum( ( XAll[i,j] - (sum(XAll[k,j]*model.X[k] for k in model.indI))/(sum(model.X[k] for k in model.indI)) )**2 for j in model.indJ ) for i in model.indI ) ) / sum((1-model.X[i]) for i in model.indI) ) >= \
( ( sum( model.X[i] * sum( ( XAll[i,j] - (sum(XAll[k,j]model.X[k] for k in model.indI))/(sum(model.X[k] for k in model.indI)) )*2 for j in model.indJ ) for i in model.indI ) ) / sum(model.X[i] for i in model.indI) )
return z
def const_rule1(model):
z = sum(model.X[i] for i in model.indI) <= np.ceil( n*phat + 1.96 * np.sqrt(phat)*np.sqrt(1-phat)*np.sqrt(n) )
return z
def const_rule2(model):
z = sum(model.X[i] for i in model.indI) >= np.floor( n*phat - 1.96 * np.sqrt(phat)*np.sqrt(1-phat)*np.sqrt(n) )
return z
model.value = Objective(rule=obj_rule , sense = maximize )
model.constr0 = Constraint(rule = const_rule0 )
model.constr1 = Constraint(rule = const_rule1 )
model.constr2 = Constraint(rule = const_rule2 )
solver = SolverFactory('ipopt')
solver.solve(model)
model.X.display()
model.pprint()
YhatAll = np.int64(1 - np.array(list(model.X.get_values().values())))
******************************************************************************
Error = np.count_nonzero(np.abs(np.int64(YAll[indTest]) - YhatAll[indTest])) / np.float64(nTest)
A = np.zeros(shape=(2,2), dtype=np.float64)
for i in np.arange(nTest):
if np.int64(YAll[indTest[i]]) == 1 and np.int64(YhatAll[indTest[i]]) == 1:
A[1,1] += 1
elif np.int64(YAll[indTest[i]]) == 0 and np.int64(YhatAll[indTest[i]]) == 0:
A[0,0] += 1
elif np.int64(YAll[indTest[i]]) == 1 and np.int64(YhatAll[indTest[i]]) == 0:
A[1,0] += 1
elif np.int64(YAll[indTest[i]]) == 0 and np.int64(YhatAll[indTest[i]]) == 1:
A[0,1] += 1
sens = A[1,1] / np.sum(A[1,:])
spec = A[0,0] / np.sum(A[0,:])
return (sens, spec)
***************************************************************************************************************
open the file
XAll = pd.read_excel("./studentnew.xlsx", sheet_name = 0)
________________
XAll = XAll[np.where( XAll[:,-1] < 2.0 )]
________________
YAll = XAll[0:1000,-1]
XAll = XAll[0:1000,0:-1]