Kaggle competition: Prudential Life Insurance Assessment
<SOS> metric: QuadraticWeightedKappa Kaggle data source: prudential-life-insurance-assessment<compute_test_metric>
import pandas as pd
import numpy as np
import xgboost as xgb
from scipy.optimize import fmin_powell
from ml_metrics import quadratic_weighted_kappa
def ToWeight(y):
w = np.zeros(y.shape, dtype=float)
 ind = y != 0
w[ind] = 1./(y[ind]**2)
 return w
def rmspe(yhat, y):
w = ToWeight(y)
 rmspe = np.sqrt(np.mean(w *(y-yhat)**2))
return rmspe
def rmspe_xg(yhat, y):
y = y.get_label()
y = np.expm1(y)
 yhat = np.expm1(yhat)
 w = ToWeight(y)
 rmspe = np.sqrt(np.mean(w *(y-yhat)**2))
return "rmspe", rmspe
def neg_rmspe(yhat, y):
y = np.expm1(y)
 yhat = np.expm1(yhat)
 w = ToWeight(y)
 rmspe = np.sqrt(np.mean(w *(y-yhat)**2))
return -rmspe<train_on_grid>
def eval_wrapper(yhat, y):
y = np.array(y)
 y = y.astype(int)
 yhat = np.array(yhat)
 yhat = np.clip(np.round(yhat), np.min(y), np.max(y)).astype(int)
 return quadratic_weighted_kappa(yhat, y)
 def get_params() :
params = {}
params["objective"] = "reg:linear"
params["eta"] = 0.05
params["min_child_weight"] = 240
params["subsample"] = 0.9
params["colsample_bytree"] = 0.67
params["silent"] = 1
params["max_depth"] = 6
plst = list(params.items())
 return plst
def apply_offset(data, bin_offset, sv, scorer=eval_wrapper):
data[1, data[0].astype(int)==sv] = data[0, data[0].astype(int)==sv] + bin_offset
score = scorer(data[1], data[2])
 return score
regressor = DecisionTreeRegressor(random_state=2)
 cv_sets = ShuffleSplit(n_splits=5, test_size=0.2)
 params = {'max_depth':range(10,40,2)}
scoring_fnc = make_scorer(neg_rmspe)
 grid = GridSearchCV(regressor,params,scoring_fnc,cv=cv_sets)
 grid = grid.fit(train[features], np.log1p(train.Sales))
DTR = grid.best_estimator_<find_best_params>
columns_to_drop = ['Id', 'Response', 'Medical_History_10','Medical_History_24']
xgb_num_rounds = 700
num_classes = 8
eta_list = [0.05] * 200
eta_list = eta_list + [0.03] * 100
eta_list = eta_list + [0.02] * 350
eta_list = eta_list + [0.01] * 50
print("Load the data using pandas")
 train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 all_data = train.append(test)
 all_data['Product_Info_2_char'] = all_data.Product_Info_2.str[0]
all_data['Product_Info_2_num'] = all_data.Product_Info_2.str[1]
all_data['Product_Info_2'] = pd.factorize(all_data['Product_Info_2'])[0]
all_data['Product_Info_2_char'] = pd.factorize(all_data['Product_Info_2_char'])[0]
all_data['Product_Info_2_num'] = pd.factorize(all_data['Product_Info_2_num'])[0]
all_data['BMI_Age'] = all_data['BMI'] * all_data['Ins_Age']
med_keyword_columns = all_data.columns[all_data.columns.str.startswith('Medical_Keyword_')]
all_data['Med_Keywords_Count'] = all_data[med_keyword_columns].sum(axis=1)
 print('Eliminate missing values')
 all_data.fillna(-1, inplace=True)
 all_data['Response'] = all_data['Response'].astype(int)
 train = all_data[all_data['Response']>0].copy()
test = all_data[all_data['Response']<1].copy()
DTR.get_params()<save_to_csv>
plst = get_params()
print(plst)
 tr=train.drop(columns_to_drop, axis=1)
 ts=test.drop(columns_to_drop, axis=1)
 skf = StratifiedKFold(train['Response'].values, n_folds=2)
 train_preds=[]
for train_index, test_index in skf:
print("TRAIN:", train_index, "TEST:", test_index)
 d_train=tr.iloc[train_index,:]
d_test=tr.iloc[test_index,:]
xgtrain = xgb.DMatrix(d_train, train['Response'].values[train_index])
 xgtest = xgb.DMatrix(d_test, label=train['Response'].values[test_index])
 model = xgb.train(plst, xgtrain, xgb_num_rounds, learning_rates=eta_list)
 tp = model.predict(xgtrain, ntree_limit=model.best_iteration)
 train_preds.extend(list(tp))
submission = pd.DataFrame({"Id": test["Id"], "Sales": np.expm1(DTR.predict(test[features])) })
 submission.to_csv("benchmark.csv", index=False )<init_hyperparams>
xgtrain = xgb.DMatrix(train.drop(columns_to_drop, axis=1), train['Response'].values)
 xgtest = xgb.DMatrix(test.drop(columns_to_drop, axis=1), label=test['Response'].values)
 plst = get_params()
print(plst)
 model = xgb.train(plst, xgtrain, xgb_num_rounds, learning_rates=eta_list)
 train_preds = model.predict(xgtrain, ntree_limit=model.best_iteration)
 print('Train score is:', eval_wrapper(train_preds, train['Response']))
<train_model><EOS> 
test_preds = model.predict(xgtest, ntree_limit=model.best_iteration)
 train_preds = np.clip(train_preds, -0.99, 8.99)
 test_preds = np.clip(test_preds, -0.99, 8.99)
 offsets = np.array([0.1, -1, -2, -1, -0.8, 0.02, 0.8, 1])
 data = np.vstack(( train_preds, train_preds, train['Response'].values))
for j in range(num_classes):
data[1, data[0].astype(int)==j] = data[0, data[0].astype(int)==j] + offsets[j]
for j in range(num_classes):
train_offset = lambda x: -apply_offset(data, x, j)
 offsets[j] = fmin_powell(train_offset, offsets[j])
 data = np.vstack(( test_preds, test_preds, test['Response'].values))
for j in range(num_classes):
data[1, data[0].astype(int)==j] = data[0, data[0].astype(int)==j] + offsets[j]
final_test_preds = np.round(np.clip(data[1], 1, 8)).astype(int)
 preds_out = pd.DataFrame({"Id": test['Id'].values, "Response": final_test_preds})
 preds_out = preds_out.set_index('Id')
 preds_out.to_csv('xgb_modifiedEtalist_offset_submission.csv' )