Kaggle competition: Google Analytics Customer Revenue Prediction
<SOS> metric: RMSE Kaggle data source: google-analytics-customer-revenue-prediction<categorify>
gc.enable()
warnings.filterwarnings("ignore")
 %matplotlib inline
boarding = {"S": 0, "C": 1, "Q": 2}
data = [train, test]
for d in data:
d['Embarked'] = d['Embarked'].map(boarding )<drop_column>
path1=".. /input/"
data_files=list(os.listdir(path1))
df_files=pd.DataFrame(data_files,columns=['File_Name'])
 df_files['Size_in_MB']=df_files.File_Name.apply(lambda x:round(os.stat(path1+x ).st_size/(1024*1024),2))
df_files
train.drop(['PassengerId','Name','SibSp','Parch','Ticket'],axis=1,inplace=True )<drop_column>
path1=".. /input/ga-customer-revenue-prediction/"
path2='.. /input/gstore-prepared-dataset/'
data_files=['.. /input/gstore-prepared-dataset/prepared_train/prepared_train',
'.. /input/gstore-prepared-dataset/prepared_test/prepared_test']
df_files=pd.DataFrame(data_files,columns=['File_Name'])
 df_files['Size_in_MB']=df_files.File_Name.apply(lambda x:round(os.stat(x ).st_size/(1024*1024),2))
df_files
test.drop(['Name','SibSp','Parch','Ticket'],axis=1,inplace=True )<prepare_x_and_y>
def feature_summary(df_fa):
print('DataFrame shape')
 print('rows:',df_fa.shape[0])
 print('cols:',df_fa.shape[1])
 col_list=['Null','Unique_Count','Data_type','Max/Min','Mean','Std','Skewness','Sample_values']
df=pd.DataFrame(index=df_fa.columns,columns=col_list)
 df['Null']=list([len(df_fa[col][df_fa[col].isnull() ])for i,col in enumerate(df_fa.columns)])
 df['Unique_Count']=list([len(df_fa[col].unique())for i,col in enumerate(df_fa.columns)])
 df['Data_type']=list([df_fa[col].dtype for i,col in enumerate(df_fa.columns)])
 for i,col in enumerate(df_fa.columns):
if 'float' in str(df_fa[col].dtype)or 'int' in str(df_fa[col].dtype):
df.at[col,'Max/Min']=str(round(df_fa[col].max() ,2)) +'/'+str(round(df_fa[col].min() ,2))
df.at[col,'Mean']=df_fa[col].mean()
df.at[col,'Std']=df_fa[col].std()
df.at[col,'Skewness']=df_fa[col].skew()
df.at[col,'Sample_values']=list(df_fa[col].unique())
 return(df.fillna('-'))
def get_val(x,col):
try:
y=x[col]
except:
y=np.nan
return(y)
 def rsme(y,pred):
return(mean_squared_error(y,pred)**0.5 )
x= train.drop("Survived", axis=1)
 y= train["Survived"]
target = test.drop("PassengerId", axis=1 )<choose_model_class>
%%time
print('reading train dataset...')
 df_train=pd.read_csv(data_files[0],dtype={'fullVisitorId':str})
 print('reading test dataset...')
 df_test=pd.read_csv(data_files[1],dtype={'fullVisitorId':str})
 print('data reading complete' )
k_fold= KFold(n_splits=10,shuffle=True,random_state=0 )<compute_train_metric>
train_cpy=df_train.copy()
knn=KNeighborsClassifier(n_neighbors=13)
 score= cross_val_score(knn,x,y,cv=k_fold,n_jobs=1,scoring='accuracy')
 print(score )<compute_test_metric>
