Beispiel #1
0
def init(file_name):
    print "Starting part II"
    step_size = 0.001
    processed_file = rewrite.run(file_name)
    result = part2a.run(processed_file)
    predict.predict(step_size, result)
    print "Part II done"
Beispiel #2
0
def finalTest(size_training, size_test, hidden_layers, lambd, num_iterations):
    print "\nBeginning of the finalTest... \n"

    images_training, labels_training, images_test, labels_test = read_dataset(size_training, size_test)
    # Setup the parameters you will use for this exercise
    input_layer_size = 784        # 28x28 Input Images of Digits
    num_labels = 10         # 10 labels, from 0 to 9 (one label for each digit)
    layers = [input_layer_size] + hidden_layers + [num_labels]
    num_of_hidden_layers = len(hidden_layers)
    # Fill the randInitializeWeights.py in order to initialize the neural network weights.
    Theta = randInitializeWeights(layers)

    # Unroll parameters
    nn_weights = unroll_params(Theta)
    res = fmin_l_bfgs_b(costFunction, nn_weights, fprime=backwards, args=(layers, images_training, labels_training, num_labels, lambd), maxfun = num_iterations, factr = 1., disp = True)
    Theta = roll_params(res[0], layers)

    print "\nTesting Neural Network... \n"

    pred_training = predict(Theta, images_training)
    print '\nAccuracy on training set: ' + str(mean(labels_training == pred_training) * 100)

    pred = predict(Theta, images_test)
    print '\nAccuracy on test set: ' + str(mean(labels_test == pred) * 100)

    # Display the images where the algorithm got wrong
    temp = (labels_test == pred)
    indexes_false = []
    for i in range(size_test):
        if temp[i] == 0:
            indexes_false.append(i)

    displayData(images_training[indexes_false, :])
Beispiel #3
0
def make_predictions(year):
    import predict
    PREDICTIVE_INDICES = [1, 3, 12, 23, 26, 27, 28, 34, 37, 40]
    print 'make_predictions(year=%d)' % year
    X,y = getXy_pcg(year)
    X = X[:,PREDICTIVE_INDICES]
    predict.predict(X, y)
Beispiel #4
0
def ave_percep(train_path, test_path, category, k):
	"""
	k refers to the category whose classified label is set 1
	'0' : 'atheism'
	'1' : 'politics'
	'2' : 'science'
	'3' : 'sports'

	"""
	print '------------------Averaged Perceptron Algorithm----------------'
	print 'Loading data...'
	#load data
	[x, y, train_size,x_test,y_test,test_size] = document_vectorize.createDataSet(train_path, test_path, category,k)
	MaxIteration = 100
	sample_num = train_size[0]
	feature_num = train_size[1]

	w = zeros(feature_num)
	b = 0
	w_a = zeros(feature_num)
	b_a = 0
	c = 1
	w_ave = zeros(feature_num)
	b_ave = 0

	print 'Start training...'
	start_time = time.time()
	for iteration in range(MaxIteration):
		miss = 0
	#	print "iteration "+str(iteration)  
		for i in range(sample_num):
			#check misclassified point and modify weight
			if ((numpy.inner(x[i].toarray(),w)+b)) * y[i] <= 0:
				miss += 1
				w = numpy.add(w, numpy.multiply(x[i].toarray(),y[i]))
				w_a = numpy.add(w_a, numpy.multiply(x[i].toarray(),y[i]*c))
				b += y[i]
				b_a += y[i] * c
				c += 1
		#print "miss="+str(miss)

		w_ave = w - numpy.multiply(w_a, 1.0/c)
		b_ave = b - b_a/float(c)
		#print "miss:"+str(miss) 
		#print "iteration "+str(iteration) 
		if miss == 0:
			break
		#print "RESULT: w_ave: " + str(w_ave) + " b_ave: " + str(b_ave)

	print 'Training time: ', time.time() - start_time
	print "Result: w: " + str(w_ave) + " b: " + str(b_ave)
	print 'Start testing'
	predict.predict(x, y, train_size, x_test, y_test, test_size, w_ave, b_ave)


	return w_ave, b_ave
Beispiel #5
0
def run_dir(in_path, out_path):
    for item in os.listdir(in_path):
        if item.endswith('.wav'):
            out_file_path = out_path + item.replace('.wav', '.TextGrid')
            predict(in_path + item, out_file_path, 'rnn')
            out_txt = out_file_path.replace('.TextGrid', '.txt')
            t = TextGrid()
            t.read(out_file_path)
            onset = int(t._TextGrid__tiers[0]._IntervalTier__intervals[1]._Interval__xmin*100)
            offset = int(t._TextGrid__tiers[0]._IntervalTier__intervals[1]._Interval__xmax*100)
            fid = open(out_txt, 'w')
            fid.write(str(onset)+'-'+str(offset))
            fid.close()
Beispiel #6
0
def primal_svm(train_dataSet, train_labels, test_dataSet, test_labels, C):
	


	train_sample_num = len(train_labels)
	train_feature_num = len(train_dataSet[0])
	test_sample_num = len(test_labels)
	test_feature_nume = len(test_dataSet[0])

	# Create a new model
	m = Model("qp")
	m.setParam('OutputFlag', False) 

	# Create variables
	lamda = 1.0 / C
	w = [m.addVar(lb = -GRB.INFINITY, name = 'w' + str(i+1)) for i in range(train_feature_num)]
	b = m.addVar(lb = -GRB.INFINITY, name = 'b')
	kathe = [m.addVar(lb = 0, name = 'kathe' + str(i+1)) for i in range(train_sample_num)]

	# Integrate new variables
	m.update()

	# Set objective: primal form
	obj = lamda * numpy.inner(w, w) / 2.0 +  numpy.sum(kathe) 
	m.setObjective(obj)

	# Add constraint
	for i in range(train_sample_num):
		m.addConstr(numpy.multiply(train_labels[i], numpy.inner(w, train_dataSet[i]) + b) >= 1 - kathe[i])
		#m.addConstr(kathe[i] >= 0)

	
	m.optimize()

	#get parameters

	w = [i.x for i in w]
	b = b.x
	kathe = [i.x for i in kathe]

	margin = 2 / numpy.sqrt(numpy.inner(w, w))
	sv = 0
	for i in range(train_sample_num):
		if  abs(numpy.inner(w, train_dataSet[i]) + b) <= 1 :
			sv += 1
	
	print 'support vectors: ',sv
	print 'margin: ',margin

	predict.predict(train_dataSet, train_labels, test_dataSet, test_labels, w, b)
def output(part_id):
    # Random Test Cases
    X = np.column_stack((np.ones(20),
                          (np.exp(1) * np.sin(np.linspace(1, 20, 20))),
                          (np.exp(0.5) * np.cos(np.linspace(1, 20, 20)))))
    y = np.sin(X[:,0] + X[:,1]) > 0

    Xm = np.array([[-1,-1],[-1,-2],[-2,-1],[-2,-2],[1,1],[1,2],[2,1],[2,2],[-1,1],
          [-1,2],[-2,1],[-2,2],[1,-1],[1,-2],[-2,-1],[-2,-2]]).reshape((16,2))
    ym = np.array([1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4]).reshape(16,1)
    t1 = np.sin(np.array(range(1,24,2)).reshape(3,4).T)
    t2 = np.cos(np.array(range(1,40,2)).reshape(5,4).T)

    fname = srcs[part_id-1].rsplit('.',1)[0]
    mod = __import__(fname, fromlist=[fname], level=1)
    func = getattr(mod, fname)

    if part_id == 1:
        J = lrCostFunction(np.array([0.25, 0.5, -0.5]), X, y, 0.1)
        grad = gradientFunctionReg(np.array([0.25, 0.5, -0.5]), X, y, 0.1)
        return sprintf('%0.5f ', np.hstack((J, grad)).tolist())
    elif part_id == 2:
        return sprintf('%0.5f ', oneVsAll(Xm, ym, 4, 0.1))
    elif part_id == 3:
        return sprintf('%0.5f ', predictOneVsAll(t1, Xm))
    elif part_id == 4:
        return sprintf('%0.5f ', predict(t1, t2, Xm))
