Example #1
0
def home(movieImdbId=''):
    global islogin
    global userId
    if movieImdbId != '':
        ML.addFavouriteMove(userId, movieImdbId)
    search = ""
    movies = []
    print(request.method)
    if request.method == 'POST':
        search = request.form['search']
        movies = jsonRequests(ML.searchMovie(search))
    else:
        movies = HomeForFirstVisit()

    if islogin == "false":
        return render_template('home.html',
                               logged="false",
                               movies=movies,
                               rate="-1")
    else:
        if ML.isUserRated(userId):
            movies = ML.reccomendMoviesFor(userId)
            movies = jsonRequests(movies)
        return render_template('home.html',
                               logged="true",
                               movies=movies,
                               rate="-1")
Example #2
0
def Run_Evaluation(keys, config, data):

    keys.sort()
    X, y = data[0][:, keys], data[1]

    if config['binary']:
        score, score_std = ML.evaluate_binary_model(
            X,
            y,
            n_splits=config['n_splits'],
            n_repeats=config['n_repeats'],
            int_cv=config['n_splits'],
            class_weight=config['class_weight'])

    else:
        score, score_std = ML.evaluate_regression_model(
            X,
            y,
            n_splits=config['n_splits'],
            n_repeats=config['n_repeats'],
            model_type=config['model_type'],
            int_cv=config['n_splits'],
            metric=config['metric'],
            target_transform=config['target_transform'])

    return score, score_std
Example #3
0
def MiPepid(input_fname, output_fname):
    # initialize the output file and write the header
    columns = [
        'sORF_ID', 'sORF_seq', 'transcript_DNA_sequence_ID', 'start_at',
        'end_at', 'classification', 'probability'
    ]
    df = pd.DataFrame(columns=columns)
    df.to_csv(output_fname, index=False)
    print('Begin writing the output file: ' + output_fname)

    # load the model
    logr, threshold = ML.load_model()

    # gather all the sORFs and write
    all_sORFs = []

    for rec in SeqIO.parse(input_fname, 'fasta'):
        DNA_seq = str(rec.seq).upper()
        obj_ORFs = ORFs(DNA_seq)
        transcript_seq_ID = rec.id
        this_sORFs = collect_and_name_sORFs_from_an_ORFs_object(
            obj_ORFs, transcript_seq_ID)
        all_sORFs += this_sORFs

        # Process in batch, 1 batch slightly greater than 1000
        if len(all_sORFs) > 1000:
            ML.predict_on_one_batch_and_write(all_sORFs, logr, threshold,
                                              output_fname)
            all_sORFs = []
            print('Wrote another 1000 sORFs.')

    if len(all_sORFs) > 0:
        ML.predict_on_one_batch_and_write(all_sORFs, logr, threshold,
                                          output_fname)
        print('Finished writing all the sORFs.')
Example #4
0
def testBagging(testSet, bag, method='DecisionStump'):
    # reading testing data
    N, L = testSet.shape
    iteration, d = bag.shape

    # change labelliing from +1/-1 to 1/0 if needed
    for i in range(N):
        if (testSet[i, L - 1] == -1):
            testSet[i, L - 1] = 0

    # yt : records the predictions in each eateration
    yt = np.zeros((iteration, N), int)
    for it in range(iteration):
        w = bag[it, :]
        if (method == 'LinearRegression'):
            pt = ML.testLinearRegression(testSet, w)
            # yt 1/0 -> +1/-1
            yt[it, :] = pt * 2 - 1
        elif (method == 'DecisionStump'):
            pt = ML.testDecisionStump(testSet, w)
            # yt -> +1/-1
            yt[it, :] = pt * 2 - 1

    p = sum(yt)
    # predicting results in y
    y = np.ones(N, int)
    for i in range(N):
        if (p[i] < 0):
            y[i] = 0

    Eout = sum(abs(y - testSet[:, L - 1]))
    print('Bagging : Out sample accuracy =', 1 - float(Eout) / float(N),
          'Eout=', Eout)

    return y
