def __init__(self):
        self.path = paths()

        self.skip_train = False
        self.k = 5
        self.iter = 1000
        self.rate = 0.1

        datapath_train = self.path.Logistic_Regression + "/data_1/train.csv"
        datapath_test = self.path.Logistic_Regression + "/data_1/test.csv"
        self.read_data(datapath_train, datapath_test)

        self.question_number = '2'
        self.question_part = 'aa'
        # self.logistic_regression()

        self.question_part = 'ab'
        print()
        self.max_accuracy = -1
        input("Press enter to move to the next part")
        print("Ridge Logistic Regression")
        self.alpha_ridge = 1.2  #random
        print("Using a alpha_ridge value of", self.alpha_ridge)
        self.logistic_regression_ridge()

        self.question_part = 'ac'
        print()
        self.max_accuracy = -1
        input("Press enter to move to the next part")
        print("Lasso Logistic Regression")
        self.alpha_lasso = 1.2  #random
        print("Using a alpha_lasso value of", self.alpha_lasso)
        self.logistic_regression_lasso()
예제 #2
0
    def __init__(self):
        self.paths = paths()
        self.load_creds()
        self.load_reddit()
        self.load_mods()

        self.comment_limit = 5
        self.load_log_comments()

        while (True):
            for mod in self.mod_list:
                if self.log_comments.get(mod) == None:
                    self.log_comments[mod] = []
                print("shaming mod " + str(mod))
                comments = self.get_comments(mod)
                for comment in comments:
                    subreddit = comment.subreddit
                    comment.reply('SHAME!! 🔔')
                    if comment.id not in self.log_comments[
                            mod] and subreddit.display_name == 'freefolk':
                        try:
                            comment.reply('SHAME!! 🔔')
                            self.log_comments[mod].append(comment.id)
                            print('shamed')
                        except:
                            print("some dipshit error")
            self.save_log_comments()
            print("sleeping")
            time.sleep(5)
    def __init__(self):
        self.path = paths()

        self.iters = 10000
        self.step_size = 0.0001
        self.alpha_ridge = 0.6  # using a random alpha value
        self.alpha_lasso = 0.6  # using a random alpha value

        self.theta = np.zeros([1, 2])  # the parameter
        self.theta_ridge = np.zeros([1, 2])  # the parameter
        self.theta_lasso = np.zeros([1, 2])  # the parameter

        self.read_data()
        self.gradient_descent()
        self.read_data()
        self.gradient_descent_ridge()
        self.read_data()
        self.gradient_descent_lasso()

        plt.scatter(self.X[:, 1], self.Y)

        Y_reg = self.X @ self.theta.T
        Y_reg_ridge = self.X @ self.theta_ridge.T
        Y_reg_lasso = self.X @ self.theta_lasso.T

        plt.plot(self.X[:, 1], Y_reg, 'g')
        plt.plot(self.X[:, 1], Y_reg_ridge, 'r')
        plt.plot(self.X[:, 1], Y_reg_lasso, 'm')

        plt.legend([
            "Normal Linear Regression", "Ridge Linear Regression",
            "Lasso Linear Regression"
        ])

        plt.show()
    def __init__(self):
        self.path = paths()
        self.dl_data = download_datasets()
        self.datapath = self.path.abalone_dataset + "/Dataset.data"
        self.alpha = .1
        self.iters = 1000
        self.k = 5

        self.read_data()
        self.question_number = '1'

        # Part a
        self.question_part = 'aa'
        self.check_pre_models()
        self.linear_regression()

        # Part b
        input("Press enter for the next part")
        self.question_part = 'ab'
        self.check_pre_models()
        self.linear_regression_closed_form()

        # Part c
        input("Press enter for the next part")
        self.question_part = 'ac'
        self.plot_errors_part_ab()

        ## Explanation/Observation
        """
        RMSE calculated in part 'b' is less than the RMSE from part 'a'. This is because in part 'b' we are using the analytical/normal solution
        of the linear regression problem, whereas in part 'a' we are computing everything manually using the gradient descent. The gradient
        descent algorithm depends upon the step-size and the number of epochs as well. If we use a bigger step-size, we might end not end up
        at the minimum value of the required function, whereas if we use a smaller step-size, computation could take an indefinite amount of
        time. The number of iterations/epochs determine how long do we want to run the gradient descent algorithm. A smaller number, would lead
        to termincation of the program before we reach the minima, whereas a bigger number would lead to more computation.
        Hence, we the RMSE depends upon the these two parameters, and it varies as we change them.
        On the other hand, the closed/normal form produces a perfect solution to the linear regression problem, but can be computationally 
        expensive based on the size of the data, as it requires inverting matrices and their multiplication as well.
        """
        # k = 1 gives the least validation error. Therefore using k = 1 as the test set, and using the rest to generate
        # training and validation set.
        input("Press enter for the next part")
        self.question_part = 'ba'
        self.lowest_val_error_index = 1  # between 0 to k-1
        self.generate_train_test_set(
        )  # generates self.remaining_data (80%) and self.test_data
        self.tune_param_ridge()
        # self.plot_tuning_ridge()
        # self.alpha_ridge = 0.7996554525892349
        # self.alpha_ridge = 1.8761746914391204
        self.linear_regression_ridge()

        input("Press enter for the next part")
        self.question_part = 'bb'
        self.lowest_val_error_index = 1
        self.generate_train_test_set()
        self.tune_param_lasso()
        # self.alpha_lasso = 0.10280447320933092
        self.linear_regression_lasso()
