def q4(argv=None): dataset, method_name, k, latex = wrapper_args( argv, 'q4', ['Digits'], ['MyLogisticRegGen', 'LogisticRegression']) Digits_X, Digits_y = prepare_digits(want_noise=False) default_order = [ ('MyLogisticRegGen', 'Digits'), ('LogisticRegression', 'Digits') ] methods = { ('MyLogisticRegGen', 'Digits'): (MyLogisticRegGen(verbose=False), Digits_X, Digits_y), ('LogisticRegression', 'Digits'): (LogisticRegression(), Digits_X, Digits_y) } if dataset == 'all': order = default_order else: order = [(method_name, dataset)] for key in order: name, dataset = key method, X, y = methods[key] print('==============') print('method: {}, dataset: {}'.format(key[0], key[1])) scores = my_cross_val(method, X, y, k) report(name, dataset, scores, latex=latex)
def q3(): # Initialize the values from the datasets boston25_x, boston25_y = prepare_boston25() boston50_x, boston50_y = prepare_boston50() # Number of folds: assignment specifies the value of 5 k_fold = 5 default_order = [ ('MyLogisticReg2', 'Boston50'), ('MyLogisticReg2', 'Boston25'), ('LogisticRegression', 'Boston50'), ('LogisticRegression', 'Boston25') ] methods = { ('MyLogisticReg2', 'Boston50'): (MyLogisticReg2(boston25_x.shape[1]), boston50_x, boston50_y), ('MyLogisticReg2', 'Boston25'): (MyLogisticReg2(boston50_x.shape[1]), boston25_x, boston25_y), ('LogisticRegression', 'Boston50'): (LogisticRegression(), boston50_x, boston50_y), ('LogisticRegression', 'Boston25'): (LogisticRegression(), boston25_x, boston25_y) } for key in default_order: name, dataset = key method, x, y = methods[key] # Using my implementation of cross validation instead of the built-in one scores = my_cross_val(method, x, y, k_fold) my_pretty_print(name, dataset, scores) print('==============')
def q3(argv=None): dataset, method_name, k, latex = wrapper_args( argv, 'q3', ['Boston50', 'Boston75', 'Digits']) x_boston, y_boston = load_dataset(load_boston) x_digits, y_digits = prepare_digits(True) x_boston_50, y_boston_50 = percentileAssignment(50, x_boston, y_boston) x_boston_75, y_boston_75 = percentileAssignment(75, x_boston, y_boston) default_order = [('MultiGaussClassify_WithFullMatrix', 'Boston50'), ('MultiGaussClassify_WithFullMatrix', 'Boston75'), ('MultiGaussClassify_WithFullMatrix', 'Digits'), ('MultiGaussClassify_WithDiagonal', 'Boston50'), ('MultiGaussClassify_WithDiagonal', 'Boston75'), ('MultiGaussClassify_WithDiagonal', 'Digits'), ('LogisticRegression', 'Boston50'), ('LogisticRegression', 'Boston75'), ('LogisticRegression', 'Digits')] methods = { ('MultiGaussClassify_WithFullMatrix', 'Boston50'): (MultiGaussClassify(len(np.unique(y_boston_50)), x_boston_50.shape[1]), x_boston_50, y_boston_50), ('MultiGaussClassify_WithFullMatrix', 'Boston75'): (MultiGaussClassify(len(np.unique(y_boston_75)), x_boston_50.shape[1]), x_boston_75, y_boston_75), ('MultiGaussClassify_WithFullMatrix', 'Digits'): (MultiGaussClassify(len(np.unique(y_digits)), x_digits.shape[1]), x_digits, y_digits), ('MultiGaussClassify_WithDiagonal', 'Boston50'): (MultiGaussClassify(len(np.unique(y_boston_50)), x_boston_50.shape[1], True), x_boston_50, y_boston_50), ('MultiGaussClassify_WithDiagonal', 'Boston75'): (MultiGaussClassify(len(np.unique(y_boston_75)), x_boston_50.shape[1], True), x_boston_75, y_boston_75), ('MultiGaussClassify_WithDiagonal', 'Digits'): (MultiGaussClassify(len(np.unique(y_digits)), x_digits.shape[1], True), x_digits, y_digits), ('LogisticRegression', 'Boston50'): (LogisticRegression(), x_boston_50, y_boston_50), ('LogisticRegression', 'Boston75'): (LogisticRegression(), x_boston_75, y_boston_75), ('LogisticRegression', 'Digits'): (LogisticRegression(), x_digits, y_digits) } if dataset == 'all': order = default_order else: order = [(method_name, dataset)] for key in order: name, dataset = key method, X, y = methods[key] print('==============') print('method: {}, dataset: {}'.format(key[0], key[1])) scores = my_cross_val(method, X, y, k) report(name, dataset, scores, True)
def q3i(argv=None): dataset, method_name, k, latex = wrapper_args( argv, 'q3i', ['Boston50', 'Boston75', 'Digits']) x_boston, y_boston = load_dataset(load_boston) x_digits, y_digits = load_dataset(load_digits) x_boston_50, y_boston_50 = percentileAssignment(50,x_boston,y_boston) x_boston_75, y_boston_75 = percentileAssignment(75,x_boston,y_boston) default_order = [ ('LinearSVC', 'Boston50'), ('LinearSVC', 'Boston75'), ('LinearSVC', 'Digits'), ('SVC', 'Boston50'), ('SVC', 'Boston75'), ('SVC', 'Digits'), ('LogisticRegression', 'Boston50'), ('LogisticRegression', 'Boston75'), ('LogisticRegression', 'Digits') ] methods = {('LinearSVC', 'Boston50'): (LinearSVC(max_iter=2000), x_boston_50, y_boston_50), ('LinearSVC', 'Boston75'): (LinearSVC(max_iter=2000), x_boston_75, y_boston_75), ('LinearSVC', 'Digits'): (LinearSVC(max_iter=2000), x_digits, y_digits), ('SVC', 'Boston50'): (SVC(gamma='scale', C=10), x_boston_50, y_boston_50), ('SVC', 'Boston75'): (SVC(gamma='scale', C=10), x_boston_75, y_boston_75), ('SVC', 'Digits'): (SVC(gamma='scale', C=10), x_digits, y_digits), ('LogisticRegression', 'Boston50'): (LogisticRegression(penalty='l2', solver='lbfgs', multi_class='multinomial', max_iter=5000), x_boston_50, y_boston_50), ('LogisticRegression', 'Boston75'): (LogisticRegression(penalty='l2', solver='lbfgs', multi_class='multinomial', max_iter=5000), x_boston_75, y_boston_75), ('LogisticRegression', 'Digits'): (LogisticRegression(penalty='l2', solver='lbfgs', multi_class='multinomial', max_iter=5000), x_digits, y_digits)} if dataset == 'all': order = default_order else: order = [(method_name, dataset)] for key in order: name, dataset = key method, X, y = methods[key] print('==============') print('method: {}, dataset: {}'.format(key[0], key[1])) scores = my_cross_val(method, X, y, k) report(name, dataset, scores, latex=latex)
def find_best_myflda2(Method, X, y, k): # Find the optimal threshold for MyFLDA2 for the given dataset threshold_perf = [] for i in range(X.shape[0]): if i % 100 == 0: print('iteration: {}'.format(i)) x = X[i] lda = Method(threshold=x) scores = my_cross_val(lda, X, y, k) threshold_perf.append((np.mean(scores), lda)) # Include using the average of the class means as the threshold lda = Method() scores = my_cross_val(lda, X, y, k) threshold_perf.append((np.mean(scores), lda)) # Sort them from highest to lowest, my_cross_val returns # accuracies, and return the best performing model threshold_perf.sort(key=lambda p: p[0], reverse=True) return threshold_perf[0][1]
def q3i(argv=None): dataset, method_name, k, latex = wrapper_args( argv, 'q3i', ['Boston50', 'Boston75', 'Digits']) Boston50_X, Boston50_y = prepare_boston50() Boston75_X, Boston75_y = prepare_boston75() Digits_X, Digits_y = prepare_digits() default_order = [ ('LinearSVC', 'Boston50'), ('LinearSVC', 'Boston75'), ('LinearSVC', 'Digits'), ('SVC', 'Boston50'), ('SVC', 'Boston75'), ('SVC', 'Digits'), ('LogisticRegression', 'Boston50'), ('LogisticRegression', 'Boston75'), ('LogisticRegression', 'Digits') ] methods = {('LinearSVC', 'Boston50'): (LinearSVC(), Boston50_X, Boston50_y), ('LinearSVC', 'Boston75'): (LinearSVC(), Boston75_X, Boston75_y), ('LinearSVC', 'Digits'): (LinearSVC(), Digits_X, Digits_y), ('SVC', 'Boston50'): (SVC(), Boston50_X, Boston50_y), ('SVC', 'Boston75'): (SVC(), Boston75_X, Boston75_y), ('SVC', 'Digits'): (SVC(), Digits_X, Digits_y), ('LogisticRegression', 'Boston50'): (LogisticRegression(), Boston50_X, Boston50_y), ('LogisticRegression', 'Boston75'): (LogisticRegression(), Boston75_X, Boston75_y), ('LogisticRegression', 'Digits'): (LogisticRegression(), Digits_X, Digits_y)} if dataset == 'all': order = default_order else: order = [(method_name, dataset)] for key in order: name, dataset = key method, X, y = methods[key] print('==============') print('method: {}, dataset: {}'.format(key[0], key[1])) scores = my_cross_val(method, X, y, k) report(name, dataset, scores, latex=latex)
def q3(argv=None): dataset, method_name, k, latex = wrapper_args( argv, 'q3', ['Boston50', 'Boston75', 'Digits']) Boston50_X, Boston50_y = prepare_boston50() Boston75_X, Boston75_y = prepare_boston75() # Note that prepare_digits adds gaussian noise to the data to # avoid singlar covariance matrices. For details, see # datasets.prepare_digits Digits_X, Digits_y = prepare_digits() default_order = [ ('MultiGaussClassify', 'Boston50'), ('MultiGaussClassify', 'Boston75'), ('MultiGaussClassify', 'Digits'), ('LogisticRegression', 'Boston50'), ('LogisticRegression', 'Boston75'), ('LogisticRegression', 'Digits') ] methods = { ('MultiGaussClassify', 'Boston50'): (MultiGaussClassify(), Boston50_X, Boston50_y), ('MultiGaussClassify', 'Boston75'): (MultiGaussClassify(), Boston75_X, Boston75_y), ('MultiGaussClassify', 'Digits'): (MultiGaussClassify(linear=False), Digits_X, Digits_y), ('LogisticRegression', 'Boston50'): (LogisticRegression(), Boston50_X, Boston50_y), ('LogisticRegression', 'Boston75'): (LogisticRegression(), Boston75_X, Boston75_y), ('LogisticRegression', 'Digits'): (LogisticRegression(), Digits_X, Digits_y) } if dataset == 'all': order = default_order else: order = [(method_name, dataset)] for key in order: name, dataset = key method, X, y = methods[key] print('==============') print('method: {}, dataset: {}'.format(key[0], key[1])) scores = my_cross_val(method, X, y, k) report(name, dataset, scores, latex=latex)
def q4(argv): x_digits, y_digits = load_dataset(load_digits) dataset, method_name, k, latex = wrapper_args(argv, 'q4', ['X1', 'X2', 'X3']) X1 = rand_proj(x_digits, 32) X2 = quad_proj(x_digits) default_order = [ ('LinearSVC', 'X1'), ('LinearSVC', 'X2'), ('SVC', 'X1'), ('SVC', 'X2'), ('LogisticRegression', 'X1'), ('LogisticRegression', 'X2'), ] methods = { ('LinearSVC', 'X1'): (LinearSVC(max_iter=2000), X1, y_digits), ('LinearSVC', 'X2'): (LinearSVC(max_iter=2000), X2, y_digits), ('SVC', 'X1'): (SVC(gamma='scale', C=10), X1, y_digits), ('SVC', 'X2'): (SVC(gamma='scale', C=10), X2, y_digits), ('LogisticRegression', 'X1'): (LogisticRegression(penalty='l2', solver='lbfgs', multi_class='multinomial', max_iter=5000), X1, y_digits), ('LogisticRegression', 'X2'): (LogisticRegression(penalty='l2', solver='lbfgs', multi_class='multinomial', max_iter=5000), X2, y_digits) } if dataset == 'all': order = default_order else: order = [(method_name, dataset)] for key in order: name, dataset = key method, X, y = methods[key] print('==============') print('method: {}, dataset: {}'.format(name, dataset)) scores = my_cross_val(method, X, y, k) report(name, dataset, scores, latex=latex)
def q3(): # Initialize the values from the datasets boston25_x, boston25_y = prepare_boston25() boston50_x, boston50_y = prepare_boston50() # Number of folds: assignment specifies the value of 5 k_fold = 5 default_order = [('MySVM2 with m = 40', 'Boston50'), ('MySVM2 with m = 200', 'Boston50'), ('MySVM2 with m = n', 'Boston50'), ('LogisticRegression', 'Boston50'), ('MySVM2 with m = 40', 'Boston25'), ('MySVM2 with m = 200', 'Boston25'), ('MySVM2 with m = n', 'Boston25'), ('LogisticRegression', 'Boston25')] methods = { ('MySVM2 with m = 40', 'Boston50'): (MySVM2(boston50_x.shape[1], 500, 40), boston50_x, boston50_y), ('MySVM2 with m = 200', 'Boston50'): (MySVM2(boston50_x.shape[1], 500, 200), boston50_x, boston50_y), ('MySVM2 with m = n', 'Boston50'): # note that we deliberately pass boston50_x.shape[0] to trigger special case that makes batch size m = n (MySVM2(boston50_x.shape[1], 500, boston50_x.shape[0]), boston50_x, boston50_y), ('LogisticRegression', 'Boston50'): (LogisticRegression(), boston50_x, boston50_y), ('MySVM2 with m = 40', 'Boston25'): (MySVM2(boston25_x.shape[1], 500, 40), boston25_x, boston25_y), ('MySVM2 with m = 200', 'Boston25'): (MySVM2(boston25_x.shape[1], 500, 200), boston25_x, boston25_y), ('MySVM2 with m = n', 'Boston25'): # note that we deliberately pass boston25_x.shape[0] to trigger special case that makes batch size m = n (MySVM2(boston25_x.shape[1], 500, boston25_x.shape[0]), boston25_x, boston25_y), ('LogisticRegression', 'Boston25'): (LogisticRegression(), boston25_x, boston25_y) } for key in default_order: name, dataset = key method, x, y = methods[key] # Using my implementation of cross validation instead of the built-in one scores = my_cross_val(method, x, y, k_fold) my_pretty_print(name, dataset, scores) print('==============')
def q3(): # Initialize the values from the datasets Boston50_X, Boston50_y, Boston50_k, Boston50_d = prepare_boston50() Boston25_X, Boston25_y, Boston25_k, Boston25_d = prepare_boston25() Digits_X, Digits_y, Digits_k, Digits_d = prepare_digits() default_order = [ ('MultiGaussClassify with full covariance matrix', 'Boston50'), ('MultiGaussClassify with full covariance matrix', 'Boston25'), ('MultiGaussClassify with full covariance matrix', 'Digits'), ('MultiGaussClassify with diagonal covariance matrix', 'Boston50'), ('MultiGaussClassify with diagonal covariance matrix', 'Boston25'), ('MultiGaussClassify with diagonal covariance matrix', 'Digits'), ('LogisticRegression', 'Boston50'), ('LogisticRegression', 'Boston25'), ('LogisticRegression', 'Digits') ] methods = { ('MultiGaussClassify with full covariance matrix', 'Boston50'): (MultiGaussClassify(Boston50_k, Boston50_d), Boston50_X, Boston50_y), ('MultiGaussClassify with full covariance matrix', 'Boston25'): (MultiGaussClassify(Boston25_k, Boston25_d), Boston25_X, Boston25_y), ('MultiGaussClassify with full covariance matrix', 'Digits'): (MultiGaussClassify(Digits_k, Digits_d), Digits_X, Digits_y), ('MultiGaussClassify with diagonal covariance matrix', 'Boston50'): (MultiGaussClassify(Boston50_k, Boston50_d, True), Boston50_X, Boston50_y), ('MultiGaussClassify with diagonal covariance matrix', 'Boston25'): (MultiGaussClassify(Boston25_k, Boston25_d, True), Boston25_X, Boston25_y), ('MultiGaussClassify with diagonal covariance matrix', 'Digits'): (MultiGaussClassify(Digits_k, Digits_d, True), Digits_X, Digits_y), ('LogisticRegression', 'Boston50'): (LogisticRegression(), Boston50_X, Boston50_y), ('LogisticRegression', 'Boston25'): (LogisticRegression(), Boston25_X, Boston25_y), ('LogisticRegression', 'Digits'): (LogisticRegression(), Digits_X, Digits_y) } for key in default_order: name, dataset = key method, X, y = methods[key] # Using my implementation of cross validation instead of the built-in one scores = my_cross_val(method, X, y, 5) my_pretty_print(name, dataset, scores)
def q3(argv=None): dataset, method_name, k, latex = wrapper_args( argv, 'q3', ['Boston50', 'Boston75'], ['MyFLDA2', 'LogisticRegression']) Boston50_X, Boston50_y = prepare_boston50() Boston75_X, Boston75_y = prepare_boston75() default_order = [('MyFLDA2', 'Boston50'), ('MyFLDA2', 'Boston75'), ('LogisticRegression', 'Boston50'), ('LogisticRegression', 'Boston75')] # Find the optimal separation for the training set print('Finding optimal threshold for MyFLDA2 on Boston50 ...') myflda_boston50 = find_best_myflda2(MyFLDA2, Boston50_X, Boston50_y, k) print('Finding optimal threshold for MyFLDA2 on Boston75 ...') myflda_boston75 = find_best_myflda2(MyFLDA2, Boston75_X, Boston75_y, k) print('Done.') methods = { ('MyFLDA2', 'Boston50'): (myflda_boston50, Boston50_X, Boston50_y), ('MyFLDA2', 'Boston75'): (myflda_boston75, Boston75_X, Boston75_y), ('LogisticRegression', 'Boston50'): (LogisticRegression(), Boston50_X, Boston50_y), ('LogisticRegression', 'Boston75'): (LogisticRegression(), Boston75_X, Boston75_y) } if dataset == 'all': order = default_order else: order = [(method_name, dataset)] for key in order: name, dataset = key method, X, y = methods[key] print('==============') print('method: {}, dataset: {}'.format(key[0], key[1])) scores = my_cross_val(method, X, y, k) report(name, dataset, scores, latex=latex)
##########################CALLING CROSS VAL############################# methods = ['MySVM2', 'LogisticRegression'] data = ['Boston50', 'Boston75'] ind = 1 for d in data: if d == "Boston50": X1 = X Y = Y50 if d == "Boston75": X1 = X Y = Y75 for i in methods: if i == "MySVM2": m = [40, 200, 0] for mi in m: p = "MySVM2 with m = " + str(mi) print(p + " " + d) my_cross_val(i, X1, Y, 5, mi) if i == "LogisticRegression": p = "LohisticRegression with " print(p + d) m = 0 my_cross_val(i, X1, Y, 5, m)
for r in R: if r>=r50: Y50=np.append(Y50,1) else: Y50=np.append(Y50,0) Y75=np.empty((0,1)) for r in R: if r>=r75: Y75=np.append(Y75,1) else: Y75=np.append(Y75,0) ##########################CALLING CROSS VAL############################# print(" 1. LinearSVC with Boston50 \n") my_cross_val("LSVC",X,Y50,10) print(" 2. LinearSVC with Boston75 \n") my_cross_val("LSVC",X,Y75,10) print(" 3. LinearSVC with Digits \n") my_cross_val("LSVC",Xdigits,Ydigits,10) print(" 4. SVC with Boston50 \n") my_cross_val("SVC",X,Y50,10) print("5. SVC with Boston75 \n") my_cross_val("SVC",X,Y75,10) print("6. SVC with Digits") my_cross_val("SVC",Xdigits,Ydigits,10) print("7. Logistic Regression with Boston50 \n") my_cross_val("LR",X,Y50,10) print("8. Logistic Regression with Boston75 \n")
Boston50_perm = np.random.permutation(Boston50_comb) Boston50_data_perm = np.array(Boston50_perm[:, :-1]) Boston50_target_perm = np.array(Boston50_perm[:, -1]) Boston75_data = np.array(Boston.data) Boston75_target = np.array(y75) Boston75_comb = np.c_[Boston75_data, Boston75_target] Boston75_perm = np.random.permutation(Boston75_comb) Boston75_data_perm = np.array(Boston75_perm[:, :-1]) Boston75_target_perm = np.array(Boston75_perm[:, -1]) k = 5 print("MyLogisticReg2 with Boston50") err_MyLR2_b50 = my_cross_val("MyLogisticReg2", Boston50_data_perm, Boston50_target_perm, k) print("MyLogisticReg2 with Boston75") err_MyLR2_b75 = my_cross_val("MyLogisticReg2", Boston75_data_perm, Boston75_target_perm, k) print("LogisticRegression with Boston50") err_LR_b50 = my_cross_val("LogisticRegression", Boston50_data_perm, Boston50_target_perm, k) print("LogisticRegression with Boston75") err_LR_b75 = my_cross_val("LogisticRegression", Boston75_data_perm, Boston75_target_perm, k)
import util import my_cross_val if __name__ == "__main__": print '1.LinearSVC with Boston50' [boston50_X, boston50_y] = util.generate_Boston50() my_cross_val.my_cross_val('LinearSVC', boston50_X, boston50_y, 10) print '2.LinearSVC with Boston75' [boston75_X, boston75_y] = util.generate_Boston75() my_cross_val.my_cross_val('LinearSVC', boston75_X, boston75_y, 10) print '3.LinearSVC with Digits' [digit_X, digit_y] = util.generate_digits() my_cross_val.my_cross_val('LinearSVC', digit_X, digit_y, 10) print '4.SVC with Boston50' [boston50_X, boston50_y] = util.generate_Boston50() my_cross_val.my_cross_val('SVC', boston50_X, boston50_y, 10) print '5.SVC with Boston75' [boston75_X, boston75_y] = util.generate_Boston75() my_cross_val.my_cross_val('SVC', boston75_X, boston75_y, 10) print '6.SVC with Digits' [digit_X, digit_y] = util.generate_digits() my_cross_val.my_cross_val('SVC', digit_X, digit_y, 10) print '7.LogisticRegression with Boston50' [boston50_X, boston50_y] = util.generate_Boston50()
import numpy as np from sklearn.linear_model import LogisticRegression from my_cross_val import my_cross_val np.seterr(divide='ignore', invalid='ignore', over='ignore') #%% Digits dataset from sklearn.datasets import load_digits digits = load_digits() digits_data = digits.data digits_target = digits.target #%% MyLogisticRegGen - Boston50 k = 5 print("MyLogisticRegGen with Digits") err_MyLR_digits = my_cross_val("MyLogisticRegGen", digits_data, digits_target, k) print("LogisticRegression with Digits") err_LR_b50 = my_cross_val("LogisticRegression", digits_data, digits_target, k)
import util import my_cross_val if __name__ == "__main__": print '1.MyFLDA2 with Boston50' [boston50_X, boston50_y] = util.generate_Boston50() my_cross_val.my_cross_val('MyFLDA2', boston50_X, boston50_y, 5) print '2.MyFLDA2 with Boston75' [boston75_X, boston75_y] = util.generate_Boston75() my_cross_val.my_cross_val('MyFLDA2', boston75_X, boston75_y, 5) print '3.LogisticRegression with Boston50' [boston50_X, boston50_y] = util.generate_Boston50() my_cross_val.my_cross_val('LogisticRegression', boston50_X, boston50_y, 5) print '4.LogisticRegression with Boston75' [boston75_X, boston75_y] = util.generate_Boston75() my_cross_val.my_cross_val('LogisticRegression', boston75_X, boston75_y, 5)
from quad_proj import quad_proj from my_cross_val import my_cross_val from sklearn.datasets import load_digits digits = load_digits() digits_data = digits.data digits_target = digits.target X1 = rand_proj(digits_data, 32) X2 = quad_proj(digits_data) X3 = rand_proj(X2, 64) #LinearSVC print("LinearSVC Cross-Validation with X1") err_X1_LSVC_CV = my_cross_val("LinearSVC", X1, digits_target, 10) mean_err_X1_LSVC_CV = np.mean(err_X1_LSVC_CV) std_err_X1_LSVC_CV = np.std(err_X1_LSVC_CV) print("LinearSVC Cross-Validation with X2") err_X2_LSVC_CV = my_cross_val("LinearSVC", X2, digits_target, 10) mean_err_X2_LSVC_CV = np.mean(err_X2_LSVC_CV) std_err_X2_LSVC_CV = np.std(err_X2_LSVC_CV) print("LinearSVC Cross-Validation with X3") err_X3_LSVC_CV = my_cross_val("LinearSVC", X3, digits_target, 10) mean_err_X3_LSVC_CV = np.mean(err_X3_LSVC_CV) std_err_X3_LSVC_CV = np.std(err_X3_LSVC_CV) #SVC print("SVC Cross-Validation with X1")
y2 = 2 * np.ones((2317, 1)) path_url = "E:/uminn notes/Computer Vision/Project/c5_segmented_resized/" listdir = os.listdir(path_url) for file in listdir: img = cv2.imread(path_url + file) h = hog(img) h3 = np.vstack((h3, h)) h3 = np.delete(h3, 0, 0) y3 = 3 * np.ones((2312, 1)) path_url = "E:/uminn notes/Computer Vision/Project/c6_segmented_resized/" listdir = os.listdir(path_url) for file in listdir: img = cv2.imread(path_url + file) h = hog(img) h4 = np.vstack((h4, h)) h4 = np.delete(h4, 0, 0) y4 = 4 * np.ones((2325, 1)) X = np.vstack((h0, h1, h2, h3, h4)) y = np.vstack((y0, y1, y2, y3, y4)) X_y = np.hstack((X, y)) X_y = np.random.permutation(X_y) X = X_y[:, 0:63] y = X_y[:, 64] Error_rate_kaggle_lsvc, mean_kaggle_lsvc, std_dev_kaggle_lsvc = my_cross_val( "LogisticRegression", X, y, 5)
Y50 = np.append(Y50, 0) Y75 = np.empty((0, 1)) for r in R: if r >= r75: Y75 = np.append(Y75, 1) else: Y75 = np.append(Y75, 0) ##########################CALLING CROSS VAL############################# methods = ['MyLogisticReg2', 'LogisticRegression'] data = ['Boston50', 'Boston75'] ind = 1 for i in methods: if i == "MyLogisticReg2": p = "My Logistic Regression with " if i == "LogisticRegression": p = "LogisticRegression with " for d in data: print(p + d) if d == "Boston50": X1 = X Y = Y50 if d == "Boston75": X1 = X Y = Y75 my_cross_val(i, X1, Y, 5)
Boston75_perm = np.random.permutation(Boston75_comb) Boston75_data_perm = np.array(Boston75_perm[:, :-1]) Boston75_target_perm = np.array(Boston75_perm[:, -1]) from sklearn.datasets import load_digits digits = load_digits() digits_data = digits.data digits_target = digits.target k = 10 #LinearSVC print("LinearSVC Cross-Validation with Boston50") err_b50_LSVC_CV = my_cross_val("LinearSVC", Boston50_data_perm, Boston50_target_perm, k) mean_err_b50_LSVC_CV = np.mean(err_b50_LSVC_CV) std_err_b50_LSVC_CV = np.std(err_b50_LSVC_CV) print("LinearSVC Cross-Validation with Boston75") err_b75_LSVC_CV = my_cross_val("LinearSVC", Boston75_data_perm, Boston75_target_perm, k) mean_err_b75_LSVC_CV = np.mean(err_b75_LSVC_CV) std_err_b75_LSVC_CV = np.std(err_b75_LSVC_CV) print("LinearSVC Cross-Validation with Digits") err_digit_LSVC_CV = my_cross_val("LinearSVC", digits_data, digits_target, k) mean_err_digit_LSVC_CV = np.mean(err_digit_LSVC_CV) std_err_digit_LSVC_CV = np.std(err_digit_LSVC_CV) #SVC
Created on Mon Feb 10 00:19:49 2020 @author: Shravya Gade """ import numpy as np from sklearn.datasets import load_digits from rand_proj import rand_proj from quad_proj import quad_proj from my_cross_val import my_cross_val #########################LOAD DIGITS############################### digits=load_digits() X,Y=digits.data,digits.target X1=rand_proj(X,32) X2=quad_proj(X) print(" 1. LinearSVC with ~X1 \n") my_cross_val("LSVC",X1,Y,10) print(" 2. LinearSVC with ~X2 \n") my_cross_val("LSVC",X2,Y,10) print(" 3. SVC with ~X1 \n") my_cross_val("SVC",X1,Y,10) print(" 4. SVC with ~X2 \n") my_cross_val("SVC",X2,Y,10) print(" 5. Logistic Regression with ~X1 \n") my_cross_val("LR",X1,Y,10) print(" 1. Logistic Regression with ~X2 \n") my_cross_val("LR",X2,Y,10)
import util import my_cross_val import rand_proj import quad_proj if __name__ == "__main__": print '1.LinearSVC with X1' [digit_X, digit_y] = util.generate_digits() X1 = rand_proj.rand_proj(digit_X, 32) my_cross_val.my_cross_val('LinearSVC', X1, digit_y, 10) print '2.LinearSVC with X2' [digit_X, digit_y] = util.generate_digits() X2 = quad_proj.quad_proj(digit_X) my_cross_val.my_cross_val('LinearSVC', X2, digit_y, 10) print '3.LinearSVC with X3' [digit_X, digit_y] = util.generate_digits() X2 = quad_proj.quad_proj(digit_X) X3 = rand_proj.rand_proj(X2, 64) my_cross_val.my_cross_val('LinearSVC', X3, digit_y, 10) print '4.SVC with X1' [digit_X, digit_y] = util.generate_digits() X1 = rand_proj.rand_proj(digit_X, 32) my_cross_val.my_cross_val('SVC', X1, digit_y, 10) print '5.SVC with X2' [digit_X, digit_y] = util.generate_digits() X2 = quad_proj.quad_proj(digit_X)
# required methods with given parameters mySVC = SVC(gamma='scale', C=10) myLinearSVC = LinearSVC(max_iter=2000) myLR = LogisticRegression(penalty='l2', solver='lbfgs', multi_class='multinomial', max_iter=5000) X_digits, y_digits = digits.data, digits.target X_1 = rand_proj.rand_proj(X_digits, 32) X_2 = quad_proj.quad_proj(X_digits) count = 1 print("LinearSVC with X_1:") dAccuracyLinearSVC= my_cross_val.my_cross_val(myLinearSVC, X_1, y_digits, 10) for item in dAccuracyLinearSVC: print('Fold ' + str(count) + ': ' + str(item)) count += 1 count = 1 print('Mean: ' + str(dAccuracyLinearSVC.mean())) print('Standard Deviation: ' + str(dAccuracyLinearSVC.std())) """ print("Test:") daAccuracyLinearSVC_test = cross_val_score(myLinearSVC, X_tilda, y_digits, cv=10) print(daAccuracyLinearSVC_test) print(1 - daAccuracyLinearSVC_test.mean()) print(daAccuracyLinearSVC_test.std()) """ print(' ')
import util import my_cross_val if __name__ == "__main__": print '1.LogisticRegGen with Digits' [digit_X, digit_y] = util.generate_digits() my_cross_val.my_cross_val('LogisticRegGen', digit_X, digit_y, 5) print '2.LogisticRegression with Digits' [digit_X, digit_y] = util.generate_digits() my_cross_val.my_cross_val('LogisticRegression', digit_X, digit_X, 5)
# required methods with given parameters mySVC = SVC(gamma='scale', C=10) myLinearSVC = LinearSVC(max_iter=2000) myLR = LogisticRegression(penalty='l2', solver='lbfgs', multi_class='multinomial', max_iter=5000) # Set dataset variables X_boston50, y_boston50 = boston50.data, boston50.target X_boston25, y_boston25 = boston25.data, boston25.target X_digits, y_digits = digits.data, digits.target count = 1 print("LinearSVC with Boston50:") b50AccuracyLinearSVC = my_cross_val.my_cross_val(myLinearSVC, X_boston50, y_boston50, 10) for item in b50AccuracyLinearSVC: print('Fold ' + str(count) + ': ' + str(item)) count += 1 count = 1 print('Mean: ' + str(b50AccuracyLinearSVC.mean())) print('Standard Deviation: ' + str(b50AccuracyLinearSVC.std())) """ print("Test:") b50aAccuracyLinearSVC_test = cross_val_score(myLinearSVC, X_boston50, y_boston50, cv=10) print(b50aAccuracyLinearSVC_test) print(1 - b50aAccuracyLinearSVC_test.mean()) print(b50aAccuracyLinearSVC_test.std()) """ print(' ')
#%% Digits dataset from sklearn.datasets import load_digits digits = load_digits() digits_data = digits.data digits_target = digits.target k = 5 #%% MultiGaussClassify - Boston50 print("MultiGaussClassify with Boston50") err_MGC_b50 = my_cross_val("MultiGaussClassify",Boston50_data_perm,Boston50_target_perm,k) print("MultiGaussClassify with Boston75") err_MGC_b75 = my_cross_val("MultiGaussClassify",Boston75_data_perm,Boston75_target_perm,k) print("MultiGaussClassify with Digits") err_MGC_dig = my_cross_val("MultiGaussClassify",digits_data,digits_target,k) print("LogisticRegression with Boston50") err_LR_b50 = my_cross_val("LogisticRegression",Boston50_data_perm,Boston50_target_perm,k) print("LogisticRegression with Boston75") err_LR_b75 = my_cross_val("LogisticRegression",Boston75_data_perm,Boston75_target_perm,k) print("LogisticRegression with Digits") err_LR_dig = my_cross_val("LogisticRegression",digits_data,digits_target,k)