from math import exp
from random import seed
from random import randrange
from csv import reader
import pandas

#dataframe = pandas.read_csv("undefined_selection0.csv", header=None)
dataframe = pandas.read_csv("sanitized_infected&normal_selection.csv", header=None)

# Make a prediction with coefficients
def predict(row, coefficients):
	yhat = coefficients[0]
	for i in range(len(row)-1):
		yhat += coefficients[i + 1] * row[i]
	return 1.0 / (1.0 + exp(-yhat))

# Estimate logistic regression coefficients using stochastic gradient descent
def coefficients_sgd(train, l_rate, n_epoch):
	coef = [0.0 for i in range(len(train[0]))]
	for epoch in range(n_epoch):
		sum_error = 0
		for row in train:
			yhat = predict(row, coef)
			error = row[-1] - yhat
			sum_error += error**2
			coef[0] = coef[0] + l_rate * error * yhat * (1.0 - yhat)
			for i in range(len(row)-1):
				coef[i + 1] = coef[i + 1] + l_rate * error * yhat * (1.0 - yhat) * row[i]
		print('>epoch=%d, lrate=%.3f, error=%.3f' % (epoch, l_rate, sum_error))
	return coef

# Calculate coefficients

dataset = dataframe.values
l_rate = 0.3
n_epoch = 100
#coef = coefficients_sgd(dataset, l_rate, n_epoch)
coef = -1.880874748034424, 1.6755533492284267, 2.693638954456851
print(coef)

# test predictions

dataset = dataframe.values
for row in dataset:
	yhat = predict(row, coef)
	print("Expected=%.3f, Predicted=%.3f [%d]" % (row[-1], yhat, round(yhat)))
	#print(yhat, round(yhat))
	#print(round(yhat))
	#if yhat >= 0.3:
	#	print(yhat)
