import sklearn.datasets
import scipy.sparse.csr
import sklearn.svm
import pickle
from sklearn.model_selection import RandomizedSearchCV
from xgboost import XGBRegressor
from pprint import pprint
import sklearn.metrics
from sklearn.metrics import mean_squared_error

prime = 262139
n_features = 3*prime+1
X, y = sklearn.datasets.load_svmlight_file("xaa", n_features, zero_based=1)

# {'base_score': 0.5,
#  'booster': 'gbtree',
#  'colsample_bylevel': 1,
#  'colsample_bytree': 1,
#  'gamma': 0,
#  'learning_rate': 0.08,
#  'max_delta_step': 0,
#  'max_depth': 8,
#  'min_child_weight': 1,
#  'missing': None,
#  'n_estimators': 100,
#  'n_jobs': 1,
#  'nthread': 1,
#  'objective': 'reg:linear',
#  'random_state': 0,
#  'reg_alpha': 0,
#  'reg_lambda': 1,
#  'scale_pos_weight': 1,
#  'seed': None,
#  'silent': True,
#  'subsample': 1}

learning_rate = [int(x)/100 for x in np.linspace(start = 1, stop = 20, num = 11)]
colsample_bytree = [int(x)/10 for x in np.linspace(start = 1, stop = 10, num = 11)]
colsample_bylevel = [int(x)/10 for x in np.linspace(start = 1, stop = 10, num = 11)]
min_child_weight = [0.8,0.9,1,1.1,1.2]
gamma = [int(x)/10 for x in np.linspace(start = 1, stop = 10, num = 5)]
max_depth = [int(x) for x in np.linspace(start = 12, stop = 40, num = 8)]
reg_alpha = [0, 0.1, 0.5, 1]
reg_lambda = [0.01, 0.05, 0.1, 0.5, 1]
subsample = [0.6, 0.7,0.8,0.9,1]
# X, y = load_svmlight_file('data.lean_full.xgb1_ll')

random_grid = {
    'learning_rate': learning_rate, 
    'colsample_bytree': colsample_bytree,
    'colsample_bylevel': colsample_bylevel,
    'min_child_weight': min_child_weight, 
    'gamma': gamma, 
    'max_depth': max_depth,
    'reg_alpha': reg_alpha,
    'reg_lambda': reg_lambda,
    'subsample': subsample}

clf = XGBRegressor(objective='reg:linear', nthread=1 , n_estimators=100, silent=True)

rf_random = RandomizedSearchCV(estimator = clf, param_distributions = random_grid, n_iter = 100, cv = 3, verbose=2, random_state=42, n_jobs = -1)

rf_random.fit(X, y)
rf_random.best_params_

# {'subsample': 0.7, 'reg_lambda': 0.05, 'reg_alpha': 0.1, 'min_child_weight': 0.8, 'max_depth': 16, 'learning_rate': 0.2, 'gamma': 0.3, 'colsample_bytree': 0.5, 'colsample_bylevel': 0.8}

#{'subsample': 0.6, 'reg_lambda': 1, 'reg_alpha': 0.5, 'min_child_weight': 0.8, 'max_depth': 40, 'learning_rate': 0.14, 'gamma': 0.1, 'colsample_bytree': 0.8, 'colsample_bylevel': 0.7}
best_random = rf_random.best_estimator_
preds = best_random.predict(X)

# preds = clf.predict(X)
print('\nMean Square error" ', mean_squared_error(y,preds))

# clf = XGBRegressor(base_score=0.5, booster='gbtree', colsample_bylevel=0.8,
#        colsample_bytree=0.5, gamma=0.3, learning_rate=0.2,
#        max_delta_step=0, max_depth=16, min_child_weight=0.8, missing=None,
#        n_estimators=400, n_jobs=1, nthread=1, objective='reg:linear',
#        random_state=0, reg_alpha=0.1, reg_lambda=0.05, scale_pos_weight=1,
#        seed=None, silent=True, subsample=0.7)

# clf = XGBRegressor(base_score=0.5, booster='gbtree', colsample_bylevel=0.7,
#        colsample_bytree=0.8, gamma=0.1, learning_rate=0.14,
#        max_delta_step=0, max_depth=40, min_child_weight=0.8, missing=None,
#        n_estimators=400, n_jobs=1, nthread=1, objective='reg:linear',
#        random_state=0, reg_alpha=0.5, reg_lambda=1, scale_pos_weight=1,
#        seed=None, silent=True, subsample=0.6)


# clf = sklearn.svm.LinearSVC()
# clf = XGBRegressor(max_depth=8, learning_rate=0.08, objective='reg:linear', nthread=1 , n_estimators=100, silent=True)
output = open('classifier.xgb.rcv1', 'wb')
pickle.dump(clf, output)
output.close()

# input = open('classifier.xgb.rcv1', 'rb')
# clf=pickle.load(input)
# input.close()
