Kaggle competition: 2019S UTS Data Analytics Assignment 3
for column in cat_cols:
features.drop([column],axis=1,inplace=True )<concatenate>
import torch
import torch.nn as nn
import pandas as pd
import numpy as np
import xgboost as xgb
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.model_selection import TimeSeriesSplit,KFold, StratifiedKFold
from time import time
import pickle
import datetime as dt
from imblearn.over_sampling import RandomOverSampler
from scipy.sparse import csc_matrix
features=pd.concat([ features,dumies],axis=1)
 features.head()<normalization>
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader, random_split
new_train = features.iloc[:1458, :]
new_test = features.iloc[1458:,:]
X_test = new_test
X_train = new_train
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X_train)
<prepare_output>
def init_read_big(train_path, test_path, train_big_path):
train = pd.read_csv(train_path)
 test = pd.read_csv(test_path)
 train_big = pd.read_csv(train_big_path)
 return train, test, train_big
def init_read_small(train_path, test_path):
train = pd.read_csv(train_path)
 test = pd.read_csv(test_path)
 return train, test
def remove_column_big(train, train_big, column_title_big):
train_small = train.copy()
column_title_small = [A for A in train.columns]
train_big = train_big[column_title_big]
train = train_big.copy()
train.columns = column_title_small
return train
def data_preprocessing(train, test):
train = train.drop(['Quote_ID'], axis=1)
 y = train.QuoteConversion_Flag.values
train = train.drop(['QuoteConversion_Flag'], axis=1)
 test = test.drop('Quote_ID', axis=1)
 train['Date'] = pd.to_datetime(pd.Series(train['Original_Quote_Date']))
train = train.drop('Original_Quote_Date', axis=1)
 test['Date'] = pd.to_datetime(pd.Series(test['Original_Quote_Date']))
test = test.drop('Original_Quote_Date', axis=1)
 train['Year'] = train['Date'].apply(lambda x: int(str(x)[:4]))
train['Month'] = train['Date'].apply(lambda x: int(str(x)[5:7]))
train['weekday'] = train['Date'].dt.dayofweek
test['Year'] = test['Date'].apply(lambda x: int(str(x)[:4]))
test['Month'] = test['Date'].apply(lambda x: int(str(x)[5:7]))
test['weekday'] = test['Date'].dt.dayofweek
train = train.drop(['Date'], axis=1)
 test = test.drop(['Date'], axis=1)