df_train['revenue_status']=df_train.totals_transactionRevenue.apply(lambda x: 0 if x==0 else 1 )
round(np.mean(score)*100,2 )<compute_train_metric>
df_browser=df_train[['device_browser','totals_transactionRevenue','revenue_status']].groupby(df_train.device_browser ).aggregate({'totals_transactionRevenue':['mean'],
'revenue_status':['count']} ).reset_index()
df_browser.columns=['device_browser','totals_transactionRevenue_mean','revenue_status_count']
df=df_browser.sort_values(by='totals_transactionRevenue_mean',ascending=False)[df_browser.totals_transactionRevenue_mean>0]
formatter = {'totals_transactionRevenue_mean':'{:4.2f}'}
display(df.style.format(formatter))
plt.subplots(figsize=(20,5))
plt.subplot(1,2,1)
 plt.title('REVENUE MEAN BY BROWSER',color='b',fontsize=12)
 plt.xlabel('Browsers',color='b',fontsize=12)
 plt.ylabel('Mean Revenue',color='b',fontsize=12)
 plt.bar(range(len(df)) ,df.totals_transactionRevenue_mean,color='grey')
 plt.xticks(range(len(df)) ,df.device_browser,rotation=90,fontsize=12)
 plt.yticks(fontsize=12)
 plt.subplot(1,2,2)
 plt.title('NUMBER OF TRANSACTIONS WITH REVENUE BY BROWSER',color='b',fontsize=12)
 plt.xlabel('Browsers',color='b',fontsize=12)
 plt.ylabel('Count of Transactions with Revenue',color='b',fontsize=12)
 plt.bar(range(len(df)) ,df.revenue_status_count,color='orange')
 plt.xticks(range(len(df)) ,df.device_browser,rotation=90,fontsize=12)
 plt.yticks(fontsize=12)
 plt.show()
dt=DecisionTreeClassifier()
score= cross_val_score(dt,x,y,cv=k_fold,n_jobs=1,scoring='accuracy')
 print(score )<compute_test_metric>
df_OS=df_train[['device_operatingSystem','totals_transactionRevenue','revenue_status']].groupby(df_train.device_operatingSystem ).aggregate({'totals_transactionRevenue':['mean'],
'revenue_status':['count']} ).reset_index()
df_OS.columns=['device_operatingSystem','totals_transactionRevenue_mean','revenue_status_count']
df=df_OS.sort_values(by='totals_transactionRevenue_mean',ascending=False)[df_OS.totals_transactionRevenue_mean>0]
display(df.style.format(formatter))
plt.subplots(figsize=(20,5))
plt.subplot(1,2,1)
 plt.title('REVENUE MEAN BY OPERATING SYSTEM',color='b',fontsize=12)
 plt.xlabel('Operating Systems',color='b',fontsize=12)
 plt.ylabel('Mean Revenue',color='b',fontsize=12)
 plt.bar(range(len(df)) ,df.totals_transactionRevenue_mean,color='grey')
 plt.xticks(range(len(df)) ,df.device_operatingSystem,rotation=90,fontsize=12)
 plt.yticks(fontsize=12)
 plt.subplot(1,2,2)
 plt.title('NUMBER OF TRANSACTIONS WITH REVENUE BY OPERATING SYSTEM',color='b',fontsize=12)
 plt.xlabel('Operating Systems',color='b',fontsize=12)
 plt.ylabel('Count of Transactions with Revenue',color='b',fontsize=12)
 plt.bar(range(len(df)) ,df.revenue_status_count,color='orange')
 plt.yticks(fontsize=12)
 plt.xticks(range(len(df)) ,df.device_operatingSystem,rotation=90,fontsize=12)
 plt.show()
round(np.mean(score)*100,2 )<compute_train_metric>
df_isM=df_train[['device_isMobile','totals_transactionRevenue','revenue_status']].groupby(df_train.device_isMobile ).aggregate({'totals_transactionRevenue':['mean'],
'revenue_status':['count']} ).reset_index()
df_isM.columns=['device_isMobile','totals_transactionRevenue_mean','revenue_status_count']
df=df_isM.sort_values(by='totals_transactionRevenue_mean',ascending=False)[df_isM.totals_transactionRevenue_mean>0]
display(df.style.format(formatter))
plt.subplots(figsize=(20,5))
plt.subplot(1,2,1)
 plt.title('REVENUE MEAN BY MOBILE',color='b',fontsize=12)
 plt.xlabel('Mobile',color='b',fontsize=12)
 plt.ylabel('Mean Revenue',color='b',fontsize=12)
 plt.bar(range(len(df)) ,df.totals_transactionRevenue_mean,color='grey')
 plt.xticks(range(len(df)) ,df.device_isMobile,rotation=90,fontsize=12)
 plt.yticks(fontsize=12)
 plt.subplot(1,2,2)
 plt.title('NUMBER OF TRANSACTIONS WITH REVENUE BY MOBILE',color='b',fontsize=12)
 plt.xlabel('Mobile',color='b',fontsize=12)
 plt.ylabel('Count of Transactions with Revenue',color='b',fontsize=12)
 plt.bar(range(len(df)) ,df.revenue_status_count,color='orange')
 plt.yticks(fontsize=12)
 plt.xticks(range(len(df)) ,df.device_isMobile,rotation=90,fontsize=12)
 plt.show()
