Kaggle competition: Titanic - Machine Learning from Disaster
init_preds_ave =(lgbm_init_pred+knn_init_pred)/2
rmse = np.sqrt(mean_squared_error(y_val, init_preds_ave))
print('Combined RMSE: ', rmse )<prepare_x_and_y>
import matplotlib.pyplot as plt
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import LabelEncoder
def distance(lon1,lat1,lon2,lat2):
az12,az21,dist = Geod(ellps='WGS84' ).inv(lon1,lat1,lon2,lat2)
 return dist
def direction(lon1,lat1,lon2,lat2):
az12,az21,dist = Geod(ellps='WGS84' ).inv(lon1,lat1,lon2,lat2)
 return az12
def shared_features(df):

 rows = len(df)
 nyc_long, nyc_lat = [-74.001541]*rows, [40.724944]*rows
jfk_long, jfk_lat = [-73.785937]*rows, [40.645494]*rows
lga_long, lga_lat = [-73.872067]*rows, [40.774071]*rows
nla_long, nla_lat = [-74.177721]*rows, [40.690764]*rows
chp_long, chp_lat = [-73.137393]*rows, [41.366138]*rows
exp_long, exp_lat = [-74.0375]*rows, [40.736]*rows
pickup_long = df.pickup_longitude.tolist()
pickup_lat = df.pickup_latitude.tolist()
dropoff_long = df.dropoff_longitude.tolist()
dropoff_lat = df.dropoff_latitude.tolist()
df = df.assign(
 year=df.pickup_datetime.dt.year,
dayofyear=df.pickup_datetime.dt.dayofyear,
weekday=df.pickup_datetime.dt.dayofweek,
time=(df.pickup_datetime.dt.hour+df.pickup_datetime.dt.minute/5),
distance=distance(pickup_long, pickup_lat, dropoff_long, dropoff_lat),
direction=direction(pickup_long, pickup_lat, dropoff_long, dropoff_lat),
pickup_dist_nyc=pd.Series(distance(pickup_long, pickup_lat, nyc_long, nyc_lat)) ,
dropoff_dist_nyc=pd.Series(distance(dropoff_long, dropoff_lat, nyc_long, nyc_lat)) ,
pickup_dist_jfk=pd.Series(distance(pickup_long, pickup_lat, jfk_long, jfk_lat)) ,
dropoff_dist_jfk=pd.Series(distance(dropoff_long, dropoff_lat, jfk_long, jfk_lat)) ,
pickup_dist_lga=pd.Series(distance(pickup_long, pickup_lat, lga_long, lga_lat)) ,
dropoff_dist_lga=pd.Series(distance(dropoff_long, dropoff_lat, lga_long, lga_lat)) ,
pickup_dist_nla=pd.Series(distance(pickup_long, pickup_lat, nla_long, nla_lat)) ,
dropoff_dist_nla=pd.Series(distance(dropoff_long, dropoff_lat, nla_long, nla_lat)) ,
pickup_dist_chp=pd.Series(distance(pickup_long, pickup_lat, chp_long, chp_lat)) ,
dropoff_dist_chp=pd.Series(distance(dropoff_long, dropoff_lat, chp_long, chp_lat)) ,
pickup_dist_exp=pd.Series(distance(pickup_long, pickup_lat, exp_long, exp_lat)) ,
dropoff_dist_exp=pd.Series(distance(dropoff_long, dropoff_lat, exp_long, exp_lat)) 
)
 return df
def exploration_features(df):

 df = shared_features(df)
 df =(
 df 
.assign(
 hour=df.pickup_datetime.dt.hour,
close_to_airport='No',
fare_per_km=df.fare_amount*1000/df.distance,
direction_bucket = pd.cut(df.direction, np.linspace(-180, 180, 37)) ,
pickup_long_bucket=pd.cut(df.pickup_longitude, bins=2550, labels=False),
pickup_lat_bucket=pd.cut(df.pickup_latitude, bins=2200, labels=False),
dropoff_long_bucket=pd.cut(df.dropoff_longitude, bins=2550, labels=False),
dropoff_lat_bucket=pd.cut(df.dropoff_latitude, bins=2200, labels=False),
pickup_long_bucket_big=pd.cut(df.pickup_longitude, bins=255, labels=False),
pickup_lat_bucket_big=pd.cut(df.pickup_latitude, bins=220, labels=False),
dropoff_long_bucket_big=pd.cut(df.dropoff_longitude, bins=255, labels=False),
dropoff_lat_bucket_big=pd.cut(df.dropoff_latitude, bins=220, labels=False)
)
.drop(columns='pickup_datetime')
.query("0 < distance")
)
 df.loc[(( df['pickup_dist_jfk']<1500)|(df['dropoff_dist_jfk']<1500)) , 'close_to_airport'] = 'JFK'