예제 #5
0
 def __init__(self):
     self.path = paths()
     self.check_create_directory()
     do_download = self.check_directory()
     if do_download:
         self.download_mnist()
     else:
         do_download = input(
             "mnist data is already present. re-download? (Enter 'y' for yes, default is no.): "
         )
         if do_download == 'y' or do_download == 'Y':
             self.download_mnist()
    def __init__(self):
        self.reg_ridge_name = "reg_ridge_model"
        self.reg_lasso_name = "reg_lasso_model"
        self.check_ridge = False
        self.check_lasso = False
        self.classes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

        self.path = paths()
        self.dl_data = download_datasets()
        self.check_prev_models()

        self.load_training_data()
        self.load_testing_data()
        retrain = False

        if self.check_ridge:
            print("Using cached models from " + self.path.mnist_models +
                  ". retraining the model would consume a lot of time")
            retrain = input(
                "Do you want to retrain the model ('y' for yes, default is no)?: "
            )
            if retrain == 'y' or retrain == 'Y':
                retrain = True
            else:
                retrain = False
        if retrain:
            self.logistic_regression_train_ridge()
        else:
            self.reg_ridge = self.pickle_load(self.reg_ridge_name)

        if self.check_lasso:
            print("Using cached models from " + self.path.mnist_models +
                  ". retraining the model would consume a lot of time")
            retrain = input(
                "Do you want to retrain the model ('y' for yes, default is no)?: "
            )
            if retrain == 'y' or retrain == 'Y':
                retrain = True
            else:
                retrain = False
        if retrain:
            self.logistic_regression_train_lasso()
        else:
            self.reg_lasso = self.pickle_load(self.reg_lasso_name)

        # part ii
        self.train_error()
        self.test_error()
        self.pretty_print_accuracy()

        # part iii
        self.plot_roc()
예제 #7
0
def index():
    root = OrderedDict()
    root['swagger'] = '2.0'
    node(root, 'info', info())
    node(root, 'host', host())
    node(root, 'basePath', base_path())
    node(root, 'schemes', schemes())
    node(root, 'consumes', consumes())
    node(root, 'produces', produces())
    node(root, 'paths', paths())
    node(root, 'definitions', definitions())
    node(root, 'parameters', parameters())
    node(root, 'responses', responses())
    node(root, 'securityDefinitions', security_definitions())
    node(root, 'security', security())
    node(root, 'tags', tags())
    node(root, 'externalDocs', external_docs())

    return jsonify(root)
예제 #8
0
def index():
    def node(parent, key, value):
        if value:
            parent[key] = value

    root = OrderedDict()
    root['swagger'] = '2.0'
    node(root, 'info', info())
    node(root, 'host', host())
    node(root, 'basePath', base_path())
    node(root, 'schemes', schemes())
    node(root, 'consumes', consumes())
    node(root, 'produces', produces())
    node(root, 'paths', paths())
    node(root, 'definitions', definitions())
    node(root, 'parameters', parameters())
    node(root, 'responses', responses())
    node(root, 'securityDefinitions', security_definitions())
    node(root, 'security', security())
    node(root, 'tags', tags())
    node(root, 'externalDocs', external_docs())

    return jsonify(root)
예제 #9
0
import model_data
import paths

node1 = 'f6p_c'
node2 = 'h_c'

