def run_log_regression(): nb_in_sample = 100 nb_out_of_sample = 100000 nb_runs = 100 nb_epochs = 0 nb_Eout = 0 lr = 0.01 eps = 0.01 for i in range(nb_runs): # generate random function l = randomline() f = target_random_function(l) # generate in sample data and out of sample data data_in_sample = data(nb_in_sample) data_out_of_sample = data(nb_out_of_sample) # create training set structure [[w0,w1,...],target_value] t_set_in = build_training_set_fmultipleparams(data_in_sample,f) t_set_out = build_training_set_fmultipleparams(data_out_of_sample,f) # run logistic regression in sample epochs,w = log_regression_sgd(t_set_in,eps,lr) # compute the out of sample error given the previously compute weights. e_out = log_regression_compute_Eout(t_set_out,w) print "Run: %s - epochs: %s"%(i, epochs) print "Eout: %s"%(e_out) nb_Eout += e_out nb_epochs += epochs print 'Number of runs:%s'%(nb_runs) print "Avg epochs: %s"%(nb_epochs / nb_runs*1.0) print "Avg Eout: %s"%(nb_Eout / nb_runs*1.0)
def run_log_regression(): nb_in_sample = 100 nb_out_of_sample = 100000 nb_runs = 100 nb_epochs = 0 nb_Eout = 0 lr = 0.01 eps = 0.01 for i in range(nb_runs): # generate random function l = randomline() f = target_random_function(l) # generate in sample data and out of sample data data_in_sample = data(nb_in_sample) data_out_of_sample = data(nb_out_of_sample) # create training set structure [[w0,w1,...],target_value] t_set_in = build_training_set_fmultipleparams(data_in_sample, f) t_set_out = build_training_set_fmultipleparams(data_out_of_sample, f) # run logistic regression in sample epochs, w = log_regression_sgd(t_set_in, eps, lr) # compute the out of sample error given the previously compute weights. e_out = log_regression_compute_Eout(t_set_out, w) print "Run: %s - epochs: %s" % (i, epochs) print "Eout: %s" % (e_out) nb_Eout += e_out nb_epochs += epochs print 'Number of runs:%s' % (nb_runs) print "Avg epochs: %s" % (nb_epochs / nb_runs * 1.0) print "Avg Eout: %s" % (nb_Eout / nb_runs * 1.0)
def run_linear_regression(N_samples, N_points): '''runs on N_samples and with N_points a linear regression computes Ein by average of the samples as well as Eout ''' print 'running Linear Regression on %s samples' % str(N_samples) print 'Each sample has %s data points' % str(N_points) Ein_avg = [] Eout_avg = [] for i in range(N_samples): d = data(N_points) l = randomline() f = target_function(l) t_set = build_training_set(d, f) wlin, X, y = linear_regression(N_points, t_set) Ein = compute_Ein(wlin, X, y) Ein_avg.append(Ein) Eout = compute_Eout(wlin, f, N_points) Eout_avg.append(Eout) print_avg('Ein', Ein_avg) print_avg('Eout', Eout_avg)
def run_linear_regression(N_samples,N_points): '''runs on N_samples and with N_points a linear regression computes Ein by average of the samples as well as Eout ''' print 'running Linear Regression on %s samples' %str(N_samples) print 'Each sample has %s data points' %str(N_points) Ein_avg = [] Eout_avg = [] for i in range(N_samples): d = data(N_points) l = randomline() f = target_function(l) t_set = build_training_set(d,f) wlin,X,y = linear_regression(N_points,t_set) Ein = compute_Ein(wlin,X,y) Ein_avg.append(Ein) Eout = compute_Eout(wlin,f,N_points) Eout_avg.append(Eout) print_avg('Ein',Ein_avg) print_avg('Eout',Eout_avg)
def get_current_pos(): savefile = open('current_pos.txt', 'w') data = inst.query('SOURce:SCENario:LOG?') savefile.write(data) savefile.close() pos = (tools.data('current_pos.txt')) return pos #print(get_current_pos()) #print(inst.query('SOURce:SCENario:LOG?')) #done = tools.data('spectracom_data.txt') #print(done)
def generate_t_set(N, f=None): ''' Generate a training set of N = 1000 points on X = [1; 1] * [1; 1] with uniform probability of picking each x that belongs X . Generate simulated noise by fipping the sign of a random 10% subset of the generated training set ''' d = data(N) if f is None: f = lambda x: sign(x[0]**2 + x[1]**2 - 0.6) t_set = build_training_set_fmultipleparams(d, f) t_set = t_set_errorNoise(t_set, N / 10) return t_set, f
def generate_t_set(N,f=None): ''' Generate a training set of N = 1000 points on X = [1; 1] * [1; 1] with uniform probability of picking each x that belongs X . Generate simulated noise by fipping the sign of a random 10% subset of the generated training set ''' d = data(N) if f is None: f = lambda x: sign(x[0]**2 + x[1]**2 -0.6) t_set = build_training_set_fmultipleparams(d,f) t_set = t_set_errorNoise(t_set,N/10) return t_set,f
def compute_Eout(wlin, f, N_points): 'number of out-of-sample points misclassifed / total number of out-of-sample points' d = data(N_points) t_set = build_training_set(d, f) X_matrix = input_data_matrix(t_set) y_vector = target_vector(t_set) g_vector = dot(X_matrix, wlin) for i in range(len(g_vector)): g_vector[i] = sign(g_vector[i]) vEout = g_vector - y_vector nEout = 0 for i in range(len(vEout)): if vEout[i] != 0: nEout = nEout + 1 Eout = nEout / (len(vEout) * 1.0) return Eout
def compute_Eout(wlin,f,N_points): 'number of out-of-sample points misclassifed / total number of out-of-sample points' d = data(N_points) t_set = build_training_set(d,f) X_matrix = input_data_matrix(t_set) y_vector = target_vector(t_set) g_vector = dot(X_matrix,wlin) for i in range(len(g_vector)): g_vector[i] = sign(g_vector[i]) vEout = g_vector - y_vector nEout = 0 for i in range(len(vEout)): if vEout[i]!=0: nEout = nEout + 1 Eout = nEout/(len(vEout)*1.0) return Eout
def run_lr_and_pla(N_samples, N_points): '''runs on N_samples and with N_points a linear regresion then from the weight vector runs PLA algorithm compute the average number of iterations of PLA with this w vector ''' print 'running Linear Regression on %s samples' %N_samples print 'Each samples has %s data points' %N_points iteration_avg = [] for i in range(N_samples): d = data(N_points) l = randomline() f = target_function(l) t_set = build_training_set(d,f) wlin,X,y = linear_regression(N_points,t_set) w_pla,iteration = PLA(N_points,wlin,f,t_set) iteration_avg.append(iteration) print_avg('Number of iterations',iteration_avg)
def run_lr_and_pla(N_samples, N_points): '''runs on N_samples and with N_points a linear regresion then from the weight vector runs PLA algorithm compute the average number of iterations of PLA with this w vector ''' print 'running Linear Regression on %s samples' % N_samples print 'Each samples has %s data points' % N_points iteration_avg = [] for i in range(N_samples): d = data(N_points) l = randomline() f = target_function(l) t_set = build_training_set(d, f) wlin, X, y = linear_regression(N_points, t_set) w_pla, iteration = PLA(N_points, wlin, f, t_set) iteration_avg.append(iteration) print_avg('Number of iterations', iteration_avg)
def run_PLA(N_samples, N_points): samples = [] # vector of 1 clasified, 0 misclassified iterations = [] #vector of iterations needed for each PLA b_misclassified = False diff = [] #vector of difference average between f and g for i in range(N_samples): # run PLA in sample d = data(N_points) l = randomline() f = target_function(l) t_set = build_training_set(d, f) w = [0, 0, 0] #Start the PLA with the weight vector w being all zeros w, iteration = PLA(N_points, w, f, t_set) iterations.append(iteration) # check if points are classified or not for i in range(len(t_set)): point = t_set[i][0] s = h(w, point) yn = t_set[i][1] if yn != s: samples.append(0) b_misclassified = True break # check difference between f and g diff.append(evaluate_diff_f_g(f, w)) if not b_misclassified: samples.append(1) b_misclassified = False print 'number of samples misclassified: %s ' % samples.count(0) print 'number of classified samples: %s ' % samples.count(1) print 'number of iteration avg: %s ' % (str( sum(iterations) / len(iterations) * 1.0)) print 'average of difference in function g: %s' % (sum(diff) / (len(diff) * 1.0))
def run_PLA(N_samples,N_points): samples = []# vector of 1 clasified, 0 misclassified iterations = []#vector of iterations needed for each PLA b_misclassified = False diff = []#vector of difference average between f and g for i in range(N_samples): # run PLA in sample d = data(N_points) l = randomline() f = target_function(l) t_set = build_training_set(d,f) w = [0,0,0] w,iteration = PLA(N_points,w,f,t_set) iterations.append(iteration) # check if points are classified or not for i in range(len(t_set)): point = t_set[i][0] s = h(w,point) yn = t_set[i][1] if yn != s: samples.append(0) b_misclassified = True break # check difference between f and g diff.append(evaluate_diff_f_g(f,w)) if not b_misclassified: samples.append(1) b_misclassified = False print 'number of samples misclassified: %s ' % samples.count(0) print 'number of classified samples: %s ' % samples.count(1) print 'number of iteration avg: %s ' % (str(sum(iterations)/len(iterations)*1.0)) print 'average of difference in function g: %s' % ( sum(diff)/(len(diff)*1.0) )
__author__ = 'tobie' import tools, math from matplotlib import pyplot as plt # Open files we need spec =open('spectracom_data.txt', 'r') ublox = open('ublox_data.txt', 'r') # take back information we need ie [time, Lat, Long, Alt] speclist = tools.data('spectracom_data.txt') ubloxlist = tools.data('ublox_data.txt') print(speclist) print(ubloxlist) def synchronisation(speclist, ubloxlist): # to compare information sent and received, they must be synchronized new_spec =[] new_ublox = [] for i in range(len(speclist)): for j in range(len(ubloxlist)): if speclist[i][0][0:6] == ubloxlist[j][0][0:6]: new_spec.append(speclist[i]) new_ublox.append(ubloxlist[j]) return (new_spec, new_ublox) new_spec = synchronisation(speclist, ubloxlist)[0] new_ublox = synchronisation(speclist, ubloxlist)[1] print(new_spec) print(new_ublox)
def player(id): return data(models.Player.query.filter(models.Player.id == id).one())
def match(id): m = models.Match.query.filter(models.Match.id == id).one() if request.method == 'GET': return data(m) if request.method == 'PUT': return modify_resource(m)
def tournament(id): return data(models.Tournament.query.filter(models.Tournament.id == id).one())
def run_pla_vs_svm(nbruns=1, N=10): solvers.options['show_progress'] = False d = [] l = 0 f = 0 t_set = [] y = [] svm_vs_pla = [] for i in range(nbruns): onBothSides = False while (not onBothSides): d = data(N) l = randomline() f = target_function(l) t_set = build_training_set(d, f) y = target_vector(t_set) if (1 in y) and (-1 in y): onBothSides = True else: onBothSides = False w = [0, 0, 0] w_pla, iteration = PLA(N, w, f, t_set) plaEout = evaluate_diff_f_g(f, w_pla) X_matrix = input_data_matrix(t_set) dimension = len(X_matrix[0]) #identity matrix of size dim X dim matrix x,I,J,typecode double P = spmatrix(1, range(dimension), range(dimension), tc='d') #vector of zeros of size dim, typecode double q = matrix([0] * (dimension), tc='d') mat = [] for t in t_set: y = t[1] temp = [x * -1.0 * y for x in t[0]] mat.append(temp) G = matrix(mat, tc='d') G = G.trans() # vectors of -1 of size t_set h = matrix([-1] * len(t_set), tc='d') #http://abel.ee.ucla.edu/cvxopt/examples/tutorial/qp.html qp_sln = solvers.qp(P, q, G, h) wsvm = list(qp_sln['x']) # number of support vectors you can get at each run count_sv = 0 for t in t_set: wsvm = array(wsvm) x = array(t[0]) y = t[1] res = fabs(y * dot(wsvm, x) - 1) if res < 0.001: count_sv = count_sv + 1 #print count_sv # Eout of svm svmEout = computeEout_svm(f, wsvm) #print 'svmEout: %s'%svmEout if (svmEout < plaEout): svm_vs_pla.append([True, count_sv]) else: svm_vs_pla.append([False, count_sv]) print "svm win pla %f" % (len(filter(lambda a: a[0] is True, svm_vs_pla)) * 1.0 / N) percent_svm_won = len([r[0] for r in svm_vs_pla if r[0] is True ]) * 1.0 / len(svm_vs_pla) print "question 9: svm beat pla %f percent of the time" % ( percent_svm_won * 100) avg_sv = sum([a[1] for a in svm_vs_pla]) * 1.0 / len(svm_vs_pla) print "avg sv:", avg_sv
def run_pla_vs_svm(nbruns = 1, N = 10): solvers.options['show_progress'] = False d = [] l = 0 f = 0 t_set = [] y = [] svm_vs_pla = [] for i in range(nbruns): onBothSides = False while(not onBothSides): d = data(N) l = randomline() f = target_function(l) t_set = build_training_set(d,f) y = target_vector(t_set) if (1 in y) and (-1 in y): onBothSides = True else: onBothSides = False w = [0,0,0] w_pla,iteration = PLA(N,w,f,t_set) plaEout = evaluate_diff_f_g(f,w_pla) X_matrix = input_data_matrix(t_set) dimension = len(X_matrix[0]) #identity matrix of size dim X dim matrix x,I,J,typecode double P = spmatrix(1, range(dimension), range(dimension), tc='d') #vector of zeros of size dim, typecode double q = matrix([0]*(dimension), tc='d') mat = [] for t in t_set: y = t[1] temp = [x * -1.0*y for x in t[0]] mat.append(temp) G = matrix(mat, tc='d') G = G.trans() # vectors of -1 of size t_set h = matrix([-1]*len(t_set), tc='d') #http://abel.ee.ucla.edu/cvxopt/examples/tutorial/qp.html qp_sln = solvers.qp(P, q, G, h) wsvm = list(qp_sln['x']) # number of support vectors you can get at each run count_sv = 0 for t in t_set: wsvm = array(wsvm) x = array(t[0]) y = t[1] res = fabs(y*dot(wsvm,x)-1) if res < 0.001: count_sv = count_sv + 1 #print count_sv # Eout of svm svmEout = computeEout_svm(f,wsvm) #print 'svmEout: %s'%svmEout if(svmEout < plaEout): svm_vs_pla.append([True,count_sv]) else: svm_vs_pla.append([False,count_sv]) print "svm win pla %f" % (len(filter(lambda a: a[0] is True, svm_vs_pla))*1.0/N) percent_svm_won = len([r[0] for r in svm_vs_pla if r[0] is True])*1.0/len(svm_vs_pla) print "question 9: svm beat pla %f percent of the time" % (percent_svm_won*100) avg_sv = sum([a[1] for a in svm_vs_pla])*1.0/len(svm_vs_pla) print "avg sv:", avg_sv