Kaggle competition: San Francisco Crime Classification
<SOS> metric: MulticlassLoss Kaggle data source: san-francisco-crime-classification<load_from_csv>
%matplotlib inline
test = pd.read_csv('.. /input/titanic/test.csv')
 test.info()<define_variables>
train = pd.read_csv(".. /input/train.csv")
 train.head()
passenger_id = test['PassengerId']<categorify>
test = pd.read_csv('.. /input/test.csv')
 test.head()
transformed_test = transform_dataset(test)
 transformed_test.info()<predict_on_test>
print('Frequency of Categories: ', train.Category.nunique())
 print('Frequency of PdDistricts: ', train.PdDistrict.nunique())
 print('Frequency of DayOfWeeks: ', train.DayOfWeek.nunique() )
Y_predict = model.predict(transformed_test)
 Y_p = pd.DataFrame(Y_predict, columns=['Survived'])
 Y_p<concatenate>
train = train.drop('Resolution', axis=1)
 train.sample(1 )
res = pd.concat([passenger_id, Y_p], axis=1)
 res<save_to_csv>
train['Date'] = pd.to_datetime(train.Dates)
 test['Date'] = pd.to_datetime(test.Dates)
 train = train.drop('Dates', axis=1)
 test = test.drop('Dates', axis=1)
 train.sample(1 )
res.to_csv('res.csv', index=None )<set_options>
train['IsDay'] = 0
train.loc[(train.Date.dt.hour > 6)&(train.Date.dt.hour < 20), 'IsDay' ] = 1
test['IsDay'] = 0
test.loc[(test.Date.dt.hour > 6)&(test.Date.dt.hour < 20), 'IsDay' ] = 1
train.sample(3 )
sns.set(style='white', context='notebook', palette='deep')
 plt.rcParams['image.cmap']='gray' 
<load_from_csv>
days_to_int_dic = {
'Monday': 1,
'Tuesday': 2,
'Wednesday': 3,
'Thursday': 4,
'Friday': 5,
'Saturday': 6,
'Sunday': 7,
}
train['DayOfWeek'] = train['DayOfWeek'].map(days_to_int_dic)
 test['DayOfWeek'] = test['DayOfWeek'].map(days_to_int_dic)
 train.DayOfWeek.unique()
col_names = ['label']+[str(x)for x in range(784)]
df = pd.concat([
pd.read_csv('.. /input/mnist-in-csv/mnist_train.csv', names=col_names, header=0),
pd.read_csv('.. /input/mnist-in-csv/mnist_test.csv', names=col_names, header=0),
pd.read_csv('.. /input/digit-recognizer/train.csv', names=col_names, header=0),
pd.read_csv('.. /input/digit-recognizer/test.csv', names=col_names[1:], header=0),
], axis=0, sort=False, ignore_index=True )<prepare_x_and_y>
train['Hour'] = train.Date.dt.hour
train['Month'] = train.Date.dt.month
train['Year'] = train.Date.dt.year
train['Year'] = train['Year']
test['Hour'] = test.Date.dt.hour
test['Month'] = test.Date.dt.month
test['Year'] = test.Date.dt.year
test['Year'] = test['Year']
train.sample(1 )
num_classes = 10
X, y = df[col_names[1:]], df[col_names[0]]<data_type_conversions>
train['HourCos'] = np.cos(( train['Hour']*2*np.pi)/24)
 train['DayOfWeekCos'] = np.cos(( train['DayOfWeek']*2*np.pi)/7)
 train['MonthCos'] = np.cos(( train['Month']*2*np.pi)/12)
 test['HourCos'] = np.cos(( test['Hour']*2*np.pi)/24)
 test['DayOfWeekCos'] = np.cos(( test['DayOfWeek']*2*np.pi)/7)
 test['MonthCos'] = np.cos(( test['Month']*2*np.pi)/12)
 train.sample(1 )
train = y.notna()
test = ~train
y_matrix =(y[:,None] == range(num_classes)).astype(int)
 Xtrain, ytrain = X[train], y_matrix[train]
Xtest , ytest = X[test] , y_matrix[test]<choose_model_class>
train = pd.get_dummies(train, columns=['PdDistrict'])
 test = pd.get_dummies(test, columns=['PdDistrict'])
 train.sample(2 )
def baseline_model() :
model = Sequential()
model.add(Conv2D(32, kernel_size=(6, 6), strides=(2, 2), activation='relu',input_shape=Xtrain[0].shape))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
 model.add(Conv2D(64,(5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Flatten())
 model.add(Dense(100, activation='relu'))
model.add(Dense(25, activation='relu'))
model.add(Dense(num_classes, activation='sigmoid'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 return model<set_options>
cat_le = LabelEncoder()
train['CategoryInt'] = pd.Series(cat_le.fit_transform(train.Category))
train.sample(5)
 cat_le.classes_
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=20,
width_shift_range=0.1,
height_shift_range=0.1,
brightness_range=None,
shear_range=5,
zoom_range=-0.4,
fill_mode='nearest',
horizontal_flip=False,
vertical_flip=False,
rescale=None,
preprocessing_function=None,
data_format=None,
validation_split=0.2
)<train_model>
train['InIntersection'] = 1
train.loc[train.Address.str.contains('Block'), 'InIntersection'] = 0
test['InIntersection'] = 1
test.loc[test.Address.str.contains('Block'), 'InIntersection'] = 0
datagen.fit(Xtrain )<train_model>
feature_cols = ['X', 'Y', 'IsDay', 'DayOfWeek', 'Month', 'Hour', 'Year', 'InIntersection',
'PdDistrict_BAYVIEW', 'PdDistrict_CENTRAL', 'PdDistrict_INGLESIDE',
'PdDistrict_MISSION', 'PdDistrict_NORTHERN', 'PdDistrict_PARK',
'PdDistrict_RICHMOND', 'PdDistrict_SOUTHERN', 'PdDistrict_TARAVAL', 'PdDistrict_TENDERLOIN']
target_col = 'CategoryInt'
train_x = train[feature_cols]
train_y = train[target_col]
test_ids = test['Id']
test_x = test[feature_cols]
augmentation = True
if augmentation:
history = estimator.fit_generator(
 datagen.flow(Xtrain, ytrain, batch_size=10),
steps_per_epoch=Xtrain.shape[0],
epochs=10 
)
 else:
history = estimator.fit(
 Xtrain, ytrain,
batch_size=32, epochs=10,
validation_split=len(ytest)/len(y)
 )<predict_on_test>
train_xgb = xgb.DMatrix(train_x, label=train_y)
 test_xgb = xgb.DMatrix(test_x )
ytest = estimator.predict_classes(Xtest )<create_dataframe>
params = {
'max_depth': 5,
'eta': 0.4,
'silent': 1,
'objective': 'multi:softprob',
'num_class': 39,
}
submit = pd.DataFrame(data={'ImageId': range(1, ytest.shape[0]+1), 'Label': ytest} )<save_to_csv>
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
submit.to_csv("submit.csv", index=None )<import_modules>
SUBMIT = not CROSS_VAL
if SUBMIT:
print('Fitting Model...')
 m = xgb.train(params, train_xgb, 10)
 res = m.predict(test_xgb)
 cols = ['Id'] + cat_le.classes_
submission = pd.DataFrame(res, columns=cat_le.classes_)
 submission.insert(0, 'Id', test_ids)
 submission.to_csv('submission.csv', index=False)
 print('Done Outputing !')
 print(submission.sample(3))
else:
print('NOT SUBMITING' )