Example #5
0
def testBagging(testSet,bag,method='DecisionStump'):
    # reading testing data
    N,L = testSet.shape
    iteration,d = bag.shape

    # change labelliing from +1/-1 to 1/0 if needed
    for i in range(N):
        if(testSet[i,L-1]==-1):
            testSet[i,L-1]=0
            
    # yt : records the predictions in each eateration 
    yt=np.zeros((iteration,N),int)
    for it in range(iteration):
        w=bag[it,:]
        if(method=='LinearRegression'):
            pt=ML.testLinearRegression(testSet,w)
            # yt 1/0 -> +1/-1
            yt[it,:]=pt*2-1
        elif(method=='DecisionStump'):
            pt=ML.testDecisionStump(testSet,w)
            # yt -> +1/-1
            yt[it,:]=pt*2-1

    p=sum(yt)
    # predicting results in y
    y=np.ones(N,int)
    for i in range(N):
        if(p[i]<0):
            y[i]=0
        
    Eout=sum(abs(y-testSet[:,L-1]))
    print('Bagging : Out sample accuracy =', 1-float(Eout)/float(N),'Eout=',Eout)
    
    return y
Example #6
0
def learn_habitats(tree, mu, migration, outgroup):
    sys.stderr.write('Learn habitat assignments\n')

    # wipe the likelihoods off of the tree (just to be sure)
    ML.TreeWipe(tree.a_node)
    # learn the likelihoods
    ML.LearnLiks(tree, mu, migration, outgroup)
Example #7
0
def main():
    # loop over symbols and make some data
    Main_daily_DF = pd.DataFrame()
    Main_weekly_DF = pd.DataFrame()
    # # READ
    # Main_daily_DF = pickle.load( open( "Main_daily_DF.p", "rb" ) )
    # Main_weekly_DF = pickle.load( open( "Main_weekly_DF.p", "rb" ) )
    for sym in symbols:
        (weekly_df, daily_df) = makeData(sym)
        Main_weekly_DF = pd.concat([Main_weekly_DF, weekly_df],
                                   ignore_index=True)
        Main_daily_DF = pd.concat([Main_daily_DF, daily_df], ignore_index=True)

    # print(weekly_df.head())
    # print(daily_df.head())
    # print(weekly_df.tail())
    # print(daily_df.tail())

#WRITE
    pickle.dump(Main_daily_DF, open("Main_daily_DF.p", "wb"))
    pickle.dump(Main_weekly_DF, open("Main_weekly_DF.p", "wb"))

    # READ
    # Main_daily_DF = pickle.load( open( "Main_daily_DF.p", "rb" ) )
    # Main_weekly_DF = pickle.load( open( "Main_weekly_DF.p", "rb" ) )

    ML.run_ML(Main_daily_DF, f'target_price_change_{days_ahead}')
    ML.run_ML(Main_weekly_DF, f'target_price_change_{days_ahead}')
    def __init__(self, values, all_data, **kwargs):

        self.period = kwargs.get('period', 0.005)
        self.start_time = values['time']
        self.previous_time = values['time']
        self.previous_be = values['be']
        self.duration = kwargs.get('duration', float('inf'))
        self.previous_vel = values['av']
        import ML
        self.ml = ML()
Example #9
0
def Run_Evaluation(keys):
    keys = [all_keys[a] for a in keys]

    (X,y), (X_test, y_test), qq, qqq = loaders.load_it_all(split_enigma=True, i_keys=keys)

    scores = []
    for q in range(config['n_repeats']):
        model = ML.train_model(X, y, param_comb=config['num_searches'], classifier='log')
        scores.append(ML.get_roc(model, X_test, y_test))

    return np.mean(scores)
