Exemplo n.º 1
0
def GradientDescentIncremental(examples_features, examples_answers,
                               learning_rate):
    num_features = len(examples_features)
    #initialising weights
    weights = []
    for i in range(0, num_features):
        weights.append(0.1)

    previous_error = 0
    examples_predictions = Predictor.Predict(weights, list(examples_features))
    present_error = MeanSquaredErrorFunction.calculateError(
        examples_answers, examples_predictions)

    while abs(present_error - previous_error) > 0.001:
        for i in range(0, len(examples_answers)):
            for j in range(0, num_features):
                weights[j] = weights[j] - learning_rate * (
                    examples_predictions[i] -
                    examples_answers[i]) * examples_features[j][i]
            examples_predictions = Predictor.Predict(weights,
                                                     list(examples_features))
        previous_error = present_error
        examples_predictions = Predictor.Predict(weights,
                                                 list(examples_features))
        present_error = MeanSquaredErrorFunction.calculateError(
            examples_answers, examples_predictions)
    print weights
    return weights
Exemplo n.º 2
0
    def predictFunction(self):
        predictedWater = self.waterplainTextEdit.toPlainText()
        predictedCement = self.cementplainTextEdit.toPlainText()
        predictedFineAggr = self.fineaggplainTextEdit.toPlainText()
        predictedFinenessMod = self.finenessmodplainTextEdit.toPlainText()
        predictedSilica = self.silicaplainTextEdit.toPlainText()
        predictedSuperplasticizer = self.superplasticizerplainTextEdit.toPlainText(
        )

        input_predictions = [
            predictedWater, predictedCement, predictedFineAggr,
            predictedFinenessMod, predictedSilica, predictedSuperplasticizer
        ]
        input_predictions = list(filter(None, input_predictions))
        print(input_predictions)
        for i in range(len(input_predictions)):
            input_predictions[i] = float(input_predictions[i])

        Predictor.inputprediction = [input_predictions]

        Predictor.make_prediction()
        self.compstrength.setText(
            self.compstrength.text() + " " +
            str(round(Predictor.make_prediction()[0][0], 2)))
        self.compstrength.adjustSize()

        self.slump.setText(self.slump.text() + " " +
                           str(round(Predictor.make_prediction()[0][1], 2)))
        self.slump.adjustSize()
Exemplo n.º 3
0
def GradientDescentBatch(examples_features, examples_answers, learning_rate):
    num_features = len(examples_features)
    #initialising weights
    weights = []
    for i in range(0, num_features):
        weights.append(0.1)
    previous_error = 0
    examples_predictions = Predictor.Predict(weights, list(examples_features))
    present_error = MeanSquaredErrorFunction.calculateError(
        examples_answers, examples_predictions)

    differences = []
    for i in range(0, len(examples_answers)):
        differences.append(0)

    while abs(present_error - previous_error) > 0.0001:
        # while present_error-previous_error>0.01:
        for i in range(0, len(examples_answers)):
            differences[i] = (examples_predictions[i] - examples_answers[i])
        for i in range(0, len(examples_features)):
            differential = 0
            for j in range(0, len(examples_answers)):
                differential = differential + (differences[j] *
                                               examples_features[i][j])
            differential = differential / len(examples_answers)
            weights[i] = weights[i] - learning_rate * differential
        previous_error = present_error
        examples_predictions = Predictor.Predict(weights,
                                                 list(examples_features))
        present_error = MeanSquaredErrorFunction.calculateError(
            examples_answers, examples_predictions)
    print weights
    return weights
Exemplo n.º 4
0
    def interpolate_predict(self, datetime, filename='interpolation'):
        """
        Predicts counts for the given date and time and interpolates the predictions onto a map.
        Saves the interpolated image to Images/Interpolation.png
        :param filename: name of the image file to be saved
        :param datetime: String defining the date for the prediction
        """
        predictions = Predictor.Predictor().predict_timeframe(datetime, datetime)
        ids = Predictor.get_prediction_ids(predictions)

        zs = predictions.values[:, ids[1]].flatten().astype('int')

        self.interpolate_data(zs, filename)
Exemplo n.º 5
0
def cli(dataset_path, out_file):
    """Train a new model.

    This will train a new model using the provided dataset, trained model
    will be dumped to OUT file.
    """
    data = pd.read_csv(dataset_path)
    train_X, test_X, train_y, test_y = train_test_split(
        X, y, test_size=0.2, random_state=1
    )

    model = Predictor()
    model.fit(train_X, train_y)
    model.dump(out_file)
