def run_logreg(): # CV print() recs, precs, accs = [], [], [] for i in range(len(cv_splits)): print('CV Epoch : ' + str(i + 1)) cv_train, cv_test = train_test_split(cv_splits[i]) cv_train_X, cv_train_Y = get_X_Y(cv_train) cv_test_X, cv_test_Y = get_X_Y(cv_test) mlr_fit = mlr.fit(cv_train_X, cv_train_Y) cv_pred = mlr.predict(cv_test_X) # The Coefficients print('Coefficients : \n', mlr.coef_) # Recall Score recall = r_s(cv_test_Y, cv_pred) print('Recall Score : \n', recall) # Precision Score precision = p_s(cv_test_Y, cv_pred) print('Precision Score : \n', precision) # Accuracy Score accuracy = a_s(cv_test_Y, cv_pred) print('Accuracy Score : \n', accuracy) # Conusion Matix print('Confusion Matrix : \n', c_m(cv_test_Y, cv_pred)) recs.append(recall) precs.append(precision) accs.append(accuracy) print() print('Average Recall Score : %f' % np.mean(recs)) print('Average Precision Score : %f' % np.mean(precs)) print('Average Accuracy Score : %f' % np.mean(accs)) print() # Test test_X, test_Y = get_X_Y(data_test) test_pred = mlr.predict(test_X) # The Coefficients print('Test Coefficients : \n', mlr.coef_) # Recall Score recall = r_s(test_Y, test_pred) print('Recall Score : \n', recall) # Precision Score precision = p_s(test_Y, test_pred) print('Precision Score : \n', precision) # Accuracy Score accuracy = a_s(test_Y, test_pred) print('Accuracy Score : \n', accuracy) # Conusion Matix print('Confusion Matrix : \n', c_m(test_Y, test_pred)) print() return None
def sgs(): # gaussian/rbf print() tg = time.time() cs = [0.1, 0.5, 1.0, 2.0, 5.0] sigmas = [0.1, 0.5, 1.0, 2.0, 4.0] hyperparams = {'C': cs, 'gamma': sigmas} rbf_svc = SVC(kernel='rbf', C=cs, gamma=sigmas, cache_size=4096) rbf_svc_clf = GridSearchCV(rbf_svc, hyperparams, cv=5) cv_train_X, cv_train_Y = get_X_Y(data_cv) rbf_svc_fit = rbf_svc_clf.fit(cv_train_X, cv_train_Y) rbf_svc_res = rbf_svc_clf.cv_results_ rbf_svc_params = rbf_svc_clf.best_params_ rbf_svc_score = rbf_svc_clf.best_score_ test_X, test_Y = get_X_Y(data_test) test_pred = rbf_svc_clf.predict(test_X) # The Coefficients print('Test Estimator : \n', rbf_svc_clf.best_estimator_) # Recall Score recall = r_s(test_Y, test_pred) print('Recall Score : \n', recall) # Precision Score precision = p_s(test_Y, test_pred) print('Precision Score : \n', precision) # Accuracy Score accuracy = a_s(test_Y, test_pred) print('Accuracy Score : \n', accuracy) # Conusion Matix print('Confusion Matrix : \n', c_m(test_Y, test_pred)) tg = time.time() - tg print('Time Secs : %f' % tg) return None
def sls(): # linear print() tl = time.time() cs = [0.1, 0.5, 1.0, 2.0, 5.0] lin_svc = SVC(C=cs, kernel='linear', cache_size=4096) hyperparams = {'C': cs} lin_svc_clf = GridSearchCV(lin_svc, hyperparams, cv=5) cv_train_X, cv_train_Y = get_X_Y(data_cv) lin_svc_fit = lin_svc_clf.fit(cv_train_X, cv_train_Y) lin_svc_res = lin_svc_clf.cv_results_ lin_svc_params = lin_svc_clf.best_params_ lin_svc_score = lin_svc_clf.best_score_ test_X, test_Y = get_X_Y(data_test) test_pred = lin_svc_clf.predict(test_X) # The Coefficients print('Test Estimator : \n', lin_svc_clf.best_estimator_) # Recall Score recall = r_s(test_Y, test_pred) print('Recall Score : \n', recall) # Precision Score precision = p_s(test_Y, test_pred) print('Precision Score : \n', precision) # Accuracy Score accuracy = a_s(test_Y, test_pred) print('Accuracy Score : \n', accuracy) # Conusion Matix print('Confusion Matrix : \n', c_m(test_Y, test_pred)) tl = time.time() - tl print('Time Secs : %f' % tl) return None
def _train_all(names, classifiers, X, y, X_train, X_test, y_train, y_test, stats=True, predict=""): """ Train each of the classifiers, and either output score or the predictions """ ## ignore numpy warnings from warnings import filterwarnings filterwarnings('ignore') ## cycle around each classifier classes = {1: "LIKELY", -1: "UNLIKELY"} score = {1: 0, -1: 0} trusts = {} predictions = {} for name, classifier in zip(names, classifiers): ## train each classifier classifier.fit(X_train, y_train) if stats == True: _get_statistics(name, classifier, X, y, X_test, y_test) if predict != "": ## Make prediction prediction = classifier.predict(predict)[0] ## Increment counter for relevant score score[prediction] += 1 predictions.update({name: prediction}) """ reveal expected true negatives, false positives, false negatives, true positives """ tn, fp, fn, tp = c_m(y_test, classifier.predict(X_test)).ravel() ## trust is the amount of time that the prediction was correct trust_score = tp / (tp + fp) if prediction == 1 else tn / (tn + fn) trust_score = round((trust_score * 100), 2) trusts.update({name: trust_score}) if predict != "": scores = pd.DataFrame({ 'Recurrence': predictions, 'Confidence': trusts }) pred_weight = scores.Recurrence * scores.Confidence weights = pd.DataFrame({'Weights': pred_weight}) scores['Recurrence'] = scores['Recurrence'].apply(lambda x: classes[x]) print(scores) classification = 1 if weights.Weights.mean() > 0 else -1 print(f"\nRecurrence judged {classes[classification]} at \ {round(abs(weights.Weights.mean()),2)} % confidence") print(f"Poll of classifiers results:") for index in score: print(f"{classes[index]}: \t\t{score[index]}")
def kp(): # polynomial print() tp = time.time() recs, precs, accs = [], [], [] alphas = [1.0] degs = [2.0, 3.0] # M hyperparams = {'alpha': alphas, 'degree': degs} poly_krr = KernelRidge(kernel='poly', alpha=alphas, degree=degs, gamma=1, coef0=1) poly_krr_clf = GridSearchCV(poly_krr, hyperparams, cv=5) for batch in data_batches: batch_train, batch_test = train_test_split(batch) cv_train_X, cv_train_Y = get_X_Y(batch_train) poly_krr_fit = poly_krr_clf.fit(cv_train_X, cv_train_Y) poly_krr_res = poly_krr_clf.cv_results_ poly_krr_params = poly_krr_clf.best_params_ poly_krr_score = poly_krr_clf.best_score_ test_X, test_Y = get_X_Y(batch_test) test_pred = poly_krr_clf.predict(test_X) # The Coefficients print('Test Estimator : \n', poly_krr_clf.best_estimator_) # Recall Score recall = r_s(test_Y, test_pred) print('Recall Score : \n', recall) # Precision Score precision = p_s(test_Y, test_pred) print('Precision Score : \n', precision) # Accuracy Score accuracy = a_s(test_Y, test_pred) print('Accuracy Score : \n', accuracy) # Conusion Matix print('Confusion Matrix : \n', c_m(test_Y, test_pred)) recs.append(recall) precs.append(precision) accs.append(accuracy) print() print('Average Test Recall Score : %f' % np.mean(recs)) print('Average Test Precision Score : %f' % np.mean(precs)) print('Average Test Accuracy Score : %f' % np.mean(accs)) tp = time.time() - tp print('Time Secs : %f' % tp) return None
def kg(): # gaussian/rbf print() tg = time.time() recs, precs, accs = [], [], [] alphas = [1.0] sigmas = [0.1, 0.5, 1.0, 2.0, 4.0] hyperparams = {'alpha': alphas, 'gamma': sigmas} rbf_krr = KernelRidge(kernel='rbf', alpha=alphas, gamma=sigmas) rbf_krr_clf = GridSearchCV(rbf_krr, hyperparams, cv=5) for batch in data_batches: batch_train, batch_test = train_test_split(batch) cv_train_X, cv_train_Y = get_X_Y(batch_train) rbf_krr_fit = rbf_krr_clf.fit(cv_train_X, cv_train_Y) rbf_krr_res = rbf_krr_clf.cv_results_ rbf_krr_params = rbf_krr_clf.best_params_ rbf_krr_score = rbf_krr_clf.best_score_ test_X, test_Y = get_X_Y(batch_test) test_pred = rbf_krr_clf.predict(test_X) # The Coefficients print('Test Estimator : \n', rbf_krr_clf.best_estimator_) # Recall Score recall = r_s(test_Y, test_pred) print('Recall Score : \n', recall) # Precision Score precision = p_s(test_Y, test_pred) print('Precision Score : \n', precision) # Accuracy Score accuracy = a_s(test_Y, test_pred) print('Accuracy Score : \n', accuracy) # Conusion Matix print('Confusion Matrix : \n', c_m(test_Y, test_pred)) recs.append(recall) precs.append(precision) accs.append(accuracy) print() print('Average Test Recall Score : %f' % np.mean(recs)) print('Average Test Precision Score : %f' % np.mean(precs)) print('Average Test Accuracy Score : %f' % np.mean(accs)) tg = time.time() - tg print('Time Secs : %f' % tg) return None
def kl(): # linear print() tl = time.time() recs, precs, accs = [], [], [] alphas = [1.0] lin_krr = KernelRidge(alpha=alphas, kernel='linear') hyperparams = {'alpha': alphas} lin_krr_clf = GridSearchCV(lin_krr, hyperparams, cv=5) for batch in data_batches: batch_train, batch_test = train_test_split(batch) cv_train_X, cv_train_Y = get_X_Y(batch_train) lin_krr_fit = lin_krr_clf.fit(cv_train_X, cv_train_Y) lin_krr_res = lin_krr_clf.cv_results_ lin_krr_params = lin_krr_clf.best_params_ lin_krr_score = lin_krr_clf.best_score_ test_X, test_Y = get_X_Y(batch_test) test_pred = lin_krr_clf.predict(test_X) # The Coefficients print('Test Estimator : \n', lin_krr_clf.best_estimator_) # Recall Score recall = r_s(test_Y, test_pred) print('Recall Score : \n', recall) # Precision Score precision = p_s(test_Y, test_pred) print('Precision Score : \n', precision) # Accuracy Score accuracy = a_s(test_Y, test_pred) print('Accuracy Score : \n', accuracy) # Conusion Matix print('Confusion Matrix : \n', c_m(test_Y, test_pred)) recs.append(recall) precs.append(precision) accs.append(accuracy) print() print('Average Test Recall Score : %f' % np.mean(recs)) print('Average Test Precision Score : %f' % np.mean(precs)) print('Average Test Accuracy Score : %f' % np.mean(accs)) tl = time.time() - tl print('Time Secs : %f' % tl) return None
def sps(): # polynomial print() tp = time.time() cs = [0.1, 0.5, 1.0, 2.0, 5.0] degs = [2.0, 3.0] # M hyperparams = {'C': cs, 'degree': degs} poly_svc = SVC(kernel='poly', C=cs, degree=degs, gamma=1, coef0=1, cache_size=4096) poly_svc_clf = GridSearchCV(poly_svc, hyperparams, cv=5) cv_train_X, cv_train_Y = get_X_Y(data_cv) poly_svc_fit = poly_svc_clf.fit(cv_train_X, cv_train_Y) poly_svc_res = poly_svc_clf.cv_results_ poly_svc_params = poly_svc_clf.best_params_ poly_svc_score = poly_svc_clf.best_score_ test_X, test_Y = get_X_Y(data_test) test_pred = poly_svc_clf.predict(test_X) # The Coefficients print('Test Estimator : \n', poly_svc_clf.best_estimator_) # Recall Score recall = r_s(test_Y, test_pred) print('Recall Score : \n', recall) # Precision Score precision = p_s(test_Y, test_pred) print('Precision Score : \n', precision) # Accuracy Score accuracy = a_s(test_Y, test_pred) print('Accuracy Score : \n', accuracy) # Conusion Matix print('Confusion Matrix : \n', c_m(test_Y, test_pred)) tp = time.time() - tp print('Time Secs : %f' % tp) return None
print ('Time Taken To Test : %f Secs.' % tl) # Round Test Predictions to Avoid Multiclass Continuous Targets Error test_pred = np.round(test_pred) # The Best Estimator print ('Test Estimator : \n', lin_krr_clf.best_estimator_) # Recall Score recall = r_s(test_Y, test_pred, average='micro') print ('Recall Score : \n', recall) # Precision Score precision = p_s(test_Y, test_pred, average='micro') print ('Precision Score : \n', precision) # Accuracy Score accuracy = a_s(test_Y, test_pred) print ('Accuracy Score : \n', accuracy) # Confusion Matix print('Confusion Matrix : \n', c_m(test_Y, test_pred)) print () # Polynomial Kernel Ridge Regression print () # Functions and Parameters alphas = [1.0] degs = [2.0, 3.0, 4.0] # M hyperparams = {'alpha' : alphas, 'degree' : degs} poly_krr = KernelRidge(kernel='poly', alpha=alphas, degree=degs, gamma=1, coef0=1) # Polynomial KRR Initializer poly_krr_clf = GridSearchCV(poly_krr, hyperparams, cv=5) # Train tp = time.time() poly_krr_fit = poly_krr_clf.fit(cv_train_X, cv_train_Y)