import pandas as pd
from sklearn.metrics import balanced_accuracy_score as bas
from sklearn.linear_model import LogisticRegression as LR
from sklearn.model_selection import cross_val_predict as cvp
from sklearn.model_selection import cross_validate as cv
from sklearn.model_selection import GridSearchCV as GScv
from sklearn.metrics import confusion_matrix as cm
from joblib import dump

#load data
print("Loading data...")
X = pd.read_csv("ChiArr.csv", header=None, dtype=int).to_numpy(dtype=int)
Y = pd.read_csv("ChiLabels.csv", header=None, dtype=int).to_numpy(dtype=int)
print("Data loaded!")

#initialize logistic regressor
clf = LR(class_weight="balanced", C=10, tol=0.0001, multi_class="ovr")

#preform grid search
print("\nPreforming Grid Search...")
parameters = {'tol':[0.0001, 0.00001, 0.000001], 'C':[0.1, 1, 10, 100]}
gs = GScv(clf, parameters, cv=5, n_jobs=-1, verbose=1, scoring="balanced_accuracy")
gs.fit(X, Y)
print("\nBest params: ",gs.best_params_)

print("\nEvaluating best estimator...")
clf = gs.best_estimator_

#preform cross validation on best estimator and print confusion matrix
scores = cv(clf, X, Y, cv=5, verbose=1, n_jobs=-1, return_estimator=True, scoring='balanced_accuracy')
y_pred = cvp(clf, X, Y, cv=5, verbose=1, n_jobs=-1)
conmat = cm(Y, y_pred) 
print("\n5-fold CV balanced_accuracy scores...")
print(scores["test_score"])
print("Confusion matrix:")
print(conmat)

#find best estimator from 5-fold CV
max = 0
j = 0
for i in range(len(scores["test_score"])):
	if scores["test_score"][i] > max:
		max = scores["test_score"][i]
		j = i

#save coefficients from best estimator
clf=scores["estimator"][j]
coef = clf.coef_
fp = open("LogRegCoef2.csv", "w")
for cls in coef:
	for feat in range(len(cls)-1):
		fp.write(str(cls[feat])+",")
	fp.write(str(cls[feat+1])+"\n")

#save biases
inter = clf.intercept_
fp = open("LogRegInt2.csv", "w")
for i in inter:
    fp.write(str(i)+",")

#save entire model
dump(clf, "LogReg.joblib")