Example #10
0
    def __init__(self, nameOfDataSet, autoregressiveLag, nNeighbors, weights='distance'):
        self.nNeighbors = nNeighbors
        self.startingDelay = 24
        MLobject = ML(nameOfDataSet)

        originalDataSeries = MLobject.calculateLaggedInstance(autoregressiveLag)
        self.originalDataSeries = originalDataSeries
        self.df = originalDataSeries.iloc[0:-self.startingDelay]
        # self.saveDataToCSV(self.df.iloc[-24:, 0], 'historicData')
        self.futureData = originalDataSeries.iloc[-self.startingDelay:]
        return
def createNGram(args,mlData,disease,lang):
    if (args.cache > 0):
        dataFile = 'nGramModel'+'_'+disease+lang + pickleExt(args)
        if not os.path.exists(dataFile):
            nGramModel = ML.Ngram(args.classifier, mlData, args.generator, args.degree, args.remove, args.balance,
                                  args.cluster)
            pickle.dump(nGramModel, open(dataFile, "wb"))

        nGramModel = pickle.load(open(dataFile, "rb"))
    else:
        nGramModel = ML.Ngram(args.classifier, mlData, args.generator, args.degree, args.remove, args.balance, args.cluster)

    return nGramModel
Example #12
0
def train(method, X_train,Y_train):
    model=None
    if method.startswith('svm'):
        model   = ML.svmtrain(X_train,Y_train)
    elif method.startswith('avp'):
        model   = ML.avptrain(X_train,Y_train,opts.iters)
    elif method.startswith('ann'):
        model   = ML.anntrain(X_train,Y_train)#,opts.iters)
    elif method.startswith('lp'):
        try:
            model    = ML.lptrain(X_train,Y_train)
        except TypeError:
            model = None
    return model
Example #13
0
    def __init__(self,
                 nameOfDataSet,
                 autoregressiveLag,
                 nNeighbors,
                 startingDelay=64):
        self.nNeighbors = nNeighbors
        self.startingDelay = startingDelay
        self.lag = autoregressiveLag
        MLobject = ML(nameOfDataSet)

        originalDataSeries = MLobject.calculateLaggedInstance(self.lag)
        self.originalDataSeries = originalDataSeries
        self.df = originalDataSeries.iloc[0:-self.startingDelay]
        self.futureData = originalDataSeries.iloc[-self.startingDelay:]
        return
Example #14
0
def upload_file():
    if request.method == 'POST':
        file1 = request.files.get('file')
        file2 = request.files.get('file2')
        if file1.filename == '' or file2.filename == '':
            return jsonify({
                'status': "fail",
                "error": "no file selected",
                "code": 444
            })
        filename = ""
        filename2 = ""
        if file1 and allowed_file(file1.filename):
            filename = secure_filename(file1.filename)
            file1.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        if file2 and allowed_file(file2.filename):
            filename2 = secure_filename(file2.filename)
            file2.save(os.path.join(app.config['UPLOAD_FOLDER'], filename2))

        (predict, ratio, before,
         after) = ML.final(UPLOAD_FOLDER + "/" + filename,
                           UPLOAD_FOLDER + "/" + filename2)
        # c.execute("INSERT INTO {} ({}, {}, {})  VALUES ( ?, ?, ?);"
        #           .format(sql.TABLE_NAME, sql.PAST_IMG_COLUMN, sql.PRESENT_IMG_COLUMN, sql.FUTURE_IMG_COLUMN),
        #           (filename, filename2, str(predict)))
        return render_template('report.html',
                               predict=predict,
                               ratio=ratio,
                               past=UPLOAD_FOLDER + "/" + filename,
                               present=UPLOAD_FOLDER + "/" + filename2,
                               before=before,
                               after=after)
    return render_template('form.html')