Beispiel #8
0
def index(path):
    if request.method == "POST":
        for file in request.files.values():
            if file and allowed_file(file.filename):
                filename = secure_filename(file.filename)
                filepath = get_savepath(filename)
                file.save(filepath)
                return """
                {
                    "filename": "%s",
                    "predict": "%s",
                }
                """ % (
                    filename,
                    predict(filepath),
                )
    else:
        return """
        <!doctype html>
        <title>Upload new File</title>
        <body>
        <form action="/" method="post" enctype="multipart/form-data">
            <input type=file name=file>
            <input type=submit value=Upload>
        </form>
        <p>%s</p>
        </body>
        """ % "<br>".join(
            os.listdir(app.config["UPLOAD_FOLDER"])
        )
Beispiel #9
0
    def post(self, request, *args, **kwargs):
	form = self.form_class(request.POST)
        context = {}
	context['atomRange'] = range(0, 36)
	context['atomNum'] = range(0,2)
        context['form'] = self.form_class

        if form.is_valid():
            atom_category = form.cleaned_data['atomCategory']
	    atom_list = form.cleaned_data['atomList']
            predicted_data = predict.predict(atom_category,atom_list)
            result_y = []
            start = -10.0
            while start <= 10.0:
                result_y.append(start)
                start = start + 0.02
            final_result = []
            for i in range(len(result_y)):
                temp = []
                temp.append(result_y[i])
                temp.append(predicted_data[0][i])
		temp.append(predicted_data[1][i])
                final_result.append(temp)
            context['result'] = final_result
        return render(request, self.template_name, context)
Beispiel #10
0
def predict_classes_three():
    classes_two = get_classes(2)

    label = []
    y = np.array([])
    i = 0
    correct = 0
    for cl in classes_two:
        classifier = get_classifier()
        vectorizer = get_vectorizer()
        classes = get_classes_3(cl)

        read_data.read_bucket_data(classifier, vectorizer, classes, cl)

        ids = create_id_set(cl, io_files.categorie2_path)

#        correct += cross_validation.score(classifier, vectorizer, 3, super_class=cl)
        label_partial, y_partial = predict.predict(classifier, vectorizer, ids)
        i += 1
        logger.debug("We have done %d classes 2", i)
        label = label + label_partial
        y = np.append(y, y_partial)

    logger.info("Correct prediction : %d, total: %d, Score:%f", correct)
    output = io_files.submission_path

    write_output(output, label, y)
Beispiel #11
0
def predict_classes_two():
    classes_one = get_classes(1)

    label = []
    y = np.array([])
    i = 0
    correct = 0
    for cl in classes_one:
        classifier = get_classifier()
        vectorizer = get_vectorizer()

        classes = get_classes_2(cl)
        read_data.read_data_from_directory(classifier, vectorizer, classes, cl)

        ids = create_id_set(cl, io_files.categorie1_path)

#        correct += cross_validation.score(classifier, vectorizer, 2, super_class=cl)
        label_partial, y_partial = predict.predict(classifier, vectorizer, ids)
        i += 1
        logger.debug("We have done %d classes 1", i)

        label = label + label_partial
        y = np.append(y, y_partial)

    total = length_file(io_files.cross_validation_path)
    logger.info("Correct prediction : %d, total: %d, Score:%f", correct, total, correct/float(total))
    output = io_files.categorie2_path
    write_output(output, label, y)
def main(csv_path,img_list):
	p=predict()
	p.train(csv_path)
	result=''
	for i in img_list:
		result+=p.predict(i)
	return result
Beispiel #13
0
def _predict(source):
    from gp_query import calibrate_query_timeout
    from predict import predict
    timeout = TIMEOUT if TIMEOUT > 0 else calibrate_query_timeout(SPARQL)
    return predict(
        SPARQL, timeout, GPS, source,
        FUSION_METHODS, MAX_RESULTS, MAX_TARGET_CANDIDATES_PER_GP)
Beispiel #14
0
def get_prediction(file_path):

    LABEL_MAP = {
        0 : "English",
        1 : "German",
        2 : "French",
        3 : "Spanish"
    }

    # TODO remove this for production
    # predictions = [[0.3, 0.7]]
    predictions = predict(file_path, app.config["PROTOTXT"], app.config["MODEL"], app.config["UPLOAD_FOLDER"])
    predictions = np.mean(predictions, axis=0).tolist()

    print predictions

    pred_with_label = {LABEL_MAP[index] : prob for index, prob in enumerate(predictions)}

    file_path = file_path + "?cachebuster=%s" % time.time()
    result = {
        "audio" : {
            "url" : "%s" % file_path,
        },
        "predictions" : pred_with_label
    }

    return result
    def run(self):
        global done
        while not self.terminated:
            if self.event.wait(1):
                try:
                    self.stream.seek(0)

                    with imageLock:
                        if not done:
                            X = np.array(Image.open(self.stream).convert('L').getdata())
                            X = X.reshape(1, X.size)
                            
                            self.currentKey = predict(Theta1, Theta2, X)

                            print self.currentKey

                            if self.currentKey == 0:#foward
                                car.goFoward()
                            elif self.currentKey == 1:#right
                                car.goRight(self.difference)
                            elif self.currentKey == 2:#left
                                car.goLeft(self.difference)
                except KeyboardInterrupt as e:
                    raise e
                    done = True

                finally:
                    self.stream.seek(0)
                    self.stream.truncate()
                    self.event.clear()
                    with lock:
                        pool.append(self)
Beispiel #16
0
def dual_svm(train_dataSet, train_labels, test_dataSet, test_labels, C):
	
	train_sample_num = len(train_labels)
	train_feature_num = len(train_dataSet[0])
	test_sample_num = len(test_labels)
	test_feature_num = len(test_dataSet[0])

	# Create a new model
	m = Model("qp")
	m.setParam('OutputFlag', False) 

	# Create variables
	#lamda = 1.0 / C
	alpha = [m.addVar(lb = 0, name = 'alpha' + str(i+1)) for i in range(train_sample_num)]
	
	
	# Integrate new variables
	m.update()

	# Set objective:dual form

	obj_sum = 0
	for i in range(train_sample_num):
		for j in range(train_sample_num):
			obj_sum += alpha[i] * alpha [j] * train_labels[i] * train_labels[j] * numpy.inner(train_dataSet[i], train_dataSet[j])

	obj =  numpy.sum(alpha) - obj_sum / 2.0

	m.setObjective(obj, GRB.MAXIMIZE)

	# Add constraint
	sample_sum = 0
	for i in range(train_sample_num):
		sample_sum += alpha[i] * train_labels[i]
		m.addConstr(alpha[i] <= C)
	m.addConstr(sample_sum == 0)

	m.optimize()

	#get parameter
	alpha = [i.x for i in alpha]
	

		

	[w, b] = estimate(train_dataSet, train_labels, alpha, C)
	predict.predict(train_dataSet, train_labels, test_dataSet, test_labels, w, b)
def main():

    # Parse arguments
    parser = argparse.ArgumentParser()

    parser.add_argument(
        "-t",
        dest="test_set",
        help="2014 test set.",
        default="/data1/nlp-data/twitter/tools/scoring/B/annotated-B-filtered-2014.txt",
    )

    parser.add_argument(
        "-m",
        dest="model",
        help="trained ML model.",
        default=os.path.join(os.getenv("BISCUIT_DIR"), "TaskB", "models", "awesome"),
    )

    args = parser.parse_args()

    # Arguments
    data = args.test_set
    model_path = args.model

    # Read SemEval 2014 data
    X = []
    with open(data, "r") as f:
        for line in f.readlines():
            toks = line.split("\t")
            sid = toks[0]
            text = toks[3]
            X.append((sid, text))
    # X = zip(note.sid_list(), note.text_list())

    # Load model
    with open(model_path + ".model", "rb") as fid:
        clf = pickle.load(fid)
    with open(model_path + ".dict", "rb") as fid:
        vec = pickle.load(fid)

    # print X

    # Predict labels of data using model
    labels = predict(X, clf, vec)

    # Format output
    tmp_out = "predictions.txt"
    with open(tmp_out, "w") as f:
        for i, label in enumerate(labels):
            print >> f, "NA\t" + str(i + 1) + "\t" + label

    # Call official scorer to evaluate results
    RUN_SCORER_CMD = "perl /data1/nlp-data/twitter/tools/scoring/B/score-B-2014.pl %s" % tmp_out
    status, output = commands.getstatusoutput(RUN_SCORER_CMD)
    print output

    # Clean up
    os.remove(tmp_out)
