Kaggle competition: Predicting Red Hat Business Value
<import_modules><EOS> 
random.seed(2016)
 def create_feature_map(features):
outfile = open('xgb.fmap', 'w')
 for i, feat in enumerate(features):
outfile.write('{0}\t{1}\tq
'.format(i, feat))
outfile.close()
def get_importance(gbm, features):
create_feature_map(features)
 importance = gbm.get_fscore(fmap='xgb.fmap')
 importance = sorted(importance.items() , key=itemgetter(1), reverse=True)
 return importance
def intersect(a, b):
return list(set(a)& set(b))
def get_features(train, test):
trainval = list(train.columns.values)
 testval = list(test.columns.values)
 output = intersect(trainval, testval)
 output.remove('people_id')
 return sorted(output)
 def run_single(train, test, features, target, random_state=0):
eta = 1.3
max_depth = 3
subsample = 0.8
colsample_bytree = 0.8
start_time = time.time()
print('XGBoost params.ETA: {}, MAX_DEPTH: {}, SUBSAMPLE: {}, COLSAMPLE_BY_TREE: {}'.format(eta, max_depth, subsample, colsample_bytree))
params = {
"objective": "binary:logistic",
"booster" : "gbtree",
"eval_metric": "auc",
"eta": eta,
"tree_method": 'exact',
"max_depth": max_depth,
"subsample": subsample,
"colsample_bytree": colsample_bytree,
"silent": 1,
"seed": random_state,
}
num_boost_round = 115
early_stopping_rounds = 10
test_size = 0.1
X_train, X_valid = train_test_split(train, test_size=test_size, random_state=random_state)
 print('Length train:', len(X_train.index))
print('Length valid:', len(X_valid.index))
y_train = X_train[target]
y_valid = X_valid[target]
dtrain = xgb.DMatrix(X_train[features], y_train)
 dvalid = xgb.DMatrix(X_valid[features], y_valid)
 watchlist = [(dtrain, 'train'),(dvalid, 'eval')]
gbm = xgb.train(params, dtrain, num_boost_round, evals=watchlist, early_stopping_rounds=early_stopping_rounds, verbose_eval=True)
 print("Validating...")
 check = gbm.predict(xgb.DMatrix(X_valid[features]), ntree_limit=gbm.best_iteration+1)
 score = roc_auc_score(X_valid[target].values, check)
 print('Check error value: {:.6f}'.format(score))
imp = get_importance(gbm, features)
 print('Importance array: ', imp)
 print("Predict test set...")
 test_prediction = gbm.predict(xgb.DMatrix(test[features]), ntree_limit=gbm.best_iteration+1)
 print('Training time: {} minutes'.format(round(( time.time() - start_time)/60, 2)))
 return test_prediction.tolist()
def simple_load() :
print("Read people.csv...")
 people = pd.read_csv(".. /input/people.csv",
dtype={'people_id': np.str,
'activity_id': np.str,
'char_38': np.int32},
parse_dates=['date'])
 print("Load train.csv...")
 train = pd.read_csv(".. /input/act_train.csv",
dtype={'people_id': np.str,
'activity_id': np.str,
'outcome': np.int8},
parse_dates=['date'])
 print("Load test.csv...")
 test = pd.read_csv(".. /input/act_test.csv",
dtype={'people_id': np.str,
'activity_id': np.str},
parse_dates=['date'])
 print("Process tables...")
 for table in [train, test]:
table['activity_category'] = table['activity_category'].str.lstrip('type ' ).astype(np.int32)
 for i in range(1, 11):
table['char_' + str(i)].fillna('type -999', inplace=True)
 table['char_' + str(i)] = table['char_' + str(i)].str.lstrip('type ' ).astype(np.int32)
 people['year'] = people['date'].dt.year
people['month'] = people['date'].dt.month
people['day'] = people['date'].dt.day
people['weekday'] = people['date'].dt.weekday
people['weekend'] =(( people.weekday == 0)|(people.weekday == 6)).astype(int)
 people.drop('date', axis=1, inplace=True)
 people['group_1'] = people['group_1'].str.lstrip('group ' ).astype(np.int32)
 for i in range(1, 10):
people['char_' + str(i)] = people['char_' + str(i)].str.lstrip('type ' ).astype(np.int32)
 for i in range(10, 38):
people['char_' + str(i)] = people['char_' + str(i)].astype(np.int32)
 print("Merge...")
 train = train.merge(people, on="people_id", suffixes=("_act", ""))
test = test.merge(people, on="people_id", suffixes=("_act", ""))
train = train.set_index("activity_id")
 test = test.set_index("activity_id")
 return train, test
def group_decision(train, test, only_certain=True):
lookup = train.groupby(["group_1", "date_act"], as_index=False)["outcome"].mean()
test = pd.merge(test.reset_index() , lookup, how="left", on=["group_1", "date_act"] ).set_index("activity_id")
 train["date_act_fillfw"] = train["date_act"]
train["date_act_fillbw"] = train["date_act"]
train["group_fillfw"] = train["group_1"]
train["group_fillbw"] = train["group_1"]
df = train.append(test)
 df = df.sort_values(by=["group_1", "date_act"])
 df["date_act_fillfw"] = df["date_act_fillfw"].fillna(method="ffill")
 df["date_act_fillbw"] = df["date_act_fillbw"].fillna(method="bfill")
 df["outcome_fillfw"] = df["outcome"].fillna(method="ffill")
 df["outcome_fillbw"] = df["outcome"].fillna(method="bfill")
 df["group_fillfw"] = df["group_fillfw"].fillna(method="ffill")
 df["group_fillbw"] = df["group_fillbw"].fillna(method="bfill")
 df["fw_same_date"] =(df["date_act_fillfw"] == df["date_act"] ).astype(int)
 df["bw_same_date"] =(df["date_act_fillbw"] == df["date_act"] ).astype(int)
 df["fw_same_group"] =(df["group_fillfw"] == df["group_1"] ).astype(int)
 df["bw_same_group"] =(df["group_fillbw"] == df["group_1"] ).astype(int)
 df["interfill"] =(df["outcome_fillfw"] *
df["fw_same_group"] +
df["outcome_fillbw"] *
df["bw_same_group"])/(df["fw_same_group"] +
df["bw_same_group"])
 df["needs cushion"] =(df["fw_same_group"] * df["bw_same_group"] - 1 ).abs()
df["cushion"] = df["needs cushion"] * df["interfill"] * -0.1 + df["needs cushion"] * 0.05
df["interfill"] = df["interfill"] + df["cushion"]
df["outcome"] = df["outcome"].fillna(df["interfill"])
 if only_certain == True:
df = df[(df["outcome"] == 0.0)|(df["outcome"] == 1.0)]
test["outcome"] = df["outcome"]
return test["outcome"]
def xgboost_return(train,test,features):
print("Process tables...")
 for table in [train, test]:
table['year'] = table['date'].dt.year
table['month'] = table['date'].dt.month
table['day'] = table['date'].dt.day
table['weekday'] = table['date'].dt.weekday
table['weekend'] =(( table.weekday == 0)|(table.weekday == 6)).astype(int)
 table.drop('date', axis=1, inplace=True)
 features.remove('date')
 features.remove('date_act')
 test["extra outcomes"] = run_single(train,test,features,"outcome")
 return test["extra outcomes"]
def model() :
train, test = simple_load()
features = get_features(train,test)
 test["outcome"] = group_decision(train, test, only_certain=False)
 test.reset_index() [["activity_id", "outcome"]].to_csv("starter_template.csv", index=False)
 test["outcome"] = test["outcome"].fillna(xgboost_return(train,test,features))
return test.reset_index() [["activity_id", "outcome"]]
def main() :
model().to_csv("submission.csv", index=False)
 if __name__ == "__main__":
main()