Esempio n. 1
0
def main():
    """Testing file to show neural network can learn linearly separable
    data."""
    data = np.genfromtxt("training.csv", delimiter=',').tolist()

    shuffle(data)

    # NOTE: We have to wrap every target value into a tuple, for the
    # purpose of being able to classify n-tuples later
    targets = list((sample[-1] if sample[-1] == 1 else 0,) for sample in data)
    features = list(sample[:-1] for sample in data)
    print "Starting to train..."
    start = time()

    num_features = len(features[0])  # Subtract one because of target values
    nn = NeuralNet(num_features, max_epochs=2, default_bias="random",
                   learn_rate=.85, scale=0.1, verbose=True)
    nn.train(features, targets)
    print "Done with training. Took {0} seconds to train." \
            .format(round(time() - start, 2))

    print "Beginning with scoring..."
    start = time()
    scored_data = np.genfromtxt("data_features.csv", delimiter=",")
    correct = np.genfromtxt("data_targets.csv", delimiter=",")
    prediction = nn.score_data(scored_data)
    print "Done with scoring. Took {0} seconds to score the dataset" \
            .format(round(time() - start, 2))
    num_incorrect = sum(1 for i in xrange(len(correct)) \
                        if correct[i] != prediction[i])
    print "Total number incorrect: {0}".format(num_incorrect)
Esempio n. 2
0
def main():

    print "Loading in the data..."
    text = open("data/lemon_training.csv").read().split("\n")
    data = list(map(int, sample.strip().split(",")) for sample in text
                if sample.strip() != "")
    print "Shuffling..."
    shuffle(data)

    # NOTE: We have to wrap every target value into a tuple, for the
    # purpose of being able to classify n-tuples later
    targets = tuple((1 if sample[-1] == 1 else 0,) for sample in data)
    features = tuple(sample[:-1] for sample in data)
    print "Starting to train..."
    start = time()

    num_features = len(features[0])  # Subtract one because of target values
    nn = NeuralNet(num_features, max_epochs=20, learn_rate=.7, scale=3, 
                   hidden_layer=[4], verbose=True, activation=("expit", 2))
    nn.train(features, targets)

    print "Done with training. Took {0} seconds to train." \
            .format(round(time() - start, 2))

    print "Beginning with scoring..."
    start = time()
    
    scored_text = open("data/lemon_testing.csv").read().split("\n")
    testing = list(map(int, sample.strip().split(',')) for sample in scored_text
                   if sample.strip() != "")
    predictions = nn.score_data(testing)
    print "Done with scoring. Took {0} seconds to score the dataset" \
            .format(round(time() - start, 2))

    with open("results.txt", "w") as f:
        f.write("IsBadBuy\n")
        for pred in predictions:
            f.write(str(pred[0, 0]) + "\n")