def answer():
    global ques
    
    
    answer = pdict.predict(nlp, encoder, model, ques, img_matrix)
    answer = str(answer[0])
    print (answer)
    return jsonify(ans = answer)
Beispiel #19
0
def test_trainset1(nn_params, X, y, struc):    
    p = predict(nn_params, X, struc)
    s = bin_to_dec(p)
    g = bin_to_dec(y)
    nz = np.where(g>0)
    g = g[nz]
    s = s[nz]
    r = np.mean(np.abs(g-s)/g)
    return r
Beispiel #20
0
def perceptron(train_path, test_path, category, k):
  """
  k refers to the category whose classified label is set 1
  '0' : 'atheism'
  '1' : 'politics'
  '2' : 'science'
  '3' : 'sports'
    
  """
  print '--------------------- Perceptron Algorithm ----------------'
  print 'Loading data...'
  
  #load data
  [x, y, train_size,x_test,y_test,test_size] = document_vectorize.createDataSet(train_path, test_path, category,k)


  MaxIteration = 300
  sample_num = train_size[0]
  feature_num = train_size[1]
  w = zeros(feature_num)
  b = 0

  print 'Start training...'
  start_time = time.time()
  for iteration in range(MaxIteration):
    miss = 0

    for i in range(sample_num):
      #check misclassified point and modify weight        
      if ((numpy.inner(x[i].toarray(),w)+b))*y[i] <= 0:
        miss += 1
        w = numpy.add(w, numpy.multiply(x[i].toarray(),y[i]))
    #print "miss:"+str(miss) 
    #print "iteration "+str(iteration)  
    #print "RESULT: w: " + str(w) + " b: " + str(b) + " miss: " + str(miss)
    
    if miss == 0 :
      break
       
  print 'Training time: ', time.time() - start_time
  print "Result: w: " + str(w) + " b: " + str(b)
  print 'Start testing'
  predict.predict(x, y, train_size, x_test, y_test, test_size, w, b)
Beispiel #21
0
def saveres(nn_params, X, struc, file_train):
    p = predict(nn_params, X, struc)
    S =  bin_to_dec(p)
    X, y = readSamples(file_train)
    db = DB("didi")
    for i in range(len(X)):
        rows = db.exe("select date from results0 where weekday(date)=%s limit 1" % X[i,1])
        db.exe("INSERT INTO results (district_id, date, slot, gap) VALUES ('%s', '%s', '%s', '%s')"
         % (X[i, 0], rows[0]["date"], X[i, 2], S[i] ))
    db.close()
def parse_command_line():
    parser = argparse.ArgumentParser(
        description="""Train, validate, and test a face detection classifier that will determine if
        two faces are the same or different.""")
    parser.add_argument("-p", "--prepare-data", help="Prepare training and validation data.",
        action="store_true")
    parser.add_argument("-t", "--train", help="""Train classifier. Use --graph to generate quality
        graphs""", action="store_true")
    parser.add_argument("-g", "--graph", help="Generate training graphs.", action="store_true")
    parser.add_argument("--weights", help="""The trained model weights to use; if not provided
        defaults to the network that was just trained""", type=str, default=None)
    parser.add_argument("--note", help="Adds extra note onto generated quality graph.", type=str)
    parser.add_argument("-s", "--is_same", help="""Determines if the two images provided are the
        same or different. Provide relative paths to both images.""", nargs=2, type=str)
    parser.add_argument("--visualize", help="""Writes out various visualizations of the facial
        images.""", action="store_true")

    args = vars(parser.parse_args())

    if os.environ.get("CAFFE_HOME") == None:
        print "You must set CAFFE_HOME to point to where Caffe is installed. Example:"
        print "export CAFFE_HOME=/usr/local/caffe"
        exit(1)

    # Ensure the random number generator always starts from the same place for consistent tests.
    random.seed(0)

    webface = WebFace()

    if args["prepare_data"] == True:
        webface.load_data()
        webface.pair_data()
    if args["visualize"] == True:
        # TODO: Adapt this to WebFace, not just LFW.
        visualize()
    if args["train"] == True:
        train(args["graph"], data=webface, weight_file=args["weights"], note=args["note"])
    if args["is_same"] != None:
        # TODO: Fill this out once we have a threshold and neural network trained.
        images = args["is_same"]
        predict(images[0], images[1])
Beispiel #23
0
	def solve(self):
	#========== Normalise the features and add neutral feature ================
		self.X = featureNormalize.featureNormalize(self.X)
		m = self.X.shape[0]
		n = self.X.shape[1]
		self.X = numpy.hstack((numpy.ones((m,1)), self.X))
		initialTheta = numpy.zeros(n+1).reshape(n+1,1)

		optimalTheta = fmin_cg(self.objectiveFunction, initialTheta, fprime=self.objectiveGrad,maxiter=self.iterations)
		objectiveValue = self.objectiveFunction(optimalTheta)
		prediction = numpy.dot(self.X, optimalTheta)
		return optimalTheta, objectiveValue, predict.predict(optimalTheta, self.X)
Beispiel #24
0
def indexGet():
    if 'keyword' in request.args:
        result = predict( request.args['keyword'] )
        if result == 1:
            return render_template('app.html', foo='分析結果:這是基本五樓文')
        if result == 2:
            return render_template('app.html', foo='分析結果:這是中等五樓文')
        if result == 3:
            return render_template('app.html', foo='分析結果:這是高等五樓文')
        else:
            return render_template('app.html', foo='分析結果:這不會出現五樓')
    return render_template('app.html', foo='')
Beispiel #25
0
def main(args):

    if args[1] == 'runseason':
        if len(args) > 2:
            run_season(args[2])
        else:
            run_season()

    elif args[1] == 'predict' and len(args) >= 4:
        print(predict(float(args[2]), float(args[3])))

    else:
        raise Exception('Invalid number of args %s' % len(args))
Beispiel #26
0
def crossvalidate(input, fold, outputdir, distance, ntrees, reuse, trainingdir,
                  randomforestdir, predictiondir):
    cases = []

    with open(input) as inputfile:
        reader = csv.reader(inputfile, delimiter=',', quotechar='"')
        for row in reader:
            cases.append((row[0], row[1:]))

    iteration = 1
    kfold = KFold(len(cases), n_folds=fold)
    for train_indices, test_indices in kfold:
        generateTrainingInput = {}

        for train_index in train_indices:
            generateTrainingInput[cases[train_index][0]] = \
                                  cases[train_index][1]

        trainingout = None
        if trainingdir:
            trainingout = os.path.join(trainingdir, 'cv-fold%d-training.csv' %
                                                    iteration)

        if reuse and trainingout and os.path.isfile(trainingout):
            print("Reusing training set CSV file %s..." % trainingout)
            trainingset = pd.read_csv(trainingout, quoting=csv.QUOTE_NONNUMERIC)
        else:
            trainingset = generateTrainingSet(generateTrainingInput, distance,
                                              output=trainingout)
        rfoutput = None
        if randomforestdir:
            rfoutput = os.path.join(randomforestdir, 'cv-fold%d-rf.joblib' %
                                          iteration)

        if reuse and rfoutput and os.path.isfile(rfoutput):
            print("Reusing RandomForest file %s..." % rfoutput)
            rf = joblib.load(rfoutput)
        else:
            rf = train(trainingset, ntrees, rfoutput)

        for test_index in test_indices:
            predictionList = predict(rf, cases[test_index][1], saveOutput=predictiondir,
                    outputdir=predictiondir, templateFile=cases[test_index][0])

            for j, model in enumerate(predictionList):
                model.label = os.path.basename(cases[test_index][1][j])

            rank(predictionList, os.path.join(outputdir, '%s-ranking.csv' %
                os.path.splitext(os.path.basename(cases[test_index][0]))[0]))

        iteration += 1
