{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"**Problem Statement**\n* Estimating customer value and extrapolating the existing value into future are the sought-after problems of any business.\n* The current problem hails from the domain of online skill-based gaming.\n* The problem is to estimate the customer values based on the features collected from different timestamps of sequences and also to predict the duration that for how much time the customer was generating revenue.\n\n**Dataset Description**\n* Provided feature space contains 22 variables spanning across multiple aspects of customer behavior.\n* Temporal variation is captured by a numbered sequence of entries for each customer.\n* The goal is to predict Y1 & Y2 which represent customer value and temporal extrapolation respectively.\n* Each customer can have N sequenced entries as input and only one set of (Y1, Y2) is expected as output.","metadata":{}},{"cell_type":"code","source":"!pip install klib","metadata":{"execution":{"iopub.status.busy":"2021-09-08T10:07:29.621628Z","iopub.execute_input":"2021-09-08T10:07:29.622021Z","iopub.status.idle":"2021-09-08T10:07:37.068748Z","shell.execute_reply.started":"2021-09-08T10:07:29.621986Z","shell.execute_reply":"2021-09-08T10:07:37.067400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n#klib is a Python library for importing, cleaning, analyzing and preprocessing data. \nimport klib","metadata":{"id":"92005069","execution":{"iopub.status.busy":"2021-09-08T10:07:37.070947Z","iopub.execute_input":"2021-09-08T10:07:37.071463Z","iopub.status.idle":"2021-09-08T10:07:37.076965Z","shell.execute_reply.started":"2021-09-08T10:07:37.071413Z","shell.execute_reply":"2021-09-08T10:07:37.075816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/beyond-analysis/train.csv')","metadata":{"id":"94a1e13b","execution":{"iopub.status.busy":"2021-09-08T10:07:37.078949Z","iopub.execute_input":"2021-09-08T10:07:37.079275Z","iopub.status.idle":"2021-09-08T10:07:41.968109Z","shell.execute_reply.started":"2021-09-08T10:07:37.079240Z","shell.execute_reply":"2021-09-08T10:07:41.966939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('../input/beyond-analysis/test.csv')","metadata":{"id":"79a17c7f","execution":{"iopub.status.busy":"2021-09-08T10:07:41.969835Z","iopub.execute_input":"2021-09-08T10:07:41.970161Z","iopub.status.idle":"2021-09-08T10:07:44.051115Z","shell.execute_reply.started":"2021-09-08T10:07:41.970131Z","shell.execute_reply":"2021-09-08T10:07:44.049902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"id":"2f71390b","outputId":"5ad8ac96-7a39-4a9d-f4a8-797c01b55fa5","execution":{"iopub.status.busy":"2021-09-08T10:07:44.053011Z","iopub.execute_input":"2021-09-08T10:07:44.053471Z","iopub.status.idle":"2021-09-08T10:07:44.088892Z","shell.execute_reply.started":"2021-09-08T10:07:44.053428Z","shell.execute_reply":"2021-09-08T10:07:44.087747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"id":"4e6afab2","outputId":"9af029c5-2366-46ff-f708-b4249324b65e","execution":{"iopub.status.busy":"2021-09-08T10:07:44.090550Z","iopub.execute_input":"2021-09-08T10:07:44.091008Z","iopub.status.idle":"2021-09-08T10:07:44.135426Z","shell.execute_reply.started":"2021-09-08T10:07:44.090932Z","shell.execute_reply":"2021-09-08T10:07:44.133935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"id":"8cd2a26a","outputId":"b3fbff21-5391-4e32-f18f-b75bd1f54958","execution":{"iopub.status.busy":"2021-09-08T10:07:44.136761Z","iopub.execute_input":"2021-09-08T10:07:44.137054Z","iopub.status.idle":"2021-09-08T10:07:44.467509Z","shell.execute_reply.started":"2021-09-08T10:07:44.137027Z","shell.execute_reply":"2021-09-08T10:07:44.466379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=klib.convert_datatypes(df)","metadata":{"id":"89b7325f","execution":{"iopub.status.busy":"2021-09-08T10:07:44.469857Z","iopub.execute_input":"2021-09-08T10:07:44.470156Z","iopub.status.idle":"2021-09-08T10:07:46.553737Z","shell.execute_reply.started":"2021-09-08T10:07:44.470127Z","shell.execute_reply":"2021-09-08T10:07:46.552647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"id":"eb545a4e","outputId":"36b53a29-a7d3-447b-e5d8-7c0433e5accc","execution":{"iopub.status.busy":"2021-09-08T10:07:46.555659Z","iopub.execute_input":"2021-09-08T10:07:46.555961Z","iopub.status.idle":"2021-09-08T10:07:46.636803Z","shell.execute_reply.started":"2021-09-08T10:07:46.555932Z","shell.execute_reply":"2021-09-08T10:07:46.635523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"klib.corr_plot(df,figsize=(20,18))","metadata":{"id":"875482b8","outputId":"4b50accc-1814-45be-a983-a44e2087b4e4","execution":{"iopub.status.busy":"2021-09-08T10:07:46.638184Z","iopub.execute_input":"2021-09-08T10:07:46.638539Z","iopub.status.idle":"2021-09-08T10:07:50.943135Z","shell.execute_reply.started":"2021-09-08T10:07:46.638508Z","shell.execute_reply":"2021-09-08T10:07:50.942281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_try = df.copy()","metadata":{"id":"44YLKhCUyCs0","execution":{"iopub.status.busy":"2021-09-08T10:07:50.944595Z","iopub.execute_input":"2021-09-08T10:07:50.944912Z","iopub.status.idle":"2021-09-08T10:07:50.980955Z","shell.execute_reply.started":"2021-09-08T10:07:50.944882Z","shell.execute_reply":"2021-09-08T10:07:50.979900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_try = df_try[['UNIQUE_IDENTIFIER', 'SEQUENCE_NO', 'STATUS_CHECK',\n         'REVENUE','ACTIVE_YN',\n       'WINNINGS_2', 'DEPOSIT',\n       'WITHDRAW', 'DEPOSIT_TRAILS', 'ENTRY_NUMBER','Y1', 'Y2']]","metadata":{"id":"_F3NVRrzvJWQ","execution":{"iopub.status.busy":"2021-09-08T10:07:50.982048Z","iopub.execute_input":"2021-09-08T10:07:50.982356Z","iopub.status.idle":"2021-09-08T10:07:51.066640Z","shell.execute_reply.started":"2021-09-08T10:07:50.982326Z","shell.execute_reply":"2021-09-08T10:07:51.065501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_try = df_try.groupby(by='UNIQUE_IDENTIFIER').median()","metadata":{"id":"2tJoKxrs3m6v","execution":{"iopub.status.busy":"2021-09-08T10:07:51.068000Z","iopub.execute_input":"2021-09-08T10:07:51.068320Z","iopub.status.idle":"2021-09-08T10:07:51.374527Z","shell.execute_reply.started":"2021-09-08T10:07:51.068290Z","shell.execute_reply":"2021-09-08T10:07:51.373477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_try.describe()","metadata":{"id":"wCxAZ6l1Luj7","outputId":"68cac7b4-83c7-4467-cee5-714038ed98e9","execution":{"iopub.status.busy":"2021-09-08T10:07:51.375846Z","iopub.execute_input":"2021-09-08T10:07:51.376164Z","iopub.status.idle":"2021-09-08T10:07:51.459329Z","shell.execute_reply.started":"2021-09-08T10:07:51.376132Z","shell.execute_reply":"2021-09-08T10:07:51.458234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df_try.iloc[:,0:-2]\ny = df_try.iloc[:,-2:]","metadata":{"id":"fca78941","execution":{"iopub.status.busy":"2021-09-08T10:07:51.460867Z","iopub.execute_input":"2021-09-08T10:07:51.461293Z","iopub.status.idle":"2021-09-08T10:07:51.469447Z","shell.execute_reply.started":"2021-09-08T10:07:51.461256Z","shell.execute_reply":"2021-09-08T10:07:51.468011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Split Data**","metadata":{"id":"5h_I0r5HrXcD"}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test=train_test_split(X, y.values, test_size=0.2,random_state=42,shuffle=True)","metadata":{"id":"b1184f53","execution":{"iopub.status.busy":"2021-09-08T10:07:51.471303Z","iopub.execute_input":"2021-09-08T10:07:51.471828Z","iopub.status.idle":"2021-09-08T10:07:51.501061Z","shell.execute_reply.started":"2021-09-08T10:07:51.471767Z","shell.execute_reply":"2021-09-08T10:07:51.499727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"id":"ie3o69o9JXqK","outputId":"ef7e9f4b-44a8-46e9-dcd9-25ccfe65e826","execution":{"iopub.status.busy":"2021-09-08T10:07:51.502369Z","iopub.execute_input":"2021-09-08T10:07:51.502702Z","iopub.status.idle":"2021-09-08T10:07:51.509614Z","shell.execute_reply.started":"2021-09-08T10:07:51.502671Z","shell.execute_reply":"2021-09-08T10:07:51.508368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"id":"1q0KLi-ELC8w","outputId":"427754a3-4f1a-4ab7-ac0d-751ec3071714","execution":{"iopub.status.busy":"2021-09-08T10:07:51.511458Z","iopub.execute_input":"2021-09-08T10:07:51.511933Z","iopub.status.idle":"2021-09-08T10:07:51.523638Z","shell.execute_reply.started":"2021-09-08T10:07:51.511882Z","shell.execute_reply":"2021-09-08T10:07:51.522657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Build Model**","metadata":{"id":"ef0VS2VS83T9"}},{"cell_type":"code","source":"# importing the libraries\nfrom keras.models import Sequential\nfrom keras.layers import Dense\nfrom keras.layers import Dropout\nfrom keras.regularizers import l1_l2,l1","metadata":{"id":"4AKu3HZAIZoQ","execution":{"iopub.status.busy":"2021-09-08T10:07:51.524837Z","iopub.execute_input":"2021-09-08T10:07:51.525123Z","iopub.status.idle":"2021-09-08T10:07:51.536868Z","shell.execute_reply.started":"2021-09-08T10:07:51.525095Z","shell.execute_reply":"2021-09-08T10:07:51.535597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Dense(units=64, input_dim=9, activation='relu',activity_regularizer=l1(0.001)))\nmodel.add(Dense(units=32, activation='relu',activity_regularizer=l1(0.001)))\n#model.add(Dense(units=16, activation='relu',kernel_initializer='normal'))\nmodel.add(Dense(2,kernel_initializer='normal'))\nmodel.compile(loss='mean_squared_error', optimizer='adam')","metadata":{"id":"hPMTmRieJ0Ar","execution":{"iopub.status.busy":"2021-09-08T10:07:51.538575Z","iopub.execute_input":"2021-09-08T10:07:51.538928Z","iopub.status.idle":"2021-09-08T10:07:51.600205Z","shell.execute_reply.started":"2021-09-08T10:07:51.538894Z","shell.execute_reply":"2021-09-08T10:07:51.599286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, y_train ,batch_size = 132, epochs = 150, validation_data=(X_test,y_test),verbose=1)","metadata":{"id":"XCRQWJNOEOzQ","outputId":"70d46fdc-d7fd-4c7b-f5db-d7cb7ffcb63c","execution":{"iopub.status.busy":"2021-09-08T10:07:51.601403Z","iopub.execute_input":"2021-09-08T10:07:51.601691Z","iopub.status.idle":"2021-09-08T10:10:09.947518Z","shell.execute_reply.started":"2021-09-08T10:07:51.601663Z","shell.execute_reply":"2021-09-08T10:10:09.946474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import mean_squared_error\nfrom sklearn.metrics import r2_score\nfrom math import sqrt\npred = model.predict(X_test)\nprint(sqrt(mean_squared_error(y_test,pred)) )\nprint(r2_score(y_test,pred))","metadata":{"id":"hjjkFCZPSipJ","outputId":"0fcda5a2-083b-4301-ee10-713124297340","execution":{"iopub.status.busy":"2021-09-08T10:10:09.949031Z","iopub.execute_input":"2021-09-08T10:10:09.949371Z","iopub.status.idle":"2021-09-08T10:10:10.379078Z","shell.execute_reply.started":"2021-09-08T10:10:09.949333Z","shell.execute_reply":"2021-09-08T10:10:10.377964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nloss     = history.history[    'loss' ]\nval_loss = history.history['val_loss' ]\nepochs   = range(len(loss))\nplt.figure()\nplt.plot  ( epochs,     loss )\nplt.plot  ( epochs, val_loss )\nplt.title ('Training and validation loss'   )","metadata":{"id":"PSKw1XSGxsjX","outputId":"bdcce521-812b-45e9-e5fb-d7acd41596b3","execution":{"iopub.status.busy":"2021-09-08T10:10:10.382528Z","iopub.execute_input":"2021-09-08T10:10:10.382841Z","iopub.status.idle":"2021-09-08T10:10:10.551498Z","shell.execute_reply.started":"2021-09-08T10:10:10.382810Z","shell.execute_reply":"2021-09-08T10:10:10.550440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nsns.regplot(y_test[:,0],pred[:,0],ci=None)\nplt.xlabel(\"Test\")\nplt.ylabel(\"Prediction\")","metadata":{"id":"zi8YPLpZzrRW","outputId":"47ff6708-6b28-4f0a-80c2-b62b542de716","execution":{"iopub.status.busy":"2021-09-08T10:10:10.553549Z","iopub.execute_input":"2021-09-08T10:10:10.553989Z","iopub.status.idle":"2021-09-08T10:10:10.810658Z","shell.execute_reply.started":"2021-09-08T10:10:10.553945Z","shell.execute_reply":"2021-09-08T10:10:10.809425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test data","metadata":{"id":"38d3c0f2"}},{"cell_type":"code","source":"test=klib.convert_datatypes(test)\ntest['cat_1'] = test['CATEGORY_1'].cat.codes\ntest['cat_2'] = test['CATEGORY_2'].cat.codes","metadata":{"id":"ddQ61-j6MNyc","execution":{"iopub.status.busy":"2021-09-08T10:10:10.812010Z","iopub.execute_input":"2021-09-08T10:10:10.812322Z","iopub.status.idle":"2021-09-08T10:10:12.249371Z","shell.execute_reply.started":"2021-09-08T10:10:10.812293Z","shell.execute_reply":"2021-09-08T10:10:12.248234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_X = test[['UNIQUE_IDENTIFIER', 'SEQUENCE_NO', 'STATUS_CHECK',\n         'REVENUE','ACTIVE_YN',\n       'WINNINGS_2', 'DEPOSIT',\n       'WITHDRAW', 'DEPOSIT_TRAILS', 'ENTRY_NUMBER']]","metadata":{"id":"o_x-gupjMR6w","execution":{"iopub.status.busy":"2021-09-08T10:10:12.250791Z","iopub.execute_input":"2021-09-08T10:10:12.251112Z","iopub.status.idle":"2021-09-08T10:10:12.305096Z","shell.execute_reply.started":"2021-09-08T10:10:12.251081Z","shell.execute_reply":"2021-09-08T10:10:12.304115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_X = test_X.groupby(by='UNIQUE_IDENTIFIER').median()","metadata":{"id":"URzMbsPxMHb6","execution":{"iopub.status.busy":"2021-09-08T10:10:12.306568Z","iopub.execute_input":"2021-09-08T10:10:12.306890Z","iopub.status.idle":"2021-09-08T10:10:12.497634Z","shell.execute_reply.started":"2021-09-08T10:10:12.306860Z","shell.execute_reply":"2021-09-08T10:10:12.496614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(test_X.values)","metadata":{"id":"VehJ_Jm6_xQl","execution":{"iopub.status.busy":"2021-09-08T10:10:12.499142Z","iopub.execute_input":"2021-09-08T10:10:12.499477Z","iopub.status.idle":"2021-09-08T10:10:14.261635Z","shell.execute_reply.started":"2021-09-08T10:10:12.499447Z","shell.execute_reply":"2021-09-08T10:10:14.260553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_final = pd.DataFrame(pred,columns=['Y1','Y2'])","metadata":{"id":"afQ6RSM-_5ON","execution":{"iopub.status.busy":"2021-09-08T10:10:14.263120Z","iopub.execute_input":"2021-09-08T10:10:14.263566Z","iopub.status.idle":"2021-09-08T10:10:14.269603Z","shell.execute_reply.started":"2021-09-08T10:10:14.263520Z","shell.execute_reply":"2021-09-08T10:10:14.268404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_final['UNIQUE_IDENTIFIER'] = test_X.index","metadata":{"id":"mfXXEnXEA3OO","execution":{"iopub.status.busy":"2021-09-08T10:10:14.271344Z","iopub.execute_input":"2021-09-08T10:10:14.271777Z","iopub.status.idle":"2021-09-08T10:10:14.284361Z","shell.execute_reply.started":"2021-09-08T10:10:14.271731Z","shell.execute_reply":"2021-09-08T10:10:14.283187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_final = test_final[['UNIQUE_IDENTIFIER','Y1','Y2']]","metadata":{"id":"3H5J0eo0NfXn","execution":{"iopub.status.busy":"2021-09-08T10:10:14.285711Z","iopub.execute_input":"2021-09-08T10:10:14.286021Z","iopub.status.idle":"2021-09-08T10:10:14.298957Z","shell.execute_reply.started":"2021-09-08T10:10:14.285991Z","shell.execute_reply":"2021-09-08T10:10:14.297836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_final","metadata":{"id":"1lSkUWf1dFIX","outputId":"c323793a-58f3-46eb-cb0b-02bcbd706a2b","execution":{"iopub.status.busy":"2021-09-08T10:10:14.300305Z","iopub.execute_input":"2021-09-08T10:10:14.300640Z","iopub.status.idle":"2021-09-08T10:10:14.321278Z","shell.execute_reply.started":"2021-09-08T10:10:14.300568Z","shell.execute_reply":"2021-09-08T10:10:14.320289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_final.to_csv('submit.csv',index=False)","metadata":{"id":"L_w_FmcLOBuh","execution":{"iopub.status.busy":"2021-09-08T10:10:14.322685Z","iopub.execute_input":"2021-09-08T10:10:14.323063Z","iopub.status.idle":"2021-09-08T10:10:14.627333Z","shell.execute_reply.started":"2021-09-08T10:10:14.323031Z","shell.execute_reply":"2021-09-08T10:10:14.626307Z"},"trusted":true},"execution_count":null,"outputs":[]}]}