# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 2
#     language: python
#     name: python2
# ---

# ## Importing necessary packages

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
from sklearn.tree import DecisionTreeRegressor
seed = 42

# +
# read datasets
train = pd.read_csv('../input/train.csv')
test = pd.read_csv('../input/test.csv')

# process columns, apply LabelEncoder to categorical features
for c in train.columns:
    if train[c].dtype == 'object':
        lbl = LabelEncoder() 
        lbl.fit(list(train[c].values) + list(test[c].values)) 
        train[c] = lbl.transform(list(train[c].values))
        test[c] = lbl.transform(list(test[c].values))

# shape        
print('Shape train: {}\nShape test: {}'.format(train.shape, test.shape))

# +
from sklearn.decomposition import PCA, FastICA
n_comp = 100

# PCA
pca = PCA(n_components=n_comp, random_state=42)
pca2_results_train = pca.fit_transform(train.drop(["y"], axis=1))
pca2_results_test = pca.transform(test)

# ICA
ica = FastICA(n_components=n_comp, random_state=42)
ica2_results_train = ica.fit_transform(train.drop(["y"], axis=1))
ica2_results_test = ica.transform(test)

# Append decomposition components to datasets
#for i in range(1, n_comp+1):
#    train['pca_' + str(i)] = pca2_results_train[:,i-1]
#    test['pca_' + str(i)] = pca2_results_test[:, i-1]
    
#    train['ica_' + str(i)] = ica2_results_train[:,i-1]
#    test['ica_' + str(i)] = ica2_results_test[:, i-1]
    
#y_train = train["y"]
#y_mean = np.mean(y_train)
train_reduced = pd.concat([pd.DataFrame(pca2_results_train), pd.DataFrame(ica2_results_train)], axis = 1)
test_reduced = pd.concat([pd.DataFrame(pca2_results_test), pd.DataFrame(ica2_results_test)], axis = 1)
# -

#X, y = train.drop('y', axis=1).values, train.y.values
#print(X.shape)
X, y = train_reduced, train.y.values
print X.shape

# +
model = DecisionTreeRegressor(random_state=seed)

DTR_params = {
    'max_depth': [4,8],
    'min_samples_split': [2,4],
    'min_samples_leaf': [1,2,4]  
}

reg = GridSearchCV(model, DTR_params, cv = 5, verbose=1, n_jobs = -1)
reg.fit(X, y)
# -

print(reg.best_score_)
print(reg.best_params_)

means = reg.cv_results_['mean_test_score']
stds = reg.cv_results_['std_test_score']
params = reg.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
    print("%f (%f) with: %r" % (mean, stdev, param))

# make predictions and save results
y_pred = xgb_model.predict(x_test)
output = pd.DataFrame({'id': test['ID'].astype(np.int32), 'y': y_pred})
#output.to_csv('xgb_6.csv', index=False)

# ## Trying base data with lasso

# +
from sklearn.linear_model import Lasso
lasso_reg = Lasso(max_iter=6000)

lasso_params = {
    'max_iter': [5000, 6000, 7000],
    'alpha': [1.55, 1.57, 1.6],
    'fit_intercept': [True,False],
    'normalize': [True, False],
    'precompute': [True, False],
    'tol': [0.004, 0.0045, 0.005],
    'selection': ['random', 'cyclic']
}

lasso_reg_cv = GridSearchCV(lasso_reg, lasso_params, cv = 5, verbose=1, n_jobs = -1)
lasso_reg_cv.fit(X, y)

# +
print(lasso_reg_cv.best_score_)
print(lasso_reg_cv.best_params_)

means = lasso_reg_cv.cv_results_['mean_test_score']
stds = lasso_reg_cv.cv_results_['std_test_score']
params = lasso_reg_cv.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
    print("%f (%f) with: %r" % (mean, stdev, param))
# -



StarcoderdataJupyterScriptsDedupFiltered