Spaces:
Runtime error
Runtime error
Mustafa Al Hamad commited on
Commit ·
d30b88c
1
Parent(s): b21b9f6
Update app.py
Browse files
app.py
CHANGED
|
@@ -29,6 +29,7 @@ from sklearn.feature_selection import SequentialFeatureSelector
|
|
| 29 |
from sklearn.model_selection import GridSearchCV, StratifiedKFold
|
| 30 |
import docx
|
| 31 |
from docx.enum.dml import MSO_THEME_COLOR_INDEX
|
|
|
|
| 32 |
def add_hyperlink(paragraph, text, url):
|
| 33 |
# This gets access to the document.xml.rels file and gets a new relation id value
|
| 34 |
part = paragraph.part
|
|
@@ -71,20 +72,10 @@ def savedoc(document,name):
|
|
| 71 |
from sklearn.metrics import accuracy_score, confusion_matrix, roc_auc_score, roc_curve, cohen_kappa_score, f1_score, recall_score, precision_score
|
| 72 |
def measures(predicted, y_test):
|
| 73 |
accuracy = accuracy_score(y_test, predicted)
|
| 74 |
-
#print('Accuracy: %f' % accuracy)
|
| 75 |
precision = precision_score(y_test, predicted)
|
| 76 |
-
#print('Precision: %f' % precision)
|
| 77 |
recall = recall_score(y_test, predicted)
|
| 78 |
-
#print('Recall: %f' % recall)
|
| 79 |
f1 = f1_score(y_test, predicted)
|
| 80 |
-
#print('F1 score: %f' % f1)
|
| 81 |
-
kappa = cohen_kappa_score(y_test, predicted)
|
| 82 |
-
#print('Cohens kappa: %f' % kappa)
|
| 83 |
-
auc = roc_auc_score(y_test, predicted)
|
| 84 |
-
#print('ROC AUC: %f' % auc)
|
| 85 |
matrix = confusion_matrix(y_test, predicted)
|
| 86 |
-
#print('Confusion Matrix')
|
| 87 |
-
#print(matrix)
|
| 88 |
return accuracy
|
| 89 |
|
| 90 |
def greet(operation,filer):
|
|
@@ -94,8 +85,13 @@ def greet(operation,filer):
|
|
| 94 |
coset = pd.read_csv(filer.name)
|
| 95 |
coset = coset.dropna(how='any')
|
| 96 |
document = Document('temp.docx')
|
|
|
|
| 97 |
if operation == "retrain":
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
def termize(x):
|
| 100 |
if str(x)[-1] == "1":
|
| 101 |
return 0
|
|
@@ -173,7 +169,6 @@ def greet(operation,filer):
|
|
| 173 |
document.add_paragraph('This report consists of the models retraining information on the new dataset with ('+str(len(coset))+') records')
|
| 174 |
records = []
|
| 175 |
|
| 176 |
-
#print(majors,catalog,acad_prog,instructor)
|
| 177 |
X = wanted.drop(columns=['GRADE'])
|
| 178 |
y = wanted['GRADE']
|
| 179 |
smote = BorderlineSMOTE(random_state = 11)
|
|
@@ -190,8 +185,6 @@ def greet(operation,filer):
|
|
| 190 |
for model in models1:
|
| 191 |
model.fit(X_smote,y_smote)
|
| 192 |
preds = cross_val_predict(model, X_smote.values,y_smote.values, cv=kf, n_jobs=-1,);
|
| 193 |
-
#print("------------AFTER SMOTE------------")
|
| 194 |
-
#print(model);
|
| 195 |
metrics[model] = measures(preds,y_smote.values)
|
| 196 |
records.append(((str(type(model).__name__),str(metrics[model]))))
|
| 197 |
document.add_paragraph(' ')
|
|
@@ -219,6 +212,16 @@ def greet(operation,filer):
|
|
| 219 |
model = max(metrics, key=metrics.get)
|
| 220 |
number = number + 1
|
| 221 |
filename = str(number)+"="+type(model).__name__+'='+str(acc)+'.sav'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
document.add_paragraph(" ")
|
| 223 |
document.add_paragraph(type(model).__name__+' has been chosen as the prediction model for achieving an accuracy of '+str(acc)+'%')
|
| 224 |
pickle.dump(model, open(filename, 'wb'))
|
|
@@ -228,10 +231,13 @@ def greet(operation,filer):
|
|
| 228 |
savedoc(document,'retraining_report.docx')
|
| 229 |
#document.save('retraining_report.docx')
|
| 230 |
return 'retraining_report.docx',str(type(model).__name__+' has been chosen as the prediction model for achieving an accuracy of '+str(acc)+'%')
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
| 235 |
dir_name = str(os.getcwd())
|
| 236 |
test = os.listdir(dir_name)
|
| 237 |
modelname = ""
|
|
@@ -242,6 +248,20 @@ def greet(operation,filer):
|
|
| 242 |
modelname = item
|
| 243 |
if maxnum == 0:
|
| 244 |
return None,"No model found, please use retrain operation to build one"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
#modelname = "VotingClassifier=0.95756598831352.sav"
|
| 246 |
loaded_model = pickle.load(open(modelname, 'rb'))
|
| 247 |
droppers = 0
|
|
@@ -261,7 +281,7 @@ def greet(operation,filer):
|
|
| 261 |
elif row['TERM'][-1] == 5:
|
| 262 |
semester = 3
|
| 263 |
c_id = catalog.index(str(row['CATALOG_NBR']))
|
| 264 |
-
in_id = instructor.index(row['INSTRUCTOR_ID'])
|
| 265 |
p_id = acad_prog.index(row['PROGRAM'])
|
| 266 |
major = 0
|
| 267 |
x = row['PROGRAM.1']
|
|
|
|
| 29 |
from sklearn.model_selection import GridSearchCV, StratifiedKFold
|
| 30 |
import docx
|
| 31 |
from docx.enum.dml import MSO_THEME_COLOR_INDEX
|
| 32 |
+
|
| 33 |
def add_hyperlink(paragraph, text, url):
|
| 34 |
# This gets access to the document.xml.rels file and gets a new relation id value
|
| 35 |
part = paragraph.part
|
|
|
|
| 72 |
from sklearn.metrics import accuracy_score, confusion_matrix, roc_auc_score, roc_curve, cohen_kappa_score, f1_score, recall_score, precision_score
|
| 73 |
def measures(predicted, y_test):
|
| 74 |
accuracy = accuracy_score(y_test, predicted)
|
|
|
|
| 75 |
precision = precision_score(y_test, predicted)
|
|
|
|
| 76 |
recall = recall_score(y_test, predicted)
|
|
|
|
| 77 |
f1 = f1_score(y_test, predicted)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
matrix = confusion_matrix(y_test, predicted)
|
|
|
|
|
|
|
| 79 |
return accuracy
|
| 80 |
|
| 81 |
def greet(operation,filer):
|
|
|
|
| 85 |
coset = pd.read_csv(filer.name)
|
| 86 |
coset = coset.dropna(how='any')
|
| 87 |
document = Document('temp.docx')
|
| 88 |
+
allowedcols = ['SID', 'TERM', 'CATALOG_NBR', 'INSTRUCTOR_ID', 'GRADE', 'CGPA', 'PROGRAM', 'PROGRAM.1']
|
| 89 |
if operation == "retrain":
|
| 90 |
+
allowedcols = allowedcols[1:]
|
| 91 |
+
for col in coset.columns:
|
| 92 |
+
if col not in allowedcols:
|
| 93 |
+
return None,str(col)+" is undefined column name, allowed columns for training are "+str(allowedcols)
|
| 94 |
+
wanted = coset#.drop(columns=['SUBJECT','SID','CRSE_ID','COURSE','ROLE','GPA','INPUT','STATUS','GRADUATION TERM','CLASS #','COLLEGE','COLLEGE.1'])
|
| 95 |
def termize(x):
|
| 96 |
if str(x)[-1] == "1":
|
| 97 |
return 0
|
|
|
|
| 169 |
document.add_paragraph('This report consists of the models retraining information on the new dataset with ('+str(len(coset))+') records')
|
| 170 |
records = []
|
| 171 |
|
|
|
|
| 172 |
X = wanted.drop(columns=['GRADE'])
|
| 173 |
y = wanted['GRADE']
|
| 174 |
smote = BorderlineSMOTE(random_state = 11)
|
|
|
|
| 185 |
for model in models1:
|
| 186 |
model.fit(X_smote,y_smote)
|
| 187 |
preds = cross_val_predict(model, X_smote.values,y_smote.values, cv=kf, n_jobs=-1,);
|
|
|
|
|
|
|
| 188 |
metrics[model] = measures(preds,y_smote.values)
|
| 189 |
records.append(((str(type(model).__name__),str(metrics[model]))))
|
| 190 |
document.add_paragraph(' ')
|
|
|
|
| 212 |
model = max(metrics, key=metrics.get)
|
| 213 |
number = number + 1
|
| 214 |
filename = str(number)+"="+type(model).__name__+'='+str(acc)+'.sav'
|
| 215 |
+
|
| 216 |
+
datavalues = {"majors":str(majors),
|
| 217 |
+
'acad_prog':str(acad_prog),
|
| 218 |
+
'catalog':str(catalog),
|
| 219 |
+
'instructor':str(instructor)
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
dfv = pd.DataFrame(datavalues,index=[0])
|
| 223 |
+
dfv.to_csv(str(number)+"="+"values.csv")
|
| 224 |
+
|
| 225 |
document.add_paragraph(" ")
|
| 226 |
document.add_paragraph(type(model).__name__+' has been chosen as the prediction model for achieving an accuracy of '+str(acc)+'%')
|
| 227 |
pickle.dump(model, open(filename, 'wb'))
|
|
|
|
| 231 |
savedoc(document,'retraining_report.docx')
|
| 232 |
#document.save('retraining_report.docx')
|
| 233 |
return 'retraining_report.docx',str(type(model).__name__+' has been chosen as the prediction model for achieving an accuracy of '+str(acc)+'%')
|
| 234 |
+
for col in coset.columns:
|
| 235 |
+
if col not in allowedcols:
|
| 236 |
+
return None,str(col)+" is undefined column name, allowed columns for prediction are "+str(allowedcols)
|
| 237 |
+
majors = []
|
| 238 |
+
catalog = []
|
| 239 |
+
acad_prog = []
|
| 240 |
+
instructor = []
|
| 241 |
dir_name = str(os.getcwd())
|
| 242 |
test = os.listdir(dir_name)
|
| 243 |
modelname = ""
|
|
|
|
| 248 |
modelname = item
|
| 249 |
if maxnum == 0:
|
| 250 |
return None,"No model found, please use retrain operation to build one"
|
| 251 |
+
dfv = pd.read_csv(str(maxnum)+"=values.csv")
|
| 252 |
+
|
| 253 |
+
cols = [majors,acad_prog,catalog,instructor]
|
| 254 |
+
indexc = 0
|
| 255 |
+
|
| 256 |
+
for column in dfv.columns:
|
| 257 |
+
if "[" in str(dfv[column][0]):
|
| 258 |
+
l = dfv[column][0].replace("'",'')
|
| 259 |
+
cols[indexc][:] = str(l).strip('][').split(', ')
|
| 260 |
+
|
| 261 |
+
for i,e in enumerate(cols[indexc]):
|
| 262 |
+
cols[indexc][i] = e.replace(' ','')
|
| 263 |
+
print(cols[indexc])
|
| 264 |
+
indexc = indexc + 1
|
| 265 |
#modelname = "VotingClassifier=0.95756598831352.sav"
|
| 266 |
loaded_model = pickle.load(open(modelname, 'rb'))
|
| 267 |
droppers = 0
|
|
|
|
| 281 |
elif row['TERM'][-1] == 5:
|
| 282 |
semester = 3
|
| 283 |
c_id = catalog.index(str(row['CATALOG_NBR']))
|
| 284 |
+
in_id = instructor.index(str(row['INSTRUCTOR_ID']))
|
| 285 |
p_id = acad_prog.index(row['PROGRAM'])
|
| 286 |
major = 0
|
| 287 |
x = row['PROGRAM.1']
|