Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<feature_engineering>
%matplotlib inline
scenario_4_sub['H_Distance'] = scenario_4_sub.apply(
 lambda row:(( row['fare_amount']-2.50)/1.56), axis=1
)<drop_column>
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv')
train = train.drop(['key','pickup_datetime'], axis = 1)
 test = test.drop(['key','pickup_datetime'], axis = 1 )<prepare_x_and_y>
passenger_id = test.PassengerId
x_train = train.iloc[:,train.columns!='fare_amount']
y_train = train['fare_amount'].values
x_test = test<import_modules>
train_df['Name_Len'] = train_df['Name'].apply(lambda x: len(x))
train_df['Survived'].groupby(pd.qcut(train_df['Name_Len'],5)).mean()
import lightgbm as lgbm<init_hyperparams>
def names(train, test):
for i in [train, test]:
i['Name_Len'] = i['Name'].apply(lambda x: len(x))
i['Name_Title'] = i['Name'].apply(lambda x: x.split(',')[1] ).apply(lambda x: x.split() [0])
 del i['Name']
return train, test
def age_impute(train, test):
for i in [train, test]:
i['Age_Null_Flag'] = i['Age'].apply(lambda x: 1 if pd.isnull(x)else 0)
 data = train.groupby(['Name_Title', 'Pclass'])['Age']
i['Age'] = data.transform(lambda x: x.fillna(x.mean()))
return train, test
def fam_size(train, test):
for i in [train, test]:
i['Fam_Size'] = np.where(( i['SibSp']+i['Parch'])== 0 , 'Solo',
np.where(( i['SibSp']+i['Parch'])<= 3,'Nuclear', 'Big'))
del i['SibSp']
del i['Parch']
return train, test
def ticket_grouped(train, test):
for i in [train, test]:
i['Ticket_Lett'] = i['Ticket'].apply(lambda x: str(x)[0])
 i['Ticket_Lett'] = i['Ticket_Lett'].apply(lambda x: str(x))
i['Ticket_Lett'] = np.where(( i['Ticket_Lett'] ).isin(['1', '2', '3', 'S', 'P', 'C', 'A']), i['Ticket_Lett'],
np.where(( i['Ticket_Lett'] ).isin(['W', '4', '7', '6', 'L', '5', '8']),
'Low_ticket', 'Other_ticket'))
i['Ticket_Len'] = i['Ticket'].apply(lambda x: len(x))
del i['Ticket']
return train, test
def cabin(train, test):
for i in [train, test]:
i['Cabin_Letter'] = i['Cabin'].apply(lambda x: str(x)[0])
 del i['Cabin']
return train, test
def cabin_num(train, test):
for i in [train, test]:
i['Cabin_num1'] = i['Cabin'].apply(lambda x: str(x ).split(' ')[-1][1:])
 i['Cabin_num1'].replace('an', np.NaN, inplace = True)
 i['Cabin_num1'] = i['Cabin_num1'].apply(lambda x: int(x)if not pd.isnull(x)and x != '' else np.NaN)
 i['Cabin_num'] = pd.qcut(train['Cabin_num1'],3)
 train = pd.concat(( train, pd.get_dummies(train['Cabin_num'], prefix = 'Cabin_num')) , axis = 1)
 test = pd.concat(( test, pd.get_dummies(test['Cabin_num'], prefix = 'Cabin_num')) , axis = 1)
 del train['Cabin_num']
del test['Cabin_num']
del train['Cabin_num1']
del test['Cabin_num1']
return train, test
def embarked_impute(train, test):
for i in [train, test]:
i['Embarked'] = i['Embarked'].fillna('S')
 return train, test
params = {
'boosting_type':'gbdt',
'objective': 'regression',
'nthread': -1,
'verbose': 0,
'num_leaves': 31,
'learning_rate': 0.05,
'max_depth': -1,
'subsample': 0.8,
'subsample_freq': 1,
'colsample_bytree': 0.6,
'reg_aplha': 1,
'reg_lambda': 0.001,
'metric': 'rmse',
'min_split_gain': 0.5,
'min_child_weight': 1,
'min_child_samples': 10,
'scale_pos_weight':1
}<create_dataframe>
test['Fare'].fillna(train['Fare'].mean() , inplace = True )
train_set = lgbm.Dataset(x_train, y_train, silent=True)
 train_set<train_model>
