Esempio n. 1
0
def accuracy_on_dataset(dataset, params):
    good = bad = 0.0
    for y, x in dataset:
        y_pred = mlp1.predict(x, params)
        good += 1 if y_pred == y else 0
        bad += 1 if y_pred != y else 0
    return good / (good + bad)
Esempio n. 2
0
def accuracy_on_dataset(dataset, params):
    total = good = 0.0
    for label, features in dataset:
        total += 1
        predicted_label = mlp1.predict(feats_to_vec(features), params)
        if predicted_label == utils.L2I[label]:
            good += 1
    return float(good) / total
Esempio n. 3
0
def test_predictions(dataset, params):
    with open('test.pred', 'w') as file:
        for index, (label, features) in enumerate(dataset):
            x = feats_to_vec(features)
            pred = model.predict(x, params)
            if not index == 0:
                file.write('\n')
            file.write(ut.I2L[pred])
Esempio n. 4
0
def accuracy_on_dataset(dataset, params):
    good = bad = 0.0
    for label, features in dataset:
        if m1.predict(features, params) == label:
            good += 1
        else:
            bad += 1

    return good / (good + bad)
Esempio n. 5
0
def get_tag(dataset, params):
    """
    Tgas the dataset based on trained params
    dataset: a list of (label, feature) pairs.
    params: list of parameters (initial values)
    """
    f = open('test.pred.mlp1', 'w')
    for label, features in dataset:
        predicted_label = mlp1.predict(feats_to_vec(features), params)
        f.write("%s\n" % utils.I2L[predicted_label])
    f.close()
Esempio n. 6
0
def accuracy_on_dataset(dataset, params):
    good = bad = 0.0
    for label, features in dataset:
        x = feats_to_vec(features)
        y = ut.L2I[label]
        pred = model.predict(x, params)
        if pred == y:
            good += 1
        else:
            bad += 1
    return good / (good + bad)
Esempio n. 7
0
def accuracy_on_dataset(dataset, params):
    good = bad = 0.0
    for label, features in dataset:
        # Compute the accuracy (a scalar) of the current parameters
        # on the dataset.
        # accuracy is (correct_predictions / all_predictions)
        if ml1.predict(features, params) == label:
            good += 1
        else:
            bad += 1
    return good / (good + bad)
Esempio n. 8
0
def accuracy_on_dataset(dataset, params, F2I):
    good = bad = 0.0
    for label, features in dataset:
        x = feats_to_vec(features, F2I)  # convert features to a vector.
        y = ut.L2I[label]  # convert the label to number
        if m1.predict(x, params) == y:
            good += 1
        else:
            bad += 1

    return good / (good + bad)
Esempio n. 9
0
def test(test_data, params):
    prediction_file = open("test.pred", 'w')
    for label, features in test_data:
        x = feats_to_vec(features)  # convert features to a vector.
        pred = ml.predict(x, params)
        for key, val in ut.L2I.items():
            if val == pred:
                label = key
                break
        prediction_file.write(str(label) + "\n")
    prediction_file.close()
Esempio n. 10
0
def accuracy_on_dataset(dataset, params):
    good = bad = 0.0
    for y, x in dataset:
        pred = ml.predict(x, params)
        if (y == pred):
            good += 1
        else:
            bad += 1
            # Compute the accuracy (a scalar) of the current parameters
            # on the dataset.
            # accuracy is (correct_predictions / all_predictions)
    return np.divide(good, (good + bad))
Esempio n. 11
0
def accuracy_on_dataset(dataset, params):
    good = bad = 0.0
    for label, features in dataset:
        # YOUR CODE HERE
        # Compute the accuracy (a scalar) of the current parameters
        # on the dataset.
        # accuracy is (correct_predictions / all_predictions)
        if utils.L2I[label] == mlp1.predict(feats_to_vec(features), params):
            good = good + 1
        else:
            bad = bad + 1
    return good / (good + bad)
Esempio n. 12
0
def run_test(test_data, params):
    pred_file = open("test.pred", 'w')
    for label, features in test_data:
        x = feats_to_vec(features)  # convert features to a vector.
        y_hat = ml.predict(x, params)
        for key, val in ut.L2I.items(
        ):  # for name, age in dictionary.iteritems():  (for Python 2.x)
            if val == y_hat:
                label = key
                break
        pred_file.write(str(label) + "\n")
    pred_file.close()
