Kaggle competition: Facebook V: Predicting Check Ins
<SOS> metric: MAP@{K} Kaggle data source: facebook-v-predicting-check-ins<define_variables>
import numpy as np
import pandas as pd
country_number = CC_dataset.shape[0]
country_number<create_dataframe>
trainDF = pd.read_csv('.. /input/train.csv')
 testDF = pd.read_csv('.. /input/test.csv' )
CC_daily_dataset = pd.DataFrame()
Fatality_daily_dataset = pd.DataFrame()
for state_id in range(0, country_number):
sample_case = CC_dataset.loc[CC_dataset['Province_State'] == state_id]
sample_case['Day1CC'] = sample_case['CC1']
sample_fatality = Fatal_dataset.loc[Fatal_dataset['Province_State'] == state_id]
sample_fatality['Day1F'] = sample_fatality['F1']
for case_index in range(1, day_number):
sample_case['Day' + str(case_index + 1)+ 'CC'] = sample_case['CC' + str(case_index + 1)] - sample_case['CC' + str(case_index)]
sample_case = sample_case.drop(['CC' + str(case_index)], axis = 1)
 sample_fatality['Day' + str(case_index + 1)+ 'F'] = sample_fatality['F' + str(case_index + 1)] - sample_fatality['F' + str(case_index)]
sample_fatality = sample_fatality.drop(['F' + str(case_index)], axis = 1)
 sample_case = sample_case.drop(['CC' + str(day_number)], axis = 1)
 sample_fatality = sample_fatality.drop(['F' + str(day_number)], axis = 1)
 CC_daily_dataset = CC_daily_dataset.append(sample_case)
 Fatality_daily_dataset = Fatality_daily_dataset.append(sample_fatality)
<define_variables>
trainDF['x'] = np.floor(trainDF['x'].values * 500.0/10.0)
 trainDF['y'] = np.floor(trainDF['y'].values * 1000.0/10.0)
 trainDF['time'] = np.floor(((trainDF['time'].astype(float)+180.0)/360.0)% 4)
 testDF['x'] = np.floor(testDF['x'].values * 500.0/10.0)
 testDF['y'] = np.floor(testDF['y'].values * 1000.0/10.0)
 testDF['time'] = np.floor(((testDF['time'].astype(float)+180.0)/360.0)% 4 )
last_day_CC = CC_dataset.iloc[:,-1].values
last_day_F = Fatal_dataset.iloc[:,-1].values<filter>
trainDF['identifier'] = trainDF['x'].astype(str)+ ' ' + trainDF['y'].astype(str)+ ' ' + trainDF['time'].astype(str)
 testDF['identifier'] = testDF['x'].astype(str)+ ' ' + testDF['y'].astype(str)+ ' ' + testDF['time'].astype(str )
CC_daily_dataset[CC_daily_dataset['Province_State'] == 223]<drop_column>
def threeMostCommon(listt):
return(' ' ).join([str(val[0])for val in Counter(listt ).most_common(3)] )
CC_daily_dataset = CC_daily_dataset.drop(['Id', 'Province_State'], axis = 1)
 Fatality_daily_dataset = Fatality_daily_dataset.drop(['Id', 'Province_State'], axis = 1 )<split>
pivotedTrain = trainDF.pivot_table('place_id','identifier', aggfunc = {'place_id':threeMostCommon} ).reset_index()
features_CC = CC_daily_dataset.iloc[:, :-1*(future_day_number)]
target_CC = CC_daily_dataset.iloc[:, -1*(future_day_number):]
features_F = CC_daily_dataset.iloc[:, :-1*(future_day_number)]
target_F = Fatality_daily_dataset.iloc[:, -1*(future_day_number):]
train_features_CC, test_features_CC, train_target_CC, test_target_CC = train_test_split(features_CC, target_CC,
test_size = 0.2)
 train_features_F, test_features_F, train_target_F, test_target_F = train_test_split(features_F, target_F,
test_size = 0.2)
<prepare_x_and_y>
out = pd.merge(testDF[['row_id','identifier']], pivotedTrain, how = 'left')
 out.sort('row_id', inplace = True)
 out.drop(['identifier'],axis = 1 ).fillna(0 ).to_csv('output.csv',index = False )