Kaggle competition: Titanic - Machine Learning from Disaster
train_dataset.drop(labels='key', axis=1, inplace=True)
 test_dataset.drop(labels='key', axis=1, inplace=True )<data_type_conversions>
train_data=pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head(10 )
train_dataset.passenger_count = train_dataset.passenger_count.astype(dtype = 'uint8' )<data_type_conversions>
test_data=pd.read_csv('/kaggle/input/titanic/test.csv')
 test_data.head()
train_dataset.pickup_longitude = train_dataset.pickup_longitude.astype(dtype = 'float32')
 train_dataset.pickup_latitude = train_dataset.pickup_latitude.astype(dtype = 'float32')
 train_dataset.dropoff_longitude = train_dataset.dropoff_longitude.astype(dtype = 'float32')
 train_dataset.dropoff_latitude = train_dataset.dropoff_latitude.astype(dtype = 'float32')
 train_dataset.fare_amount = train_dataset.fare_amount.astype(dtype = 'float32' )<count_missing_values>
train_data=train_data.drop(['PassengerId', 'Ticket','Cabin'],axis=1)
 test_data=test_data.drop(['PassengerId', 'Ticket','Cabin'],axis=1 )
train_dataset.isnull().sum()<data_type_conversions>
test_data['Fare'].fillna(7,inplace=True)
 test_data.isnull().sum()
train_dataset['pickup_datetime'] = pd.to_datetime(arg=train_dataset['pickup_datetime'], infer_datetime_format=True)
 test_dataset['pickup_datetime'] = pd.to_datetime(arg=test_dataset['pickup_datetime'], infer_datetime_format=True )<feature_engineering>
train_data['Embarked'].fillna('S',inplace=True )
def add_new_date_time_features(dataset):
dataset['hour'] = dataset.pickup_datetime.dt.hour
dataset['day'] = dataset.pickup_datetime.dt.day
dataset['month'] = dataset.pickup_datetime.dt.month
dataset['year'] = dataset.pickup_datetime.dt.year
dataset['day_of_week'] = dataset.pickup_datetime.dt.dayofweek
return dataset
train_dataset = add_new_date_time_features(train_dataset)
 test_dataset = add_new_date_time_features(test_dataset )<count_values>
train_data['Sex']=train_data['Sex'].map({'male':1,'female':0})
 test_data['Sex']=test_data['Sex'].map({'male':1,'female':0} )
train_dataset.fare_amount[(train_dataset.fare_amount <= 0)|(train_dataset.fare_amount >= 350)].count()<define_variables>
def title(x):
if 'Mr.' in x.split() :
return 'Mr'
elif 'Master.' in x.split() :
return 'Master'
elif 'Miss.' in x.split() :
return 'Miss'
elif 'Mrs.' in x.split() :
return 'Mrs'
else:
return 'X'
print(f'Row count before elimination - {train_dataset.shape[0]}')
 train_dataset = train_dataset[train_dataset.fare_amount.between(0, 350, inclusive=False)]
print(f'Row count after elimination - {train_dataset.shape[0]}' )<count_values>
train_data['Title']=train_data['Name'].apply(lambda x:title(x))
test_data['Title']=test_data['Name'].apply(lambda x:title(x))
train_dataset.passenger_count[(train_dataset.passenger_count < 1)|(train_dataset.passenger_count > 8)].count()<define_variables>
train_data['Title'].value_counts()
print(f'Row count before elimination - {train_dataset.shape[0]}')
 train_dataset = train_dataset[train_dataset.passenger_count.between(0, 8, inclusive=False)]
print(f'Row count after elimination - {train_dataset.shape[0]}' )<normalization>
train_data=train_data.drop('Name',axis=1)
 test_data=test_data.drop('Name',axis=1 )
def degree_to_radion(degree):
return degree*(np.pi/180)
 def calculate_distance(pickup_latitude, pickup_longitude, dropoff_latitude, dropoff_longitude):
from_lat = degree_to_radion(pickup_latitude)
 from_long = degree_to_radion(pickup_longitude)
 to_lat = degree_to_radion(dropoff_latitude)
 to_long = degree_to_radion(dropoff_longitude)
 radius = 6371.01
lat_diff = to_lat - from_lat
long_diff = to_long - from_long
a = np.sin(lat_diff / 2)**2 + np.cos(degree_to_radion(from_lat)) * np.cos(degree_to_radion(to_lat)) * np.sin(long_diff / 2)**2
c = 2 * np.arctan2(np.sqrt(a), np.sqrt(1 - a))
return radius * c 
<feature_engineering>
train_data.groupby('Title' ).mean() ['Age']
train_dataset['distance'] = calculate_distance(train_dataset.pickup_latitude, train_dataset.pickup_longitude, train_dataset.dropoff_latitude, train_dataset.dropoff_longitude)
 test_dataset['distance'] = calculate_distance(test_dataset.pickup_latitude, test_dataset.pickup_longitude, test_dataset.dropoff_latitude, test_dataset.dropoff_longitude )<sort_values>
train_data['age'] = train_data.groupby('Title')['Age'].transform(lambda x: x.fillna(x.median()))
test_data['age'] = test_data.groupby('Title')['Age'].transform(lambda x: x.fillna(x.median()))
train_dataset.sort_values(by='distance' )<count_values>
train_data=train_data.drop('Age',axis=1)
 test_data=test_data.drop('Age',axis=1 )
train_dataset.distance[(train_dataset.distance == 0)].count()<filter>
train_data=pd.get_dummies(train_data,drop_first=True)
 test_data=pd.get_dummies(test_data,drop_first=True )
train_dataset[(train_dataset.pickup_latitude != train_dataset.dropoff_latitude)& 
(train_dataset.pickup_longitude != train_dataset.dropoff_latitude)& 
(train_dataset.distance == 0)].count()<feature_engineering>
X=train_data.drop('Survived',axis=1 ).values
y=train_data['Survived'].values
test_data=test_data.values
def add_distances_from_airport(dataset):
jfk_coords =(40.639722, -73.778889)
 ewr_coords =(40.6925, -74.168611)
 lga_coords =(40.77725, -73.872611)
 dataset['pickup_jfk_distance'] = calculate_distance(jfk_coords[0], jfk_coords[1], dataset.pickup_latitude, dataset.pickup_longitude)
 dataset['dropof_jfk_distance'] = calculate_distance(jfk_coords[0], jfk_coords[1], dataset.dropoff_latitude, dataset.dropoff_longitude)
 dataset['pickup_ewr_distance'] = calculate_distance(ewr_coords[0], ewr_coords[1], dataset.pickup_latitude, dataset.pickup_longitude)
 dataset['dropof_ewr_distance'] = calculate_distance(ewr_coords[0], ewr_coords[1], dataset.dropoff_latitude, dataset.dropoff_longitude)
 dataset['pickup_lga_distance'] = calculate_distance(lga_coords[0], lga_coords[1], dataset.pickup_latitude, dataset.pickup_longitude)
 dataset['dropof_lga_distance'] = calculate_distance(lga_coords[0], lga_coords[1], dataset.dropoff_latitude, dataset.dropoff_longitude)
 return dataset
train_dataset = add_distances_from_airport(train_dataset)
 test_dataset = add_distances_from_airport(test_dataset )<filter>