md = model_data.model_data()
md.load_model_cobra_sbml("e_coli_core.xml")
md.run_FBA()
"""
P = paths.paths(md, ignore_weights = 0, max_metabolite_occurance = 50)


print(P.get_shortest_path(node1, node2))

all_paths = P.get_all_shortest_paths(node1, node2)
all_paths = [" --> ".join([i for i in p if i in md.reactions]) for p in all_paths]
print(all_paths)
"""

P = paths.paths(md, ignore_weights=0, max_metabolite_occurance=50)
print(P.get_sub_graph([node1, node2]))
예제 #10
0
from nilearn.input_data import NiftiMasker, MultiNiftiMasker
from nilearn.image import math_img, mean_img

from sklearn.model_selection import GridSearchCV, LeaveOneGroupOut
from sklearn.linear_model import Ridge, RidgeCV
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import r2_score
from sklearn.decomposition import PCA
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.feature_selection import SelectFromModel, RFECV

from sklearn.preprocessing import StandardScaler

import paths
paths = paths.paths()

nb_blocks = 9


def generate_fmri_data_for_subject(subject):
    """
	Input : Take as input each fmri file. One file = One block
	Load all fmri data and apply a global mask mak on it. The global mask is computed using the mask from each fmri run (block). 
	Applying a global mask for a subject uniformize the data. 
	Output: Output fmri_runs for a subject, corrected using a global mask
	"""
    fmri_filenames = sorted(
        glob.glob(
            os.path.join(paths.rootpath, "fmri-data/en", "sub-%03d" % subject,
                         "func", "resample*.nii")))
예제 #11
0
def run_tests():

    ### initlaization

    # ros node initalization
    nh = rospy.init_node('interatction', anonymous=True)

    # create driver for receiving mavros msg
    drv = mavros_driver.mavros_driver(nh)

    # publisher for sp
    target = pub_target.pub_target()

    # lock for publisher thread
    lock = threading.Lock()

    # state: posctr, velctr
    st = state.state(lock, drv)

    # paths
    path = paths.paths(st, target)

    # modes
    sp = setpoint.setpoint(st, target)  #set points
    #cl = circle.circle(st, target) #circle mode

    # follow threads
    follow_thr = follow.thread_control(st, path)

    # pose publisher rate
    rate = 20

    # signal flag for running threads
    run_event = threading.Event()
    run_event.set()

    # thread that sends position
    #move_t = threading.Thread(target=move_pub, args=(rate, pub_pose, pose_msg, pub_twist, twist_msg, state, run_event))
    move_t = threading.Thread(target=move_pub, args=(rate, st, run_event))
    move_t.start()

    #  ctrl-c handler: aslo used to shut down during flight
    def ctrlC_handler(a, b):
        print 'teleop program:'
        print 'disarm'
        drv.arm(False)
        print '> start closing all threads'
        run_event.clear()
        move_t.join()
        follow_thr.stop_thread()
        print "> threads succesfully closed. Leaving program"
        sys.exit()

    # catch ctrl-c
    signal.signal(signal.SIGINT, ctrlC_handler)
    print "set offboard"

    # go into offboard mode
    drv.set_mode("OFFBOARD")

    # arm
    drv.arm(True)

    # wait some seconds until reaching hover position
    time.sleep(6.0)

    interactive_mode = True
    # main loop in manual mode
    if interactive_mode:

        # show usage
        usage()

        while 1:

            # read input from console
            user_input = sys.stdin.readline()

            # split input
            args = user_input.split()

            # cases
            if len(args) > 5:
                print "too many argumets"
                usage()

            elif len(args) == 0:
                print "no argument given"
                usage()

            else:

                # leave program
                if str(args[0]) == "exit":
                    print "leaving program"
                    ctrlC_handler(0, 0)

                # set position
                elif str(args[0]) == "p":
                    # reset relative position
                    reset(pose_rel)

                    # close circle thread if running
                    follow_thr.stop_thread()

                    # set new relative position

                    if len(args[1:]) > 4:
                        print "too many arguments"
                        usage()

                    elif len(args[1:]) < 4:
                        print "not enough arguments"
                        usage()

                    else:
                        for ind, arg in enumerate(args[1:]):

                            if if_isNum(arg):
                                pose_rel[ind] = float(arg)

                            else:
                                print arg + " is not a number"
                                reset(pose_rel)

                        st.set_state("posctr")
                        sp.do_step(pose_rel)

                # bezier point
                elif str(args[0]) == "b":

                    # reset relative position
                    reset(pose_rel)

                    # close circle thread if running
                    follow_thr.stop_thread()

                    # set new relative position

                    if len(args[1:]) > 4:
                        print "too many arguments"
                        usage()

                    elif len(args[1:]) < 4:
                        print "not enough arguments"
                        usage()

                    else:
                        for ind, arg in enumerate(args[1:]):

                            if if_isNum(arg):
                                pose_rel[ind] = float(arg)

                            else:
                                print arg + " is not a number"
                                reset(pose_rel)

                        st.set_state("bezier")
                        sp.do_step_bez(pose_rel)

                # set mode
                elif str(args[0]) == "mode":

                    mode = args[1].upper()

                    if str(mode) == "OFFBOARD":
                        # go into offboard mode
                        drv.set_mode("OFFBOARD")
                    else:
                        print "This mode is not yet supported"

                # arm
                elif str(args[0]) == "arm":
                    drv.arm(True)

                # path mode
                elif str(args[0]) == "c":

                    # close circle thread if running
                    follow_thr.stop_thread()

                    correct_input = True
                    if len(args[1:]) > 4:
                        print "too many arguments"
                        usage()

                    elif len(args[1:]) < 4:
                        print "too few arguments"
                        usage()
                    else:
                        axis = []
                        for ind, arg in enumerate(args[2:]):
                            if if_isNum(arg):
                                axis.append(float(arg))
                            else:
                                print arg + "not a number"
                                correct_input = False
                        radius = 0.0
                        if if_isNum(args[1]):
                            radius = float(args[1])

                        if correct_input:
                            path.circle(radius, axis, [1.0, 0.0, 0.0])

                            # start thread
                            follow_thr.start_thread()

                else:
                    print "this input is not supported"
                    usage()

            # dont waste cpu
            time.sleep(1)

    # start landing
    print "start landing"
    drv.land()

    # join thread
    run_event.clear()
    move_t.join()
