Kaggle competition: San Francisco Crime Classification
discriminant_analysis, tree, gaussian_process, model_selection 
<load_from_csv>
train = pd.read_csv('.. /input/train.csv', parse_dates=['Dates'])
 test = pd.read_csv('.. /input/test.csv', parse_dates=['Dates'], index_col='Id' )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<create_dataframe>
train.isnull().sum()
passenger_id = test['PassengerId'].copy()<concatenate>
train.isnull().sum()
traintest = pd.concat([train, test], axis=0, sort=False )<filter>
pd.options.display.max_columns=100
train = pd.read_csv('.. /input/train.csv', parse_dates=['Dates'])
 test = pd.read_csv('.. /input/test.csv', parse_dates=['Dates'], index_col='Id')
 def feature_engineering(data):
data['Date'] = pd.to_datetime(data['Dates'].dt.date)
 data['Day'] = data['Dates'].dt.day
data['DayOfWeek'] = data['Dates'].dt.weekday
data['Month'] = data['Dates'].dt.month
data['Year'] = data['Dates'].dt.year
data['Hour'] = data['Dates'].dt.hour
data['Minute'] = data['Dates'].dt.minute
data['Block'] = data['Address'].str.contains('block', case=False ).apply(lambda x: 1 if x == True else 0)
 data.drop(columns=['Dates','Date','Address'], inplace=True)
 return data
train = feature_engineering(train)
 test = feature_engineering(test)
 train.drop(columns=['Descript','Resolution'], inplace=True )
isnull = traintest.isnull().sum().reset_index()
isnull.columns = ['Feature', 'Total_null']
total_null = isnull[isnull['Total_null']>0]
total_null<feature_engineering>
le1 = LabelEncoder()
train['PdDistrict'] = le1.fit_transform(train['PdDistrict'])
 test['PdDistrict'] = le1.transform(test['PdDistrict'])
 le2 = LabelEncoder()
X = train.drop(columns=['Category'])
 y= le2.fit_transform(train['Category'] )
traintest['Title'] = traintest['Name'].str.split(", ", expand=True)[1].str.split(".", expand=True)[0]
replace = {
'Ms': 'Miss',
'Dona': 'Miss',
'Mlle': 'Miss',
'Mme': 'Miss',
'Don': 'Mr',
'Sir': 'Mr'
}
traintest.replace({'Title': replace}, inplace=True )<categorify>
train_xgb = xgb.DMatrix(X, label=y)
 test_xgb = xgb.DMatrix(test )
df_nan_ages = traintest['Age'].isnull()
title_ages = traintest[['Title', 'Age']].groupby('Title' ).mean().to_dict() ['Age']
traintest['Age'][df_nan_ages] = traintest[df_nan_ages]['Title'].apply(lambda x: title_ages[x])
 traintest.fillna({
'Fare': traintest['Fare'].mode() [0],
'Embarked': traintest['Embarked'].mode() [0]
}, inplace=True)
 traintest.drop(columns='Cabin', inplace=True)
 traintest.drop(columns='PassengerId', inplace=True )<feature_engineering>
params = {
'max_depth': 6,
'eta': 0.3,
'num_boost_rounds' : 150 ,
'silent': 1,
'objective': 'multi:softprob',
'eval_metric' : 'mlogloss',
'learning_data' : 0.07,
'num_class': 39,
}
traintest['FamilySize'] = traintest.SibSp + traintest.Parch + 1
traintest['IsAlone'] =(traintest['FamilySize'] == 1)*1
traintest['AgeStage'] = traintest['Age']
traintest['AgeStage'][traintest['Age'] <= 11] = 'Child'
traintest['AgeStage'][(traintest['Age'] <= 20)&(traintest['Age'] > 11)] = 'Young'
traintest['AgeStage'][(traintest['Age'] <= 40)&(traintest['Age'] > 20)] = 'Adult'
traintest['AgeStage'][traintest['Age'] > 40] = 'Old'
traintest.drop(columns='Name', inplace=True )<filter>
CROSS_VAL = False
if CROSS_VAL:
print('Doing Cross-validation...')
 cv = xgb.cv(params, train_xgb, nfold=3, early_stopping_rounds=10, metrics='mlogloss', verbose_eval=True)
 cv
<categorify><EOS> 
SUBMIT = not CROSS_VAL
if SUBMIT:
print('Fitting Model...')
 m = xgb.train(params, train_xgb, 10)
 res = m.predict(test_xgb)
 submission = pd.DataFrame(res, columns=le2.inverse_transform 
(np.linspace(0, 38, 39, dtype='int16')) ,
index=test.index)
 submission.to_csv('submission.csv', index='Id')
 print(submission.sample(3))
else :
print('FAIL' )