Kaggle competition: San Francisco Crime Classification
<SOS> metric: MulticlassLoss Kaggle data source: san-francisco-crime-classification<choose_model_class>
import pandas as pd
import numpy as np
from sklearn import preprocessing
from sklearn.preprocessing import LabelEncoder as le
from sklearn.preprocessing import MultiLabelBinarizer
from sklearn.ensemble import RandomForestClassifier
from sklearn import svm
import matplotlib.pyplot as plt
import seaborn as sns
from collections import OrderedDict
from sklearn.cross_validation import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import f1_score
from sklearn.metrics import recall_score
from sklearn.metrics import precision_score
import xgboost as xgb
from sklearn.tree import DecisionTreeClassifier
<init_hyperparams>
ROOT_FILENAME = ".. /input/"
TRAIN_FILENAME = 'train.csv'
TEST_FILENAME = 'test.csv'
RESULT_FILENAME = 'res2.cv'
best_param = [[
[
{'learning_rate': 0.25, 'n_estimators': 300, 'random_state': 0}
],
[
{'max_samples': 0.5, 'n_estimators': 300, 'random_state': 0}
],
[
{'criterion': 'entropy', 'max_depth': 8, 'n_estimators': 50, 'random_state': 0}
],
[
{'learning_rate': 0.05, 'max_depth': 2, 'n_estimators': 300, 'random_state': 0}
],
[
{'criterion': 'entropy', 'max_depth': 8, 'n_estimators': 300, 'oob_score': True, 'random_state': 0}
],
[
{'max_iter_predict': 10, 'random_state': 0}
],
[
{'fit_intercept': True, 'random_state': 0, 'solver': 'newton-cg'}],
[{
'alpha': 0.1,
}],
[{}],
[{
'n_neighbors': 7,
'weights': 'distance',
'algorithm':'brute'
}],
[{
'C': 1,
'gamma': 0.1,
'decision_function_shape': 'ovo',
'probability': True,
'random_state': 0
}],
[
{'learning_rate': 0.05, 'max_depth': 4, 'n_estimators': 100, 'seed': 0}
]
],
[
[
{'learning_rate': 0.25, 'n_estimators': 300, 'random_state': 0}
],
[
{'max_samples': 1.0, 'n_estimators': 300, 'random_state': 0}
],
[
{'criterion': 'entropy', 'max_depth': None, 'n_estimators': 50, 'random_state': 0}
],
[
{'learning_rate': 0.05, 'max_depth': 6, 'n_estimators': 300, 'random_state': 0}
],
[
{'criterion': 'entropy', 'max_depth': None, 'n_estimators': 50, 'oob_score': True, 'random_state': 0}
],
[
{'max_iter_predict': 10, 'random_state': 0}
],
[
{'fit_intercept': True, 'random_state': 0, 'solver': 'lbfgs'}],
[{
'alpha': 0.1,
}],
[{}],
[{
'n_neighbors': 7,
'weights': 'distance',
'algorithm':'brute'
}],
[{
'C': 1,
'gamma': 0.1,
'decision_function_shape': 'ovo',
'probability': True,
'random_state': 0
}],
[
{'learning_rate': 0.05, 'max_depth': 4, 'n_estimators': 100, 'seed': 0}
]
]
]<find_best_params>
train = pd.read_csv(ROOT_FILENAME+TRAIN_FILENAME, parse_dates=['Dates'], index_col=False)
 print(train.shape)
 train.head(3 )
for i in range(len(vote_ests)) :
for clf, param in zip(vote_ests[i], best_param[i]):
print('The best parameter for {} is {}'.format(clf[1].__class__.__name__, param[0]))
clf[1].set_params(**param[0] )<train_on_grid>
test = pd.read_csv(ROOT_FILENAME+TEST_FILENAME, parse_dates=['Dates'], index_col=False)
 print(test.shape)
 test.head(3 )
grid_hards = []
for i in range(len(vote_ests)) :
grid_hard = ensemble.VotingClassifier(estimators = vote_ests[i], voting = 'hard')
 grid_hard_cv = model_selection.cross_validate(grid_hard, X_trains[i], y_train, cv = cv_split)
 grid_hard.fit(X_trains[i], y_train)
 grid_hards.append(grid_hard)
 print("Hard Voting w/Tuned Hyperparameters Training w/bin score mean: {:.2f}".format(grid_hard_cv['train_score'].mean() *100))