Beispiel #27
0
def predict_classes_one():
    classifier = get_classifier()
    vectorizer = get_vectorizer()
    classes = get_classes(1)

    read_data.read_data(classifier, vectorizer, classes)

    cross_validation.score(classifier, vectorizer, 1)

    ids = create_id_set(0, io_files.test_path)
    label, y = predict.predict(classifier, vectorizer, ids)

    output = io_files.categorie1_path
    write_output(output, label, y)
Beispiel #28
0
def test_trainset(nn_params, X, y, struc):    
    p = predict(nn_params, X, struc)
    s = bin_to_dec(p)
    g = bin_to_dec(y)
    g = np.ma.array(g, mask=False)
    s = np.ma.array(s, mask=False)
    g = g * 1.0
    a = np.where(g == 0)
    # g[a] = 0.1e300
    g.mask[a] = True
    g = g.compressed()
    s.mask[a] = True
    s = s.compressed()
    r = np.mean(np.abs(g-s)/g)
    return r
Beispiel #29
0
def evaluate(date=yesterday,company={}):
	companies = Company.objects.filter(**company)
	c_list = []
	for _company in companies:
		c_dict = {"ticker":_company.ticker,}
		pred = predict(date=date,company=c_dict)
		perf = performance(date=date,company=c_dict)
		try:
			accu = 100*abs((perf['avg_change'] - pred['avg_change'])/perf['avg_change'])
		except Exception, e:
			accu = None
		try:
			p_accu = 100*abs((perf['avg_perc'] - pred['avg_perc'])/perf['avg_perc'])
		except Exception, e:
			p_accu = None
Beispiel #30
0
def main():
    db = MySQLdb.connect(host="localhost",user="******",passwd="123456",db="xataka")

    X_train = getX(db,0,20)                     ## getX returns the X matrix to by constructing it from posts starting with offset 0 and limited up to 20(1st and 2nd argument respectively)
    Y_train = getY(db,0,20)                     ## getY returns the Y matrix to by constructing it from post catgories starting with posts from offset 0 and limited up to 20(1st and 2nd argument respectively)
    
    X_test = getX(db,20,6)
    Y_test = getY(db,20,6)

    train(X_train,Y_train,100)                  ## train takes as arguments X,Y and batchsize
    pred = predict(X_test,Y_test,1000)          ## predict takes as arguments X,Y and batchsize
    acc = prcntagaccurcy(pred,Y_test)           ## calculate the accuracy of classifier
    print "Accuracy :", acc , "%"

    stop = timeit.default_timer()               ## calculate time taken by program to run
    print stop-start, "seconds"         
datagen.fit(np.rollaxis(X_train, 3, 1))
#%%

#%%
X_train = np.rollaxis(X_train, 3, 1)
X_test = np.rollaxis(X_test, 3, 1)
#%%
model = get_resnet_model()
# Fit the model on the batches generated by datagen.flow().
model.fit_generator(datagen.flow(X_train, y_train, batch_size=64),
                    epochs=100,
                    validation_data=(X_test, y_test),
                    steps_per_epoch=len(X_train) / 64)

model.save_weights('intern_task_1_weights.h5')
model.save_weights('intern_task_1_weights_with_model.h5')
#%%
score, acc = model.evaluate(X_test, y_test, batch_size=32)
print('Test score:', score)
print('Test accuracy:', acc)
#%%
test = np.zeros([1, 64, 64])
test[0] = Digits[21]
predict = np.zeros([1, 64, 64, 1])
#predict[0] = X_train[5106:5656]
out = model.predict_classes(X_test)
#%%
from predict import predict
predict()
Beispiel #32
0
def api():
    # gender,	married, dependents,	education,	self_employed,	applicantIncome,	coapplicantIncome,	loanAmount,	loan_amount_term,	credit_history,	property_area
    if 'gender' in fl_requests.args:
        gender = fl_requests.args['gender']
        if gender != 'Male' and gender != 'Female':
            return "Error: gender must be Male or Female but got " + fl_requests.args[
                'gender']
    else:
        return "Error: No gender field provided. Please specify gender."

    if 'married' in fl_requests.args:
        married = fl_requests.args['married']
        if married != 'Yes' and married != 'No':
            return "Error: gender must be Yes or No but got " + fl_requests.args[
                'married']
    else:
        return "Error: No married field provided. Please specify married."

    if 'dependents' in fl_requests.args:
        dependents = fl_requests.args['dependents']
        if dependents != '0' and dependents != '1' and dependents != '2' and dependents != '3+':
            return "Error: dependents must be 0 or 1 or 2 or 3+ but got " + fl_requests.args[
                'dependents']
    else:
        return "Error: No dependents field provided. Please specify dependents."

    if 'education' in fl_requests.args:
        education = fl_requests.args['education']
        if education != 'Graduate' and education != 'Not Graduate':
            return "Error: education must be Graduate or Not Graduate but got " + fl_requests.args[
                'education']
    else:
        return "Error: No education field provided. Please specify education."

    if 'self_employed' in fl_requests.args:
        self_employed = fl_requests.args['self_employed']
        if self_employed != 'Yes' and self_employed != 'No':
            return "Error: self_employed must be Yes or No but got " + fl_requests.args[
                'self_employed']
    else:
        return "Error: No self_employed field provided. Please specify self_employed."

    if 'applicant_income' in fl_requests.args:
        applicantIncome = int(fl_requests.args['applicant_income'])
        if applicantIncome < 0:
            return "Error: applicant_income must be greater than or equal 0 but got " + fl_requests.args[
                'applicant_income']
    else:
        return "Error: No applicant_income field provided. Please specify applicant_income."

    if 'coapplicant_income' in fl_requests.args:
        coapplicantIncome = int(fl_requests.args['coapplicant_income'])
        if coapplicantIncome < 0:
            return "Error: coapplicant_income must be greater than or equal 0 but got " + fl_requests.args[
                'coapplicant_income']
    else:
        return "Error: No coapplicant_income field provided. Please specify coapplicant_income."

    if 'loan_amount' in fl_requests.args:
        loanAmount = int(fl_requests.args['loan_amount'])
        if loanAmount < 0:
            return "Error: loan_amount must be greater than or equal 0 but got " + fl_requests.args[
                'loan_amount']
    else:
        return "Error: No loan_amount field provided. Please specify loan_amount."

    if 'loan_amount_term' in fl_requests.args:
        loan_amount_term = int(fl_requests.args['loan_amount_term'])
        if loan_amount_term < 0 or loan_amount_term >= 600:
            return "Error: loan_amount_term must be between 0 - 599 " + fl_requests.args[
                'loan_amount_term']
    else:
        return "Error: No loan_amount_term field provided. Please specify loan_amount_term."

    if 'credit_history' in fl_requests.args:
        credit_history = fl_requests.args['credit_history']
        if credit_history != 'Yes' and credit_history != 'No':
            return "Error: credit_history must be Yes or No but got " + fl_requests.args[
                'credit_history']

        if credit_history == 'Yes':
            credit_history = 1
        else:
            credit_history = 0
    else:
        return "Error: No credit_history field provided. Please specify credit_history."

    if 'property_area' in fl_requests.args:
        property_area = fl_requests.args['property_area']
        if property_area != 'Rural' and property_area != 'Urban' and property_area != 'Semiurban':
            return "Error: property_area must Rural Yes or Urban or Semiurban but got " + fl_requests.args[
                'property_area']

    else:
        return "Error: No property_area field provided. Please specify property_area."

    return pd.predict(gender, married, dependents, education, self_employed,
                      applicantIncome, coapplicantIncome, loanAmount,
                      loan_amount_term, credit_history, property_area)