Example #15
0
def main():
    """This is the main function that initializes all machine learning runs. The function instantiates many objects
        of the ML class, each object relating to a different run. The ML class is passed a list of configuration
         settings which are described more in depth below"""
    print("Start Time: ", time.time())
    ML("MLP", "regression", [10, .1], "off", 0, 0, "./data/abalone.data")
    """ ML params in order:
Example #16
0
def testAdaBoost_Stump(testSet,bag,alphaT):
    # reading testing data
    N,L = testSet.shape
    iteration,d = bag.shape

    # change labelliing from +1/-1 to 1/0 if needed
    for i in range(N):
        if(testSet[i,L-1]==-1):
            testSet[i,L-1]=0
            
    # yt : records the predictions in each eateration 
    yt=np.zeros((N,iteration),int)
    for it in range(iteration):
        stump=bag[it,:]
        pt=ML.testDecisionStump(testSet,stump)
        # yt -> +1/-1
        yt[:,it]=pt*2-1

    # H(xn) = sign(sum(alphaT*ht(xn)))
    p=np.dot(yt,alphaT)
    # predicting results in y
    y=np.ones(N,int)
    for i in range(N):
        if(p[i]<0):
            y[i]=0
        
    Eout=sum(abs(y-testSet[:,L-1]))
    print('AdaBoost_Stump : Out sample accuracy =', 1-float(Eout)/float(N),'Eout=',Eout)
    
    return y   
Example #17
0
def testAdaBoost_Stump(testSet, bag, alphaT):
    # reading testing data
    N, L = testSet.shape
    iteration, d = bag.shape

    # change labelliing from +1/-1 to 1/0 if needed
    for i in range(N):
        if (testSet[i, L - 1] == -1):
            testSet[i, L - 1] = 0

    # yt : records the predictions in each eateration
    yt = np.zeros((N, iteration), int)
    for it in range(iteration):
        stump = bag[it, :]
        pt = ML.testDecisionStump(testSet, stump)
        # yt -> +1/-1
        yt[:, it] = pt * 2 - 1

    # H(xn) = sign(sum(alphaT*ht(xn)))
    p = np.dot(yt, alphaT)
    # predicting results in y
    y = np.ones(N, int)
    for i in range(N):
        if (p[i] < 0):
            y[i] = 0

    Eout = sum(abs(y - testSet[:, L - 1]))
    print('AdaBoost_Stump : Out sample accuracy =', 1 - float(Eout) / float(N),
          'Eout=', Eout)

    return y
    def trainModel(self):
        '''Trains the model using the current training and testing image sets.'''

        #QtHelper.dialog( 'model training is not yet implemented' )

        toHD5.pairsToHD5(self.comp_dir.text(), self.ground_dir.text())

        ML.Train()
Example #19
0
    def __init__(self, dataName, lag):
        MLobject = ML(dataName)
        originalDataSeries = MLobject.calculateLaggedInstance(lag)
        X = originalDataSeries
        X_train, X_test, y_train, y_test = train_test_split(X,
                                                            y,
                                                            test_size=0.2,
                                                            random_state=12)
        # instantiate the regressor class
        regressor = LinearRegression()

        # fit the build the model by fitting the regressor to the training data
        regressor.fit(X_train, y_train)

        # make a prediction set using the test set
        prediction = regressor.predict(X_test)
        return
def update_entsoe():
    for country in dp.countries_generation.keys():
        if country == 'CZE':
            continue
        params = {}
        params['country'] = country
        ml = ML.ML_Generation(params)
        ml.fill_missing()
Example #21
0
    def cv(self, feature_set, model_type, params, folds=5, multi=False):
        train_x = self.training_data
        train_y = [
            convert_met(word, multi=multi) for word in self.training_data
        ]
        train_x, _, con_dvx = train_dv(train_x, feature_set)

        return ML.run_cv(train_x, train_y, model_type, params, folds)
Example #22
0
def predict(method,model, X_test, Y_test):
    # predict
    if method.startswith('svm'):
        preds = ML.svmtest(model,X_test,Y_test)
    elif method.startswith('avp'):
        preds = ML.avptest(model,model)
    elif method.startswith('ann'):
        preds = ML.anntest(X_test,model)
    elif method.startswith('lp'):
        preds = ML.lptest(X_test,model)
    else:
        preds = ML.lptest(X_test,[(1.0/15) 
                    for x in X_test[0]])


   
    return preds
 def __init__(self):
     self.simple_append = {'wind_onshore': 'VTE', 'hydro_pumped': 'PVE', 'hydro_run': 'VE', #'nuclear': 'JE',
                           'solar': 'FVE', 'hydro_reservoir': ''}
     self.simple_append_reverse = {v: k for k, v in self.simple_append.iteritems()}
     self.ml_object = ML.ML('CZE')
     self.entsoe_data = self.ml_object.data
     self.missing_dates = {}
     self.__get_ceps_data()
     self.ml_object.close_database_connection()
Example #24
0
def HomeForFirstVisit():
    MoviesIMDB = ML.getMoviesImmdbID()
    x = []
    for i in range(20):
        r = requests.get('http://www.omdbapi.com/?i=' + 'tt' +
                         str(MoviesIMDB[i]) + '&apikey=765f0331')
        x.append(r.json())

    return x
Example #25
0
def recommendMovies(movieTitle=''):
    global islogin
    global userId
    movies = jsonRequests(ML.searchMovie(movieTitle))
    print(movies)
    return render_template('recommendedMovies.html',
                           logged="true",
                           movies=movies,
                           rate="-1")
class Machine_Learning():
    def __init__(self, values, all_data, **kwargs):

        self.period = kwargs.get('period', 0.005)
        self.start_time = values['time']
        self.previous_time = values['time']
        self.previous_be = values['be']
        self.duration = kwargs.get('duration', float('inf'))
        self.previous_vel = values['av']
        import ML
        self.ml = ML()

    def move_torso(self, angle=1, percent_max_speed=0.4):
        torso = {
            'RHP': 1.0,
            'LHP': 1.0,
            'RSP': 0.480417754569,
            'LSP': 0.0496083550914,
            'RSR': 1.12532637076,
            'LSR': -1.10966057441,
            'RER': -2.13838120104,
            'LER': 2.18263145891,
            'REY': -0.258485639687,
            'LEY': 0.853785900783,
            'RWY': 0.167101827676,
            'LWY': -0.180156657963
        }
        for joint in torso:
            Robot.move_limbs(joint, angle * torso[joint] * 0.0174533,
                             percent_max_speed)

    def move_legs(self, angle=1, percent_max_speed=0.4):
        legs = ['RKP', 'LKP']
        for joint in legs:
            Robot.move_limbs(joint, angle * 0.0174533, percent_max_speed)

    def algo(self, values, all_data):
        print values['time'], values['be']
        if values['time'] - self.start_time < self.duration:
            action = self.ml.get_action([values['be'], values['av']])
            if action == 0:
                "legs out -"
                self.move_legs(-1)
            elif action == 1:
                "legs in +"
                self.move_legs(1)
            elif action == 2:
                "torso out +"
                self.move_torso(1)
            elif action == 3:
                "torso in -"
                self.move_torso(-1)
            elif action == 4:
                pass
        else:
            return 'switch'
Example #27
0
    def __init__(self, arg):
        super(Model, self).__init__()
        self.arg = arg
        if arg == "ML":
            self.Model = ML.Model(arg)

        if arg == "OTHER":
            self.Model = OTHER.Model(arg)

        if arg == "None":
            self.Model = None_Model(arg)
Example #28
0
    def __init__(self):
        self.admin_user_id = 671

        self.mdb = DB.MongoDB(db_url, db_name=db_name)
        self.ml = ML.MlKnn()
        self.rating_df = self.__get_rating_df_from_db__()
        self.rating_df_ml = self.rating_df.pivot(index='userId',
                                                 columns='movieId',
                                                 values='rating').fillna(0.0)

        self.movie_df = None
Example #29
0
def Bagging(trainSet,iteration,method='DecisionStump',caseNum=0):
    # reading training data 
    N,L = trainSet.shape
    # default caseNum is N
    if (caseNum<=0):
        caseNum=N

    if(method=='LinearRegression'):
        # bag is to record g(it) in each iteration
        bag = np.zeros((iteration,L),float)
        # dataSet is the for bootstrapping (resampling)
        dataSet = np.zeros((N,L),float)
        for it in range(iteration):
            print("iteration No.",it+1)
            # booststrap dataSet
            for case in range(caseNum):
                idx=np.random.randint(caseNum)
                dataSet[case,:]=trainSet[idx,:]

            w=ML.LinearRegression(dataSet)
            bag[it,:]=w[:]
            y=ML.testLinearRegression(dataSet,w)
        return bag
        
    elif(method=='DecisionStump'):
        # bag is to record g(it) in each iteration
        bag = np.zeros((iteration,3),float)
        # dataSet is the for bootstrapping (resampling)
        dataSet = np.zeros((N,L),float)
        for it in range(iteration):
            print("iteration No.",it+1)
            # booststrap dataSet
            for case in range(caseNum):
                idx=np.random.randint(caseNum)
                dataSet[case,:]=trainSet[idx,:]

            stump=ML.DecisionStump(dataSet)
            bag[it,:]=stump[:]
            y=ML.testDecisionStump(dataSet,stump)
        return bag
Example #30
0
def Bagging(trainSet, iteration, method='DecisionStump', caseNum=0):
    # reading training data
    N, L = trainSet.shape
    # default caseNum is N
    if (caseNum <= 0):
        caseNum = N

    if (method == 'LinearRegression'):
        # bag is to record g(it) in each iteration
        bag = np.zeros((iteration, L), float)
        # dataSet is the for bootstrapping (resampling)
        dataSet = np.zeros((N, L), float)
        for it in range(iteration):
            print("iteration No.", it + 1)
            # booststrap dataSet
            for case in range(caseNum):
                idx = np.random.randint(caseNum)
                dataSet[case, :] = trainSet[idx, :]

            w = ML.LinearRegression(dataSet)
            bag[it, :] = w[:]
            y = ML.testLinearRegression(dataSet, w)
        return bag

    elif (method == 'DecisionStump'):
        # bag is to record g(it) in each iteration
        bag = np.zeros((iteration, 3), float)
        # dataSet is the for bootstrapping (resampling)
        dataSet = np.zeros((N, L), float)
        for it in range(iteration):
            print("iteration No.", it + 1)
            # booststrap dataSet
            for case in range(caseNum):
                idx = np.random.randint(caseNum)
                dataSet[case, :] = trainSet[idx, :]

            stump = ML.DecisionStump(dataSet)
            bag[it, :] = stump[:]
            y = ML.testDecisionStump(dataSet, stump)
        return bag
Example #31
0
def estimate_states(tree, outgroup, migration_matrix):
    # estimate the states (by making each node trifurcating...)
    root = tree.root
    kids = root.GetKids()
    if kids[0].name in outgroup:
        true_root = kids[1]
    else:
        true_root = kids[0]
    lik_score = ML.EstimateStates(true_root)
    k = 1 + len(migration_matrix) * (len(migration_matrix.values()[0]) - 1)
    aic_score = 2 * k - 2 * lik_score

    return true_root, lik_score, aic_score
Example #32
0
def iterative(Map, Matrix, LHS, RHS, ExactSolution, List):
    Prec = ML.MultiLevelPreconditioner(Matrix, False)
    Prec.SetParameterList(List)
    Prec.ComputePreconditioner()

    Solver = AztecOO.AztecOO(Matrix, LHS, RHS)
    Solver.SetPrecOperator(Prec)
    if (List['solver'] == "gmres"):
        Solver.SetAztecOption(AztecOO.AZ_solver, AztecOO.AZ_gmres)
    elif List['solver'] == "cg":
        Solver.SetAztecOption(AztecOO.AZ_solver, AztecOO.AZ_gmres)
    else:
        print "Solver type not correct"
    Solver.SetAztecOption(AztecOO.AZ_output, 16)
    err = Solver.Iterate(List['iters'], List['tol'])
Example #33
0
def AdaBoost_Stump(trainSet, iteration):
    # reading training data
    N, L = trainSet.shape

    # Final H(x)=sign(sum(alphaT*ht(x)))
    # alphaT : records the weights of ht in each iteration
    alphaT = np.zeros(iteration, float)
    # epsonT : records the error rate with regarding weights Un
    epsonT = np.zeros(iteration, float)
    # Un : the weights (penalty) for each example
    Un = np.ones(N, float) / N  # initial value: Un[i] = 1/N

    # bag : records ht in each iteration, ht =[dim,s(1/-1),thita]
    bag = np.zeros((iteration, 3), float)

    # y : ground truth
    y = np.array(trainSet[:, L - 1])

    # iteration
    for it in range(iteration):
        print('iteration No.', it + 1)
        stump = ML.WeightedStump(trainSet, Un)
        bag[it, :] = stump[:]
        p = ML.testDecisionStump(trainSet, stump)
        # sum(Un:missClassified)/sum(Un)
        diff = abs(p - y)
        et = np.dot(diff, Un) / sum(Un)
        epsonT[it] = et
        # update Un for t+1 iteration
        for case in range(N):
            if (diff[case] == 1):
                Un[case] = Un[case] * math.sqrt((1 - et) / et)
            else:
                Un[case] = Un[case] * math.sqrt(et / (1 - et))

        alphaT[it] = math.log(math.sqrt((1 - et) / et))
Example #34
0
	def itrerative_state_parameter_estimation(self,Y,max_it):

		"""Two part iterative algorithm, consisting of a state estimation step followed by a
		parameter estimation step
		
		Arguments:
		---------
		Y: list of matrices
			Observation vectors
		max_it: int
			maximum number of iterations """


		xi_est=[]
		kernel_weights_est=[]
		# generate a random state sequence
		Xb=np.random.rand(len(Y),self.model.nx)
		Pb=pb.zeros([len(Y),self.model.nx**2])
		Mb=pb.zeros([len(Y),self.model.nx**2])

		# iterate
		keep_going = 1
		it_count = 0
		print " Estimatiing IDE's kernel and field weights"
		t0=time.time()
		while keep_going:
			ML_instance=ML.para_state_estimation(self.model)
			temp=ML_instance.estimate_kernel(Xb,Pb,Mb)
			kernel_weights_est.append(temp)
			self.model.kernel.weights=temp
			self.model.gen_ssmodel();FrobNorm=self.model.FroNorm()
			skf_instance=SKF.SKF(self.model.x0,self.model.P0,self.model.A,self.model.C,self.model.Sigma_e,self.model.Sigma_epsilon)
			Xb,Pb,Mb=skf_instance.rtssmooth(Y)
			self.model.Xb=Xb
			self.model.Pb=Pb 
			self.model.kernel_weights_est=kernel_weights_est
			self.model.x0=Xb[0,:].reshape(self.model.nx,1)
			self.model.P0=Pb[0,:].reshape(self.model.nx,self.model.nx)

			print it_count, " Kernel current estimate: ", self.model.kernel.weights
			print it_count,"current estimate Frobenius norm: ",FrobNorm 

			if it_count == max_it:
				keep_going = 0
			it_count += 1
		print "Elapsed time in seconds is", time.time()-t0
Example #35
0
def AdaBoost_Stump(trainSet,iteration):
    # reading training data 
    N,L = trainSet.shape

    # Final H(x)=sign(sum(alphaT*ht(x)))
    # alphaT : records the weights of ht in each iteration
    alphaT=np.zeros(iteration,float)
    # epsonT : records the error rate with regarding weights Un
    epsonT=np.zeros(iteration,float)
    # Un : the weights (penalty) for each example
    Un=np.ones(N,float)/N # initial value: Un[i] = 1/N

    # bag : records ht in each iteration, ht =[dim,s(1/-1),thita]
    bag=np.zeros((iteration,3),float)

    # y : ground truth
    y=np.array(trainSet[:,L-1])
        
    # iteration
    for it in range(iteration):
        print('iteration No.',it+1)
        stump=ML.WeightedStump(trainSet,Un)
        bag[it,:]=stump[:]
        p=ML.testDecisionStump(trainSet,stump)
        # sum(Un:missClassified)/sum(Un)
        diff=abs(p-y)
        et=np.dot(diff,Un)/sum(Un)
        epsonT[it]=et
        # update Un for t+1 iteration
        for case in range(N):
            if(diff[case]==1):
                Un[case]=Un[case]*math.sqrt((1-et)/et)
            else:
                Un[case]=Un[case]*math.sqrt(et/(1-et))

        alphaT[it]=math.log(math.sqrt((1-et)/et))

    return (bag,alphaT)
def setup_func():
    ML.init("", "")
Example #37
0
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 12 15:34:32 2015

@author: david
RUN

"""

