Kaggle competition: Google Analytics Customer Revenue Prediction
X_train_norm = X_train.astype('float32')
 X_test_norm = X_test.astype('float32')
 X_train_norm /= 255
X_test_norm /= 255<choose_model_class>
import numpy as np
import pandas as pd
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import LabelEncoder
from sklearn.ensemble import RandomForestRegressor as RFF
import xgboost as xgb
from pprint import pprint
import math
from scipy.stats import kurtosis, skew
learning_rate_reduction=ReduceLROnPlateau(monitor='val_loss',
patience=5,
verbose=1,
factor=0.2
)<choose_model_class>
plt.rcParams['figure.figsize'] =(12,6 )
early_stopping = EarlyStopping(monitor='val_loss',
mode='min',
verbose=1,
patience=10
)<choose_model_class>
def load_data(data='train',n=2):
df = pd.DataFrame()
for i in range(n):
if data=='train':
if i > 8 :
break
dfpart = pd.read_pickle(pjoin(data_root,f'train_{i}.pkl'))
elif data=='test':
if i > 2 :
break
dfpart = pd.read_pickle(pjoin(data_root,f'test_{i}.pkl'))
df = pd.concat([df,dfpart])
 del dfpart
return df
model_check_point = ModelCheckpoint('model.h5', save_best_only=True )<choose_model_class>
df_train = load_data(n=9)
 df_test = load_data('test',n=4 )
class DenseNet:
def __init__(self, input_shape=None, dense_blocks=3, dense_layers=-1, growth_rate=12, nb_classes=None,
dropout_rate=None, bottleneck=False, compression=1.0, weight_decay=1e-4, depth=40):
if nb_classes == None:
raise Exception(
 'Please define number of classes(e.g.num_classes=10 ).This is required for final softmax.')
 if compression <= 0.0 or compression > 1.0:
raise Exception('Compression have to be a value between 0.0 and 1.0.')
 if type(dense_layers)is list:
if len(dense_layers)!= dense_blocks:
raise AssertionError('Number of dense blocks have to be same length to specified layers')
 elif dense_layers == -1:
dense_layers = int(( depth - 4)/ 3)
 if bottleneck:
dense_layers = int(dense_layers / 2)
 dense_layers = [dense_layers for _ in range(dense_blocks)]
else:
dense_layers = [dense_layers for _ in range(dense_blocks)]
self.dense_blocks = dense_blocks
self.dense_layers = dense_layers
self.input_shape = input_shape
self.growth_rate = growth_rate
self.weight_decay = weight_decay
self.dropout_rate = dropout_rate
self.bottleneck = bottleneck
self.compression = compression
self.nb_classes = nb_classes
def build_model(self):
img_input = Input(shape=self.input_shape, name='img_input')
 nb_channels = self.growth_rate
x = Conv2D(2*self.growth_rate,(3,3),
padding='same', strides =(1,1),
kernel_regularizer=keras.regularizers.l2(self.weight_decay))(img_input)
 for block in range(self.dense_blocks-1):
x, nb_channels = self.dense_block(x, self.dense_layers[block], nb_channels, self.growth_rate,
self.dropout_rate, self.bottleneck, self.weight_decay)
 x = self.transition_layer(x, nb_channels, self.dropout_rate, self.compression, self.weight_decay)
 nb_channels = int(nb_channels*self.compression)
 x, nb_channels = self.dense_block(x, self.dense_layers[-1], nb_channels, self.growth_rate, self.dropout_rate, self.weight_decay)
 x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 x = GlobalAveragePooling2D()(x)
 prediction = Dense(self.nb_classes, activation='softmax' )(x)
 return Model(inputs=img_input, outputs=prediction, name='densenet')
 def dense_block(self, x, nb_layers, nb_channels, growth_rate, dropout_rate=None, bottleneck=False, weight_decay=1e-4):
for i in range(nb_layers):
cb = self.convolution_block(x, growth_rate, dropout_rate, bottleneck)
 nb_channels += growth_rate
x = concatenate([cb,x])
 return x, nb_channels
def convolution_block(self, x, nb_channels, dropout_rate=None, bottleneck=False, weight_decay=1e-4):
if bottleneck:
bottleneckWidth = 4
x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 x = Conv2D(nb_channels * bottleneckWidth,(1, 1),
kernel_regularizer=keras.regularizers.l2(weight_decay))(x)
 if dropout_rate:
x = Dropout(dropout_rate )(x)
 x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 x = Conv2D(nb_channels,(3, 3), padding='same' )(x)
 if dropout_rate:
x = Dropout(dropout_rate )(x)
 return x
def transition_layer(self, x, nb_channels, dropout_rate=None, compression=1.0, weight_decay=1e-4):
x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 x = Conv2D(int(nb_channels * compression),(1, 1), padding='same',
kernel_regularizer=keras.regularizers.l2(weight_decay))(x)
 if dropout_rate:
x = Dropout(dropout_rate )(x)
 x = AveragePooling2D(( 2, 2), strides=(2, 2))(x)
 return x<choose_model_class>
print(f'
densenet = DenseNet(( 28,28,1), nb_classes=10, depth=21 )<choose_model_class>
def rmse(y_true, y_pred):
return round(np.sqrt(mean_squared_error(y_true, y_pred)) , 5)
 def split_data(df=df_train,rate=.8):
df = df.sort_values('date' ).copy()
df.drop(['fullVisitorId','visitId','visitStartTime'],axis=1,inplace=True)
 df['Revenue'] = np.log1p(df['Revenue'])
 global X_train,X_valid,y_train,y_valid
n_train = int(len(df)*rate)
 X_train = df.drop(['Revenue','date'],axis=1 ).iloc[:n_train]
X_valid = df.drop(['Revenue','date'],axis=1 ).iloc[n_train:]
y_train = df['Revenue'].iloc[:n_train]
y_valid = df['Revenue'].iloc[n_train:]
print(X_train.shape,X_valid.shape)
 def encode_data(verbose=False):
global df_train_encoded,df_test_encoded
df_train_encoded = df_train.copy()
df_test_encoded = df_test.copy()
for col in df_train.columns:
if df_train_encoded[col].dtype == 'object' and col not in ['fullVisitorId','visitId','visitStartTime','date']:
if verbose:
print(col)
 lb = LabelEncoder()
lb.fit(list(df_train_encoded[col].unique())+ list(df_test_encoded[col].unique()))
df_train_encoded[col] = lb.transform(df_train_encoded[col])
 df_test_encoded[col] = lb.transform(df_test_encoded[col])
 def run_xgb() :
params = {
'objective':'reg:linear',
'eval_metric':'rmse',
'learning_rate':.01,
'eta': 0.1,
'max_depth': 10,
'subsample': 0.6,
'colsample_bytree': 0.6,
'lambda':1,
'random_state': 42,
'silent':True
}
xgb_train_data = xgb.DMatrix(X_train, y_train)
 xgb_val_data = xgb.DMatrix(X_valid, y_valid)
 model = xgb.train(params, xgb_train_data,
num_boost_round=1000,
evals= [(xgb_train_data, 'train'),(xgb_val_data, 'valid')],
early_stopping_rounds=10,
verbose_eval=20 
)
 return model
def submit() :
test_matrix = xgb.DMatrix(X_test)
 y_pred = clf.predict(test_matrix,ntree_limit=clf.best_ntree_limit)
 df_test['PredictedLogRevenue'] = y_pred
submit = df_test[['PredictedLogRevenue','fullVisitorId']].groupby('fullVisitorId' ).PredictedLogRevenue.sum().reset_index()
submit.to_csv('submit.csv',index=False)
 test(y_pred)
 def test(predict):
y_test = df_test['totals_transactionRevenue']
print(rmse(y_test,predict))
model = densenet.build_model()
model_optimizer = Adam(lr=1E-3, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
 model.compile(loss='categorical_crossentropy', optimizer=model_optimizer, metrics=['accuracy'])
 model.summary()<train_model>
def prepare_data(df_train,df_test,
del_col=['fullVisitorId','visitId','visitStartTime','date']):
df_train = df_train.sort_values('date' ).copy()
df_train = df_train.drop(del_col,axis=1 ).copy()
df_test = df_test.drop(del_col,axis=1 ).copy()
df_train['totals_transactionRevenue'] = np.log1p(df_train['totals_transactionRevenue'])
 df_test['totals_transactionRevenue'] = np.log1p(df_test['totals_transactionRevenue'])
 global X_train,X_valid,y_train,y_valid,X_test,y_test
n_train = int(len(df_train)*.8)
 X_train = df_train.drop(['totals_transactionRevenue'],axis=1 ).iloc[:n_train]
X_valid = df_train.drop(['totals_transactionRevenue'],axis=1 ).iloc[n_train:]
y_train = df_train['totals_transactionRevenue'].iloc[:n_train]
y_valid = df_train['totals_transactionRevenue'].iloc[n_train:]
X_test = df_test.drop(['totals_transactionRevenue'],axis=1)
 y_test = df_test['totals_transactionRevenue']
history = model.fit(X_train_norm,
y_train,
batch_size = batch_size,
epochs = num_epochs,
validation_split = 0.1,
shuffle = True,
callbacks = [learning_rate_reduction, early_stopping]
)<save_model>
encode_data(verbose=True)
 prepare_data(df_train_encoded,df_test_encoded,del_col=['fullVisitorId','visitId',
'visitStartTime','date','totals_totalTransactionRevenue','totals_transactions'])
model.save('model.h5' )<predict_on_test>