print(theta)

# Plot the decision boundary
plot_x = array([min(X[:, 1]) - 2, max(X[:, 2]) + 2])
plot_y = (-1.0 / theta[2]) * (theta[1] * plot_x + theta[0])
plt.plot(plot_x, plot_y)
plt.scatter(X[pos, 1], X[pos, 2], marker='o', c='b')
plt.scatter(X[neg, 1], X[neg, 2], marker='x', c='r')
plt.xlabel('Exam 1 score')
plt.ylabel('Exam 2 score')
plt.legend(['Decision Boundary', 'Admitted', 'Not Admitted'])
plt.title('Training Set')
plt.show()

# Compute accuracy on the training set
p = predict(array(theta), X)
counter = 0
for i in range(y.size):
    if p[i] == y[i]:
        counter += 1
print 'Train Accuracy on training set: %f' % (counter / float(y.size) * 100.0)

# Plot and Compute accuracy on the test set

X_new = ones((X_test.shape[0], 3))
X_new[:, 1:3] = X_test
X_test = X_new

pos = where(y_test == 1)  # instances of class 1
neg = where(y_test == 0)  # instances of class 0
plot_x = array([min(X_test[:, 1]) - 2, max(X_test[:, 2]) + 2])
Beispiel #34
0
in_path = imageFolder +  "*.jpg"
#print(in_path)
for img_file in glob.glob(in_path):
    label_file = img_file[:-3] + "txt"
    # print(label_file)
    if os.path.exists(label_file):
        with open(label_file) as file:
            label = file.read().decode("utf-8").strip().upper()
            labels.append(label)

        img = cv2.imread(img_file).astype(np.float32)/255.
        shape = img.shape
        image_width = shape[0] * shape[1] / image_height
        img = cv2.resize(img,(image_width, image_height))
        img = img.swapaxes(0,1)

        images.append(img)
        # after pooling, the feature length or the height of the image is resize to height/4
        seq_lens.append(image_width / 4)
        imagePaths.append(img_file.split('/')[-1])

batch_inputs, batch_seq_len = utils.pad_input_sequences(images)
results, costTime = pred.predict(batch_inputs, batch_seq_len/4)

print("Cost Time: ", costTime)
for i, res in enumerate(results):
    if labels[i] != res:
        print("Image: {0}, label: {1}, predict: {2}".format(imagePaths[i], labels[i], res))

    
Beispiel #35
0
from predict import predict
from tweets import get_tweets
import json
import os
import torch
import argparse

if __name__ == "__main__":
    my_parser = argparse.ArgumentParser()
    my_parser.version = '1.0'
    my_parser.add_argument("search_term", help="The search to be used in order to retrieve tweets")
    my_parser.add_argument('-n','--num_tweets', action='store',type=int,default=500,help='No of tweets to be retrieved, must be less than 1000')
    args = my_parser.parse_args()
    search_term = args.search_term
    no_of_tweets = args.num_tweets
    
    if not os.path.isfile('twitterAPIkeys.json'):
        raise FileNotFoundError('Please make sure a \'twitterAPIkeys.json\' exists in the src folder!')
    
    with open('twitterAPIkeys.json') as f:
        APIkeys_dict = json.load(f)
    
    
    tweet_df = get_tweets(search_term, no_of_tweets, APIkeys_dict, retweets=False)
    inputs = list(tweet_df['tweet_text'].values)
    outputs = predict(inputs)
    outputs_2d = tsne_cpu(outputs)
    # print(outputs_2d)
    tweet_df['x_coord'] = outputs_2d[:,0]
    tweet_df['y_coord'] = outputs_2d[:,1]
    visualise(tweet_df)
Beispiel #36
0
        print(
            "Your start date cannot occur after your target date. Please retry.\n\n"
        )

    else:
        break

#get population density
while True:
    inputPopulationDensity = float(
        input(
            "Enter the population density of your area (people/square mile): ")
    )
    if (inputPopulationDensity < 0):
        print(
            "You cannot have a negative population density. Please retry.\n\n")
    else:
        break

#create plot
plot(datesDifference.days, inputPopulationDensity, startDateObj, targetDateObj)

#return results
print("\n\n\n\n\n")
print("There will be approximately " +
      str(int(predict(datesDifference.days, inputPopulationDensity))) +
      " covid-19 cases on " + targetDateObj.strftime("%m/%d/%Y"))
print(
    "\nCovid-19 projection successfully saved as a pdf in your project directory"
)
Beispiel #37
0
def latent_factor_model(F, N , alpha, lambda1, pos_num, neg_sum, topN):
    print("f**k")
    # 获取函数开始时间
    start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    # 将测试数据集插入数据库中
    insert_table.insert_num_info()  # 插入用户,条目,评价个数
    insert_table.insert_info_rating()  # 插入训练集
    insert_table.insert_info_rating_test()  # 插入测试集
    insert_table.insert_info_item()  # 插入每个条目的具体信息
    # 首先获得用户数和item数
    # 打开数据库
    db = pymysql.connect("127.0.0.1", "root", "1351", "qb_db")
    # 使用cursor() 方法创建一个游标对象
    my_cursor = db.cursor()
    my_cursor.execute("SELECT * FROM info_num")
    data = my_cursor.fetchone()
    user_num = data[0]  # 用户数
    item_num = data[1]  # 条目数
    rating_num = data[2]  # 评价数

    # 初始化P Q矩阵,随机生成
    user_item_class = init_model.init_model(F, user_num, item_num)
    user_class = user_item_class[0]
    item_class = user_item_class[1]
    # 初始化用户-信息矩阵,将将其存入字典中
    user_item = dict()
    user_item = init_model.init_user_item(user_num, item_num)

    print("开始迭代训练。。。")
    for step in range(0, N):
        # 从数据集中依次取出user以及该user喜欢的items集
        my_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        print("第", step, "次迭代,时间:", my_time)
        for user in user_item:
            user_positive = dict()
            # 得到用户的正样本,
            user_positive = select_sample.select_positive_sample(user_item[user], pos_num)
            # 得到用户的负样本,
            user_negative = select_sample.select_negative_sample(user_item[user], neg_sum, user_positive)
            # 将正负样本集合合并
            user_samples = dict()
            user_samples.clear()
            user_samples = user_positive.copy()
            user_samples.update(user_negative)
            # print("正负样本集:", len(user_samples))
            for item in user_samples:
                # predict(user, item, P, Q)
                eui = round(user_samples[item] - predict.predict(user, item, user_class[user], item_class[item]), 5)
                for f in range(1, F+1):
                    user_class[user][f] += alpha * (eui * item_class[item][f] - lambda1 * user_class[user][f])
                    item_class[item][f] += alpha * (eui * user_class[user][f] - lambda1 * item_class[item][f])
                    user_class[user][f] = round(user_class[user][f], 5)
                    item_class[item][f] = round(item_class[item][f], 5)
        alpha *= 0.9
    print("迭代训练完成。。。")
    print("开始将user_class 和 item_class写入文件")
    #将 user_class 和 item_class写入文件
    fo = open("ml-100k/result/user_class.txt", "w")
    for x in user_class.keys():
        for y in user_class[x].keys():
            one_user_class = str(x) + "  " + str(y) + "  " + str(user_class[x][y])
            fo.write(one_user_class)
            fo.write("\n")
    fo.close()
    fo = open("ml-100k/result/item_class.txt", "w")
    for x in item_class.keys():
        for y in item_class[x].keys():
            one_item_class = str(x) + "  " + str(y) + "  " + str(item_class[x][y])
            fo.write(one_item_class)
            fo.write("\n")
    fo.close()
    print("写入文件完成!")
    print("计算每个用户的推荐列表。。。", end=' ')
    recommend_top_result = recommend_top.recommend_top([user_class, item_class], topN, user_num, item_num)
    print("计算完成!")
    print("计算准确度和召回率。。。")
    sum_accuracy.sum_accuracy(recommend_top_result, topN)
    end_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    print("开始时间:", start_time, ";结束时间:", end_time)