Esempio n. 13
0
def pred(pred_data, params):
    """ Test classifier
    """

    I2L = {utils.L2I[l]: l for l in utils.L2I}

    with open("test.pred", "w+") as file:
        for features in pred_data:
            x = feats_to_vec(features)  # convert features to a vector.
            y_hat = ll.predict(x, params)
            file.write(I2L[y_hat])
            file.write("\n")
Esempio n. 14
0
def accuracy_on_dataset(dataset, params):
    good = bad = 0.0
    for label, features in dataset:
        # YOUR CODE HERE
        # Compute the accuracy (a scalar) of the current parameters
        # on the dataset.
        # accuracy is (correct_predictions / all_predictions)
        x = feats_to_vec(features)
        y_pred = mlp1.predict(x, params)
        good += 1 if y_pred==L2I[label] else 0
        bad += 1 if y_pred!=L2I[label] else 0
    return good / (good + bad)
Esempio n. 15
0
def accuracy_on_dataset(dataset, params):
    # in case of no data set, like xor
    if not dataset:
        return 0

    good = bad = 0.0
    for label, features in dataset:
        y_prediction = mlp1.predict(features, params)
        if y_prediction == label:
            good += 1
        else:
            bad += 1
    return good / (good + bad)
Esempio n. 16
0
def accuracy_on_dataset(dataset, params):
    good = bad = 0.0
    for label, features in dataset:
        # Compute the accuracy (a scalar) of the current parameters
        # on the dataset.
        # accuracy is (correct_predictions / all_predictions)
        pred = mlp1.predict(feats_to_vec(features), params)
        if pred == ut.L2I[label]:
            good += 1
        else:
            bad += 1
        pass
    return good / (good + bad)
Esempio n. 17
0
def accuracy_on_dataset(dataset, params):
    good = total = 0.0
    for label, features in dataset:
        x = feats_to_vec(features)  # convert features to a vector.
        y = L2I.get(label)  # convert the label to number if needed.
        if mlp1.predict(
                x,
                params) == y:  # compare the prediction and the correct label
            good += 1
        total += 1
        # Compute the accuracy (a scalar) of the current parameters
        # on the dataset.
        # accuracy is (correct_predictions / all_predictions)
    return good / total
Esempio n. 18
0
def accuracy_on_dataset(dataset, params):
    good = bad = 0.0
    for label, features in dataset:
        x = feats_to_vec(features)  # convert features to a vector.
        y = ut.L2I[label]  # convert the label to number if needed.
        pred = ml.predict(x, params)
        if (y == pred):
            good += 1
        else:
            bad += 1
            # Compute the accuracy (a scalar) of the current parameters
            # on the dataset.
            # accuracy is (correct_predictions / all_predictions)
    return good / (good + bad)
def accuracy_on_dataset(dataset, params):
    good = bad = 0.0
    for label, features in dataset:

        x = feats_to_vec(features)
        y = utils.L2I[label]

        y_hat = mlp1.predict(x, params)
        if y == y_hat:
            good += 1
        else:
            bad += 1
        pass
    return good / (good + bad)
Esempio n. 20
0
def CreatePredictionsFile(data, parameters):
    file_predictions = open("test.pred", 'w')
    # list of languages
    languages_list = utils.L2I.items()
    for tag, features in data:
        x = feats_to_vec(features)  # convert features to a vector.
        predicted_language = mlp1.predict(x, parameters)
        for language, text in languages_list:  # for name, age in dictionary.iteritems():  (for Python 2.x)
            if predicted_language == text:
                tag = language
                break
        file_predictions.write(str(tag) + "\n")
        # close the file
    file_predictions.close()
Esempio n. 21
0
def accuracy_on_dataset(dataset, params):
    good = bad = 0.0
    for y, x in dataset:
        # YOUR CODE HERE
        # Compute the accuracy (a scalar) of the current parameters
        # on the dataset.
        # accuracy is (correct_predictions / all_predictions)
        y_hat = ml.predict(x, params)
        if (y == y_hat):
            good += 1
        else:
            bad += 1

    return good / (good + bad)
