Kaggle competition: Google Analytics Customer Revenue Prediction
if in_testing_mode:
dfTrain["predicted_selected_text"] = dfTrain.apply(lambda df: predict_test(df.text, df.sentiment, df.text_w, df.text_w_n), axis=1)
 dfTrain["jaccard"] = dfTrain.apply(lambda df: jaccard(df.selected_text, df.predicted_selected_text), axis=1)
 print("average jaccard score of train data is: ",dfTrain["jaccard"].mean())
 print("average jaccard score of positive train data is: ",dfTrain[dfTrain['sentiment'] == 'positive']["jaccard"].mean())
 print("average jaccard score of negative train data is: ",dfTrain[dfTrain['sentiment'] == 'negative']["jaccard"].mean())
 print("average jaccard score of neutral train data is: ",dfTrain[dfTrain['sentiment'] == 'neutral']["jaccard"].mean() )<set_options>
import numpy as np
import pandas as pd
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
import xgboost as xgb
from pprint import pprint
import math
from scipy.stats import kurtosis, skew
from IPython import embed
from IPython.terminal.embed import InteractiveShellEmbed
from sklearn.model_selection import KFold
import random
get_ipython().run_line_magic('matplotlib', 'inline')
 sns.set_style("whitegrid")
<load_from_csv>
plt.rcParams['figure.figsize'] =(12,6 )
train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 test = pd.read_csv("/kaggle/input/Kannada-MNIST/test.csv")
 validation = pd.read_csv("/kaggle/input/Kannada-MNIST/Dig-MNIST.csv")
 sample = pd.read_csv("/kaggle/input/Kannada-MNIST/sample_submission.csv" )<prepare_x_and_y>
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
Y_train = to_categorical(train.iloc[:,0])
 X_train = train.iloc[:, 1:].values
X_valid = validation.iloc[:, 1:].values
y_valid = to_categorical(validation.iloc[:,0])
 Y_test = to_categorical(test.iloc[:, 0])
 X_test = test.iloc[:, 1:].values 
<normalization>
%%time
df_train = load_data(n=9)
 df_test = load_data('test',n=4 )
X_train = X_train/255
X_valid = X_valid/255
X_test = X_test/255<choose_model_class>
del_cols=[item for item in df_train.columns if "Date" in item]
df_train=df_train.drop(del_cols,axis=1 ).copy()
df_test=df_test.drop(del_cols,axis=1 ).copy()
train_datagen = ImageDataGenerator(
 rotation_range=12,
width_shift_range=0.25,
height_shift_range=0.25,
shear_range=12,
zoom_range=0.25 
)
 valid_datagen = ImageDataGenerator(
 rotation_range=12,
width_shift_range=0.25,
height_shift_range=0.25,
shear_range=12,
zoom_range=0.25)
 valid_datagen_simple = ImageDataGenerator()<split>