Beispiel #38
0
    if config.config['multicpu']:
        fleet.init(is_collective=True)
        optimizer = fleet.distributed_optimizer(optimizer)
        Recmodel = fleet.distributed_model(Recmodel)
        print('using fleet multicpu training', Recmodel)
    Neg_k = 1
    bpr = BPRLoss(Recmodel, config.config)
    f = open(f'logger/train_logger_{name_datasets}.txt', 'w')
    f_test = open(f'logger/test_logger_{name_datasets}.txt', 'w')

    for epoch in range(config.TRAIN_epochs):
        if epoch % 10 == 0:
            cprint("[TEST]")
            preds = predict(train_dataset,
                            Recmodel,
                            epoch,
                            multigpu=config.config['multigpu'],
                            multicpu=config.config['multicpu'])
            result = Test(train_dataset,
                          Recmodel,
                          epoch,
                          multicpu=config.config['multicpu'],
                          multigpu=config.config['multigpu'])
            print(epoch, result, file=f_test, flush=True)
        output_information = BPR_train_original(train_dataset,
                                                Recmodel,
                                                bpr,
                                                epoch,
                                                neg_k=Neg_k,
                                                w=None)
        log_output = f'EPOCH[{epoch+1}/{config.TRAIN_epochs}] {output_information}'
Beispiel #39
0
# Initialize fitting parameters
initial_theta = zeros((3, 1))

# Run minimize() to obtain the optimal theta
Result = op.minimize(fun=computeCost,
                     x0=initial_theta,
                     args=(X, y),
                     method='TNC',
                     jac=computeGrad)
theta = Result.x

# Plot the decision boundary
plot_x = array([min(X[:, 1]), max(X[:, 1])])
plot_y = (-1.0 / theta[2]) * (theta[1] * plot_x + theta[0])
plt.plot(plot_x, plot_y)
plt.scatter(X[pos, 1], X[pos, 2], marker='o', c='b')
plt.scatter(X[neg, 1], X[neg, 2], marker='x', c='r')
plt.xlabel('Exam 1 score')
plt.ylabel('Exam 2 score')
plt.legend(['Decision Boundary', 'Admitted', 'Not Admitted'])
plt.show()

# Compute accuracy on the training set
p = predict(array(theta), X)
counter = 0
for i in range(y.size):
    if p[i] == y[i]:
        counter += 1
print 'Train Accuracy: %f' % (counter / float(y.size) * 100.0)
                all_tickers,
                index=0,
            ).upper()
            period = st.slider(
                "How many periods would you like to forecast into the future?",
                15,
                120,
                15,
                15,
                format="%d days",
            )
            ticker_obj = get_ticker_object(ticker)
            ticker_info = ticker_obj.info

            train_df = load_prediction_data(ticker)
            pr_ob = predict(train_df)
            fig, fig2, fig3 = pr_ob.prediction(int(period))
            fig.update_layout(
                title=f"Prediction for {ticker_info['longName']}",
                yaxis_title="Adj Close Price",
                xaxis_title="Date",
            )
            """
            #### The next visual shows the actual (black dots) and predicted (blue line) values over time.
            """
            st.plotly_chart(fig, use_container_width=True)
            """
            #### The next few visuals show a high level trend of predicted values.
            
            """
            st.write(fig2)
Beispiel #41
0
#!/usr/bin/env python

# considering good review as TRUE and bad review as FALSE

import string
import globalVars
from preprocessing import processingDriver
from probTable import createTable
from predict import predict
# from bayesClassifier import

VOCABULARY = []

# driver code
training = "trainingSet.txt"
testing = "testSet.txt"

trainFeatures = []
testFeatures = []

probTable = []

processingDriver(training, testing, trainFeatures, testFeatures)
createTable(trainFeatures, probTable)
predict(trainFeatures, probTable)
    def get_audio():
        return randomizer.get_noisy_speech(_set='dev')

    set_snr(j[0])
    for i in range(0, EPOCHS):
        hm, bg, mfcc_feature = generate_sample(_n_filt=NFILT,
                                               _winlen=WINLEN,
                                               _winstep=WINSTEP,
                                               _winfunc=np.hamming,
                                               _generator=get_audio,
                                               to_write=True)
        logmmse_from_file(
            os.path.join(os.path.dirname(__file__),
                         '../sample_test/mixed.wav'),
            os.path.join(os.path.dirname(__file__),
                         '../sample_test/logmmse.wav'))
        sequence_feature = np.array(
            to_sequence_with_stride(mfcc_feature,
                                    left_pad=PAD_L,
                                    right_pad=PAD_R))
        # sys model
        prediction = predict(sequence_feature, 'sys_model')
        estimate = get_estimate(bg,
                                prediction,
                                _winlen=WINLEN,
                                _winstep=WINSTEP,
                                _winfunc=np.hamming)
        write_wav('sys_model.wav', estimate.astype(np.float32))
        evaluate(estimated_sources=['sys_model.wav', 'logmmse.wav'])
    os.rename('pesq_results.txt', str(j[1]) + 'db.txt')
Beispiel #43
0
# neural network parameters.

print('Loading Saved Neural Network Parameters ...')

# Load the weights into variables Theta1 and Theta2
data = scipy.io.loadmat('./data/ex3weights.mat')
Theta1 = data['Theta1']
Theta2 = data['Theta2']

# ================= Part 3: Implement Predict =================
#  After training the neural network, we would like to use it to predict
#  the labels. You will now implement the "predict" function to use the
#  neural network to predict the labels of the training set. This lets
#  you compute the training set accuracy.

pred = predict(Theta1, Theta2, X)

print('Training Set Accuracy: %f\n' %
      np.mean(np.double(pred == np.squeeze(y))) * 100)

input("Program paused. Press Enter to continue...")

#  To give you an idea of the network's output, you can also run
#  through the examples one at the a time to see what it is predicting.

#  Randomly permute examples
rp = np.random.permutation(range(m))

plt.figure()
for i in range(m):
    # Display
Beispiel #44
0
#  displaying the hidden units to see what features they are capturing in
#  the data.

print('\nVisualizing Neural Network... \n')

displayData(Theta1[:, 1:])

input('\nProgram paused. Press enter to continue.\n')

# # ================= Part 10: Implement Predict =================
#  After training the neural network, we would like to use it to predict
#  the labels. You will now implement the "predict" function to use the
#  neural network to predict the labels of the training set. This lets
#  you compute the training set accuracy.

pred = (predict(Theta1, Theta2, X) + 1) % 10

print('\nTraining Set Accuracy: %f\n' %
      (np.mean(np.double(pred == y.T)) * 100))

rp = np.random.permutation(m)

for i in range(m):
    # Display
    print('\nDisplaying Example Image\n')
    t = np.array([X[rp[i]]])
    displayData(t)

    pred = predict(Theta1, Theta2, t)
    print('\nNeural Network Prediction: %d (digit %d)\n' % (pred,
                                                            (pred + 1) % 10))
Beispiel #45
0
# neural network parameters.

print('Loading Saved Neural Network Parameters ...')

# Load the weights into variables Theta1 and Theta2
data = scipy.io.loadmat('ex3weights.mat')
Theta1 = data['Theta1']
Theta2 = data['Theta2']

## ================= Part 3: Implement Predict =================
#  After training the neural network, we would like to use it to predict
#  the labels. You will now implement the "predict" function to use the
#  neural network to predict the labels of the training set. This lets
#  you compute the training set accuracy.

pred = predict(Theta1, Theta2, X)

accuracy = sum(1 if a==b else 0 for a,b in zip(pred,y))/len(pred) * 100
print('\nTraining Set Accuracy: %0.01f%%\n' % accuracy)

input('Program paused. Press <Enter> to continue...')

#  To give you an idea of the network's output, you can also run
#  through the examples one at a time to see what it is predicting.

#  Randomly permute examples
rp = np.random.permutation(m)

for i in range(m):

    # Display 
Beispiel #46
0
print("db = " + str(grads["db"]))
print("cost = " + str(cost))