예제 #12
0
 def test_n1(self):
   self.assertEqual(paths(1), 2)
예제 #13
0
 def test_n2(self):
   self.assertEqual(paths(2), 6)
예제 #14
0
파일: d07p1p2.py 프로젝트: akx/aoc2018
with open('input-day7.txt') as infp:
    for m in step_re.finditer(infp.read()):
        a, b = m.groups()
        graph[a].add(b)
        graph[b]  # not actually a no-op
        reverse_graph[b].add(a)
        reverse_graph[a]

start_node = [key for (key, deps) in reverse_graph.items() if not deps][0]
end_node = [key for (key, deps) in graph.items() if not deps][0]

#pprint.pprint(graph)

full_deps = collections.defaultdict(set)

for path in paths(reverse_graph, end_node):
    for i in range(len(path)):
        full_deps[path[i]].update(set(path[i + 1:]))


def part1():
    seen = set()
    order = []
    available = set()
    keys = set(graph)
    while seen < keys:
        satisfied = {
            key
            for (key, deps) in full_deps.items()
            if set(deps) <= (seen | available) and key not in seen
        }
예제 #15
0
파일: golem.py 프로젝트: joaosoda/golem-py
origin_src = 'remote' if re.match(regex, args.source) else 'local'
origin_dst = 'remote' if re.match(regex, args.destination) else 'local'

if __name__ == '__main__':
    print 'golem at work'
    sftp_src = remote.sftpclient(args.source) if origin_src == 'remote' else None
    sftp_dst = remote.sftpclient(args.destination) if origin_dst == 'remote' else None

    root_src = remote.info(args.source).root if origin_src == 'remote' else os.path.abspath(args.source)
    root_dst = remote.info(args.destination).root if origin_dst == 'remote' else os.path.abspath(args.destination)

    ignore = ignore(root_src + os.sep + 'config.json', sftp_src)

    if args.backup:
        print 'backuping folder ' + root_dst
        zip(root_dst, paths(root_dst, ignore, sftp_dst), args.backup, sftp_dst)

    for file_src in paths(root_src, ignore, sftp_src):
        file_dst = file_src.replace(root_src, root_dst)
        if utils.isdir(file_src, sftp_src):
            print 'creating folder ' + file_dst
            utils.mkdir(file_dst, sftp_dst)
        else:
            data_src = copy(file_src, sftp_src)
            if exists(file_dst):
                data_dst = copy(file_dst, sftp_dst)
                md5_data_src = md5(data_src).digest()
                md5_data_dst = md5(data_dst).digest()
                if md5_data_src == md5_data_dst:
                    print 'unchanged ' + file_dst
                else: