Kaggle competition: Mercedes-Benz Greener Manufacturing
<SOS> metric: R2Score Kaggle data source: mercedes-benz-greener-manufacturing<groupby>
%matplotlib inline
train.groupby(['TicketPrefix'])['TicketPrefix'].count()<feature_engineering>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
train.loc[train.TicketPrefix.str.startswith('A'), 'TicketPrefix'] = 'A'
train.loc[train.TicketPrefix.str.startswith('C'), 'TicketPrefix'] = 'C'
train.loc[train.TicketPrefix.str.startswith('F'), 'TicketPrefix'] = 'F'
train.loc[train.TicketPrefix.str.startswith('P'), 'TicketPrefix'] = 'P'
train.loc[train.TicketPrefix.str.startswith('S'), 'TicketPrefix'] = 'S'
train.loc[train.TicketPrefix.str.startswith('W'), 'TicketPrefix'] = 'W'
test.loc[test.TicketPrefix.str.startswith('A'), 'TicketPrefix'] = 'A'
test.loc[test.TicketPrefix.str.startswith('C'), 'TicketPrefix'] = 'C'
test.loc[test.TicketPrefix.str.startswith('F'), 'TicketPrefix'] = 'F'
test.loc[test.TicketPrefix.str.startswith('P'), 'TicketPrefix'] = 'P'
test.loc[test.TicketPrefix.str.startswith('S'), 'TicketPrefix'] = 'S'
test.loc[test.TicketPrefix.str.startswith('W'), 'TicketPrefix'] = 'W'
train.groupby(['TicketPrefix'])['TicketPrefix'].count()<data_type_conversions>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
train['Alone'] =(( train.Parch + train.SibSp)== 0 ).astype(int)
 test['Alone'] =(( test.Parch + test.SibSp)== 0 ).astype(int)
 train.head()<drop_column>
uniq_vals = {}
for col in cat_cols:
uniq_vals[col] = len(train[col].unique())
 cat_cols
train = train.drop(['Name', 'SibSp', 'Parch', 'Embarked'], axis=1)
 test = test.drop(['Name', 'SibSp', 'Parch', 'Embarked'], axis=1)
 train.head()<categorify>
train['eval_set'] = 0; test['eval_set'] = 1
df = pd.concat([train, test], axis=0, copy=True,sort = True)
 df.reset_index(drop=True, inplace=True)
 df
def encode_ticket(t):
e = {
'No': 0,
'A': 1,
'P': 2,
'S': 3,
'C': 4,
'W': 5,
'F': 6
}
return e.get(t, -1)
 train['Ticket'] = train.TicketPrefix.apply(encode_ticket)
 test['Ticket'] = test.TicketPrefix.apply(encode_ticket)
 train.head()<categorify>
def add_new_col(x):
if x not in new_col.keys() :
return int(len(new_col.keys())/2)
 return new_col[x]
for c in cat_cols:
new_col = train_cleaned.groupby(c ).y.mean().sort_values().reset_index()
new_col = new_col.reset_index().set_index(c ).drop('y', axis=1)['index'].to_dict()
df[c + '_new'] = df[c].apply(add_new_col)
 df_new = df.drop(cat_cols, axis=1)
 df_new.head()
train.Title = LabelEncoder().fit_transform(train.Title)
 test.Title = LabelEncoder().fit_transform(test.Title)
 train.head()<drop_column>
import xgboost as xgb
from xgboost import XGBRegressor
from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
train.drop(['TicketPrefix'], axis=1, inplace=True)
 test.drop(['TicketPrefix'], axis=1, inplace=True)
 train.head()<concatenate>
xgb_params = {
'n_trees': 500,
'eta': 0.005,
'max_depth': 3,
'subsample': 0.9,
'colsample_bytree': 0.6,
'objective': 'reg:linear',
'eval_metric': 'rmse',
'base_score': np.log(y_mean),
'silent': 1
}
dtrain = xgb.DMatrix(X_train, np.array(np.log(y_train)))
 dtest = xgb.DMatrix(X_test)
 def the_metric(y_pred, y):
y_true = y.get_label()
return 'r2', r2_score(y_true, y_pred)
 cv_result = xgb.cv(xgb_params,
dtrain,
num_boost_round=2000,
nfold = 3,
early_stopping_rounds=50,
feval=the_metric,
verbose_eval=100,
show_stdv=False 
)
 num_boost_rounds = len(cv_result)
 print('num_boost_rounds=' + str(num_boost_rounds))
model = xgb.train(dict(xgb_params, silent=0), dtrain, num_boost_round=num_boost_rounds)
 y_train_pred = np.exp(model.predict(dtrain))
y_pred = np.exp(model.predict(dtest))
print('First 5 predicted test values:', y_pred[:5])
<drop_column><EOS> 
output = pd.DataFrame({'id': test['ID'].astype(np.int32), 'y': y_pred})
 output.to_csv('sub_16_encoded.csv', index=False )