Kaggle competition: Titanic - Machine Learning from Disaster
train = load_df(10000000, features='model')
 test['pickup_datetime'] = test['pickup_datetime'].str.slice(0, 15)
 test['pickup_datetime'] = pd.to_datetime(test['pickup_datetime'], utc=True, format='%Y-%m-%d %H:%M')
 test = modelling_features(test)
 train =(train 
.query(f'{test.pickup_longitude.min() -0.1} <= pickup_longitude <= {test.pickup_longitude.max() +0.1}')
.query(f'{test.pickup_latitude.min() -0.1} <= pickup_latitude <= {test.pickup_latitude.max() +0.1}')
.query(f'{test.dropoff_longitude.min() -0.1} <= dropoff_longitude <= {test.dropoff_longitude.max() +0.1}')
.query(f'{test.dropoff_latitude.min() -0.1} <= dropoff_latitude <= {test.dropoff_latitude.max() +0.1}')
)
 x_train, x_val, y_train, y_val = get_split_sets(train)
 x_train['fare_per_km'] = y_train*1000/(x_train.distance+5)
 fares_by_direction = x_train.query('5 < distance' ).groupby('direction_bucket')['fare_per_km'].mean()
x_train['adj_dist'] = [fares_by_direction[i] for i in x_train.direction_bucket]*x_train.distance/fares_by_direction.max()
x_val['adj_dist'] = [fares_by_direction[i] for i in x_val.direction_bucket]*x_val.distance/fares_by_direction.max()
test['adj_dist'] = [fares_by_direction[i] for i in test.direction_bucket]*test.distance/fares_by_direction.max()
x_train = x_train.drop(columns=['fare_per_km', 'direction_bucket'])
 x_val = x_val.drop(columns=['direction_bucket'])
 x_test = test.drop(columns=['key', 'direction_bucket'] )<compute_test_metric>
from sklearn import preprocessing
from tensorflow.python import keras
from sklearn.impute import SimpleImputer
import tensorflow as tf
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Dropout
lin_final_model, lin_final_rmse, lin_final_pred = lin_model(x_train, x_val, y_train, y_val )<find_best_params>
train_data = '/kaggle/input/titanic/train.csv'
test_data = "/kaggle/input/titanic/test.csv"
knn_cols = ['pickup_longitude','pickup_latitude','dropoff_longitude','dropoff_latitude']
k_choices = [18,24,30,40]
knn_final_model, knn_final_rmse, knn_final_pred = knn_model(x_train[knn_cols], x_val[knn_cols], y_train, y_val, k_choices )<train_on_grid>
train_panda = pd.read_csv(train_data, index_col = "PassengerId")
 train_panda.head()
lgbm_params = {
'objective': 'regression',
'boosting': 'gbdt',
'reg_sqrt': True,
'learning_rate': 0.03,
'num_leaves': 1200,
'max_depth': -1,
'max_bin': 5000,
'num_rounds': 5000,
'early_stopping_round': 50,
'metric': 'rmse'
}
lgbm_final_model, lgbm_final_rmse, lgbm_final_pred = lgbm_model(lgbm_params, x_train, x_val, y_train, y_val )<train_model>
test_panda = pd.read_csv(test_data)
 test_panda.head()
print('Linear Regression RMSE', lin_final_rmse)
 print('KNN RMSE', knn_final_rmse)
 print('LightGBM RMSE', lgbm_final_rmse )<compute_test_metric>
encoder = preprocessing.LabelEncoder()
d = {}
for a in np.linspace(0,1,101):
final_preds_ave =(lgbm_final_pred*(1-a)+ knn_final_pred * a)
 rmse = np.sqrt(mean_squared_error(y_val, final_preds_ave))
d[a] = rmse
alpha = min(d, key=d.get)
 print('Best weight to give KNN: ', alpha )<save_to_csv>
