Kaggle competition: Porto Seguro’s Safe Driver Prediction
<SOS> metric: NormalizedGini Kaggle data source: porto-seguro’s-safe-driver-prediction<feature_engineering>
seed =45
% matplotlib inline
train['SalePrice'] = np.log1p(train['SalePrice'])
 numeric_feats = all_data.dtypes[all_data.dtypes != "object"].index
skewed_feats = train[numeric_feats].apply(lambda x: skew(x.dropna()))
skewed_feats = skewed_feats[skewed_feats > 0.75]
skewed_feats = skewed_feats.index
all_data[skewed_feats] = np.log1p(all_data[skewed_feats] )<data_type_conversions>
path = '.. /input/'
train = pd.read_csv(path+'train.csv',na_values=-1)
 test = pd.read_csv(path+'test.csv',na_values=-1)
 print('Number rows and columns:',train.shape)
 print('Number rows and columns:',test.shape )
for feature in all_data.select_dtypes(exclude=['object'] ).columns:
all_data[feature].fillna(all_data[feature].median() , inplace = True)
 for feature in all_data.select_dtypes(include=['object'] ).columns:
all_data[feature].fillna(all_data[feature].value_counts().idxmax() , inplace = True )<categorify>
ps_cal = train.columns[train.columns.str.startswith('ps_calc')]
train = train.drop(ps_cal,axis =1)
 test = test.drop(ps_cal,axis=1)
 train.shape
all_data = pd.get_dummies(all_data)
 print(all_data.shape )<prepare_x_and_y>
k= pd.DataFrame()
k['train']= train.isnull().sum()
k['test'] = test.isnull().sum()
k
X_train = all_data[:train.shape[0]]
X_test = all_data[train.shape[0]:]
y = train.SalePrice<compute_train_metric>
def missing_value(df):
col = df.columns
for i in col:
if df[i].isnull().sum() >0:
df[i].fillna(df[i].mode() [0],inplace=True )
def rmse_cv(model):
rmse= np.sqrt(-cross_val_score(model, X_train, y, scoring="neg_mean_squared_error", cv = 5))
return(rmse )<define_search_space>
missing_value(train)
 missing_value(test )
alphas = [0.0005, 0.001, 0.01, 0.03, 0.05, 0.1]
l1_ratios = [1.5, 1.1, 1, 0.9, 0.8, 0.7, 0.5]<compute_train_metric>
def descrictive_stat_feat(df):
df = pd.DataFrame(df)
 dcol= [c for c in train.columns if train[c].nunique() >5]
dcol.remove('id')
 d_median = df[dcol].median(axis=0)
 d_mean = df[dcol].mean(axis=0)
 q1 = df[dcol].apply(np.float32 ).quantile(0.25)
 q3 = df[dcol].apply(np.float32 ).quantile(0.75)
 for c in dcol:
df[c+str('_median_range')] =(df[c].astype(np.float32 ).values > d_median[c] ).astype(np.int8)
 df[c+str('_mean_range')] =(df[c].astype(np.float32 ).values > d_mean[c] ).astype(np.int8)
 df[c+str('_q1')] =(df[c].astype(np.float32 ).values < q1[c] ).astype(np.int8)
 df[c+str('_q3')] =(df[c].astype(np.float32 ).values > q3[c] ).astype(np.int8)
 return df
cv_elastic = [rmse_cv(ElasticNet(alpha = alpha, l1_ratio=l1_ratio)).mean()
for(alpha, l1_ratio)in product(alphas, l1_ratios)]<choose_model_class>
train = descrictive_stat_feat(train)
 test = descrictive_stat_feat(test )
ENet = make_pipeline(RobustScaler() , ElasticNet(alpha=0.0005, l1_ratio=.9, random_state=3))<compute_train_metric>
category_col = [c for c in train.columns if train[c].nunique() <= 104]
len(category_col )
n_folds = 5
def rmsle_cv(model):
kf = KFold(n_folds, shuffle=True, random_state=42 ).get_n_splits(train.values)
 rmse= np.sqrt(-cross_val_score(model, X_train.values, y, scoring="neg_mean_squared_error", cv = kf))