from sklearn.model_selection import train_test_split
train_dataset[(train_dataset.distance>90)&(train_dataset.fare_amount<70)]<prepare_x_and_y>
from sklearn.model_selection import train_test_split
selected_predictors = [
'pickup_longitude',
'pickup_latitude',
'dropoff_longitude',
'dropoff_latitude',
'pickup_jfk_distance',
'dropof_jfk_distance',
'pickup_ewr_distance',
'dropof_ewr_distance',
'pickup_lga_distance',
'dropof_lga_distance',
'hour',
'month',
'year',
'distance'
]
X = train_dataset.loc[:, selected_predictors].values
y = train_dataset.iloc[:, 0].values
X_test_dataset = test_dataset.loc[:, selected_predictors].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=1/20 )<compute_train_metric>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=85 )
rand_forest_regressor = RandomForestRegressor()
rand_forest_regressor.fit(X_train, y_train)
 y_rand_forest_predict = rand_forest_regressor.predict(X_test)
 random_forest_model_error = sqrt(mean_squared_error(np.expm1(y_test), np.expm1(y_rand_forest_predict)))
 print(f' Random Forest Mean Squared Error - {random_forest_model_error}' )<choose_model_class>
scaler=MinMaxScaler()
X_train=scaler.fit_transform(X_train)
 X_test=scaler.transform(X_test)
 test_data=scaler.transform(test_data )
<train_model>
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Dropout
from tensorflow.keras.callbacks import EarlyStopping
XGB_model = XGBRegressor(learning_rate=0.3, max_depth=6, n_estimators=500)
 XGB_model.fit(X_train, y_train)
 y_XGB_predict = XGB_model.predict(X_test)
 XGB_model_error = sqrt(mean_squared_error(np.expm1(y_test), np.expm1(y_XGB_predict)))
 print(f'XGBoost Mean Squared Error - {XGB_model_error}' )<train_model>
max_features = X_train.shape[1]
lgb_model = lgb.LGBMRegressor(objective='regression',num_leaves=35, n_estimators=300)
 lgb_model.fit(X_train, y_train)
 y_LGB_predict = lgb_model.predict(X_test)
 LGB_model_error = sqrt(mean_squared_error(np.expm1(y_test), np.expm1(y_LGB_predict)))
 print(f'LGBM Mean Squared Error - {LGB_model_error}' )<compute_test_metric>
model = Sequential()
model.add(Dense(50, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(25, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(units=1,activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam' )
ensembled_prediction =(0.5*np.expm1(y_XGB_predict)) +(0.5*np.expm1(y_LGB_predict))
ensembled_prediction_error = sqrt(mean_squared_error(np.expm1(y_test), ensembled_prediction))
print(f'Ensembled Mean Squared Error - {ensembled_prediction_error}' )<save_to_csv>
model.fit(x=X_train,
y=y_train,
epochs=80,
validation_data=(X_test, y_test),
)
y_XGB_predict = np.expm1(XGB_model.predict(X_test_dataset))
submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['fare_amount'] = y_XGB_predict
submission.to_csv('xgb_submission.csv', index=False)
 submission.head(10 )<save_to_csv>
loss=pd.DataFrame(model.history.history )
y_LGB_predict = np.expm1(lgb_model.predict(X_test_dataset))
submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['fare_amount'] = y_LGB_predict
submission.to_csv('lgbm_submission.csv', index=False)
 submission.head(10 )<save_to_csv>
predictions=model.predict_classes(test_data )
ensembled_prediction =(0.5*y_XGB_predict)+(0.5*y_LGB_predict)
 submission.to_csv('ensembled_submission.csv', index=False)
 submission.head(10 )<load_from_csv>
predictions=predictions.ravel()
predictions
<define_variables>
test_data1=pd.read_csv('/kaggle/input/titanic/test.csv')
 test_data1.head()
traintypes = {'fare_amount': 'float32',
'pickup_datetime': 'str',
'pickup_longitude': 'float32',
'pickup_latitude': 'float32',
'dropoff_longitude': 'float32',
'dropoff_latitude': 'float32',
'passenger_count': 'uint8'}
cols = list(traintypes.keys() )<load_from_csv>
output=pd.DataFrame({'PassengerId':test_data1['PassengerId'],'Survived':predictions})
 output.to_csv('my_submission.csv',index=False )