for A in train.columns:
if train[A].dtype=='object':
lbl = preprocessing.LabelEncoder()
lbl.fit(list(train[A].values)+ list(test[A].values))
train[A] = lbl.transform(list(train[A].values))
test[A] = lbl.transform(list(test[A].values))
train = train.fillna(-999)
 test = test.fillna(-999)
 print("
Pre-processing_big complete!!")
 return train, test, y
X_standardize = pd.DataFrame(X_scaled, columns = X_train.columns)
 X_standardize.head()<save_to_csv>
train_path = '/kaggle/input/2019s-uts-data-analytics-assignment-3/Assignment3_TrainingSet.csv'
test_path = '/kaggle/input/2019s-uts-data-analytics-assignment-3/Assignment3_TestSet.csv'
train, test = init_read_small(train_path, test_path)
 train, test, y = data_preprocessing(train, test )
ridge = Ridge()
ridge.fit(X_train, y_log)
 y_sale = ridge.predict(X_test)
 y_predict_ridge = np.expm1(y_sale)
 submission = pd.DataFrame(y_predict_ridge)
 sample = pd.read_csv('.. /input/sample_submission.csv')
 sample['SalePrice']=submission[0]
sample.to_csv('Ridge.csv',index = False )<predict_on_test>
sc = StandardScaler()
train = sc.fit_transform(train)
 test = sc.fit_transform(test )
y_train = ridge.predict(X_train )<import_modules>
from sklearn.linear_model import Lasso,ElasticNet
from sklearn import metrics<save_to_csv>
class Net(nn.Module):
def __init__(self, D_in, H=15, D_out=1):
super().__init__()
self.fc1 = nn.Linear(D_in, H)
 self.fc2 = nn.Linear(H, D_out)
 self.relu = nn.ReLU()
def forward(self, x):
x = self.fc1(x)
 x = self.relu(x)
 x = self.fc2(x)
 return x.squeeze()
lasso = Lasso(alpha=0.01)
 lasso.fit(X_train, y_log)
 y_lasso_pred = lasso.predict(X_test)
 lasso_predict = np.expm1(y_lasso_pred)
 submission = pd.DataFrame(lasso_predict)
 sample = pd.read_csv('.. /input/sample_submission.csv')
 sample['SalePrice']=submission[0]
sample.to_csv('Lasso.csv',index = False )<predict_on_test>
class EarlyStopping(object):
def __init__(self, mode='min', min_delta=0, patience=10, percentage=False):
self.mode = mode
self.min_delta = min_delta
self.patience = patience
self.best = None
self.num_bad_epochs = 0
self.is_better = None
self._init_is_better(mode, min_delta, percentage)
 if patience == 0:
self.is_better = lambda a, b: True
self.step = lambda a: False
def step(self, metrics):
if self.best is None:
self.best = metrics
return False
if np.isnan(metrics):
return True
if self.is_better(metrics, self.best):
self.num_bad_epochs = 0
self.best = metrics
else:
self.num_bad_epochs += 1
if self.num_bad_epochs >= self.patience:
return True
return False
def _init_is_better(self, mode, min_delta, percentage):
if mode not in {'min', 'max'}:
raise ValueError('mode ' + mode + ' is unknown!')
 if not percentage:
if mode == 'min':
self.is_better = lambda a, best: a < best - min_delta
if mode == 'max':
self.is_better = lambda a, best: a > best + min_delta
else:
if mode == 'min':
self.is_better = lambda a, best: a < best -(
 best * min_delta / 100)
 if mode == 'max':
self.is_better = lambda a, best: a > best +(
 best * min_delta / 100 )
y_lasso_train = lasso.predict(X_train )<save_to_csv>
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" )
ENet = make_pipeline(RobustScaler() , ElasticNet(alpha=0.0005, l1_ratio=.9, random_state=3))
ENet.fit(X_train, y_log)
 ENet_train_pred = ENet.predict(X_train)
 ENet_pred = np.expm1(ENet.predict(X_test))
sub = pd.DataFrame(ENet_pred)
 sub.head()
sample = pd.read_csv('.. /input/sample_submission.csv')
 sample['SalePrice']=sub[0]
sample.to_csv('Enet.csv',index = False)
<compute_test_metric>
n_fold = 5
random_state = 999
D_in, H = 30, 15
models = []
train_no = 1
training_cycle = 20000
repetition = 1
training_sequence = 1
for A in range(repetition):
kf = StratifiedKFold(n_splits = n_fold , shuffle = True, random_state = random_state+A)
 for train_index, val_index in kf.split(train, y):
train_X = train[train_index]
val_X = train[val_index]
train_y = y[train_index]
val_y = y[val_index]
train_X_torch = torch.tensor(train_X)
 val_X_torch = torch.tensor(val_X)
 train_y_torch = torch.tensor(train_y)
 val_y_torch = torch.tensor(val_y)
 print(f'
urrently Training sequence no {training_sequence}')
 net = Net(D_in, H ).to(device)
 criteria = nn.MSELoss()
optimizer = optim.Adam(net.parameters() , weight_decay=0.0001)
 es = EarlyStopping(patience=20)
 for epoch in range(training_cycle):
print(f'Training Epoch No {epoch}')
 inputs = train_X_torch.to(device)
 labels = train_y_torch.to(device)
 inputs_valid = val_X_torch.to(device)
 labels_valid = val_y_torch.to(device)
 optimizer.zero_grad()
outputs = net(inputs.float())
 outputs_valid = net(inputs_valid.float())
 loss = criteria(outputs, labels.float())
 loss_valid = criteria(outputs_valid, labels_valid.float())
 metric_valid = loss_valid.detach().numpy()
loss.backward()
optimizer.step()
print(f'Current Loss is {metric_valid}')
 if es.step(metric_valid):
break
models.append(net)
 training_sequence += 1
del net, es, metric_valid
ridge.score(X_train,y_log )<compute_test_metric>
ENet.score(X_train,y_log )<compute_test_metric>
test_torch = torch.tensor(test)
 pred_df = sum([net(test_torch.float())for net in models])/(n_fold*repetition )
lasso.score(X_train,y_log )<compute_test_metric>
test_predict_numpy = pred_df.detach().numpy()
print('Ridge MSE: ',metrics.mean_squared_error(y_log,y_train))<compute_test_metric>
for A in range(len(test_predict_numpy)) :
if test_predict_numpy[A] > np.mean(test_predict_numpy):
test_predict_numpy[A] = 1
else:
test_predict_numpy[A] = 0
print('Enet MSE: ',metrics.mean_squared_error(y_log,ENet_train_pred))<compute_test_metric>
sum(test_predict_numpy )
print('Lasso MSE: ',metrics.mean_squared_error(y_log,y_lasso_train))<set_options>
sample = pd.read_csv('/kaggle/input/2019s-uts-data-analytics-assignment-3/Assignment3_Random_Submission-Kaggle.csv')
 sample.QuoteConversion_Flag = test_predict_numpy
sample.to_csv('pytorch_model.csv', index=False)
 print("