Exemplo n.º 6
0
def main():
    numpoint = 200
    numclass = 10
    features,isFull,classId,centers = getFeatures(numpoint, numclass)
    constArray = getConstraints(numpoint, isFull, classId)
    '''
    l = CKMeans(constArray, features, 10)
    kmeansoutput = l.getCKMeans()

    trainerkmeans = Trainer(kmeansoutput, classId, np.transpose(features))
    heteClstrFeatureId, heteClstrId = trainerkmeans.getHeterogenous()

    svmkmeans = trainerkmeans.trainSVM(heteClstrFeatureId, None)

    predictor = Predictor(kmeansoutput, classId, None, svm=svmkmeans)
    svkmeans = predictor.getSV()

    visualiseAfterClustering( kmeansoutput, features, classId, centers, isFull, 'k-means', sv=svkmeans)
    '''

    clusterer = complexCudaCKMeans(np.transpose(features), numclass, classId, isFull, stepweight= 0.05, maxweight=1)
    kmeansoutput = clusterer.cukmeans()

    trainer = Trainer(kmeansoutput, classId, np.transpose(features))
    heteClstrFeatureId, heteClstrId = trainer.getHeterogenousClusterId()

    svmkmeans = trainer.trainSVM(heteClstrFeatureId, None)

    predictor = Predictor(kmeansoutput, classId, None, svm=svmkmeans)
    svkmeans = predictor.getSupportVectors()

    visualiseAfterClustering(kmeansoutput, features, classId, centers, isFull, 'cudackmeans', sv=svkmeans)

    ckmeans = scipyCKMeans(np.transpose(features), isFull, classId, k=numclass, maxiter=20, thres=10 ** -10)
    kmeansoutput = ckmeans.getCKMeans()

    trainer = Trainer(kmeansoutput, classId, np.transpose(features))
    heteClstrFeatureId, heteClstrId = trainer.getHeterogenousClusterId()

    svm = trainer.trainSVM(heteClstrFeatureId, None)

    predictor = Predictor(kmeansoutput, classId, None, svm=svm)
    sv = predictor.getSupportVectors()

    visualiseAfterClustering(kmeansoutput, features, classId, centers, isFull, 'ck-means', sv=sv)

    clusterclassid = [[classId[featureidx] for featureidx in cluster if isFull[featureidx]] for cluster in kmeansoutput[0]]
    print any([(len(set(cluster)) != 1) for cluster in clusterclassid])
Exemplo n.º 7
0
def main(filename):
	split_file(filename, 0.75)
	pitches_training = get_data("train_"+filename)
	pitches_testing = get_data("test_"+filename)
	freqTable = FrequencyTable(pitches_training)
	print(Predictor(freqTable, pitches_testing).accuracy)
	generate_accuracy("train_"+filename, "test_"+filename)
