Example #1
0
def perceptron(feature_matrix, labels, T):
    """
    Runs the full perceptron algorithm on a given set of data. Runs T
    iterations through the data set, there is no need to worry about
    stopping early.

    Args:
        feature_matrix -  A numpy matrix describing the given data. Each row
            represents a single data point.
        labels - A numpy array where the kth element of the array is the
            correct classification of the kth row of the feature matrix.
        T - An integer indicating how many times the perceptron algorithm
            should iterate through the feature matrix.

    Returns: A tuple where the first element is a numpy array with the value of
    theta, the linear classification parameter, after T iterations through the
    feature matrix and the second element is a real number with the value of
    theta_0, the offset classification parameter, after T iterations through
    the feature matrix.
    """
    nsamples, nfeatures = feature_matrix.shape
    theta = np.zeros((nfeatures, ))
    theta_0 = 0
    for t in range(T):
        for i in get_order(nsamples):
            theta, theta_0 = perceptron_single_step_update(
                feature_matrix[i], labels[i], theta, theta_0)
    return theta, theta_0
Example #2
0
	def __init__(self, 
		filter_id,
		group_id,
		req,
		out_dir=".",
		order = None,
		min_identity = 30,
		min_blast_evalue = 0.01,
		save_to_ITOL = False,
		sep = ",",
		report_unfit = False):

		self.req = req
		self.order = order
		self.filter_id = filter_id
		self.group_id = group_id
		self.out_dir = os.path.abspath(out_dir)
		self.min_identity = min_identity
		self.min_blast_evalue = min_blast_evalue
		self.save_to_ITOL = save_to_ITOL
		self.sep = sep
		self.report_unfit = report_unfit

		if self.order == None: ## user didn't override the order argument
			d = os.path.abspath(os.path.dirname(__file__))
			data_env = os.path.join(d, 'data/')
			self.order = utils.get_order(self.req,data_env)

		## get blast versions
		self.blastp = self._get_blast_version("blastp")
		self.makeblastdb = self._get_blast_version("makeblastdb")

		utils.assure_path_exists(os.path.join(self.out_dir,group_id+ "_GROUP"))
		utils.write_log(os.path.join(self.out_dir,group_id + "_GROUP", "LOG"), "STEP 4 : GROUP ORFs", vars(self), "")
Example #3
0
def average_perceptron(feature_matrix, labels, T):
    """
    Runs the average perceptron algorithm on a given set of data.
	Classifier(x) = sign(theta.x + theta_0)
    
	Args:
        feature_matrix
        labels 
        T - times the perceptron iterate through the feature matrix.

    Returns: 
        theta, theta_0 - tuple  first element nparray average
        theta second element is a real
        number with the value of the average theta_0.

    """
    nsamples, nfeatures = feature_matrix.shape
    theta = np.zeros((nfeatures, ))
    theta_0 = 0
    sum_theta = np.zeros((nfeatures, ))
    sum_theta_0 = 0
    for t in range(T):
        for i in get_order(nsamples):
            theta, theta_0 = perceptron_single_step_update(
                feature_matrix[i], labels[i], theta, theta_0)
            sum_theta = sum_theta + theta
            sum_theta_0 = sum_theta_0 + theta_0
    theta_averaged = sum_theta / (nsamples * T)
    theta_0_averaged = sum_theta_0 / (nsamples * T)
    return theta_averaged, theta_0_averaged
Example #4
0
def run(args):
    args = copy.deepcopy(args)
    if "filter_id" not in vars(args):
        filter_id = args.id
    elif args.filter_id is None:
        filter_id = args.id
    else:
        filter_id = args.filter_id

    args.out_dir = os.path.abspath(args.out_dir)
    filter_dir = os.path.join(args.out_dir, filter_id + "_FILTER")
    group_dir = os.path.join(args.out_dir, args.id + "_GROUP")
    utils.assure_path_exists(group_dir)
    blast_dir = os.path.join(group_dir, "blast_files")

    if args.order is None:  ## user didn't override the order argument
        d = os.path.abspath(os.path.dirname(__file__))
        data_env = os.path.join(d, 'data/')
        args.order = utils.get_order(args.hmm_db, data_env)

    ## write LOG file
    get_blast_version(args.blastp, "blastp")
    get_blast_version(args.makeblastdb, "makeblastdb")
    utils.write_log(os.path.join(group_dir, "LOG"), "STEP 4 : GROUP ORFs",
                    vars(args), "")

    run_blast(args, group_dir, filter_dir, blast_dir)
    group_operons(args, group_dir, filter_dir, blast_dir)
    return
Example #5
0
def run():
    for k, v in switches.items():
        print(f"Enter '{k}' for {v} ")

    inp = input(':  ').lower()

    if inp == 'o':
        get_order()
        run()
    elif inp == 'c':
        create_time()
        run()
    elif inp == 'l':
        show_unpaid_bill()
        run()
    elif inp == 'p':
        pay_bill()
        run()
    elif inp == 'e':
        print('program closed !')
        return None
    else:
        print('Invalid input')
        run()
Example #6
0
def pegasos(feature_matrix, labels, T, L):
    """
    Runs the Pegasos algorithm on a given set of data. Runs T
    iterations through the data set, there is no need to worry about
    stopping early.

    For each update, set learning rate = 1/sqrt(t),
    where t is a counter for the number of updates performed so far (between 1
    and nT inclusive).


    Args:
        feature_matrix - A numpy matrix describing the given data. Each row
            represents a single data point.
        labels - A numpy array where the kth element of the array is the
            correct classification of the kth row of the feature matrix.
        T - An integer indicating how many times the algorithm
            should iterate through the feature matrix.
        L - The lamba value being used to update the Pegasos
            algorithm parameters.

    Returns: A tuple where the first element is a numpy array with the value of
    the theta, the linear classification parameter, found after T
    iterations through the feature matrix and the second element is a real
    number with the value of the theta_0, the offset classification
    parameter, found after T iterations through the feature matrix.
    """
    theta = np.zeros((feature_matrix.shape[1], ))
    theta_0 = 0
    update_counter = 0  # number_of updates
    for t in range(T):
        for i in get_order(feature_matrix.shape[0]):
            update_counter += 1
            eta = 1.0 / np.sqrt(update_counter)
            theta, theta_0 = pegasos_single_step_update(
                feature_matrix[i], labels[i], L, eta, theta, theta_0)
    return theta, theta_0