Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<load_from_csv>
warnings.filterwarnings(action='ignore', category=DataConversionWarning)
 warnings.filterwarnings(action='ignore', category=DeprecationWarning)
 warnings.filterwarnings(action='ignore', category=FutureWarning )
dftest = pd.read_csv('.. /input/test.csv' )<feature_engineering>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 data = train.append(test, sort=False )
newtimeStamp=pd.to_datetime(dftest['pickup_datetime'])
 dftest['Hour'] = newtimeStamp.apply(lambda x : x.hour)
 dftest['Month'] = newtimeStamp.apply(lambda x : x.month)
 dftest['Date'] = newtimeStamp.apply(lambda x : x.date() )<feature_engineering>
types = pd.DataFrame(data.dtypes ).rename(columns={0: 'type'} ).sort_values(by=['type'],ascending=False)
 types
Day_of_Week=newtimeStamp.apply(
 lambda x :
calendar.day_name[datetime.date(x.year,x.month,x.day)
.weekday() ] 
)
 dmap = {0:'Mon',1:'Tue',2:'Wed',3:'Thu',4:'Fri',5:'Sat',6:'Sun'}
Day_of_Week = newtimeStamp.apply(
 lambda x :
dmap[datetime.date(x.year,x.month,x.day)
.weekday() ] 
)
 dftest['Day of Week'] = Day_of_Week<feature_engineering>
def check_missing(df):
null_val = df.isnull().sum()
percent = 100 * df.isnull().sum() /len(df)
 missing_table = pd.concat([null_val, percent], axis=1)
 col = missing_table.rename(columns = {0 : 'Num', 1 : 'Rate'})
 return col