params, grads, costs = optimize(w,
                                b,
                                X,
                                Y,
                                num_iterations=100,
                                learning_rate=0.009,
                                print_cost=False)
print("w = " + str(params["w"]))
print("b = " + str(params["b"]))
print("dw = " + str(grads["dw"]))
print("db = " + str(grads["db"]))

print("predictions = " + str(predict(w, b, X)))

d = model(train_set_x,
          train_set_y,
          test_set_x,
          test_set_y,
          num_iterations=2000,
          learning_rate=0.005,
          print_cost=True)

# Example of a picture that was wrongly classified.
index = 5
plt.imshow(test_set_x[:, index].reshape((num_px, num_px, 3)))
plt.waitforbuttonpress()
#print ("y = " + str(test_set_y[0, index]) + ", you predicted that it is a \"" + classes[d["Y_prediction_test"][0, index]].decode("utf-8") +  "\" picture.")
Beispiel #47
0
def predict_function(data):
    result = pr.predict(data)
    return result
Beispiel #48
0
"""
Created on Thu Aug 30 09:28:26 2018

@author: BJoseph
"""

import pandasdb
import accuweathertest
import predict
import os
from sqlalchemy import Column, Integer, String, Float, DateTime

db_name = 'bjos'
table_name = 'forecast2'

df = predict.predict()
df.columns = ['Rel Hum' if x == 'Rel Hum (%)' else x for x in df.columns]