Esempio n. 22
0
def accuracy_on_dataset(dataset, params):
    good = bad = 0.0
    for label, features in dataset:
        # YOUR CODE HERE
        # Compute the accuracy (a scalar) of the current parameters
        # on the dataset.
        # accuracy is (correct_predictions / all_predictions)
        feat_vec = feats_to_vec(features)
        y_hat = mlp.predict(feat_vec, params)

        if label == y_hat:
            good += 1
        else:
            bad += 1
    return good / (good + bad)
Esempio n. 23
0
def accuracy_on_dataset(dataset, params):
    good = bad = 0.0
    for label, features in dataset:
        # YOUR CODE HERE
        # Compute the accuracy (a scalar) of the current parameters
        # on the dataset.
        # accuracy is (correct_predictions / all_predictions
        x = feats_to_vec_uni(features)
        y = L2I[label]
        y_hat = ll.predict(x, params)
        if y - y_hat == 0:
            good += 1
        else:
            bad += 1
        pass
    return good / (good + bad)
Esempio n. 24
0
def accuracy_on_dataset(dataset, params):
	good = bad = 0.0
	local_l2i = ut.L2I
	for label, features in dataset:
		feat_vec = feats_to_vec(features)
		y_hat = mpl1.predict(feat_vec, params)

		if local_l2i[label] == y_hat:
			good += 1
		else:
			bad += 1

	# Compute the accuracy (a scalar) of the current parameters
	# on the dataset.
	# accuracy is (correct_predictions / all_predictions)

	return good / (good + bad)
Esempio n. 25
0
def create_test_file(data_set, params):
    """
    create file with results of languages
    :param data_set: bigrams of 2 letters
    :param params
    :return: file with result
    """
    test_file = open("test.pred", 'w')
    for l, features in data_set:
        x = feats_to_vec(features)
        index = mlp1.predict(x, params)
        for key, value in ut.L2I.items():
            if value == index:
                l = key
                break
        test_file.write(l + "\n")
    test_file.close()
Esempio n. 26
0
def accuracy_on_dataset(dataset, params):
    """
    calculates the accuracy of the prediction on a given data set
    :param dataset: bigrams of 2 letters and languages
    :param params, dataset
    :return: accuracy of the prediction by precentage
    """
    good = bad = 0.0
    for label, features in dataset:
        x = feats_to_vec(features)
        y = ut.L2I[label]
        # prediction returned the correct label
        if mlp1.predict(x, params) == y:
            good += 1
        else:
            bad += 1
    return good / (good + bad)
Esempio n. 27
0
def accuracy_on_dataset(dataset, params, Xor=False):
    good = bad = 0.0
    for label, features in dataset:

        if Xor == False:
            x = feats_to_vec(features)  # convert features to a vector
        else:
            x = np.array(features)  # convert features to a vector

        pred = ml.predict(x, params)
        if pred == label:
            good += 1
        else:
            bad += 1
        pass

    return good / (good + bad)
def create_test_pred_file(test_data, params):
    """
    creates a 'test.pred' file
    :param test_data: test data to be predicted
    :param params: trained params
    :return:
    """
    f = open("test.pred", 'w')
    for label, features in test_data:
        x = feats_to_vec(features)
        y_hat = mlp1.predict(x, params)
        for l, i in utils.L2I.items():
            if y_hat == i:
                label = l
                break
        f.write(label + "\n")
    f.close()
def accuracy_on_dataset(dataset, params):
    good = bad = 0.0

    for label, features in dataset:

        # YOUR CODE HERE
        x = feats_to_vec(features)  # convert features to a vector.
        y = label  # convert the label to number if needed.

        # Compute the accuracy (a scalar) of the current parameters
        # on the dataset.
        # accuracy is (correct_predictions / all_predictions)
        if mlp.predict(x, params) == y:
            good += 1
        else:
            bad += 1

    return good / (good + bad)
Esempio n. 30
0
def accuracy_on_dataset(dataset, params):
    good = bad = 0.0
    for label, features in dataset:
        # YOUR CODE HERE
        # Compute the accuracy (a scalar) of the current parameters
        # on the dataset.
        # accuracy is (correct_predictions / all_predictions)
        # one_hot = features  XOR PROBLEM

        label = utils.L2I[label]
        one_hot = feats_to_vec(features)

        predicted_label = mlp.predict(one_hot, params)
        if (predicted_label != label):
            bad += 1
        else:
            good += 1

    return good / (good + bad)