print("Data
cols = check_missing(data)
 types.join(cols ).sort_values(by="Rate", ascending=False )
dftest['qty'] = dftest['pickup_datetime'].apply(lambda x: 1 )<feature_engineering>
data.drop(['Cabin'], axis=1, inplace = True)
 data["Embarked"] = data["Embarked"].fillna(data["Embarked"].mode() [0])
 data["Embarked"] = data["Embarked"].map({"S": 0, "C" : 1, "Q" : 2})
 title_mapping = {
' (.+)Mr\.(.+)': 1, ' (.+)Master\.(.+)': 1,
' (.+)Dr\.(.+)': 2, ' (.+)Don\.(.+)': 2, ' (.+)Major\.(.+)': 2,
' (.+)Sir\.(.+)':2, ' (.+)Col\.(.+)': 2, ' (.+)Jonkheer\.(.+)': 2,
' (.+)Capt\.(.+)': 2,' (.+)Countess\.(.+)': 2, ' (.+)Dona\.(.+)': 2,
' (.+)Rev\.(.+)': 3,
' (.+)Ms\.(.+)': 4, ' (.*)Miss\.(.+)': 4, ' (.+)Mrs\.(.+)': 4,
' (.+)Mme\.(.+)': 4,' (.+)Lady\.(.+)': 4, ' (.+)Mlle\.(.+)': 4
}
data["Title"] = data["Name"].replace(title_mapping, regex=True ).astype('int')
 data["Sex"] = data["Sex"].map({"male": 0, "female": 1} )
dftest['timestamp'] = newtimeStamp.apply(lambda x : time.mktime(
(x.year,x.month,x.day,
x.hour,x.minute,x.second,
1,x.day,-1)) )<feature_engineering>
data['FamilySize'] = data["Parch"] + data["SibSp"]
data['IsFamily'] = data["Parch"] + data["SibSp"]
data.loc[data['IsFamily'] > 1, 'IsFamily'] = 2
data.loc[data['IsFamily'] == 1, 'IsFamily'] = 1
data.loc[data['IsFamily'] == 0, 'IsFamily'] = 0
DEFAULT_SURVIVAL_VALUE = 0.5
data['FamilySurvival'] = DEFAULT_SURVIVAL_VALUE
data['LastName'] = data['Name'].apply(lambda x: str.split(x, ",")[0])
 for grp, grp_df in data.groupby(['LastName', 'Fare']):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
data.loc[data['PassengerId'] == passID, 'FamilySurvival'] = 1
elif(smin==0.0):
data.loc[data['PassengerId'] == passID, 'FamilySurvival'] = 0
for _, grp_df in data.groupby('Ticket'):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
if(row['FamilySurvival'] == 0)|(row['FamilySurvival']== 0.5):
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
data.loc[data['PassengerId'] == passID, 'FamilySurvival'] = 1
elif(smin==0.0):
data.loc[data['PassengerId'] == passID, 'FamilySurvival'] = 0
dftest['Travel distance'] = list(map(lambda x1,x2,x3,x4:
geo.distance(( x3,x1),(x4,x2)).miles,
dftest['pickup_longitude'], dftest['dropoff_longitude'],
dftest['pickup_latitude'], dftest['dropoff_latitude']))<groupby>
train_target = data[:891]["Survived"].values
data.drop(['Name', 'PassengerId', 'Age', 'Fare', 'Ticket', 'LastName'], axis = 1, inplace = True)
 data = pd.get_dummies(data, columns=["Embarked", "Title", "Sex", "IsFamily", "FamilySurvival"], drop_first=True)
 train = data[:891]
test = data[891:]
data.dtypes
dfm=dftest.groupby(['Day of Week','Hour'] ).count()
fp=dfm['qty'].unstack()
fp<import_modules>
possible_features = train.columns.copy().drop('Survived')
 selector = SelectKBest(f_classif, len(possible_features))
selector.fit(train[possible_features], train_target)
 scores = -np.log10(selector.pvalues_)
 indices = np.argsort(scores)[::-1]
print('Feature importances:')
 for i in range(len(scores)) :
print('%.2f %s' %(scores[indices[i]], possible_features[indices[i]]))
from sklearn import preprocessing<categorify>
fparams = \
['Sex_1', 'Title_4', 'FamilySurvival_1.0', 'Pclass', 'FareBin',
'FamilySurvival_0.5', 'Embarked_1', 'IsFamily_1', 'IsFamily_2',
'Parch', 'Title_3', 'AgeBin', 'SibSp']
train_features = train[fparams].values
test_features = test[fparams].values
CV_SPLIT_NUM = 6
N_ESTIMATORS = 300
DEBUG_MODE = False
coltest=list(dftest.columns)
 idy=coltest[2:7]
idy.append(coltest[12])
 idy.append(coltest[13])
 idy<prepare_x_and_y>
rfgs_parameters = {
'n_estimators': [N_ESTIMATORS],
'max_depth' : [2,3,4],
'max_features': [2,3,4],
"min_samples_split": [2,3,4],
"min_samples_leaf": [2,3,4]
}
if not DEBUG_MODE:
rfc_cv = GridSearchCV(RandomForestClassifier() , rfgs_parameters, cv=CV_SPLIT_NUM)
 rfc_cv.fit(train_features, train_target)
 print("RFC GridSearch score: "+str(rfc_cv.best_score_))
print("RFC GridSearch params: ")
 print(rfc_cv.best_params_ )
X_train = dftrain[idy]
y_train = dftrain['fare_amount']
X_test = dftest[idy]<import_modules>
gbcgs_parameters = {
'loss' : ["deviance","exponential"],
'n_estimators' : [N_ESTIMATORS],
'learning_rate': [0.02,0.03,0.04,0.05,0.06],
'max_depth': [2,3,4],
'max_features': [2,3,4],
"min_samples_split": [2,3,4],
'min_samples_leaf': [2,3,4]
}
if not DEBUG_MODE:
gbc_cv = GridSearchCV(GradientBoostingClassifier() , gbcgs_parameters, cv=CV_SPLIT_NUM)
 gbc_cv.fit(train_features, train_target)
 print("GBC GridSearch score: "+str(gbc_cv.best_score_))
print("GBC GridSearch params: ")
 print(gbc_cv.best_params_ )
<choose_model_class>
svcgs_parameters = {
'kernel': ['rbf'],
'C': [10,20,30,40,50,60,70],
'gamma': [0.005,0.006,0.007,0.008,0.009,0.01,0.011],
'probability': [True]
}
if not DEBUG_MODE:
svc_cv = GridSearchCV(svm.SVC() , svcgs_parameters, cv=CV_SPLIT_NUM)
 svc_cv.fit(train_features, train_target)
 print("SVC GridSearch score: "+str(svc_cv.best_score_))
print("SVC GridSearch params: ")
 print(svc_cv.best_params_ )
<train_model>
if not DEBUG_MODE:
vc = VotingClassifier(estimators=[('rfc', rfc_cv.best_estimator_),('gbc', gbc_cv.best_estimator_),('svm', svc_cv.best_estimator_)], voting='soft', n_jobs=4, weights=[3,1,2])
 vc = vc.fit(train_features, train_target )
<prepare_output><EOS> 
arr = [
{'model': vc, 'filename': 'vc_submission.csv'},
{'model': rfc_cv.best_estimator_, 'filename': 'rfc_submission.csv'},
{'model': gbc_cv.best_estimator_, 'filename': 'gbc_submission.csv'},
{'model': svc_cv.best_estimator_, 'filename': 'svc_submission.csv'}
]
if not DEBUG_MODE:
for v in arr:
survived = v['model'].predict(test_features)
 pred = pd.DataFrame(pd.read_csv(".. /input/test.csv")['PassengerId'])
 pred['Survived'] = survived.astype(int)
 pred.to_csv(".. /working/" + v['filename'], index = False )