print(f'the dataframe is: {df}')
print(f'column names are: {df.columns}')
df_dtypes = [
    DateTime(),
    Float(),
    Float(),
    Integer(),
    Integer(),
    Integer(),
    Integer(),
    Integer(),
    Integer(),
    Float()
Beispiel #49
0
# List of bus lines/stops to predict.  Use routefinder.py to look up
# lines/stops for your location, copy & paste results here.  The 4th
# element on each line can then be edited for brevity if desired.
stops = [
    ('actransit', '210', '0702640', 'Ohlone College'),
    ('actransit', '210', '0702630', 'Union Landing'),
    ('actransit', '232', '0704440', 'Fremont BART'),
    ('actransit', '232', '0704430', 'NewPark Mall'),
]

# Populate a list of predict objects from stops[].  Each then handles
# its own periodic NextBus server queries.  Can then read or extrapolate
# arrival times from each object's predictions[] list (see code later).
predictList = []
for s in stops:
    predictList.append(predict(s))

time.sleep(1)  # Allow a moment for initial results

while True:
    currentTime = time.time()
    print
    for pl in predictList:
        print pl.data[1] + ' ' + pl.data[3] + ':'
        if pl.predictions:  # List of arrival times, in seconds
            for p in pl.predictions:
                # Extrapolate from predicted arrival time,
                # current time and time of last query,
                # display in whole minutes.
                t = p - (currentTime - pl.lastQueryTime)
                print '\t' + str(int(t / 60)) + ' minutes'
Beispiel #50
0
 def predict_emotion(self, image):
     image.resize([NETWORK.input_size, NETWORK.input_size], refcheck=False)
     emotion, confidence = predict(image, self.model, self.shape_predictor)
     return emotion, confidence
Beispiel #51
0
#!/usr/bin/env python3

import json

import numpy as np
import pandas as pd
from sklearn.metrics import classification_report

from load_data import load_test_data
from predict import predict

X_df, y_df = load_test_data()
y_pred = predict(X_df)
y_true = (y_df['PINCP'] > 84770).astype(int)

# drop nans
nans = np.isnan(y_pred).ravel()
failures = np.sum(nans)
y_true_clean, y_pred_clean = y_true[~nans], y_pred[~nans]
report = classification_report(y_true_clean,
                               y_pred_clean,
                               target_names=['High Income', 'Low Income'],
                               output_dict=True)
report['failures'] = failures / len(y_true)

with open('./report.json', 'w') as f:
    json.dump(report, f)

print('done')
Beispiel #52
0
    Config.MASK_WEIGHT = 1000

    model = get_model(Config.model_name)
    optimizer = optim.Adam(model.parameters(), lr=0.001)
    # exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=Config.N_EPOCH * len(train_loader) // 3, gamma=0.1)
    lr_scheduler = ReduceLROnPlateau(optimizer,
                                     mode='min',
                                     factor=0.1,
                                     patience=5,
                                     verbose=True)
    model = training(model,
                     optimizer,
                     scheduler=lr_scheduler,
                     n_epoch=Config.N_EPOCH,
                     writer=writer)
    predict(model)

    Config.expriment_id = 11
    writer = SummaryWriter(
        logdir=os.path.join("board/", str(Config.expriment_id)))

    Config.model_name = "basic_unet"
    Config.MODEL_SCALE = 1
    Config.BATCH_SIZE = 8
    Config.MASK_WEIGHT = 1000
    Config.FOCAL_ALPHA = 0.99

    model = get_model(Config.model_name)
    optimizer = optim.Adam(model.parameters(), lr=0.001)
    # exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=Config.N_EPOCH * len(train_loader) // 3, gamma=0.1)
    lr_scheduler = ReduceLROnPlateau(optimizer,
Beispiel #53
0
# In this part of the exercise, we load some pre-initiated
# neural network parameters

print('Loading Saved Neural Network Parameters ...')

data = scio.loadmat('ex3weights.mat')
theta1 = data['Theta1']
theta2 = data['Theta2']

# ===================== Part 3: Implement Predict =====================
# After training the neural network, we would like to use it to predict
# the labels. You will now implement the "predict" function to use the
# neural network to predict the labels of the training set. This lets
# you compute the training set accuracy.

pred = pd.predict(theta1, theta2, X)

print('Training set accuracy: {}'.format(np.mean(pred == y) * 100))

raw_input('Program paused. Press ENTER to continue')

# To give you an idea of the network's output, you can also run
# thru the examples one at a time to see what it is predicting
"""
def getch():
    import termios
    import sys, tty

    def _getch():
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
print('Gradient at initial theta (zeros): \n')
print('', np.round(grad[0], 4), '\n', np.round(grad[1], 4), '\n',
      np.round(grad[2], 4))
print('\nExpected gradients (approx):\n -0.1000\n -12.0092\n -11.2628\n')

input('\nProgram paused. Press enter to continue.\n')

##============= Part 3: Optimizing =============##

result = logisticRegression(X, y, initial_theta)
theta = result[0]
cost = costFunction(result[0], X, y)
print('Cost at theta found by fminunc:', np.round(cost, 3))
print('Expected cost (approx): 0.203\n')
print('theta: \n')
print('', np.round(theta[0], 4), '\n', np.round(theta[1], 4), '\n',
      np.round(theta[2], 4))
print('Expected theta (approx):\n')
print(' -25.161\n 0.206\n 0.201\n')

plotDecisionBoundary(theta, X_raw, y_raw)

input('\nProgram paused, press enter to continue.\n')

##============= Part 4: Predictions and Accuracies =============##

pred = predict(theta, X)
accu = np.mean(pred == y) * 100
print('Training Accuracy:{:0.1f}%'.format(accu))
print('Expected accuracy (approx): 89.0%\n')
Beispiel #55
0
def main():

    img = Image.open('Absentism_pic.png')
    st.image(img, width=700)

    html_temp = """
    <div style="background-color:tomato;padding:10px">
    <h2 style="color:white;text-align:center;">Predicting Absent Hours for Employees </h2>
    </div>
    """
    st.markdown(html_temp, unsafe_allow_html=True)

    #Option to user whether bulk upload data or Single Sample check
    st.subheader('Select a Data Input Options')
    Data_Option = st.selectbox('', [
        '--Select an option--', 'Upload csv file for bulk data',
        'Input single data via UI', 'Re-train Model'
    ])

    if Data_Option == 'Upload csv file for bulk data':
        #Data input via CSV
        st.subheader('Instructions')
        st.set_option('deprecation.showfileUploaderEncoding', False)
        st.write('Please upload a .CSV file')
        st.write(
            'Please maintain the below columns in the exact same sequence in your file .'
        )
        st.write(
            'R_code,Transportation expense,Office Distance,Age,Body mass index'
        )
        st.write('As of now we are not handling empty cells')
        uploaded_file = st.file_uploader("Choose a CSV file", type="csv")

        if uploaded_file is not None:

            try:
                uploaded_data = pd.read_csv(uploaded_file)
                if st.button('Predict Absent Hours'):
                    pred_hours = pt.predict(uploaded_data)
                    #st.write(type(pred_hours))
                    Pred_hours = pd.Series(pred_hours,
                                           name='Predicted_Absent_hours')
                    FinalData = pd.concat([uploaded_data, Pred_hours], axis=1)
                    st.subheader('Predictions')
                    st.write(FinalData)
            except:
                st.write('Please follow the instructions !!')

    elif Data_Option == 'Input single data via UI':

        #Input for Reason for Absence of the Employee
        reason = st.selectbox("Select Reason for Absense ",
                              tuple(gv.reason_label.keys()))
        st.write('You selected the reason as :', reason)
        v_reason = gv.get_value(reason, gv.reason_label)
        st.write('Reason Code : ', v_reason)

        #Input for Office travel expenses

        Travel_Expense = st.number_input(
            'Travel Expenses to Office in Dollars ', 1.0, 1000.0)
        st.write('Travel Expenses to Office as :', Travel_Expense)

        #Input for Employees Distance from Office
        Distance = st.number_input('Distance from Residence to Work in kms',
                                   1.0, 100.0)
        st.write('You Selected Distance as :', Distance)

        #Input Employees Age
        Age = st.number_input('Enter Employee Age in Years', 0.0, 100.0)
        st.write('You entered Employee Age as ', Age, 'years')

        #Input for Employess Body Mass Index
        BMI = st.number_input('Enter a BMI index', 0.0, 100000.0)
        st.write('You selected Body Mass Index as :', BMI)
        pred_hours = ''

        #creating a dataframe to give input to the code
        columns = [
            'R_code', 'Transportation expense', 'Office Distance', 'Age',
            'Body mass index'
        ]
        df_test = pd.DataFrame(
            {
                'R_code': [v_reason],
                'Transportation expense': [Travel_Expense],
                'Office Distance': [Distance],
                'Age': [Age],
                'Body mass index': [BMI]
            },
            columns=columns)

        if st.button('Predict Absent Hours'):

            pred_hours = pt.predict(df_test)
            st.success('Prediction {}'.format(pred_hours[0]))

    elif Data_Option == 'Re-train Model':
        st.subheader('Instructions')
        st.set_option('deprecation.showfileUploaderEncoding', False)
        st.write('Please upload a training file in .CSV format')
        st.write(
            'Please maintain the below columns in the exact same sequence in your file .'
        )
        st.write(
            'R_code,Transportation expense,Office Distance,Age,Body mass index',
            'Absnt_hr_estimate')
        st.write('As of now we are not handling empty cells')
        uploaded_file = st.file_uploader("Upload CSV file", type="csv")

        if uploaded_file is not None:

            uploaded_data = pd.read_csv(uploaded_file)
            if st.button('Re-Train Model'):
                ReTrain_status = ml.re_train(uploaded_data)

                st.success(ReTrain_status)

    else:
        st.write('')
Beispiel #56
0
def extendPreds():
    rng = random.randint(0, len(starts)-1)
    predictions.extend(predict(starts[rng], model, tokenizer))
displayData(Theta1[:, 1:])
plt.draw()
plt.show(block=False)

print('\nProgram paused. Press enter to continue.\n')
pause()

"""## Part 10: Implement Predict =================
  After training the neural network, we would like to use it to predict
  the labels. You will now implement the "predict" function to use the
  neural network to predict the labels of the training set. This lets
  you compute the training set accuracy.
"""

p = predict(Theta1, Theta2, X).reshape(m, 1)


rand_indices = np.random.choice(m, 5)

print(p[rand_indices])
print(y[rand_indices])

print('\nTraining Set Accuracy: ', np.multiply(np.mean((p == y).astype(int)), 100), '\n')
print('Program paused. Press enter to continue.\n')
pause()



"""##  To give you an idea of the network's output, you can also run
  through the examples one at the a time to see what it is predicting.
Beispiel #58
0
    # run predict step
    '''
    xn - predicted state (mu_k+1|k)
    Pn - predicted covariance (sigma_k+1|k)
    A  - linearized state transition matrix
    W  - Process noise covariance
    V  - Measurement noise covariance
    rN - Vector measurement in newtonian frame
    rB - Vector measurement in body frame
    '''
    # get measurement vector
    rB1 = np.reshape(rB1hist[:, ii + 1], (3, 1))
    rB2 = np.reshape(rB2hist[:, ii + 1], (3, 1))
    rB = np.vstack((rB1, rB2))

    xn, A = predict(xk, whist[:, ii + 1], dt)
    Pn = A * Pk * np.transpose(A) + W

    # run measurement step
    y, R, C = measurement(xn[0:4], rN)

    # run innovation step to find z
    z, S = innovation(R, rN, rB, Pn, V, C)

    # Kalman Gain
    L = Pn @ np.transpose(C) @ LA.inv(S)

    # update step
    xk, Pk = update(L, z, xn, Pn, V, C)

    q_mekf[0:4, ii + 1] = np.reshape(xk[0:4], 4)
Beispiel #59
0
poly = PolynomialFeatures(6)
XX = poly.fit_transform(data2[:,0:2])                                                         # create more features from data as terms of X1 and X2 upto the sixth power, called feature mapping, helps us fit the data better
initial_theta = np.zeros(XX.shape[1])
print(costFunctionReg(initial_theta, 1, XX, y))
fig, axes = plt.subplots(1,3, sharey = True, figsize=(17,5))

# Decision boundaries
# Lambda = 0 : No regularization --> too flexible, overfitting the training data
# Lambda = 1 : Looks about right
# Lambda = 100 : Too much regularization --> high bias

for i, C in enumerate([0, 2, 100]):
    # Optimize costFunctionReg
    res2 = minimize(costFunctionReg, initial_theta, args=(C, XX, y), method=None, jac=gradientReg, options={'maxiter':3000})
    
    # Accuracy
    accuracy = 100*sum(predict(res2.x, XX) == y.ravel())/y.size    

    # Scatter plot of X,y
    plotData(data2, 'Microchip Test 1', 'Microchip Test 2', 'y = 1', 'y = 0', axes.flatten()[i])
    
    # Plot decisionboundary
    x1_min, x1_max = X[:,0].min(), X[:,0].max(),
    x2_min, x2_max = X[:,1].min(), X[:,1].max(),
    xx1, xx2 = np.meshgrid(np.linspace(x1_min, x1_max), np.linspace(x2_min, x2_max))
    h = sigmoid(poly.fit_transform(np.c_[xx1.ravel(), xx2.ravel()]).dot(res2.x))
    h = h.reshape(xx1.shape)
    axes.flatten()[i].contour(xx1, xx2, h, [0.5], linewidths=1, colors='g')       
    axes.flatten()[i].set_title('Train accuracy {}% with Lambda = {}'.format(np.round(accuracy, decimals=2), C))

plt.show()
Beispiel #60
0
# Regularization factor
l = 0.5

# Run minimize() to obtain the optimal coefs
#w = op.fmin_bfgs(computeCostreg,w_init,args=(F, y, l),fprime=computeGradreg)

Result = op.minimize(fun = computeCostreg, x0 = w_init, args = (F, y,l),jac = computeGradreg);
w = Result.x;


# Plot the decision boundary
plotBoundary(X, y, degree, w)
print(w)
# Compute accuracy on the training set
p = predict(array(w), F)
counter = 0
for i in range(y.size):
    if p[i] == y[i]:
        counter += 1
print('Train Accuracy: %f' % (counter / float(y.size) * 100.0))

p = predict(array(w), F_test)
counter = 0
for i in range(y_test.size):
    if p[i] == y_test[i]:
        counter += 1
print('Test Accuracy: %f' % (counter / float(y_test.size) * 100.0))