"""
# Before Building Model
"""
"""
### imports & variables & load & preprocess
"""
#################################################
# imports 
import numpy as np
import pandas as pd 
import os,random
import warnings
warnings.filterwarnings("ignore")

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import uniform as sp_randFloat
from scipy.stats import randint as sp_randInt

from sklearn import svm

#################################################
# variables
TRAIN_PATH = "../input/titanic/train.csv"
TEST_PATH = "../input/titanic/test.csv"
SAMPLE_SUBMISSION_PATH = "../input/titanic/gender_submission.csv"
SUBMISSION_PATH = "submission.csv"

ID = "PassengerId"
TARGET = "Survived"

SEED = 2022
def seed_everything(seed=SEED):
    random.seed(seed)
    os.environ['PYTHONHASHSEED'] = str(seed)
    np.random.seed(seed)
    
seed_everything()

RS_CV = 3
RS_N_ITER = 50
RS_N_JOBS = -1
RS_SCORING = 'roc_auc'
#################################################
# load 
train = pd.read_csv(TRAIN_PATH)
test = pd.read_csv(TEST_PATH)
#################################################
#preprocess 
#delete unnessary column
drop_elements = ['PassengerId', 'Name', 'Ticket', 'Cabin']
train = train.drop(drop_elements, axis = 1)
test = test.drop(drop_elements, axis = 1)

#2.find null data and fill new data 
def checkNull_fillData(df):
    for col in df.columns:
        if len(df.loc[df[col].isnull() == True]) != 0:
            if df[col].dtype == "float64" or df[col].dtype == "int64":
                df.loc[df[col].isnull() == True,col] = df[col].mean()
            else:
                df.loc[df[col].isnull() == True,col] = df[col].mode()[0]
                
checkNull_fillData(train)
checkNull_fillData(test)

#3.one hot encoding 
str_list = [] 
num_list = []
for colname, colvalue in train.iteritems():
    if type(colvalue[1]) == str:
        str_list.append(colname)
    else:
        num_list.append(colname)
        
train = pd.get_dummies(train, columns=str_list)
test = pd.get_dummies(test, columns=str_list)
#################################################
"""
# Build Model
"""
"""
### split data & search best model
"""
#################################################
#split input data and target data 
X = train.drop([TARGET],axis=1)
y = train[TARGET]
#################################################
# search best parameter and model
model = svm.SVC()

from sklearn.utils.fixes import loguniform
parameters = {
    'C': loguniform(1e0, 1e5),
#  'gamma': loguniform(1e-4, 1e-3),
#  'kernel': ['rbf'],
 'class_weight':['balanced', None]
}
rs = RandomizedSearchCV(estimator=model, 
                           param_distributions = parameters,
                           scoring=RS_SCORING,
                           cv = RS_CV, 
                           n_iter = RS_N_ITER, 
                           n_jobs=RS_N_JOBS,
                           verbose=10)
rs.fit(X, y)

# best model 
print(rs.best_params_)
bestModel = rs.best_estimator_
#################################################
"""
# After Building Model
"""
"""
### predict & submit
"""
#################################################
#predict
X_test = test
pred_test = bestModel.predict(X_test)
#################################################
#submit
sub = pd.read_csv(SAMPLE_SUBMISSION_PATH)
sub[TARGET] = pred_test
sub.to_csv(SUBMISSION_PATH, index=False)
sub.head()
#################################################