Kaggle competition: San Francisco Crime Classification
<SOS> metric: MulticlassLoss Kaggle data source: san-francisco-crime-classification<prepare_x_and_y>
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.impute import SimpleImputer
import lightgbm as lgb
import numpy as np
X_trains, y_train, X_tests = [], None,[]
for traintest in traintest_set:
train = traintest[traintest['Survived'].notnull() ]
y_train = train['Survived']
X_train = train.drop(columns='Survived')
 print("X train shape: ", X_train.shape)
 print("Y train shape: ", y_train.shape)
 X_trains.append(X_train)
 test = traintest[traintest['Survived'].isnull() ]
X_test = test.drop(columns='Survived')
 print("X test shape: ", X_test.shape)
 X_tests.append(X_test )<choose_model_class>
train = pd.read_csv('.. /input/train.csv', parse_dates=['Dates'])
 test = pd.read_csv('.. /input/test.csv', parse_dates=['Dates'], index_col='Id' )
MLA = [
ensemble.AdaBoostClassifier() ,
ensemble.BaggingClassifier() ,
ensemble.ExtraTreesClassifier() ,
ensemble.GradientBoostingClassifier() ,
ensemble.RandomForestClassifier() ,
gaussian_process.GaussianProcessClassifier() ,
linear_model.LogisticRegressionCV() ,
linear_model.PassiveAggressiveClassifier() ,
linear_model.RidgeClassifierCV() ,
linear_model.SGDClassifier() ,
linear_model.Perceptron() ,
naive_bayes.BernoulliNB() ,
naive_bayes.GaussianNB() ,
neighbors.KNeighborsClassifier() ,
svm.SVC(probability=True),
svm.NuSVC(probability=True),
svm.LinearSVC() ,
tree.DecisionTreeClassifier() ,
tree.ExtraTreeClassifier() ,
XGBClassifier()
]<split>
train.drop_duplicates(inplace=True)
 train.replace({'X': -120.5, 'Y': 90.0}, np.NaN, inplace=True)
 test.replace({'X': -120.5, 'Y': 90.0}, np.NaN, inplace=True)
 imp = SimpleImputer(strategy='mean')
 for district in train['PdDistrict'].unique() :
train.loc[train['PdDistrict'] == district, ['X', 'Y']] = imp.fit_transform(
 train.loc[train['PdDistrict'] == district, ['X', 'Y']])
 test.loc[test['PdDistrict'] == district, ['X', 'Y']] = imp.transform(
 test.loc[test['PdDistrict'] == district, ['X', 'Y']] )
cv_split = model_selection.ShuffleSplit(n_splits = 10, test_size =.3, train_size =.7, random_state = 0 )<create_dataframe>
def feature_engineering(data):
data['Date'] = pd.to_datetime(data['Dates'].dt.date)
 data['n_days'] =(
 data['Date'] - data['Date'].min() ).apply(lambda x: x.days)
 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)
 data.drop(columns=['Dates','Date','Address'], inplace=True)
 return data
MLAs = [copy.deepcopy(MLA), copy.deepcopy(MLA)]
MLA_columns = ['MLA Name', 'MLA Parameters','MLA Train Accuracy Mean', 'MLA Test Accuracy Mean', 'MLA Test Accuracy 3*STD' ,'MLA Time']
result_table = []
for i in range(len(X_trains)) :
MLA_compare = pd.DataFrame(columns = MLA_columns)
 MLA_predict = y_train.copy()
print("Y train shape: ", y_train.shape)
 row_index = 0
for alg in MLAs[i]:
target = y_train.copy()
MLA_name = alg.__class__.__name__
MLA_compare.loc[row_index, 'MLA Name'] = MLA_name
MLA_compare.loc[row_index, 'MLA Parameters'] = str(alg.get_params())
 print("Y train shape: ", y_train.shape)
 print(MLA_name)
 cv_results = model_selection.cross_validate(alg, X_trains[i], target, cv = cv_split)
 MLA_compare.loc[row_index, 'MLA Time'] = cv_results['fit_time'].mean()
MLA_compare.loc[row_index, 'MLA Train Accuracy Mean'] = cv_results['train_score'].mean()
MLA_compare.loc[row_index, 'MLA Test Accuracy Mean'] = cv_results['test_score'].mean()
MLA_compare.loc[row_index, 'MLA Test Accuracy 3*STD'] = cv_results['test_score'].std() *3
alg.fit(X_trains[i], y_train)
 MLA_predict[MLA_name] = alg.predict(X_trains[i])
 row_index+=1