def dummies(train, test, columns = ['Pclass', 'Sex', 'Embarked', 'Ticket_Lett', 'Cabin_Letter', 'Name_Title', 'Fam_Size']):
for column in columns:
train[column] = train[column].apply(lambda x: str(x))
test[column] = test[column].apply(lambda x: str(x))
good_cols = [column+'_'+i for i in train[column].unique() if i in test[column].unique() ]
train = pd.concat(( train, pd.get_dummies(train[column], prefix = column)[good_cols]), axis = 1)
 test = pd.concat(( test, pd.get_dummies(test[column], prefix = column)[good_cols]), axis = 1)
 del train[column]
del test[column]
return train, test
model = lgbm.train(params, train_set = train_set, num_boost_round=300 )<predict_on_test>
def drop(train, test, bye = ['PassengerId']):
for i in [train, test]:
for z in bye:
del i[z]
return train, test
pred_test_y = model.predict(x_test, num_iteration = model.best_iteration )<save_to_csv>
train, test = names(train, test)
 train, test = age_impute(train, test)
 train, test = cabin_num(train, test)
 train, test = cabin(train, test)
 train, test = embarked_impute(train, test)
 train, test = fam_size(train, test)
 test['Fare'].fillna(train['Fare'].mean() , inplace = True)
 train, test = ticket_grouped(train, test)
 train, test = dummies(train, test, columns = ['Pclass', 'Sex', 'Embarked', 'Ticket_Lett',
'Cabin_Letter', 'Name_Title', 'Fam_Size'])
 train, test = drop(train, test )
submission = pd.read_csv('.. /input/new-york-city-taxi-fare-prediction/sample_submission.csv')
 submission['fare_amount'] = pred_test_y
submission.to_csv('s.csv', index=False)
 submission.head(20 )<feature_engineering>
rf = RandomForestClassifier(max_features='auto', oob_score=True, random_state=1, n_jobs=-1)
 param_grid = { "criterion" : ["entropy"],
"min_samples_leaf" : [6, 4, 2],
"min_samples_split" : [12],
"n_estimators": [825, 800, 775, 750]}
gs = GridSearchCV(estimator=rf, param_grid=param_grid, scoring='accuracy', cv=3, n_jobs=-1)
 gs = gs.fit(train.iloc[:, 1:], train.iloc[:, 0])
 print(gs.best_params_ )
warnings.filterwarnings("ignore")
 def preparedataset(datasetname):
datasetname['pickup_year']=0
datasetname['pickup_month']=0
datasetname['pickup_day']=0
datasetname['pickup_hour']=0
datasetname['dis'] =0
datasetname['x_dis']=0
datasetname['y_dis']=0
datasetname.head()
for k in range(len(datasetname.index)) :
datetime=dt.strptime(datasetname['pickup_datetime'][k].replace("UTC",''),"%Y-%m-%d %H:%M:%S ")
 datasetname['pickup_year'][k]=datetime.year
datasetname['pickup_month'][k]=datetime.month
datasetname['pickup_day'][k]=datetime.day
datasetname['pickup_hour'][k]=datetime.hour
datasetname['x_dis'] =(datasetname['dropoff_longitude'] - datasetname['pickup_longitude'])
 datasetname['y_dis'] =(datasetname['dropoff_latitude'] - datasetname['pickup_latitude'])
 datasetname['dis'] =(( datasetname['dropoff_longitude'] - datasetname['pickup_longitude'])**2 +(datasetname['dropoff_latitude']-datasetname['pickup_latitude'])**2)**.5
datasetname=datasetname.drop(['pickup_datetime'],axis=1)
 datasetname=datasetname.drop(['pickup_longitude'],axis=1)
 datasetname=datasetname.drop(['dropoff_latitude'],axis=1)
 datasetname=datasetname.drop(['dropoff_longitude'],axis=1)
 datasetname=datasetname.drop(['pickup_latitude'],axis=1)
 return datasetname 
<prepare_output>
rf = RandomForestClassifier(criterion='entropy',
n_estimators=775,
min_samples_split=12,
min_samples_leaf=2,
max_features='auto',
oob_score=True,
random_state=2,
n_jobs=-1)
 rf.fit(train.iloc[:, 1:], train.iloc[:, 0])
 Y_pred = rf.predict(test)
 print("%.4f" % rf.oob_score_ )
<save_to_csv><EOS> 
output = pd.DataFrame({'PassengerId': passenger_id, 'Survived': Y_pred})
 output.to_csv('my_submission.csv', index=False )