import csv
import numpy as np
import readfile
import sklearn
import sklearn.preprocessing
import sklearn.model_selection
import sklearn.ensemble
import sklearn.metrics

X, y = readfile.read_train('./part1/train_2008.csv')
#X = X[:, 3:]
#clean_X = sklearn.preprocessing.scale(X, axis=0, with_mean=True, with_std=True, copy=True)

bad_feat = [333, 0, 23, 334, 15, 241, 242, 243, 244, 245] # Remove Identifiers, Weights
print("Use", X.shape[1] - len(bad_feat), "features")

# Data preprocessing
clean_X = np.zeros((X.shape[0], X.shape[1]))
for feat in range(X.shape[1]):
    mu = np.mean(X[:, feat])
    std = np.std(X[:, feat])
    if std == 0: std = 1

    if feat in bad_feat:
        clean_X[:, feat] = 0
    else:
        clean_X[:, feat] = (X[:, feat] - mu) / std
print(clean_X)

######################

num_folds = 5
kf = sklearn.model_selection.KFold(n_splits=num_folds)

# Iterate through cross-validation folds:
for n_est in [10, 20, 50, 100, 500, 1000]:
    cv_auc = []
    for train_index, test_index in kf.split(clean_X):
        # Training and testing data points for this fold:
        x_train, x_test = clean_X[train_index], clean_X[test_index]
        y_train, y_test = y[train_index], y[test_index]

        adaboost = sklearn.ensemble.AdaBoostClassifier(n_estimators=n_est)
        adaboost.fit(x_train, y_train)
        pred = adaboost.predict_proba(x_test)[:, 1]

        fpr, tpr, thresholds = sklearn.metrics.roc_curve(y_test, pred, pos_label=1)
        cv_auc.append(sklearn.metrics.auc(fpr, tpr))
        print(cv_auc)
