{"cells":[{"metadata":{},"cell_type":"markdown","source":"Here was the function I used to calculate time since last action for the test data. It proved to be a decent feature (best one I have currently other than the obvious user accuracy history and question accuracy history). I am sure there is a better one out there that captures a similar idea, but this function could also be applied to other things regarding users' \"state.\" I will warn this was computationally expensive and took 4-5 hours to submit.\n\nAlso, just for reference last_record is a dataframe with just the user_id and last timestamp for each user."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"def user_state(test_df, last_record):\n    #The first part of the function calculates the time since last action and updates last_record with the new timestamp\n    action_time = np.empty(len(test_df))\n    for i in range(len(test_df)):\n        if test_df.user_id.iloc[i] in last_record.user_id.values: \n            #check if the user_id is in the DF ... else add it\n            new_time = test_df.timestamp.iloc[i]\n            #new timestamp\n            old_time = last_record.loc[last_record.user_id == test_df.user_id.iloc[i], 'timestamp'].values[0]\n            #looking up old timestamp\n            if (new_time > old_time):\n                #is the new timestamp greater\n                action_time[i] = new_time - old_time\n                #then calculate time differential\n                last_record.loc[last_record.user_id == test_df.user_id.iloc[i], 'timestamp'] = new_time\n                #update timestamp value\n            \n            elif (new_time == old_time):\n                #else is it equal\n                try: #try just in case there is some index nonsense case\n                    action_time[i] = action_time[i-1] #fill w/ prior value\n                    \n                except: \n                    action_time[i] = time_since_median #this shouldn't matter\n            \n            else:\n                #this shouldn't happen\n                action_time[i] = time_since_median\n                \n        else:\n            #add new row to DF and fill value with \n            action_time[i] = time_since_median\n            last_record.loc[len(last_record)] = test_df[['user_id','timestamp']].iloc[i]\n            \n    return action_time, last_record","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","collapsed":true,"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}