import ML
import math
import time
import numpy as np

train=ML.csvRead('hw_train.csv')
#train=ML.csvRead('TrainData.csv')
test=ML.csvRead('hw_test.csv')
#test=ML.csvRead('TestData.csv')
N,L=train.shape
#print('DecisionStump')
stump=ML.DecisionStump(train)
y=ML.testDecisionStump(test,stump)

#tic=time.time()

#bag=ML.Bagging(train,30,'LinearRegression',)
#bag=ML.Bagging(train,30)
bag,aT = ML.AdaBoost_Stump(train,1000)

#toc=time.time()
Example #38
0
	    for i in range(len(samples)):

                # Creates train corpus for cross validation
                info('Model for ',str(i))
                X_train = samples[:i]+samples[i+1:]
                X_train = list(itertools.chain(*X_train))
                Y_train = classes[:i]+classes[i+1:]
                Y_train = list(itertools.chain(*Y_train))
                # Creates test corpus for cross validation (1 sample)
                Y_test  = classes[i]
                X_test  = samples[i]

                # Train and test
                model=train(opts.method,X_train,Y_train)
                preds=predict(opts.method,model,X_test,Y_test)
                res=ML.voted(preds)
                prob= ML.proba(preds)

                # Calculate metrics
                if res==answers[problems[i][0]]:
                    pref=""
                    N_Acc+=1
                    tp+=1
                else:
                    if res=='':
                        fn+=1
                    else:
                        fp+=1
                        fn+=1
                    pref="**"
                Total+=1