cat_features = ["Sex", "Embarked"]
lgbm_test_pred = lgbm_final_model.predict(x_test, num_iteration=lgbm_final_model.best_iteration)
 knn_test_pred = knn_final_model.predict(x_test[['pickup_longitude','pickup_latitude','dropoff_longitude','dropoff_latitude']])
 submission_pred =(lgbm_test_pred*(1-alpha)+ knn_test_pred * alpha)
 submission = pd.DataFrame({'key': test.key, 'fare_amount': submission_pred})
 submission.to_csv('submission_10_10_20_comb.csv', index=False )<load_from_csv>
train_panda["Embarked"] = train_panda["Embarked"].astype(str )
train = pd.read_csv(".. /input/new-york-city-taxi-fare-prediction/train.csv", nrows = 1000000)
 test = pd.read_csv(".. /input/new-york-city-taxi-fare-prediction/test.csv" )<sort_values>
encoded_train = train_panda[cat_features].apply(encoder.fit_transform)
 encoded_test = test_panda[cat_features].apply(encoder.fit_transform )
train.isnull().sum().sort_values(ascending=False )<sort_values>
num_features = ["Survived","Pclass","Age","SibSp","Parch","Fare"]
test_features= ["Pclass","Age","SibSp","Parch","Fare"]
test.isnull().sum().sort_values(ascending=False )<drop_column>
training_data = train_panda[num_features].join(encoded_train)
 test_data = test_panda[test_features].join(encoded_test )
train = train.drop(train[train.isnull().any(1)].index, axis = 0 )<drop_column>
training_data.isnull().sum()
train = train.drop(train[train['fare_amount']<0].index, axis=0)
 train.shape<define_variables>
test_data.isnull().sum()
train[train['passenger_count']>6]<drop_column>
my_imputer = SimpleImputer()
imputed_train = pd.DataFrame(my_imputer.fit_transform(training_data))
imputed_test_data = pd.DataFrame(my_imputer.fit_transform(test_data))
train = train.drop(train[train['passenger_count']==208].index, axis = 0 )<filter>
imputed_train.columns = training_data.columns
imputed_test_data.columns = test_data.columns
train[train['pickup_latitude']<-90]<filter>
X = imputed_train.drop("Survived", axis = 1)
 y = imputed_train["Survived"]
train[train['pickup_latitude']>90]<drop_column>
tf.random.set_seed(42 )
train = train.drop(((train[train['pickup_latitude']<-90])|(train[train['pickup_latitude']>90])).index, axis=0 )<filter>
model = Sequential()
train[train['pickup_longitude']<-180]<filter>
model.add(Dense(7, activation = "relu", input_shape =(7,)))
 model.add(Dropout(0.2))
train[train['pickup_longitude']>180]<drop_column>
model.add(Dense(50, activation = "relu"))
model.add(Dropout(0.2))
model.add(Dense(50, activation = "relu"))
train = train.drop(((train[train['pickup_longitude']<-180])|(train[train['pickup_longitude']>180])).index, axis=0 )<filter>
model.add(Dense(2, activation = "softmax"))
train[train['dropoff_latitude']<-90]<filter>
model.compile(loss=keras.losses.sparse_categorical_crossentropy, optimizer = "adam", metrics = ['accuracy'] )
train[train['dropoff_latitude']>90]<drop_column>
model.fit(X, y,
batch_size=1,
epochs=700,
validation_split = 0.2,
verbose = 0 )
train = train.drop(((train[train['dropoff_latitude']<-90])|(train[train['dropoff_latitude']>90])).index, axis=0 )<filter>
preds = model.predict(imputed_test_data )
train[train['dropoff_latitude']<-180]|train[train['dropoff_latitude']>180]<data_type_conversions>
predictions = np.array(model.predict(imputed_test_data)).argmax(axis=1 )
train['key'] = pd.to_datetime(train['key'])
 train['pickup_datetime'] = pd.to_datetime(train['pickup_datetime'] )<data_type_conversions>
passenger_id=test_panda["PassengerId"]
results=passenger_id.to_frame()
results["Survived"]=predictions
<install_modules><EOS> 
results.to_csv("Titanic_ai_model.csv", index=False )