return(rmse )<compute_test_metric>
def OHE(df1,df2,column):
cat_col = column
len_df1 = df1.shape[0]
df = pd.concat([df1,df2],ignore_index=True)
 c2,c3 = [],{}
print('Categorical feature',len(column))
for c in cat_col:
if df[c].nunique() >2 :
c2.append(c)
 c3[c] = 'ohe_'+c
df = pd.get_dummies(df, prefix=c3, columns=c2,drop_first=True)
 df1 = df.loc[:len_df1]
df2 = df.loc[len_df1:]
print('Train',df1.shape)
 print('Test',df2.shape)
 return df1,df2
score = rmsle_cv(ENet)
 print("ElasticNet score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))<compute_test_metric>
train1,test1 = OHE(train,test,category_col )
def rmsle(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred))<compute_test_metric>
X = train.drop(['target','id'],axis=1)
 y = train['target'].astype('category')
 x_test = test.drop('id',axis=1)
 test_id = test['id']
ENet.fit(X_train.values, y)
 ENet_train_pred = ENet.predict(X_train.values)
 print('RMSLE score on train data:')
 print(rmsle(y, ENet_train_pred))<predict_on_test>
del train,test,train1,test1
ENet_pred = np.expm1(ENet.predict(X_test.values))<save_to_csv>
sub = pd.DataFrame()
sub['Id'] = test['Id']
sub['SalePrice'] = ENet_pred
sub.to_csv('submission.csv',index=False )<load_from_csv>
def runXGB(xtrain,xvalid,ytrain,yvalid,xtest,eta=0.07,num_rounds=100,max_depth=8):
params = {
'objective':'binary:logistic',
'max_depth':max_depth,
'learning_rate':eta,
'eval_metric':'auc',
'min_child_weight':6,
'subsample':0.8,
'colsample_bytree':0.8,
'seed':seed,
'reg_lambda':1.3,
'reg_alpha':8,
'gamma':10,
'scale_pos_weight':1.6,
'nthread':-1
}
dtrain = xgb.DMatrix(xtrain,label=ytrain)
 dvalid = xgb.DMatrix(xvalid,label=yvalid)
 dtest = xgb.DMatrix(xtest)
 watchlist = [(dtrain,'train'),(dvalid,'test')]
model = xgb.train(params,dtrain,num_rounds,watchlist,early_stopping_rounds=300,verbose_eval=20)
 pred = model.predict(dvalid,ntree_limit=model.best_ntree_limit)
 pred_test = model.predict(dtest,ntree_limit=model.best_ntree_limit)
 return pred_test,model
train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv', index_col='Id')
 test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv', index_col='Id' )<concatenate>
kf = StratifiedKFold(n_splits=5,random_state=seed,shuffle=True)
 pred_test_full =0
cv_score = []
i=1
for train_index,test_index in kf.split(X,y):
print('{} of KFold {}'.format(i,kf.n_splits))
xtr,xvl = X.loc[train_index],X.loc[test_index]
ytr,yvl = y[train_index],y[test_index]
pred_test,xg_model = runXGB(xtr,xvl,ytr,yvl,x_test,num_rounds=500,eta=0.1)
 pred_test_full += pred_test
cv_score.append(xg_model.best_score)
 i+=1
train['IsTrain'] = 1
test['IsTrain'] = 0
DataRaw = pd.concat([train, test] )<count_values>
print(cv_score)
 print('Mean cv_score',np.mean(cv_score))
print("Number of features not counting the target:" + str(len(DataRaw.columns)- 1 - 1))<define_variables>
pred_xgb = pred_test_full/kf.n_splits
<define_variables><EOS> 
y_pred = pred_xgb
submit = pd.DataFrame({'id':test_id,'target':y_pred})
 submit.to_csv('xgb_porto.csv',index=False)