def setup_func():
    ML.init(
        "",
        master_key="",
        )
else:
    print("PlayerMap is already updated, so not creating new PlayerMap")



print("------------------------------------------------------------------------------------------------------------------")
checkFanduel = str(input("Would you like to check yesterday's prediction results? (Y/N)\n"))
if(checkFanduel.lower() == "y" or checkFanduel.lower() == "yes"):
    print("Checking yesterday's results")
    ReadWriteFiles.check_yesterday_fanduel(currentMap)
    print("Done checking. Wrote results to 'yesterday_results.txt'")

if(not isUpdated):
    print("Generating features/labels")
    (trainingFeatures_arr,testingFeatures_arr,todayFeatures_arr) = ML.generate_features(currentMap,today_playerMap,injuredIDMap,injuredTodayMap)
    

    (trainingLabels_arr,testingLabels_arr) = ML.generate_labels(currentMap)
    

    print("Done generating features and labels")
else:
    (trainingFeatures_arr,trainingLabels_arr,todayFeatures_arr,testingFeatures_arr,testingLabels_arr) = ReadWriteFiles.readCSVFiles()
    print("Features and Labels are already updated -- just reading those files now, so not creating new ones")



today_playerIDS = Util.extract_playerIDS(todayFeatures_arr)

if(not isUpdated):