def alumni(): len_data = get_data_length() alumni = get_all_alumni() # POST METHOD if request.method == "POST": file = check_upload_file(request) if not file: flash('Ouch! Terjadi Error, Silahkan Periksa Ulang File Anda!', 'danger') return redirect(request.url) try: timestr = time.strftime("%Y%m%d") filename = timestr + "_" + secure_filename(file.filename) path = os.path.join(Config.ROOT_DIRECTORY, "NBC", "static", "upload", "alumni", filename) file.save(path) csv = pd.read_csv(path) for i, row in csv.iterrows(): # check if the data already exist exst = get_an_alumni(row['NIM']) if exst: flash( 'ID Terduplikasi, Tidak ada perubahan yang dilakukan.', 'danger') return redirect(request.url) for index, row in csv.iterrows(): data_alumni = Alumni(id=row['NIM'], school_type=row['Tipe Sekolah'], gender=row['Gender'], school_city=row['Kota Sekolah'], ket_lulus=row['Keterangan Lulus']) # make ipk ipk = (row['IPS_1'] + row['IPS_2'] + row['IPS_3'] + row['IPS_4']) / 4 # save the IP value data_nilai = Nilai(id_alumni=row['NIM'], semester_1=row['IPS_1'], semester_2=row['IPS_2'], semester_3=row['IPS_3'], semester_4=row['IPS_4'], ipk=float("%.2f" % ipk)) save_to_db(data_alumni) save_to_db(data_nilai) flash('Successfully menambahkan data alumni', 'success') return redirect(request.url) except Exception as e: flash('Error: {}'.format(e), 'danger') return redirect(request.url) return render_template('admin_data_alumni.html', data=alumni, len_data=len_data, modal_for='alumni')
def training(): training_data = get_all_training() len_data = get_data_length() if request.method == "POST": file = check_upload_file(request) if not file: flash('Ouch! Terjadi Error, Silahkan Periksa Ulang File Anda!', 'danger') return redirect(request.url) try: # should delete this or no ? delete_all_training() timestr = time.strftime("%Y%m%d") filename = timestr + "_" + secure_filename(file.filename) path = os.path.join(Config.ROOT_DIRECTORY, "NBC", "static", "upload", "training", filename) file.save(path) csv = pd.read_csv(path) for i, row in csv.iterrows(): # check if the data already exist exst = get_a_training(row['NIM']) if exst: flash( 'ID Terduplikasi, Tidak ada perubahan yang dilakukan.', 'danger') return redirect(request.url) for index, row in csv.iterrows(): # make ipk ipk = (row['IPS_1'] + row['IPS_2'] + row['IPS_3'] + row['IPS_4']) / 4 data_training = Training(id=row['NIM'], school_type=row['Tipe Sekolah'], gender=row['Gender'], school_city=row['Kota Sekolah'], ket_lulus=row['Keterangan Lulus'], semester_1=row['IPS_1'], semester_2=row['IPS_2'], semester_3=row['IPS_3'], semester_4=row['IPS_4'], ipk=float("%.2f" % ipk)) save_to_db(data_training) flash('Success menambahkan data alumni', 'success') return redirect( url_for('dashboard.cross_validation', dt='training')) except Exception as e: flash('Error: {}'.format(e), 'danger') return redirect(request.url) return render_template('admin_data_training.html', data=training_data, len_data=len_data, modal_for='data training')
def register(): if request.method == "POST": email = request.form.get('email') password = request.form.get('password') first_name = request.form.get('firstname') last_name = request.form.get('lastname') phone = request.form.get('phone') if not email or not password or not first_name or not last_name: flash('Please fill all form!', 'danger') return redirect(url_for('dashboard.create_user')) if phone: phone = '+62' + phone data = User(email=email, password=password, first_name=first_name, last_name=last_name, registered_on=datetime.datetime.utcnow(), phone_number=phone, role='Admin') save_to_db(data) flash('User berhasil dibuat', 'success') return redirect(url_for('auth.login')) return render_template('register.html')
def create_alumni(): if request.method == "POST": try: # get data from form id = request.form.get('angkatan') + '.11.' + request.form.get( 'id').zfill(4) semester_1 = float(request.form.get('semester_1')) semester_2 = float(request.form.get('semester_2')) semester_3 = float(request.form.get('semester_3')) semester_4 = float(request.form.get('semester_4')) ipk = (semester_1 + semester_2 + semester_3 + semester_4) / 4 # check if the nim is conflict or not exst = get_an_alumni(id) if exst: flash('Error: NIM Conflict!', 'danger') return redirect(request.url) # make an Alumni model object que_alumni = Alumni(id=id, school_type=request.form.get('school_type'), gender=request.form.get('gender'), school_city=request.form.get('school_city'), ket_lulus=request.form.get('ket_lulus')) que_nilai = Nilai(id_alumni=id, semester_1=semester_1, semester_2=semester_2, semester_3=semester_3, semester_4=semester_4, ipk=float("%.2f" % ipk)) # insert into database save_to_db(que_alumni) save_to_db(que_nilai) flash('Success membuat data alumni.', 'success') return redirect(url_for('dashboard.create_alumni')) except Exception as e: flash('Error: {}'.format(e)) return redirect(request.url) return render_template('admin_create_alumni.html')
def cross_validation(dt): try: # query all the data if dt == 'alumni': dtobj = get_all_alumni() else: dtobj = get_all_training() if dtobj.empty: flash('Tidak ada data.', 'danger') return redirect(url_for('dashboard.alumni')) # convert class label into number, so that it will not be encoded dtobj['ket_lulus'] = dtobj['ket_lulus'].replace( ['Tidak Tepat Waktu', 'Tepat Waktu'], [0, 1]) # based on you should never change what you itterating over, make a list and make a new data frame mydf = [] for i, row in dtobj.iterrows(): mydf.append({ 'id': row['id'], 'school_type': row['school_type'], 'gender': row['gender'], 'school_city': row['school_city'], 'semester_1': convert_nilai(row['semester_1']), 'semester_2': convert_nilai(row['semester_2']), 'semester_3': convert_nilai(row['semester_3']), 'semester_4': convert_nilai(row['semester_4']), 'ipk': convert_nilai(row['ipk']), 'ket_lulus': row['ket_lulus'] }) df = pd.DataFrame(mydf) # One Hot Encoder enc = pd.get_dummies(df.drop(['id'], axis=1)) # Split X, and Y x = np.array(enc.drop('ket_lulus', axis=1)) y = np.array(enc['ket_lulus']) # determine the label for confusion matrix labels = np.unique(y) # create the model model = MultinomialNB() # initialize kfold kf = KFold(n_splits=10) # initialize confusion matrix cf = np.array([[0, 0], [0, 0]]) f1 = [] recall = [] precision = [] scores = [] for train_index, test_index in kf.split(x): x_train, x_test = x[train_index], x[test_index] y_train, y_test = y[train_index], y[test_index] # fit the model, then predict the test fold model.fit(x_train, y_train) y_pred = model.predict(x_test) # compute confusion matrix, ROC curve and AUC cf += confusion_matrix(y_test, y_pred, labels=labels) # calculate the accuracy, f1-score, recall, precision f1.append(f1_score(y_test, y_pred)) recall.append(recall_score(y_test, y_pred)) precision.append(precision_score(y_test, y_pred)) scores.append(model.score(x_test, y_test)) dfcv = pd.concat([ pd.DataFrame(scores, columns=['accuracy']), pd.DataFrame(precision, columns=['precision']), pd.DataFrame(recall, columns=['recall']), pd.DataFrame(f1, columns=['f1']) ], axis=1) path_model_kfold = os.path.join(Config.STATIC_DIRECTORY, 'kfold_model.png') path_result_kfold = os.path.join(Config.STATIC_DIRECTORY, 'kfold_result.png') create_bar_chart(path_model_kfold, f1, recall, precision, scores) if dt == 'training': dfcf = pd.DataFrame(cf, columns=['P_Negative', 'P_Positive']) create_bar_chart(path_result_kfold, f1, recall, precision, scores) dfcv.to_csv('current_model_cv.csv', index=False, encoding='utf-8') dfcf.to_csv('current_model_cf.csv', index=False, encoding='utf-8') # if create model button is clicked if request.method == "POST": delete_all_training() create_bar_chart(path_result_kfold, f1, recall, precision, scores) dtobj['ket_lulus'] = dtobj['ket_lulus'].replace( [0, 1], ['Tidak Tepat Waktu', 'Tepat Waktu']) for i, row in dtobj.iterrows(): data = Training(id=row['id'], school_type=row['school_type'], gender=row['gender'], school_city=row['school_city'], semester_1=row['semester_1'], semester_2=row['semester_2'], semester_3=row['semester_3'], semester_4=row['semester_4'], ipk=row['ipk'], ket_lulus=row['ket_lulus']) save_to_db(data) dfcf = pd.DataFrame(cf, columns=['P_Negative', 'P_Positive']) dfcv.to_csv('current_model_cv.csv', index=False, encoding='utf-8') dfcf.to_csv('current_model_cf.csv', index=False, encoding='utf-8') flash('Successfully create a model', 'success') return redirect(url_for('dashboard.index')) # < END POST REQUEST > return render_template('cross_validation.html', scores=dfcv, cf=cf, dt=dt) except Exception as e: flash('Error: {}'.format(e), 'danger') return redirect(url_for('dashboard.alumni'))
def predict_csv(): train_data = get_all_training() if request.method == "POST": try: file = check_upload_file(request) if not file: flash('Ouch! Terjadi Error, Silahkan Periksa Ulang File Anda!', 'danger') return redirect(request.url) timestr = time.strftime("%Y%m%d") filename = timestr + "_" + secure_filename(file.filename) path = os.path.join(Config.ROOT_DIRECTORY, "NBC", "static", "upload", "prediksi", filename) file.save(path) csv = pd.read_csv(path) for i, row in csv.iterrows(): exst = get_a_prediction(row['NIM']) if exst: flash( 'ID Terduplikasi, Tidak ada perubahan yang dilakukan.', 'danger') return redirect(request.url) # END FOR # make list of dict data_target = [] for index, row in csv.iterrows(): ipk = (row['IPS_1'] + row['IPS_2'] + row['IPS_3'] + row['IPS_4']) / 4 # save the data into database # the reason is, i want to store the value with number data type, not discret. obj_testing = Testing(id=row['NIM'], school_type=row['Tipe Sekolah'], gender=row['Gender'], school_city=row['Kota Sekolah'], semester_1=row['IPS_1'], semester_2=row['IPS_2'], semester_3=row['IPS_3'], semester_4=row['IPS_4'], ipk=float("%.2f" % ipk)) save_to_db(obj_testing) # convert all IP and insert it to list data_target.append({ 'id': row['NIM'], 'school_type': row['Tipe Sekolah'], 'gender': row['Gender'], 'school_city': row['Kota Sekolah'], 'semester_1': convert_nilai(row['IPS_1']), 'semester_2': convert_nilai(row['IPS_2']), 'semester_3': convert_nilai(row['IPS_3']), 'semester_4': convert_nilai(row['IPS_4']), 'ipk': convert_nilai(ipk) }) # END FOR y_pred = predict_data(train_data, data_target) # make data frame from y pred y_pred = pd.DataFrame(y_pred, columns=['result']) y_pred['result'] = y_pred['result'].replace( [0, 1], ['Tidak Tepat Waktu', 'Tepat Waktu']) # insert into database # combine the result and the data dfr = pd.concat([pd.DataFrame(data_target), y_pred], axis=1, sort=True) for i, row in dfr.iterrows(): obj_hasil = Hasil(id_testing=row['id'], result=row['result']) save_to_db(obj_hasil) flash('Prediksi Berhasil', 'success') return redirect(url_for('dashboard.predict')) except Exception as e: flash('Error: {}'.format(e), 'danger') return redirect(request.url) return render_template('admin_prediksi_csv.html')
def create_predict(): if request.method == "POST": try: # get training data for prediction train_data = get_all_training() if train_data.empty: flash( 'Tidak ada data training, silahkan buat model terlebih dahulu.', 'danger') return redirect(request.url) if not request.form.get('id'): flash('Fill all empty form!', 'danger') return redirect(request.url) # get the data from frontend form id = request.form.get('angkatan') + '.11.' + request.form.get( 'id').zfill(4) # check if the nim already in databases exst = get_a_prediction(id) # if already in databases, return with alert error if exst: flash('Error, NIM Duplicated!', 'danger') return redirect(request.url) semester_1 = float(request.form.get('semester_1')) semester_2 = float(request.form.get('semester_2')) semester_3 = float(request.form.get('semester_3')) semester_4 = float(request.form.get('semester_4')) ipk = (semester_1 + semester_2 + semester_3 + semester_4) / 4 # change the value into categorical test_data = { 'id': id, 'school_type': request.form.get('school_type'), 'gender': request.form.get('gender'), 'school_city': request.form.get('school_city'), 'semester_1': convert_nilai(semester_1), 'semester_2': convert_nilai(semester_2), 'semester_3': convert_nilai(semester_3), 'semester_4': convert_nilai(semester_4), 'ipk': convert_nilai(ipk) } y_pred = predict_data(train_data, test_data, index=True) # convert the result into string if y_pred == 0: y_pred = 'Tidak Tepat Waktu' else: y_pred = 'Tepat Waktu' # insert into database que_test = Testing(id=test_data['id'], school_type=test_data['school_type'], gender=test_data['gender'], school_city=test_data['school_city'], semester_1=semester_1, semester_2=semester_2, semester_3=semester_3, semester_4=semester_4, ipk=float("%.2f" % ipk)) que_hasil = Hasil(id_testing=test_data['id'], result=y_pred) save_to_db(que_test) save_to_db(que_hasil) flash('Success memprediksi data.', 'success') return redirect(url_for('dashboard.predict')) except Exception as e: flash('Error: {}'.format(e), 'danger') return redirect(request.url) return render_template('admin_create_prediksi.html')