-
-
Save goyder/7afd63c6e7684c425ce14fcd7de74ec8 to your computer and use it in GitHub Desktop.
2-m-logistic-regression.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fileinput | |
import random | |
import logistic_regression | |
# Config | |
data_folder = "../data" | |
input_filename = "processed.iris.data" | |
# Filepaths | |
input_data_filepath = data_folder + "/" + input_filename | |
# Read in dataset | |
print "Inputting data from '" + input_data_filepath + "'" | |
rows = [] | |
for row in fileinput.input(input_data_filepath): | |
content = row.split(",") | |
if len(content) > 1: | |
content = map(float, content) | |
rows.append(content) | |
print "Data read in." | |
# Split into X and Y, training and test | |
print "Generating test and training data." | |
train_proportion = 0.75 | |
X = map(lambda x: x[:-1], rows) | |
Y = map(lambda x: x[-1], rows) | |
X_train = [] | |
Y_train = [] | |
X_test = [] | |
Y_test = [] | |
for n in range(len(X)): | |
if random.random() < train_proportion: | |
X_train.append(X[n]) | |
Y_train.append(Y[n]) | |
else: | |
X_test.append(X[n]) | |
Y_test.append(Y[n]) | |
# Establish optimisation paramaeters | |
feature_count = 4 | |
lr = 0.001 | |
iter = 20 | |
# Run the model and generate predictions | |
print "Generate model and run training." | |
lr_model = logistic_regression.LogisticRegression(feature_count, lr, iter) | |
lr_model.fit(X_train, Y_train) | |
Y_hat = lr_model.predict(X_test) | |
# Compare and contrast: | |
print "Comparing Y_test to Y_hat." | |
for n in range(len(X_test)): | |
print str(n) + " - " + str(Y_test[n]) + " - " + str(Y_hat[n]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment