Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<feature_engineering>
import numpy as np
import pandas as pd
df_train = add_new_date_time_features(df_train)
 df_test = add_new_date_time_features(df_test )<feature_engineering>
trainData = pd.read_csv('.. /input/train.csv')
 testData = pd.read_csv(".. /input/test.csv" )
def calculate_abs_different(df):
df['abs_diff_longitude'] =(df.dropoff_longitude - df.pickup_longitude ).abs()
df['abs_diff_latitude'] =(df.dropoff_latitude - df.pickup_latitude ).abs()
calculate_abs_different(df_train)
 calculate_abs_different(df_test )<feature_engineering>
print(trainData.dtypes.sort_values())
 print(testData.dtypes.sort_values() )
def convert_different_miles(df):
df['abs_diff_longitude'] = df.abs_diff_longitude*50
df['abs_diff_latitude'] = df.abs_diff_latitude*69
convert_different_miles(df_train)
 convert_different_miles(df_test )<feature_engineering>
trainData.isnull().sum() [trainData.isnull().sum() >0]
meas_ang = 0.506
def add_distance(df):
df['Euclidean'] =(df.abs_diff_latitude**2 + df.abs_diff_longitude**2)**0.5
df['delta_manh_long'] =(df.Euclidean*np.sin(np.arctan(df.abs_diff_longitude / df.abs_diff_latitude)-meas_ang)).abs()
df['delta_manh_lat'] =(df.Euclidean*np.cos(np.arctan(df.abs_diff_longitude / df.abs_diff_latitude)-meas_ang)).abs()
df['distance'] = df.delta_manh_long + df.delta_manh_lat
df.drop(['abs_diff_longitude', 'abs_diff_latitude','Euclidean', 'delta_manh_long', 'delta_manh_lat'], axis=1, inplace=True)
 add_distance(df_train)
 add_distance(df_test )<prepare_x_and_y>
testData.isnull().sum() [testData.isnull().sum() >0]
def calculate_direction(pickup_lat, pickup_lon, dropoff_lat, dropoff_lon):

 R_earth = 6371
pickup_lat, pickup_lon, dropoff_lat, dropoff_lon = map(np.radians,
[pickup_lat, pickup_lon,
dropoff_lat, dropoff_lon])
 dlat = dropoff_lat - pickup_lat
dlon = pickup_lon - dropoff_lon
a = np.arctan2(np.sin(dlon * np.cos(dropoff_lat)) ,np.cos(pickup_lat)* np.sin(dropoff_lat)- np.sin(pickup_lat)* np.cos(dropoff_lat)* np.cos(dlon))
return a<feature_engineering>
trainData.Age=trainData.Age.fillna(trainData.Age.mean())
 testData.Age=testData.Age.fillna(trainData.Age.mean())
 trainData.Fare=trainData.Fare.fillna(trainData.Fare.mean())
 testData.Fare=testData.Fare.fillna(trainData.Fare.mean())
 trainData.Embarked=trainData.Embarked.fillna(trainData.Embarked.mode() [0])
 testData.Embarked=testData.Embarked.fillna(trainData.Embarked.mode() [0] )
train_df['direction'] = calculate_direction(train_df['pickup_latitude'].values, train_df['pickup_longitude'].values,
train_df['dropoff_latitude'].values , train_df['dropoff_longitude'].values)
 df_test['direction'] = calculate_direction(df_test['pickup_latitude'].values, df_test['pickup_longitude'].values,
df_test['dropoff_latitude'].values , df_test['dropoff_longitude'].values )<feature_engineering>
trainData.drop(['PassengerId','Name','Cabin','Ticket'],axis=1,inplace=True)
 testData.drop(['PassengerId','Name','Cabin','Ticket'],axis=1,inplace=True )
train_df['pickup_latitude'].apply(lambda x: np.radians(x))
train_df['pickup_longitude'].apply(lambda x: np.radians(x))
train_df['dropoff_latitude'].apply(lambda x: np.radians(x))
train_df['dropoff_longitude'].apply(lambda x: np.radians(x))
df_test['pickup_latitude'].apply(lambda x: np.radians(x))
df_test['pickup_longitude'].apply(lambda x: np.radians(x))
df_test['dropoff_latitude'].apply(lambda x: np.radians(x))
df_test['dropoff_longitude'].apply(lambda x: np.radians(x))<prepare_x_and_y>
combined=pd.concat([trainData, testData], sort=False)
 print(combined.dtypes.sort_values() )
df_train.drop(columns=['pickup_datetime'], inplace=True)
 y = df_train['fare_amount']
df_train = df_train.drop(columns=['fare_amount'] )<split>
length = trainData.shape[0]
combined=pd.concat([trainData, testData], sort=False)
 combined=pd.get_dummies(combined)
 trainData=combined[:length]
testData=combined[length:]
trainData.Survived=trainData.Survived.astype('int' )
x_train,x_test,y_train,y_test = train_test_split(df_train,y,random_state=123,test_size=0.1 )<init_hyperparams>
x=trainData.drop("Survived",axis=1)
 y=trainData['Survived']
xtest=testData.drop("Survived",axis=1 )
params = {
'boosting_type':'gbdt',
'objective': 'regression',
'nthread': 4,
'num_leaves': 31,
'learning_rate': 0.1,
'max_depth': -1,
'subsample': 0.8,
'bagging_fraction' : 1,
'max_bin' : 10000 ,
'bagging_freq': 10,
'metric': 'rmse',
'zero_as_missing': True,
'num_rounds':50000
}<train_model>
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
from sklearn.metrics import mean_absolute_error
train_set = lgbm.Dataset(x_train, y_train, silent=False,categorical_feature=['year','month','day','day_of_week'])
 valid_set = lgbm.Dataset(x_test, y_test, silent=False,categorical_feature=['year','month','day','day_of_week'])
 model = lgbm.train(params, train_set = train_set, num_boost_round=10000,early_stopping_rounds=1000,verbose_eval=500, valid_sets=valid_set )<drop_column>
RF = RandomForestClassifier(random_state=1)
 results = cross_val_score(RF,x,y,scoring='accuracy',cv=5)
 print(results)
 np.mean(results)
test_key = df_test['key']
df_test.drop(columns=["pickup_datetime",'key'], axis=1, inplace=True )<predict_on_test>
RF.fit(x, y)
<save_to_csv><EOS> 
predictions=RF.predict(xtest)
 column_name = pd.read_csv('.. /input/test.csv')
 output=pd.DataFrame({'PassengerId':column_name['PassengerId'],'Survived':predictions})
 output.to_csv('submission.csv', index=False )