lr=LogisticRegression()
score= cross_val_score(lr,x,y,cv=k_fold,n_jobs=1,scoring='accuracy')
 print(score )<compute_test_metric>
revenue_transaction_visualization(df_train,'geoNetwork_country','COUNTRY' )
round(np.mean(score)*100,2 )<compute_train_metric>
revenue_transaction_visualization(df_train,'geoNetwork_city','CITY' )
rf = RandomForestClassifier(n_estimators=50,max_depth=6,random_state=0)
 score = cross_val_score(rf,x,y, cv=k_fold, n_jobs=1, scoring='accuracy')
 print(score )<compute_test_metric>
revenue_transaction_visualization(df_train,'geoNetwork_networkDomain','NETWORKDOMAIN' )
round(np.mean(score)*100,2 )<predict_on_test>
revenue_transaction_visualization(df_train,'geoNetwork_subContinent','SUBCONTINENT' )
rf=RandomForestClassifier(n_estimators=30,max_depth=6, random_state=0)
 rf.fit(x,y)
 target_pred= rf.predict(target )<save_to_csv>
revenue_transaction_visualization(df_train,'geoNetwork_continent','CONTINENT' )
output = pd.DataFrame({'PassengerId': test.PassengerId, 'Survived': target_pred})
 output.to_csv('Titanic_RF_Final.csv', index=False)
 print("submission was Successfull!" )<import_modules>