Exemplo n.º 8
0
def getReco(X, k):
    #X = load_data("data", 16385, 3400)
    #print(myX)
    #mmwrite(Xfile, X)
    #create two list of lists [[int]], [[float]]
    cx = sps.coo_matrix(X)

    #print(time.time())
    iprev = -1
    X1, X2 = [], []
    for i, j, v in zip(cx.row, cx.col, cx.data):
        if i != iprev:
            #row changed
            X1.append([])
            X2.append([])
            iprev = i
        X1[i].append(j)
        X2[i].append(v)

    mypdsparse = Predictor.Predictor()
    ypred = mypdsparse.run(X1, X2, k)
    #n = X.shape[0]
    #print(time.time())
    #dump_data(X, "mydata")
    #print(time.time())
    return np.array(ypred)
    #subprocess.run(["./multiPred", "mydata.X", "model.txt", "-p", str(k), "rec_labels.y", str(k)])
    #recy = []
    '''
Exemplo n.º 9
0
    def communities(self, parallel):
        print "Training classifiers..."
        # multiprocessing.freeze_support()
        # cores=mp.cpu_count()
        # cores = multiprocessing.cpu_count()
        # pool = mp.ProcessingPool(4)
        # subgraphs={}
        classifiers = {}
        deg_centra = {}
        between_centra = {}
        load_centra = {}
        avg_nei_deg = {}
        harmonic_centra = {}
        close_centra = {}
        # score={}
        # for i,y in enumerate(pool.imap(self.get_classifiers,self.comm2node.keys())):
        #     print i
        # subgraph=y[0]
        # classifiers[i]=y[1]
        # deg_centra[i],between_centra[i],load_centra[i],avg_nei_deg[i],harmonic_centra[i],close_centra[i]=self.attributes(y[0])
        # score=dict(score,**y[2])
        if parallel == False:
            start_time = time.time()
            for comm in self.comm2node.keys():
                print comm
                # nodes=self.comm2node[comm]
                subgraph = self.graph.subgraph(self.comm2node[comm])
                # subgraphs[comm]=subgraph
                classifiers[comm] = Predictor.training(subgraph)
                deg_centra[comm], between_centra[comm], load_centra[
                    comm], avg_nei_deg[comm], harmonic_centra[
                        comm], close_centra[comm] = self.attributes(subgraph)
            print 'non-parallel:', time.time() - start_time, 's'
        else:
            ppservers = ()
            if len(sys.argv) > 1:
                ncpus = int(sys.argv[1])
                # Creates jobserver with ncpus workers
                job_server = pp.Server(ncpus, ppservers=ppservers)
            else:
                # Creates jobserver with automatically detected number of workers
                job_server = pp.Server(ppservers=ppservers)

            print "pp 可以用的工作核心线程数", job_server.get_ncpus(), "workers"
            # comms=list(self.comm2node.keys())
            start_time = time.time()
            jobs = [(comm,
                     job_server.submit(self.get_classifiers, (comm, ), (),
                                       ("Predictor", )))
                    for comm in self.comm2node.keys()]
            # print "yes"
            for comm, job in jobs:
                print comm
                classifiers[comm] = job()[1]
                deg_centra[comm],between_centra[comm],load_centra[comm],\
                avg_nei_deg[comm],harmonic_centra[comm],close_centra[comm]=self.attributes(job()[0])
            print 'parallel:', time.time() - start_time, 's'

        return classifiers, deg_centra, between_centra, load_centra, avg_nei_deg, harmonic_centra, close_centra
Exemplo n.º 10
0
def get_prediction(filename):
    preprocessor = Preprocessor.Preprocessor()
    # trainer = Trainer.Trainer(preprocessor)
    predictor = Predictor.Predictor(preprocessor)
    # image_name_list = os.listdir(os.path.join(os.getcwd(), "img"))
    # for image_name in image_name_list:
    #     image_path = os.path.join(os.path.join(os.getcwd(), "img"), image_name)
    return predictor.predict([filename])
Exemplo n.º 11
0
def getText():
    inpText = inputTxt.get("1.0","end")
    #print(inpText)
    if(inpText=="" or inpText==" " or inpText=="\n"):
        return 
    # prdict here
    Label(master,text=Predictor.predict(preprocessText(pd.Series([inpText]))),font = myFont).grid(row=5,column=3)
    pass
Exemplo n.º 12
0
def upload_file():
    if request.method == 'POST':
        
        file = request.files['file']
        # if user does not select file, browser also
        # submit an empty part without filename
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], file.filename))
        filedir = '/Users/nitish/Desktop/AI/'+file.filename
        return(Predictor.use_classifier(filedir))
Exemplo n.º 13
0
    def get_classifiers(self, comm):
        # subgraphs={}
        # classifiers={}
        # import Predictor
        # import pathos
        # print pathos.helpers.mp.current_process()
        # print comm
        subgraph = self.graph.subgraph(self.comm2node[comm])
        classifier = Predictor.training(subgraph)

        return subgraph, classifier
def ExpectedRewardsList(gameAuthority, evaluator, startingPositionsList,
                        numberOfSimulations, nextPlayer, epsilon):
    expectedRewardsList = []
    for positionNdx in range(len(startingPositionsList)):
        startingPosition = startingPositionsList[positionNdx]
        expectedReward = Predictor.ExpectedReward(
            evaluator,
            gameAuthority,
            numberOfSimulations,
            startingPosition=startingPosition,
            nextPlayer=nextPlayer,
            epsilon=epsilon)
        expectedRewardsList.append(expectedReward)
    return expectedRewardsList
Exemplo n.º 15
0
 def scanFunction(self):
     '''
         This Function will display the segments of image data.
         This is a temporary activity. 
     '''
     # Using Segmenter.py file
     S = Seg.Segmenter(self.pic)
     segments = S.segment()
     p = []
     for s in segments:
         cv2.imwrite('color_img.png', s)
         img = cv2.imread('color_img.png')
         p.append(pr.predict(img))
     self.textBrowser.append(str(p))
Exemplo n.º 16
0
def predict():
    if request.method == 'POST':
        myurl = request.form['url']
    else:
        myurl = request.args.get('url')

    result1, result2, result3 = Predictor.predict(myurl)

    print(myurl, result1, result2, result3)

    if result1 == -1 and result2 == -1 and result3 == -1:
        return render_template('/results_safe.html', url=myurl)
    else:
        return render_template('/results_unsafe.html', url=myurl)
Exemplo n.º 17
0
def main():
    root = Tk()
    root.title("DWAYNE")

    root.columnconfigure(0, weight=1)
    root.rowconfigure(0, weight=1)

    game = Game()

    makePredictor = lambda game : Predictor.NondeterministicPlayer(game, .5, \
          [Experts.NondeterministicSequenceExpert(game.getMoves(), k) for k in range(5)])

    GUI = SetupGUI(root, game, makePredictor)
    GUI.show()

    root.mainloop()
Exemplo n.º 18
0
def predict():
    if request.method == 'POST':
        myurl = request.form['url']
    else:
        myurl = request.args.get('url')

    print(myurl)
    result1, result2 = Predictor.predict(myurl)
    if result1 == 0:
        ans1 = "Legitimate"
    else:
        ans1 = "Phishing"

    if result2 == 0:
        ans2 = "Legitimate"
    else:
        ans2 = "Phishing"

    return "<div> <h1>Results</h1><h2>MY URL " + myurl + " is :</h2><h2> Acc. to Model 1 :" + ans1 + "</h2><h2> Acc. to Model 2 :" + ans2 + "</h2></div>"
Exemplo n.º 19
0
def predict():
    if request.method == 'POST':
        file = request.files.get('image').read()
        npimg = np.fromstring(file, np.uint8)
        img = cv2.imdecode(npimg,cv2.IMREAD_COLOR)
        result = Predictor.predict(img)
        response = {}
        if not result['sudokufound']:
            response = {
                "sudokufound":False,
                "statusCode":200,
                "sudoku":result['sudoku']
                }
        else: 
            response = {
                 "sudokufound":True,
                "statusCode":200,
                 "sudoku":result['sudoku']
            }
        return jsonify(response)
Exemplo n.º 20
0
 def __init__(self, date, writeHour):
     self.date = date
     self.writeHour = writeHour
     self.currentTableName = "recommendations"
     self.historicalTableName = "historical_performance"
     #use the get date class TODO
     #TODO import stocks from the sentimentscore folder
     self.a = Predictor.Predictor(self.date, s.stocks, "ticktalk",
                                  "daySummaries", "root", "", "localhost")
     host = 'localhost'
     password = ''
     user = '******'
     databaseName = 'ticktalk'
     connectString = "host=\"" + host + "\", user=\"" + user + "\", passwd=\"" + password + "\", db=\"" + databaseName + "\""
     self.db = pymysql.connect(autocommit=True,
                               host=host,
                               user=user,
                               passwd=password,
                               db=databaseName)
     #set up cursor
     self.cur = self.db.cursor()
Exemplo n.º 21
0
    def populate_good_company_data():
        company_list_accuracy = pd.read_csv('NSE_company_names.csv',
                                            header=None)
        company_list_accuracy[2] = 0
        count = company_list_accuracy.count()[0]
        arr = np.array([])

        for i in range(count):
            company_name = (company_list_accuracy.iloc[i][0])
            company_name = company_name.split('/')[1]
            df = Data.get_online_company_data(company_name)
            accuracy = Predictor(company_name).get_accuracy()
            date_now = dt.date.today() - dt.timedelta(3)
            if (date_now > df.iloc[-1].name.date()) and accuracy >= 0.9:
                accuracy = float('nan')
            print(company_name, accuracy, count, i)
            arr = np.append(arr, accuracy)

        df = pd.DataFrame(arr)
        company_list_accuracy[2] = df
        company_list_accuracy.dropna(inplace=True)
        company_list_accuracy.to_csv('good_company.csv', index=False)
Exemplo n.º 22
0
    def calculate():
        # Highlight which input has issues
        if firstTeamInput.get() not in autocompleteList or len(
                firstTeamInput.get()) is 0:
            firstTeamInput.configure(bg='red')
        else:
            firstTeamInput.configure(bg='white')

        # Highlight which input has issues
        if secondTeamInput.get() not in autocompleteList or len(
                secondTeamInput.get()) is 0:
            secondTeamInput.configure(bg='red')
        else:
            secondTeamInput.configure(bg='white')
        if (firstTeamInput.get() in autocompleteList
                and secondTeamInput.get() in autocompleteList):
            firstTeamInput.configure(bg='white')
            secondTeamInput.configure(bg='white')
            # Send team values to the predictor to do analysis
            predictor = pred.Predictor()
            teamOneScore, teamTwoScore = predictor.analyze(
                firstTeamInput.get(), secondTeamInput.get())
            if (teamOneScore > teamTwoScore):
                message = 'Expected Winner: \n' + firstTeamInput.get(
                ) + '\nScore: ' + str(int(teamOneScore)) + ' - ' + str(
                    int(teamTwoScore))
            elif (teamTwoScore > teamOneScore):
                message = 'Expected Winner: \n' + secondTeamInput.get(
                ) + '\nScore: ' + str(int(teamTwoScore)) + ' - ' + str(
                    int(teamOneScore))
            else:
                message = 'Expected a tie. \n' + '\nScore: ' + teamTwoScore + ' - ' + teamOneScore
        else:
            message = 'Please enter a valid team.'
        winnerLabel.configure(text=message)
        winnerLabel.grid(row=4, padx=10, pady=10)
Exemplo n.º 23
0
def main():
    logging.info("learnTicTacToeWithDecoderMLP.py main()")

    authority = tictactoe.Authority()
    positionTensorShape = authority.PositionTensorShape()
    moveTensorShape = authority.MoveTensorShape()
    playerList = authority.PlayersList()

    if args.startWithNeuralNetwork is not None:
        raise NotImplementedError(
            "main(): Start with a neural network is not implemented...")
    else:
        if args.startWithAutoencoder is not None:
            autoencoderNet = autoencoder.position.Net()
            autoencoderNet.Load(args.startWithAutoencoder)

            decoderMLP = Decoder.BuildAnMLPDecoderFromAnAutoencoder(
                autoencoderNet, decodingLayerSizesList)
        else:
            raise NotImplementedError(
                "main(): Starting without an autoencoder is not implemented..."
            )

    # Create the optimizer
    logging.debug(decoderMLP)
    for name, param in decoderMLP.named_parameters():
        if 'decoding' in name:
            param.requires_grad = True
        else:
            param.requires_grad = True
        print("name = {}; param.requires_grad = {}".format(
            name, param.requires_grad))

    optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad,
                                        decoderMLP.parameters()),
                                 lr=args.learningRate,
                                 betas=(0.5, 0.999))

    # Loss function
    loss = torch.nn.MSELoss()

    # Initial learning rate
    learningRate = args.learningRate

    # Output monitoring file
    epochLossFile = open(os.path.join(args.outputDirectory, 'epochLoss.csv'),
                         "w",
                         buffering=1)  # Flush the buffer at each line
    epochLossFile.write(
        "epoch,trainingMSE,validationMSE,averageRewardAgainstRandomPlayer,winRate,drawRate,lossRate\n"
    )

    # First game with a random player, before any training
    (numberOfWinsForEvaluator, numberOfWinsForRandomPlayer,
     numberOfDraws) = Predictor.SimulateGamesAgainstARandomPlayer(
         decoderMLP, authority, args.numberOfGamesAgainstARandomPlayer)
    winRate = numberOfWinsForEvaluator / (
        numberOfWinsForEvaluator + numberOfWinsForRandomPlayer + numberOfDraws)
    lossRate = numberOfWinsForRandomPlayer / (
        numberOfWinsForEvaluator + numberOfWinsForRandomPlayer + numberOfDraws)
    drawRate = numberOfDraws / (numberOfWinsForEvaluator +
                                numberOfWinsForRandomPlayer + numberOfDraws)
    logging.info(
        "Against a random player, winRate = {}; drawRate = {}; lossRate = {}".
        format(winRate, drawRate, lossRate))

    epochLossFile.write('0' + ',' + '-' + ',' + '-' + ',' +
                        str(winRate - lossRate) + ',' + str(winRate) + ',' +
                        str(drawRate) + ',' + str(lossRate) + '\n')

    playerToEpsilonDict = {
        playerList[0]: args.epsilon,
        playerList[1]: args.epsilon
    }

    for epoch in range(1, args.numberOfEpochs + 1):
        logging.info("Epoch {}".format(epoch))
        decoderMLP.train()

        # Generate positions
        minimumNumberOfMovesForInitialPositions = MinimumNumberOfMovesForInitialPositions(
            epoch)
        maximumNumberOfMovesForInitialPositions = args.maximumNumberOfMovesForInitialPositions
        logging.info("Generating positions...")
        startingPositionsList = Predictor.SimulateRandomGames(
            authority, minimumNumberOfMovesForInitialPositions,
            maximumNumberOfMovesForInitialPositions,
            args.numberOfPositionsForTraining)
        #print ("main(): startingPositionsList = {}".format(startingPositionsList))

        startingPositionsTensor = StartingPositionsTensor(
            startingPositionsList)
        logging.info(
            "Evaluating expected reward for each starting position...")
        expectedRewardsList = Predictor.ExpectedRewardsList(
            authority,
            decoderMLP,
            startingPositionsList,
            args.numberOfSimulations,
            playerList[1],
            playerToEpsilonDict=playerToEpsilonDict)
        #print ("main(): expectedRewardsList = {}".format(expectedRewardsList))
        expectedRewardsTensor = ExpectedRewardsTensor(expectedRewardsList)

        # Since the samples are generated dynamically, there is no need for minibatches: all samples are always new
        optimizer.zero_grad()

        # Forward pass
        outputTensor = decoderMLP(startingPositionsTensor)

        # Calculate the error and backpropagate
        trainingLoss = loss(outputTensor, expectedRewardsTensor)
        logging.info("trainingLoss.item() = {}".format(trainingLoss.item()))

        trainingLoss.backward()

        # Move in the gradient descent direction
        optimizer.step()

        # Validation
        decoderMLP.eval()
        validationStartingPositionsList = Predictor.SimulateRandomGames(
            authority, minimumNumberOfMovesForInitialPositions,
            maximumNumberOfMovesForInitialPositions,
            args.numberOfPositionsForValidation)

        validationStartingPositionsTensor = StartingPositionsTensor(
            validationStartingPositionsList)
        logging.info(
            "Evaluating expected reward for each validation starting position..."
        )
        validationExpectedRewardsList = Predictor.ExpectedRewardsList(
            authority,
            decoderMLP,
            validationStartingPositionsList,
            args.numberOfSimulations,
            playerList[1],
            playerToEpsilonDict=playerToEpsilonDict)
        validationExpectedRewardsTensor = ExpectedRewardsTensor(
            validationExpectedRewardsList)

        validationOutputTensor = decoderMLP(validationStartingPositionsTensor)
        validationLoss = loss(validationOutputTensor,
                              validationExpectedRewardsTensor)
        logging.info("validationLoss.item() = {}".format(
            validationLoss.item()))

        # Play against a random player
        (numberOfWinsForEvaluator, numberOfWinsForRandomPlayer,
         numberOfDraws) = Predictor.SimulateGamesAgainstARandomPlayer(
             decoderMLP, authority, args.numberOfGamesAgainstARandomPlayer)
        winRate = numberOfWinsForEvaluator / (numberOfWinsForEvaluator +
                                              numberOfWinsForRandomPlayer +
                                              numberOfDraws)
        lossRate = numberOfWinsForRandomPlayer / (numberOfWinsForEvaluator +
                                                  numberOfWinsForRandomPlayer +
                                                  numberOfDraws)
        drawRate = numberOfDraws / (numberOfWinsForEvaluator +
                                    numberOfWinsForRandomPlayer +
                                    numberOfDraws)
        logging.info(
            "Against a random player, winRate = {}; drawRate = {}; lossRate = {}"
            .format(winRate, drawRate, lossRate))

        epochLossFile.write(
            str(epoch) + ',' + str(trainingLoss.item()) + ',' +
            str(validationLoss.item()) + ',' + str(winRate - lossRate) + ',' +
            str(winRate) + ',' + str(drawRate) + ',' + str(lossRate) + '\n')
        if epoch % 10 == 0:
            filepath = os.path.join(args.outputDirectory,
                                    'tictactoe_' + str(epoch) + '.bin')
            decoderMLP.Save(filepath)
Exemplo n.º 24
0
Arquivo: test.py Projeto: tgaldes/UCLA
import Predictor
import stocks as s
a = Predictor.Predictor('2016-06-03', s.stocks, "ticktalk", "daySummaries",
                        "root", "", "localhost")
print a.getSignedDailyMentions('MSFT', True)
print a.getSignedDailyMentions('MSFT', False)
Exemplo n.º 25
0
# -*- coding: utf-8 -*-
"""
Created on Mon Jun  4 08:56:23 2018

@author: florefe
"""

import Predictor as pr
import pandas as pd
import numpy as np

num_files_training_with = 4
predictor = pr.Predictor()
predictor.load_trainers(1,num_files_training_with)
#test_data = [[29.99254335, -95.42296647, '12/8/2017 1:33']]

inputs = pd.read_csv('../data/notification_submission_dest_coords.csv')

#predict arrival at current spot and store as delta
test_data = []
for i in range(0,6):
    test_data.append([[inputs['lat'][i], inputs['lng'][i],
                       inputs['route_begin'][i], inputs['timestamp'][i],
                       inputs['dest_lat'][i],
                       inputs['dest_lng'][i]]])

delta =[]

for test in test_data:
    for t in test:
        delta.append(predictor.predict(t[0], t[1], t[2], t[3]))
	prod_desc_file = sys.argv[1]
	train_file = sys.argv[2]
	attribute_file = sys.argv[3]
	
	#Create dataframes for the input files
	prod_desc_df = get_df(prod_desc_file)
	train_df = get_df(train_file)
	attribute_df = get_df(attribute_file)

	test_df = get_df('test.csv')
	

	start_time = time.time()

	if not os.path.isfile('train_features.csv') and not os.path.isfile('test_features_test.csv'):
		get_training_processed(prod_desc_df,train_df,attribute_df)
		get_test_processed(prod_desc_df,test_df,attribute_df)

		#Generate features
		IPCA.ipca()

	#Make Predictions and get root mean squared error for each model
	if os.path.isfile('SVR_model_rbf.pkl') and os.path.isfile('SVR_model_linear.pkl') and os.path.isfile('SVR_model_poly.pkl') and os.path.isfile('GBR_model.pkl') and os.path.isfile('random_forest_model.pkl'):
		error_dict = Predictor.get_predictions()
		print error_dict

	runtime = time.time() - start_time
	print '-----'
	print '%.2f seconds to run' % runtime
	print '-----'
	print 'done'
Exemplo n.º 27
0
        self.topBox.pack_start(self.searchField, True, True, 0)
        self.searchField.set_text("is")

        #Add search button
        self.search = Gtk.Button(label="Search")
        self.topBox.pack_start(self.search, True, True, 0)
        self.search.connect("clicked", on_search_click,
                            self.searchField.get_text())

        #Add the Suggestions label
        self.label = Gtk.Label(label="Suggestions:")
        self.box.pack_start(self.label, True, True, 0)


win = mainWindow()

#Connect the closing of the main window, to ending the script with Gtk.main_quit
win.connect("delete-event", Gtk.main_quit)

#Run the createButtons function
createButtons(buttonAmount)

#Show the window and it's children(buttons)
win.show_all()

#Start the predictor
Predictor.initiateDB("MarkovComplete.db")

#Executes the main function of Gtk
Gtk.main()
Exemplo n.º 28
0
import json
import base64
from Predictor import *
from django.shortcuts import render, render_to_response
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from .forms import UserFormLogin, ImageForm
from django.template import RequestContext
from django.contrib import auth
from .models import User, Image
from django.views.decorators.csrf import csrf_protect
from acne import settings
import matplotlib.image as mpimg

p = Predictor(
    '/home/zjc/acne/myacne/keras_model/bottleneck_fc_model_architecture.json',
    '/home/zjc/acne/myacne/keras_model/bottleneck_fc_model_weights.h5',
    '/home/zjc/acne/myacne/keras_model/top_seven_model_architecture.json',
    '/home/zjc/acne/myacne/keras_model/top_seven_model_weights.h5')


#@csrf_protect
def userlogin(request):
    if request.method == 'POST':
        uf = UserFormLogin(request.POST)
        #return HttpResponse("post ok")
        if uf.is_valid():
            #return HttpResponse("the uf is valid")
            username = uf.cleaned_data['username']
            password = uf.cleaned_data['password']
            if username and password:
                userResult = User.objects.filter(Username=username,
Exemplo n.º 29
0
if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("-file",
                        "-f",
                        help="File to load",
                        type=str,
                        default=r'/home/fillan/Datasets/JoelPapersTest.txt')
    parser.add_argument('-log',
                        help='Log save location',
                        type=str,
                        default=None)
    parser.add_argument('-look_back',
                        '-l',
                        help="Number of periods to look back on",
                        type=int,
                        default=5)
    parser.add_argument('-load',
                        '-o',
                        help="Load location",
                        type=str,
                        default=None)
    args = parser.parse_args()
    text = Predictor.load_file(args.file)
    X, Y = Predictor.create_dataset(text, args.look_back)
    model = Predictor.load_model(args.load)
    model.compile(loss='mean_squared_error', optimizer='adam')
    metrics = model.evaluate(X, Y)
    with open(args.log, 'a') as f:
        f.write(os.linesep + "Test Set" + os.linesep)
        f.write(str(metrics) + os.linesep)
Exemplo n.º 30
0
env = gym.make("2048-v0")
move_mapper_dict = {0: 0, 1: 2, 2: 3, 3: 1}
move_name = ["UP", "RIGHT", "DOWN", "LEFT"]
scores = []

num_of_games = 100
current_game = 1
while current_game <= num_of_games:
    has_game_ended = False
    observation = env.reset()
    prev_state_responses = []
    while not has_game_ended:
        print(env.env.get_board())
        state = [data_handler.treat_features(env.env.get_board().flatten())]
        state_string = str(state)
        predictions = predictor.predict(state).pop()
        prediction = random.randint(0, 4)

        previously_encountered = True
        while previously_encountered:
            if np.sum(predictions[0]) == 0:
                prediction = random.randint(0, 10) % 4
                break

            prediction = predictions.argmax(axis=1)[0]
            state_response = state_string + "#" + str(prediction)
            previously_encountered = state_response in prev_state_responses
            if not previously_encountered:
                prev_state_responses.append(state_response)

            predictions[0][prediction] = 0
Exemplo n.º 31
0
def getConvertUp(word):
    #This function takes a string, looks it up in the database, converts the output to a list, and then updates the suggestions
    return updateSuggestions(convertSuggestion(Predictor.getWords(word)), buttonAmount)
Exemplo n.º 32
0
import numpy as np
from DataHandler import DataHandler
import Predictor as predictor

data_handler = DataHandler(16, 4, "state_responses_vsmall.csv")
features, labels = data_handler.extract_features_labels()
print np.shape(features)
predictions = predictor.predict(features).pop()

print predictions
Exemplo n.º 33
0
def getConvertUp(word):
    #This function takes a string, looks it up in the database, converts the output to a list, and then updates the suggestions
    return updateSuggestions(convertSuggestion(Predictor.getWords(word)),
                             buttonAmount)
Exemplo n.º 34
0
    ],
    [
        2, 1, 1, 2, 1, 1, 2, 1, 1, 3, 0, 1, 3, 0, 1, 3, 0, 1, 3, 0, 1, 3, 0, 1,
        3, 0, 1, 2, 1, 1, 3, 0, 1, 2, 1, 1, 3, 0, 1, 3, 0, 0, 2, 1, 1, 1, 2, 0
    ],
    [
        2, 1, 1, 2, 1, 1, 1, 2, 0, 3, 0, 1, 3, 0, 1, 3, 0, 1, 3, 0, 1, 3, 0, 1,
        3, 0, 1, 2, 1, 1, 2, 1, 1, 1, 2, 0, 3, 0, 1, 1, 2, 1, 2, 1, 1, 1, 2, 0
    ],
    [
        3, 0, 1, 3, 0, 1, 2, 1, 1, 3, 0, 1, 3, 0, 1, 3, 0, 1, 3, 0, 1, 3, 0, 1,
        2, 1, 1, 2, 1, 1, 3, 0, 1, 3, 0, 1, 3, 0, 1, 3, 0, 0, 3, 0, 1, 1, 2, 0
    ]
]

pr.entrenar(x, y)
print("modelo entrenado")

print("presicion: ", pr.probar(xp, yp))

array = pr.predecir([1, 2, 9, 4, 1, 2, 4, 2, 1, 1, 1])
print("prediccion: ", array)

print("interpretacion")
pr.interpretar(array)

print("arbol")
pr.imprimir()

#pr.entrenarR(x,y)
Exemplo n.º 35
0
        self.topBox.pack_start(self.searchField, True, True, 0)
        self.searchField.set_text("is")

        #Add search button
        self.search = Gtk.Button(label = "Search")
        self.topBox.pack_start(self.search, True, True, 0)
        self.search.connect("clicked", on_search_click, self.searchField.get_text())

        #Add the Suggestions label
        self.label = Gtk.Label(label="Suggestions:")
        self.box.pack_start(self.label, True, True, 0)


win = mainWindow()

#Connect the closing of the main window, to ending the script with Gtk.main_quit
win.connect("delete-event", Gtk.main_quit)

#Run the createButtons function
createButtons(buttonAmount)

#Show the window and it's children(buttons)
win.show_all()

#Start the predictor
Predictor.initiateDB("MarkovComplete.db")

#Executes the main function of Gtk
Gtk.main()