print("Hard Voting w/Tuned Hyperparameters Test w/bin score mean: {:.2f}".format(grid_hard_cv['test_score'].mean() *100))
print("Hard Voting w/Tuned Hyperparameters Test w/bin score 3*std: +/- {:.2f}".format(grid_hard_cv['test_score'].std() *100*3))
print('-'*10)
<save_to_csv>
weekdays = {'Monday':0., 'Tuesday':1., 'Wednesday':2., 'Thursday': 3., 'Friday':4., 'Saturday':5., 'Sunday':6.}
weekdays2 = {'Monday':0., 'Tuesday':0., 'Wednesday':0., 'Thursday': 0., 'Friday':0., 'Saturday':1., 'Sunday':1}
alg_name = 'GridHardVoting'
feature_index = 0
prediction = grid_hards[feature_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 = 'GridHardVoting'
feature_index = 1
prediction = grid_hards[feature_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<import_modules>
def getHourZn(hour):
if(hour >= 2 and hour < 8): return 1;
if(hour >= 8 and hour < 12): return 2;
if(hour >= 12 and hour < 14): return 3;
if(hour >= 14 and hour < 18): return 4;
if(hour >= 18 and hour < 22): return 5;
if(hour < 2 or hour >= 22): return 6;
import numpy as np
import pandas as pd<load_from_csv>
def feature_engineering(data):
data['Day'] = data['Dates'].dt.day
data['Month'] = data['Dates'].dt.month
data['Year'] = data['Dates'].dt.year-2003
data['Hour'] = data['Dates'].dt.hour
data['Minute'] = data['Dates'].dt.minute
data['Day_Num'] = [float(weekdays[w])for w in data.DayOfWeek]
data['WeekOfYear'] = data['Dates'].dt.weekofyear
data['District_Num'] = [float(districts[t])for t in data.PdDistrict]
data['HourZn'] = preprocessing.scale(list(map(getHourZn, data['Dates'].dt.hour)))
 data['isWeekday'] = [float(weekdays2[w])for w in data.DayOfWeek]
data['X'] = preprocessing.scale(list(map(lambda x: x+122.4194, data.X)))
 data['Y'] = preprocessing.scale(list(map(lambda x: x-37.7749, data.Y)))
 data['Address_Type'] = list(map(define_address, data.Address))
return data
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_data = pd.read_csv('/kaggle/input/titanic/test.csv' )<define_variables>
X_loc = ['X', 'Y', 'District_Num', 'Address_Type']
X_time = ['Minute', 'Hour']
X_date = ['Year','Month', 'Day', 'Day_Num', 'HourZn']
X_all = X_loc + X_time + X_date
train = feature_engineering(train)
 train['Category_Num'] = [float(categories[t])for t in train.Category]
X_train, X_test, y_train, y_test = train_test_split(train[X_all], train['Category_Num'], test_size = 0.2, random_state = 0)
 test = feature_engineering(test)
women = train_data[train_data['Sex'] == 'female']['Survived']
rate_women = sum(women)/len(women)
 print('% of women who survived:', rate_women )<define_variables>
clf = RandomForestClassifier(max_features="log2", max_depth=14, n_estimators=25,
min_samples_split=300, oob_score=False ).fit(X_train,y_train)
 y = clf.predict(X_test)
 recall_score(y,y_test, average='micro')
 recall_score(y,y_test, average='macro')
 recall_score(y,y_test, average='weighted')
 precision_score(y,y_test, average='micro')
 precision_score(y,y_test, average='macro')
 precision_score(y,y_test, average='weighted')
 accuracy_score(y,y_test)
 f1_score(y,y_test, average='micro')
 f1_score(y,y_test, average='macro')
 f1_score(y,y_test, average='weighted')
men = train_data[train_data.Sex == 'male']['Survived']
rate_men = sum(men)/len(men)
 print('% of men who survived:', rate_men )<groupby>
knn = KNeighborsClassifier(n_neighbors=5)
 knn.fit(X_train, y_train)
 y = knn.predict(X_test)
 recall_score(y,y_test, average='micro')
 recall_score(y,y_test, average='macro')
 recall_score(y,y_test, average='weighted')
 precision_score(y,y_test, average='micro')
 precision_score(y,y_test, average='macro')
 precision_score(y,y_test, average='weighted')
 accuracy_score(y,y_test)
 f1_score(y,y_test, average='micro')
 f1_score(y,y_test, average='macro')
 f1_score(y,y_test, average='weighted')
<groupby><EOS> 
lgr = LogisticRegression()
lgr.fit(X_train,y_train)
 y = lgr.predict(X_test)
 recall_score(y,y_test, average='micro')
 recall_score(y,y_test, average='macro')
 recall_score(y,y_test, average='weighted')
 precision_score(y,y_test, average='micro')
 precision_score(y,y_test, average='macro')
 precision_score(y,y_test, average='weighted')
 accuracy_score(y,y_test)
 f1_score(y,y_test, average='micro')
 f1_score(y,y_test, average='macro')
 f1_score(y,y_test, average='weighted')