import numpy as np
from sklearn.datasets import load_svmlight_file
from sklearn.linear_model import LogisticRegression
from sklearn.svm import LinearSVC

from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import *
from sklearn import metrics
from sklearn.ensemble import BaggingClassifier
import pandas as pd



import utils

#input: X_train, Y_train and X_test
#output: Y_pred
def logistic_regression_pred(X_train, Y_train, X_test):
	#train a logistic regression classifier using X_train and Y_train. Use this to predict labels of X_test
	#use default params for the classifier	
	clf=LogisticRegression(random_state =545510477)
	clf.fit(X_train, Y_train)
	pred=clf.predict(X_test)
	
	return pred

#input: X_train, Y_train and X_test
#output: Y_pred
"""

def bagging(X_train, Y_train, X_test):
	clf=BaggingClassifier(LogisticRegression(random_state =545510477),n_estimators=10)
	pred_list=np.zeros((X_test.shape[0], 10))
	for i in range(10):
		clf.fit(X_train, Y_train)
		pred_list[:,i]=clf.predict(X_test)
	pred=pd.DataFrame(pred_list)
	pred['result']=pred.mean(axis=1)
	for i in range(pred.shape[0]):
		if pred['result'][i]<=0.5:
			pred.set_value(i,'result',0)
		else:
			pred.set_value(i,'result',1)
	pred=pred['result'].values
	print pred

	return pred"""

def svm_pred(X_train, Y_train, X_test):
	#train a SVM classifier using X_train and Y_train. Use this to predict labels of X_test
	#use default params for the classifir
	print X_train.shape
	clf=LinearSVC(random_state =545510477)
	
	clf.fit(X_train, Y_train)
	pred=clf.predict(X_test)

	
	return pred

#input: X_train, Y_train and X_test
#output: Y_pred
def decisionTree_pred(X_train, Y_train, X_test):
	#train a logistic regression classifier using X_train and Y_train. Use this to predict labels of X_test
	#IMPORTANT: use max_depth as 5. Else your test cases might fail.

	clf=DecisionTreeClassifier(max_depth=5,random_state=545510477)
	clf.fit(X_train, Y_train)
	pred=clf.predict(X_test)
	

	return pred


#input: Y_pred,Y_true
#output: accuracy, auc, precision, recall, f1-score
def classification_metrics(Y_pred, Y_true):
	#Calculate the above mentioned metrics
	#NOTE: It is important to provide the output in the same order
	acc=accuracy_score(Y_true, Y_pred)
	
	auc = roc_auc_score(Y_true, Y_pred)
	
	precision=precision_score(Y_true, Y_pred)
	recall=recall_score(Y_true, Y_pred)
	f1=f1_score(Y_true, Y_pred)
	return acc, auc, precision, recall, f1

#input: Name of classifier, predicted labels, actual labels
def display_metrics(classifierName,Y_pred,Y_true):
	print "______________________________________________"
	print "Classifier: "+classifierName
	acc, auc_, precision, recall, f1score = classification_metrics(Y_pred,Y_true)
	print "Accuracy: "+str(acc)
	print "AUC: "+str(auc_)
	print "Precision: "+str(precision)
	print "Recall: "+str(recall)
	print "F1-score: "+str(f1score)
	print "______________________________________________"
	print ""

def main():
	X_train, Y_train = utils.get_data_from_svmlight("../deliverables/features_svmlight.train")
	X_test, Y_test = utils.get_data_from_svmlight("../data/features_svmlight.validate")
	#display_metrics("bagging",bagging(X_train,Y_train,X_test),Y_test)
	display_metrics("Logistic Regression",logistic_regression_pred(X_train,Y_train,X_test),Y_test)
	display_metrics("SVM",svm_pred(X_train,Y_train,X_test),Y_test)
	display_metrics("Decision Tree",decisionTree_pred(X_train,Y_train,X_test),Y_test)

if __name__ == "__main__":
	main()
	
