def perform_test(self, instance_matrix, labels_list, weights_list, coefficients, mean_point, weights_all, path): print "Test Starts...\n" my_optimization = optimization.Optimization() errors_list = [] for j in range(len(weights_list)): print "%d th test..." % j error_count = 0 for i in range(len(instance_matrix)): if int((my_optimization.sig_test((np.asarray(instance_matrix[i])), np.asarray(weights_list[j]) ))) != int(labels_list[i]): error_count += 1 errors_list.append(float(error_count)/float(len(instance_matrix))) print "%d th test finished." % j if coefficients is not None: error_center_counter, error_mean_counter, error_all_counter = 0, 0, 0 print "test for middle and center point..." print mean_point, coefficients, weights_all[0] for i_ in range(len(instance_matrix)): if int((my_optimization.sig_test(instance_matrix[i_], np.asarray(mean_point).transpose()))) != \ int(labels_list[i_]): error_mean_counter += 1 if int((my_optimization.sig_test(instance_matrix[i_], np.asarray(coefficients).transpose()))) != \ int(labels_list[i_]): error_center_counter += 1 if int((my_optimization.sig_test(instance_matrix[i_], np.asarray(weights_all[0]).transpose()))) != \ int(labels_list[i_]): error_all_counter += 1 print "test for middle and center point finished." errors_list.append(float(error_mean_counter)/float(len(instance_matrix))) errors_list.append(float(error_center_counter)/float(len(instance_matrix))) errors_list.append(float(error_all_counter)/float(len(instance_matrix))) self.write_error_to_file(errors_list, path)
def gradient_descent_equal(self, data, label): weights_equal = [] for i in range(len(data)): reg_opt = optimization.Optimization() weights_equal.append( reg_opt.gradient_descent_equal(data[i], label[i])) return weights_equal
def gradient_descent_random(self, my_data, number_of_training, number_of_training_instances): weights_random = [] for i in range(0, number_of_training): print i, "th training." reg_opt = optimization.Optimization() weights_random.append( reg_opt.gradient_descent_random( my_data.train_matrix, my_data.train_class_list, my_data.Data().get_random_index_list( number_of_training_instances, my_data.train_matrix))) return weights_random
def perform_test_for_all_point(self, instance_matrix, labels_list, weights_all_list, path): errors_list = [] print "Test Starts...\n" my_optimization = optimization.Optimization() for i in range(len(weights_all_list)): print "%dth test..." % i error_count = 0 for j in range(len(instance_matrix)): if int((my_optimization.sig_test(np.transpose(np.asarray(instance_matrix[j])), np.asarray(weights_all_list[i][0]) ))) != int(labels_list[j]): error_count += 1 errors_list.append(float(error_count)/float(len(instance_matrix))) print "%d th test finished." % i self.write_error_to_file(errors_list, path)
def gradient_descent_random_general(self, data_set, label_set, number_of_training, number_of_training_instances): weights_random = [] my_data = Data() for i in range(0, number_of_training): print i, "th training." reg_opt = optimization.Optimization() weights_random.append( reg_opt.gradient_descent_random( data_set, label_set, my_data.get_random_index_list(number_of_training_instances, data_set))) return weights_random
def center_point(self, points): opt = Opt.Optimization() points = np.asarray(points) n, d = points.shape # np.ceil(a) return the ceiling of the input, element-wise, get the bigger integer than a, -1.7->-1, 0.2->1 z = int(np.log2(np.ceil(n/(2*((d+1)**2))))) # initialize empty stacks / buckets with z+1 rows buckets = [[] for l in range(z+1)] # also add B_0, so size z+1 # push initial points with trivial proofs with depth 1, proofs consist of a factor and a hull for s in points: buckets[0].append((s, [[(1, s)]])) print "Height of tree is: %d" % z # loop terminates when a point is in the bucket B_z while len(buckets[z]) == 0: # initialize proof to be empty stack proof = [] # let l be the max such that B_(l-1) has at least d+2 points l = opt.find_l(buckets, d) # <editor-fold desc="Description"> """ pop d + 2 points q_1, ... , q_d+2 from B_l-1, points_list denotes the list of points p_1, to p_(d+2), proofs_list denotes the collection of proofs for each point p_i zip(*iterables) : make an iterator that aggregates elements from each of the iterables. """ # </editor-fold> idx = opt.pop(buckets[l-1], d+2) points_list, proofs_list = zip(*idx) # calculate the radon partition radon_pt, alphas, partition_idx_tuple = opt.radon_partition(points_list) # <editor-fold desc="Description"> """ TODO: the proof parts should be "ordered" according to the paper Given a set of d+2 points with disjoint proofs of depth r, the Radon point of P has depth at least 2r. Let (P_1, P_2) be the Radon partition for P, and let c be the Radon point. For each p_i in P, order the parts in the proof partition of p_i """ # </editor-fold> for k in range(2): # <editor-fold desc="Description"> """ The ITERATEDTVERBERG algorithm is very similar to the ITERATEDRADON algorithm, the key difference is that each successive approximation carries with it a proof of its depth. When we combine d+2 points of depth r into a Radon point c, we can rearrange the proofs to get a new proof that c has depth 2r ('ABCDEF', [1,0,1,0,1,1]) --> A C E F, compress(data, selector), parts of proofs in positive/ negative partition for that points, by using boolean index in partition_idx_tuple """ # </editor-fold> radon_pt_proof_list = list(compress(proofs_list, partition_idx_tuple[k])) # factors of the radon point in regard to the hulls consisting of the partitions radon_pt_factor_tuple = alphas[k] # <editor-fold desc="Description"> """ form a proof of depth 2^(l+1) for the radon point, by lemma 4.1 In the case of the fig-2 of paper, the i is range(2), as next step we will get the radon point of depth 4, so l=1, then we get the depth of the four points which form the second order radon partition, the use of i here is just get the depth of the previous point, and depth is also the number of parts of the disjoint partitions of the proof of the previous point, so later we use ps[i] indicate the i part of the partitions of the proof of previous radon point generally, we will prune based on the partitions of the proof of the points which form the current radon point """ # </editor-fold> for i in range(2 ** (l-1)): pt_alphas, pt_hulls = [], [] # enumerate the proof list, get the index and proof, i is the number of partitions in each proof for j, ps in enumerate(radon_pt_proof_list): # <editor-fold desc="Description"> """ get the i-th part of proof of point j In the example of the paper, this is the second order point in the positive/negative partition in radon_pt_proof_list, which contains two points(p_left, p_right) in this case. Then we want to get the i-th part of the proof of p_left, which is basically the two lines which form the proof of p_left the format of {p_left, {p1, p2}, {p3, p4}}, the last two disjoint subsets form the proof, the p_left has depth 2, as r=2 in this case """ # </editor-fold> parts_i_of_proof_of_j = ps[i] for ppt in parts_i_of_proof_of_j: # <editor-fold desc="Description"> """ Adjust the factors of the proofs to be able to describe the radon point as a combination of it's proofs """ # </editor-fold> alpha = radon_pt_factor_tuple[j] * ppt[0] # adjust the factors hull = ppt[1] # Add the alpha and hull to the new union proof pt_alphas.append(alpha) pt_hulls.append(hull) # reduce the hull of the radon point, that is consisting of the proof parts, to d+1 hull points proof_after_pruning, non_hull = opt.prune_zipped(pt_alphas, pt_hulls) proof.append(proof_after_pruning) buckets[0].extend(non_hull) # add the outside isolated point to buckets[0] again buckets[l].append((radon_pt, proof)) return buckets[z]
def gradient_descent_all(self, data_matrix, label): weights_all = [] reg_opt = optimization.Optimization() weights_all.append(reg_opt.gradient_descent_equal(data_matrix, label)) return weights_all