df_all = pd.concat([df_train,df_test] ).reset_index(drop=True )
<choose_model_class>
print(f'
model = keras.Sequential([
keras.layers.Conv2D(64, kernel_size=3, activation='relu', input_shape=(28,28,1)) ,
keras.layers.BatchNormalization() ,
keras.layers.Conv2D(32, kernel_size=5, activation='relu'),
keras.layers.MaxPooling2D(pool_size=(2, 2)) ,
keras.layers.Dropout(0.25),
keras.layers.Conv2D(128, kernel_size=3, activation='relu'),
keras.layers.BatchNormalization() ,
keras.layers.Conv2D(128, kernel_size=3, activation='relu'),
keras.layers.BatchNormalization() ,
keras.layers.Dropout(0.5),
keras.layers.Conv2D(256, kernel_size=3, activation='relu'),
keras.layers.BatchNormalization() ,
keras.layers.Conv2D(512, kernel_size=3, activation='relu'),
keras.layers.BatchNormalization() ,
keras.layers.BatchNormalization() ,
keras.layers.Dropout(0.5),
keras.layers.Conv2D(1024, kernel_size = 3, strides=2, padding='same', activation='relu'),
keras.layers.BatchNormalization() ,
keras.layers.MaxPooling2D(pool_size=(2, 2)) ,
keras.layers.Dropout(0.5),
keras.layers.Flatten() ,
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(256, activation='relu'),
keras.layers.BatchNormalization() ,
keras.layers.Dense(1024,activation='relu'),
keras.layers.BatchNormalization() ,
keras.layers.Dropout(0.2),
keras.layers.Dense(10, activation='softmax')
 ])
 model.summary()<choose_model_class>
df_train_x = df_all[df_all.date <= "2018-06-30"].copy()
df_train_y = df_all[df_all.date >= "2018-08-15"].copy()
id_train = df_all[(df_all.date >= "2018-01-15")&(df_all.date <= "2018-06-30")].fullVisitorId.drop_duplicates()
df_train_x = df_train_x[df_train_x.fullVisitorId.isin(id_train)].copy()
df_train_y = df_train_y[df_train_y.fullVisitorId.isin(id_train)].copy()
df_test_x = df_all[(df_all.date >= "2016-11-15")&(df_all.date <= "2018-10-15")].copy()
id_test = df_all[(df_all.date >= "2018-05-01")&(df_all.date <= "2018-10-15")].fullVisitorId.drop_duplicates()
df_test_x = df_test_x[df_test_x.fullVisitorId.isin(id_test)].copy()
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'] )<train_model>
%time
global df_train_x_encoded,df_train_y_encoded, df_all_encoded, df_test_x_encoded
df_train_x_encoded = df_train_x.copy()
df_train_y_encoded = df_train_y.copy()
df_all_encoded = df_all.copy()
df_test_x_encoded = df_test_x.copy()
df_train_x_encoded["date"]=(datetime.datetime(2018,6,30)-df_train_x_encoded.date ).dt.days.astype("int64")/365
df_train_y_encoded["date"]=(datetime.datetime(2018,10,15)-df_train_y_encoded.date ).dt.days.astype("int64")/365
df_test_x_encoded["date"]=(datetime.datetime(2018,10,15)-df_test_x_encoded.date ).dt.days.astype("int64")/365
cat_cols=["channelGrouping","device_browser","device_deviceCategory",
"device_operatingSystem","geoNetwork_city","geoNetwork_continent","geoNetwork_country","trafficSource_medium",
"trafficSource_adwordsClickInfo.isVideoAd","trafficSource_isTrueDirect","device_isMobile"]
last_cols = cat_cols + ["fullVisitorId","date"]
df_train_x_agg_last = df_train_x_encoded[last_cols].groupby("fullVisitorId",as_index=False ).last().sort_values("fullVisitorId" ).reset_index(drop=True ).copy()
df_test_x_agg_last = df_test_x_encoded[last_cols].groupby("fullVisitorId",as_index=False ).last().sort_values("fullVisitorId" ).reset_index(drop=True ).copy()
df_train_y_agg_last = df_train_y_encoded[last_cols].groupby("fullVisitorId",as_index=False ).last().sort_values("fullVisitorId" ).reset_index(drop=True ).copy()
history = model.fit_generator(train_datagen.flow(X_train, Y_train, batch_size = 1800),
epochs=70,validation_data =(valid_datagen.flow(X_valid, y_valid, batch_size = 30)) ,
verbose=1
)<train_model>
ec= ce.OrdinalEncoder(cols=cat_cols,handle_unknown='impute')
 ec.fit(pd.concat([df_train_x_agg_last,df_train_y_agg_last,df_test_x_agg_last]))
df_train_x_agg_last=ec.transform(df_train_x_agg_last)
 df_train_y_agg_last=ec.transform(df_train_y_agg_last)
 df_test_x_agg_last=ec.transform(df_test_x_agg_last )
valid_loss,valid_acc = model.evaluate(X_valid, y_valid, verbose=0)
 print('Validation Accuracy: %.2f' %(valid_acc*100))
print('
Validation Loss: %.2f' %(valid_loss))
train_loss, train_acc = model.evaluate(X_train, Y_train, verbose = 1)
 print('
Training accuracy: %.2f
' %(train_acc*100))<predict_on_test>
%%time
num_cols = [item for item in df_train.columns if "totals" in item]
sum_cols = num_cols + ["fullVisitorId"]
df_train_x_agg_sum = df_train_x_encoded[sum_cols].groupby("fullVisitorId",as_index=False ).sum().sort_values("fullVisitorId" ).reset_index(drop=True ).copy()
df_test_x_agg_sum = df_test_x_encoded[sum_cols].groupby("fullVisitorId",as_index=False ).sum().sort_values("fullVisitorId" ).reset_index(drop=True ).copy()
df_train_y_agg_sum = df_train_y_encoded[sum_cols].groupby("fullVisitorId",as_index=False ).sum().sort_values("fullVisitorId" ).reset_index(drop=True ).copy()
df_train_x_agg_sum['totals_transactionRevenue'] = np.log1p(df_train_x_agg_sum['totals_transactionRevenue'])
 df_test_x_agg_sum['totals_transactionRevenue'] = np.log1p(df_test_x_agg_sum['totals_transactionRevenue'])
 df_train_y_agg_sum['totals_transactionRevenue'] = np.log1p(df_train_y_agg_sum['totals_transactionRevenue'])
 df_train_x_agg = pd.merge(df_train_x_agg_sum,df_train_x_agg_last, how='left',on="fullVisitorId" ).sort_values("fullVisitorId" ).reset_index(drop=True ).copy()
df_train_y_agg = pd.merge(df_train_y_agg_sum,df_train_y_agg_last, how='left',on="fullVisitorId" ).sort_values("fullVisitorId" ).reset_index(drop=True ).copy()
df_test_x_agg = pd.merge(df_test_x_agg_sum,df_test_x_agg_last, how='left',on="fullVisitorId" ).sort_values("fullVisitorId" ).reset_index(drop=True ).copy()
df_train_y_agg=pd.merge(id_train.to_frame() , df_train_y_agg, how='left',on="fullVisitorId" ).sort_values("fullVisitorId" ).reset_index(drop=True ).copy()
df_train_y_agg["totals_transactionRevenue"]=df_train_y_agg["totals_transactionRevenue"].fillna(0 )
label_hot = model.predict(X_test)
 label = np.argmax(label_hot,1)
 id_ = np.arange(0,label.shape[0] )<load_from_csv>
df_train_x_agg[df_train_x_agg.totals_transactionRevenue>0]
sample = pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv' )<save_to_csv>
train_x = df_train_x_agg.drop(["fullVisitorId"],axis=1 ).values
train_y = df_train_y_agg.totals_transactionRevenue.values
test_x_id = df_test_x_agg.fullVisitorId.values
test_x = df_test_x_agg.drop(["fullVisitorId"],axis=1 ).values
grid_params={'max_depth': [4,5,6],
'subsample': [0.95],
'colsample_bytree': [1.0]
}
xgb_model = xgb.XGBClassifier(early_stopping_rounds=50,eval_metric="rmse",verbose=3,n_jobs=-1)
 grid_search = GridSearchCV(xgb_model,
grid_params,
cv=3,
scoring="neg_mean_squared_error",
n_jobs=1,
verbose=3)
 grid_search.fit(train_x,train_y)
 predict = grid_search.predict(test_x )
<install_modules><EOS> 
predict_df=pd.DataFrame()
predict_df["fullVisitorId"]=test_x_id
predict_df["PredictedLogRevenue"]=predict
predict_df.to_csv("xgboost_cv.csv", index=False )