result_table.append(( MLA_compare, MLA_predict))<sort_values>
train = feature_engineering(train)
 train.drop(columns=['Descript','Resolution'], inplace=True)
 test = feature_engineering(test )
MLA_compare = result_table[0][0]
MLA_compare.sort_values(by = ['MLA Test Accuracy Mean'], ascending = False, inplace = True)
 MLA_compare<sort_values>
le1 = LabelEncoder()
train['PdDistrict'] = le1.fit_transform(train['PdDistrict'])
 test['PdDistrict'] = le1.transform(test['PdDistrict'])
 le2 = LabelEncoder()
y = le2.fit_transform(train.pop('Category'))
MLA_compare = result_table[1][0]
MLA_compare.sort_values(by = ['MLA Test Accuracy Mean'], ascending = False, inplace = True)
 MLA_compare<save_to_csv>
train_set = lgb.Dataset(
 train, label=y, categorical_feature=['PdDistrict'], free_raw_data=False )
prediction_df = pd.DataFrame()
alg_name = 'XGBClassifier'
feature_index = 0
alg_index = 19
prediction = MLAs[feature_index][alg_index].predict(X_tests[feature_index])
 temp = {'PassengerID': passenger_id, 'Survived': prediction.astype(int)}
result = pd.DataFrame(temp)
 result.to_csv('result_%s_feature%s.csv'%(alg_name, feature_index), index=False)
 prediction_df[alg_name] = prediction
alg_name = 'GradientBoostingClassifier'
feature_index = 0
alg_index = 3
prediction = MLAs[feature_index][alg_index].predict(X_tests[feature_index])
 temp = {'PassengerID': passenger_id, 'Survived': prediction.astype(int)}
result = pd.DataFrame(temp)
 result.to_csv('result_%s_feature%s.csv'%(alg_name, feature_index), index=False)
 prediction_df[alg_name] = prediction
alg_name = 'RidgeClassifierCV'
feature_index = 1
alg_index = 8
prediction = MLAs[feature_index][alg_index].predict(X_tests[feature_index])
 temp = {'PassengerID': passenger_id, 'Survived': prediction.astype(int)}
result = pd.DataFrame(temp)
 result.to_csv('result_%s_feature%s.csv'%(alg_name, feature_index), index=False)
 prediction_df[alg_name] = prediction
alg_name = 'LogisticRegressionCV'
feature_index = 1
alg_index = 6
prediction = MLAs[feature_index][alg_index].predict(X_tests[feature_index])
 temp = {'PassengerID': passenger_id, 'Survived': prediction.astype(int)}
result = pd.DataFrame(temp)
 result.to_csv('result_%s_feature%s.csv'%(alg_name, feature_index), index=False)
 prediction_df[alg_name] = prediction 
<compute_test_metric>
params = {
'objective': 'multiclass',
'num_class': 39
}
cosine_similarity(prediction_df.T )<import_modules>
cv_results = lgb.cv(params, train_set, metrics='multi_logloss', early_stopping_rounds=10 )
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.optimizers import Adam
from keras.regularizers import l2
from keras.callbacks import EarlyStopping
from sklearn import preprocessing
from keras import regularizers<choose_model_class>
print('Best score: ', min(cv_results['multi_logloss-mean']))
<save_to_csv>
num_boost_round = np.argmin(cv_results['multi_logloss-mean'])
 print('Best epoch: ', num_boost_round )
<choose_model_class>
bst = lgb.train(params, train_set, num_boost_round=num_boost_round )
vote_est = [ 
('ada', ensemble.AdaBoostClassifier()), 
('bc', ensemble.BaggingClassifier()), 
('etc',ensemble.ExtraTreesClassifier()), 
('gbc', ensemble.GradientBoostingClassifier()), 
('rfc', ensemble.RandomForestClassifier()), 
('gpc', gaussian_process.GaussianProcessClassifier()), 
('lr', linear_model.LogisticRegressionCV()), 
('bnb', naive_bayes.BernoulliNB()), 
('gnb', naive_bayes.GaussianNB()), 
('knn', neighbors.KNeighborsClassifier()), 
('svc', svm.SVC(probability=True)) , 
('xgb', XGBClassifier())
 ] 
<define_variables>
predictions = bst.predict(test )
<define_search_space><EOS> 
submission = pd.DataFrame(predictions,
columns=le2.classes_,
index=test.index)
 submission.to_csv('LGBM - base model.csv', index_label='Id' )