df.loc[(( df['pickup_dist_lga']<1500)|(df['dropoff_dist_lga']<1500)) , 'close_to_airport'] = 'LaGuardia'
df.loc[(( df['pickup_dist_nla']<1500)|(df['dropoff_dist_nla']<1500)) , 'close_to_airport'] = 'Newark'
return df<load_pretrained>
df_train = pd.read_csv('/kaggle/input/titanic/train.csv')
 df_train.head(10 )
train = load_df(5000000, features='explore' )<statistical_test>
df_test = pd.read_csv('/kaggle/input/titanic/test.csv')
 df_test.head(10 )
ks = scipy.stats.ks_2samp(
 train.where(train.distance > 50 ).dropna() ['fare_amount'],
train.where(train.distance <= 50 ).dropna() ['fare_amount'] 
)
 print('p-value:', ks[1] )<count_values>
print('shape of the training data {}'.format(df_train.shape))
print('The number of passengers who survived {}'.format(np.array(df_train['Survived'].value_counts() [1])))
 print('The number of passengers who didn't survived {}'.format(np.array(df_train['Survived'].value_counts() [1])))
 print('There were {} men while {} women on Titanic'.format(df_train['Sex'].value_counts() [0], df_train['Sex'].value_counts() [1]))
print('Average age of passengers was {}'.format(df_train['Age'].mean()))
print('Average age of female passengers was {} while that of men was {}'.format(df_train.groupby('Sex')['Age'].mean() [0],\
df_train.groupby('Sex')['Age'].mean() [1]))
print()
print()
print("Checking for NULL values in each column")
 print(df_train.isnull().sum())
 df_train.groupby('Embarked')['Survived'].value_counts()
long_trips = train[train.distance>75000].fare_amount.count()
print(long_trips, 'trips over 75km.' )<feature_engineering>
df_train['Age'].fillna(value= df_train['Age'].mean() , inplace=True)
 df_train['Embarked'].fillna(method='ffill', inplace=True)
 df_test['Age'].fillna(value= df_test['Age'].mean() , inplace=True)
 df_test['Fare'].fillna(value= df_test['Fare'].mean() , inplace=True)
 sex = LabelEncoder()
embark = LabelEncoder()
df_train['Sex'] = sex.fit_transform(df_train['Sex'])
 df_test['Sex'] = sex.fit_transform(df_test['Sex'])
 df_train['Embarked'] = embark.fit_transform(df_train['Embarked'])
 df_test['Embarked'] = embark.fit_transform(df_test['Embarked'])
 features = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']
X_train = df_train[features]
y_train = df_train['Survived']
X_test = df_test[features]
def modelling_features(df):
df = shared_features(df)
 df = df.assign(
 sin_time=np.sin(2*np.pi*df['time']/24),
cos_time=np.cos(2*np.pi*df['time']/24),
sin_direction=np.sin(2*np.pi*df['direction']/360),
cos_direction=np.cos(2*np.pi*df['direction']/360),
sin_dayofyear=np.sin(2*np.pi*df['dayofyear']/365),
cos_dayofyear=np.cos(2*np.pi*df['dayofyear']/365),
sin_weekday=np.sin(2*np.pi*df['weekday']/6),
cos_weekday=np.cos(2*np.pi*df['weekday']/6),
direction_bucket=pd.cut(df['direction'], bins=37, labels=False)
 ).drop(columns=['pickup_datetime', 'time', 'direction', 'weekday', 'dayofyear'])
 return df<feature_engineering>
rfc = RandomForestClassifier(max_depth = 8 , n_estimators = 125, random_state= 4)
 rfc = rfc.fit(X_train, y_train)
 predict = rfc.predict(X_test)
 output = pd.DataFrame({'PassengerId': df_test.PassengerId, 'Survived': predict})
 output.to_csv('my_submission.csv', index=False )