revenue_transaction_visualization(df_train,'geoNetwork_metro','METRO' )
import warnings, re, string
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from nltk.tokenize import word_tokenize
from nltk.stem import SnowballStemmer
from nltk.corpus import stopwords
from sklearn.linear_model import PassiveAggressiveClassifier, LogisticRegression, SGDClassifier
from sklearn.metrics import precision_score, r2_score, recall_score
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import cross_val_score, KFold
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from wordcloud import WordCloud<set_options>
revenue_transaction_visualization(df_train,'trafficSource_adContent','AdCONTENT' )
warnings.filterwarnings('ignore')
 pd.set_option('max_colwidth', -1)
 Colors = {'Reds':'
Etq = ['Not Disaster', 'Disaster']
PATH_DATA = '.. /input/nlp-getting-started/'<load_from_csv>
print('Train data: Unique AdContent value count')
 display(df_train['trafficSource_adContent'].groupby(df_train.trafficSource_adContent ).count())
 print('Test data: Unique AdContent value count')
 display(df_test['trafficSource_adContent'].groupby(df_test.trafficSource_adContent ).count())
df =(pd.read_csv(f'{PATH_DATA}train.csv'
, sep = ','
, header = 0)
.sample(frac = 1, random_state = 0))
df.head()<feature_engineering>
df_train['trafficSource_isTrueDirect'].replace({np.nan:'False'},inplace=True)
 df_isTD=df_train[['trafficSource_isTrueDirect','totals_transactionRevenue','revenue_status']].groupby(df_train.trafficSource_isTrueDirect ).aggregate({'totals_transactionRevenue':['mean'],
'revenue_status':['count']} ).reset_index()
df_isTD.columns=['trafficSource_isTrueDirect','totals_transactionRevenue_mean','revenue_status_count']
df=df_isTD.sort_values(by='totals_transactionRevenue_mean',ascending=False)[df_isTD.totals_transactionRevenue_mean>0]
display(df.style.format(formatter))
plt.subplots(figsize=(20,5))
plt.subplot(1,2,1)
 plt.title('REVENUE MEAN BY isTrueDirect',color='b',fontsize=12)
 plt.xlabel('isTrueDirect',color='b',fontsize=12)
 plt.ylabel('Mean Revenue',color='b',fontsize=12)
 plt.bar(range(len(df)) ,df.totals_transactionRevenue_mean,color='grey')
 plt.xticks(range(len(df)) ,df.trafficSource_isTrueDirect,rotation=90,fontsize=12)
 plt.yticks(fontsize=12)
 plt.subplot(1,2,2)
 plt.title('NUMBER OF TRANSACTIONS WITH REVENUE BY isTrueDirect',color='b',fontsize=12)
 plt.xlabel('isTrueDirect',color='b',fontsize=12)
 plt.ylabel('Count of Transactions with Revenue',color='b',fontsize=12)
 plt.bar(range(len(df)) ,df.revenue_status_count,color='orange')
 plt.yticks(fontsize=12)
 plt.xticks(range(len(df)) ,df.trafficSource_isTrueDirect,rotation=90,fontsize=12)
 plt.show()
def clean_df(df):
df.text = df.text.apply(lambda x: x.lower())
 df.text = df.text.apply(lambda x: re.sub(r'https?://\S+|www\.\S+', '', x))
df.text = df.text.apply(lambda x: re.sub(r'\[.*?\]', '', x))
df.text = df.text.apply(lambda x: re.sub(r'<.*?>', '', x))
df.text = df.text.apply(lambda x: re.sub(r'[%s]' % re.escape(string.punctuation), '', x))
df.text = df.text.apply(lambda x: re.sub(r'
', '', x))
df.text = df.text.apply(lambda x: re.sub(r'\w*\d\w*', '', x))
return df
df = clean_df(df)
 df.head()<string_transform>
revenue_transaction_visualization(df_train,'trafficSource_medium','MEDIUM' )
Punct_List = dict(( ord(punct), None)for punct in string.punctuation)
 Stemmer_List = SnowballStemmer('english')
 def TxNormalize(text):
tokens = word_tokenize(str(text ).replace('/',' ' ).translate(Punct_List))
return [x for x in tokens if x not in stopwords.words('english')]
def Crear_WordCloud(ax, tokens, Color, Titulo, Theme):
if len(tokens)> 0:
wc = WordCloud(width = 6000
, height = 3500
, min_font_size = 60
, max_words = 100
, background_color = 'white'
, colormap = Theme
, random_state = 0
).generate(tokens)
 ax.imshow(wc)
 ax.set_title(Titulo, fontsize = 60, color = Color, y = 1.03)
 ax.axis('off')
 def Plot(ax, df, Color, Titulo):
datos = df.word.value_counts(sort = True ).nlargest(25)
 ax.barh(datos.index, datos.values, color = Color)
 ax.tick_params(left = False, bottom = False)
 ax.invert_yaxis()
[spine.set_visible(False)for spine in ax.spines.values() ]
ax.spines['left'].set_visible(True)
 ax.set_title(Titulo.capitalize() , fontsize = 14)
 ax.grid(axis = 'x', alpha = 0.25)
 def tokenize(df, filtro):
tokens = []
for i in df[df.target == filtro].text:
tokens += TxNormalize(i)
 return tokens<string_transform>
revenue_transaction_visualization(df_train,'trafficSource_referralPath','referralPath' )
Words = [ 
(df[(df.target == i)].text 
.apply(TxNormalize)
.explode() 
.str.cat(sep = ' '))
for i in range(2)]<choose_model_class>
df_combi['date'] = pd.to_datetime(df_combi['visitStartTime'], unit='s')
 df_combi['day_of_week'] = df_combi['date'].dt.dayofweek
df_combi['hour'] = df_combi['date'].dt.hour
df_combi['day'] = df_combi['date'].dt.day
df_combi['month'] = df_combi['date'].dt.month
df_combi['revenue_status']=df_combi.totals_transactionRevenue.apply(lambda x: 0 if x==0 else 1 )
Params = {'Regresión Logística': LogisticRegression(C = 1e5
, class_weight = 'balanced'
, random_state = 0)
 , 'Pasive Aggressive Classifier': PassiveAggressiveClassifier(max_iter = 50
, random_state = 0)
 , 'SGD': SGDClassifier(max_iter = 5
, tol = None
, random_state = 0)
 , 'Naive Bayes': MultinomialNB(alpha = 1)
 , 'Support Vector Machine': SVC(kernel = 'linear'
, C = 1
, gamma = 'scale'
, random_state = 0)}
def Modelo(Clf, X, y):
clf = Pipeline([ 
('tfidf', TfidfVectorizer(ngram_range =(1, 4)
 , analyzer = 'word'
, stop_words = 'english'))
,('Classifier', Clf)
 ])
 return clf.fit(X, y )<compute_train_metric>
%%time
for col in df_combi.columns:
if(( df_combi[col].dtype=='object')&(col!='fullVisitorId')) :
df_combi[col]=df_combi[col].apply(lambda x:str(x ).lower())
 df_combi.replace('nan',np.nan,inplace=True )
X = df.text
y = df.target
Result = {}
kf = KFold(n_splits = 10)
 for i in Params:
Score = []
clf = Modelo(Params[i], X, y)
 Score.append(Params[i].__class__.__name__)
 Score.append(cross_val_score(clf, X, y, cv = kf, scoring = 'accuracy' ).mean())
 Result[i] = Score
Models =(pd.DataFrame(Result, index=['Tipo', 'Cross Val'])
.transpose() 
.sort_values(by = 'Cross Val', ascending = False)
.reset_index() 
.rename(columns = {'index':'Modelo'}))
Models<load_from_csv>
%%time
df_combi.drop(['device_isMobile'],axis=1,inplace=True)
 cat_col=['channelGrouping','device_deviceCategory','tsadclick_slot','tsadclick_adNetworkType','tsadclick_isVideoAd','trafficSource_medium',
'geoNetwork_continent']
for col in cat_col:
df_combi[col]=df_combi[col].apply(lambda x: str(x ).replace(" ","_"))
dummy=pd.DataFrame()
for col in cat_col:
if col.find('_')!=-1:
col_name=col.split('_')[1]
else:
col_name=col
dummy=pd.concat([dummy,pd.get_dummies(df_combi[col],prefix=col_name)],axis=1)
 print('Newly created dummy cols:',len(dummy.columns))
df_combi=pd.concat([df_combi,dummy],axis=1)
 df_combi.drop(cat_col,axis=1,inplace=True )
df =(pd.read_csv(f'{PATH_DATA}test.csv'
, sep = ','
, header = 0))
df = clean_df(df)
 df.head()<predict_on_test>
train=df_combi[:len(train_cpy)]
test=df_combi[len(train_cpy):]
Best = Modelo(Params[Models['Modelo'][0]], X, y)
 df['target'] = Best.predict(df.text )<save_to_csv>
%%time
train['geoNetwork_networkDomain'].fillna('unknown.unknown',inplace=True)
 test['geoNetwork_networkDomain'].fillna('unknown.unknown',inplace=True)
 train['geoNetwork_networkDomain']=train.geoNetwork_networkDomain.apply(lambda x: x.replace('.',' '))
test['geoNetwork_networkDomain']=test.geoNetwork_networkDomain.apply(lambda x: x.replace('.',' '))
df[['id','target']].to_csv('submission.csv', index = False )<set_options>
%%time
Tvect=TfidfVectorizer(ngram_range=(1,2),max_features=20000)
 vect=Tvect.fit(train['geoNetwork_networkDomain'])
 train_vect=vect.transform(train['geoNetwork_networkDomain'])
 test_vect=vect.transform(test['geoNetwork_networkDomain'])
 svd=TruncatedSVD(n_components=10)
 vect_cols=['vect'+str(x)for x in range(1,11)]
df_train_vect=pd.DataFrame(svd.fit_transform(train_vect),columns=vect_cols)
 df_test_vect=pd.DataFrame(svd.fit_transform(test_vect),columns=vect_cols )
%matplotlib inline
%reload_ext autoreload
%autoreload 2<import_modules>
X=train.drop(['sessionId','visitId','date','geoNetwork_networkDomain','tsadclick_gclId'],axis=1)
 X_test=test.drop(['sessionId','visitId','date','geoNetwork_networkDomain','tsadclick_gclId'],axis=1 )
import numpy as np
import pandas as pd
import os
from sklearn.metrics import accuracy_score, f1_score
from sklearn.metrics import precision_score, recall_score
from sklearn.metrics import roc_auc_score, roc_curve, auc
from sklearn.model_selection import train_test_split
import time
import torch
import transformers
from transformers import BertConfig
from transformers import get_linear_schedule_with_warmup<define_variables>
X=pd.concat([X,df_train_vect],axis=1)
 X_test=pd.concat([X_test,df_test_vect],axis=1 )
sys.path.insert(1, '.. /input/tweet-classification-huggingface-wandb1/' )<import_modules>
%%time
agg_func={}
agg_col=['fullVisitorId']
for col in [x for x in X.columns if x not in ['fullVisitorId']]:
if col=='totals_transactionRevenue':
agg_func[col]=['sum']
agg_col.append(str(col)+'_sum')
 elif col=='revenue_status':
agg_func[col]=['sum']
agg_col.append(str(col)+'_sum')
 else:
agg_func[col]=['sum','max','min','mean','var','std']
agg_col.append(str(col)+'_sum')
 agg_col.append(str(col)+'_max')
 agg_col.append(str(col)+'_min')
 agg_col.append(str(col)+'_mean')
 agg_col.append(str(col)+'_var')
 agg_col.append(str(col)+'_std')
 X=X.groupby(X.fullVisitorId ).aggregate(agg_func ).reset_index()
X.columns=agg_col
X_test=X_test.groupby(X_test.fullVisitorId ).aggregate(agg_func ).reset_index()
X_test.columns=agg_col
import config
import dataset
import model
import train
import util<set_options>
%%time
y_dummy=X.revenue_status_sum.apply(lambda x: 0 if x==0 else 1)
 y=X.totals_transactionRevenue_sum.apply(lambda x: np.log1p(x))
X=X.drop(['totals_transactionRevenue_sum','fullVisitorId','revenue_status_sum'],axis=1)
 col=['fullVisitorId','totals_transactionRevenue_sum']
final=X_test[col]
final.columns=['fullVisitorId','PredictedLogRevenue']
X_test=X_test.drop(['fullVisitorId','totals_transactionRevenue_sum','revenue_status_sum'],axis=1 )
device = 'cuda' if torch.cuda.is_available() else 'cpu'<init_hyperparams>
%%time
model=LGBMRegressor(boosting_type='gbdt',num_leaves=31,max_depth=-1,learning_rate=0.01,n_estimators=1000,max_bin=255,subsample_for_bin=50000,
objective=None,min_split_gain=0,min_child_weight=3,min_child_samples=10,subsample=1,subsample_freq=1,colsample_bytree=1,
reg_alpha=0.1,reg_lambda=0,seed=17,silent=False,nthread=-1,n_jobs=-1)
 k=1
splits=5
avg_score=0
skf = StratifiedKFold(n_splits=splits, shuffle=True, random_state=200)
 print('
Starting KFold iterations...')
 for train_index,test_index in skf.split(X,y_dummy):
df_X=X.iloc[train_index,:]
df_y=y.iloc[train_index]
val_X=X.iloc[test_index,:]
val_y=y.iloc[test_index]
model.fit(df_X,df_y)
 preds_x=pd.Series(model.predict(val_X))
acc=rsme(val_y,preds_x)
 print('Iteration:',k,' rmse:',acc)
 if k==1:
score=acc
model1=model
preds=pd.Series(model.predict(X_test))
else:
preds1=pd.Series(model.predict(X_test))
preds=preds+preds1
if score>acc:
score=acc
model1=model
avg_score=avg_score+acc
k=k+1
print('
Best score:',score,' Avg Score:',avg_score/splits)
 preds=preds/splits
warmup_epochs = 0
epochs = 5
model_name = 'bert-base-uncased'
test_size = 0.3
dropout = 0.1
num_classes = 2
linear_in = 768
max_len = 160
train_bs = 16
valid_bs = 8
start_lr = 2e-5
SEED = 42<define_variables>
submission=pd.read_csv(path1+'sample_submission.csv')
 submission=submission.join(final.set_index('fullVisitorId'),on='fullVisitorId',lsuffix='_sub')
 submission.drop('PredictedLogRevenue_sub',axis=1,inplace=True)
 submission.fillna(0,inplace=True)
 submission.to_csv('LGBM_submission.csv',index=False )