Kaggle competition: Titanic - Machine Learning from Disaster
x_train = train_df.iloc[:,train_df.columns!='fare_amount']
y_train = train_df['fare_amount'].values
x_test = test_df<train_model>
!pip install pycaret==1.0
rg=RandomForestRegressor()
rg.fit(x_train,y_train)
 y_predict=rg.predict(x_test)
 y_predict<save_to_csv>
import os
import random
import numpy as np
import pandas as pd
submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['fare_amount'] = y_predict
submission.to_csv('submission_1.csv', index=False)
 submission.head(10 )<import_modules>
def random_seed_initialize(seed=42):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns<load_from_csv>
random_seed_initialize(777 )
train_df = pd.read_csv('.. /input/train.csv', nrows = 5_000_000 )<compute_test_metric>
train_data = pd.read_csv('.. /input/titanic/train.csv')
 test_data = pd.read_csv('.. /input/titanic/test.csv')
 submission_data = pd.read_csv('.. /input/titanic/gender_submission.csv' )
def haversine_np(lon1, lat1, lon2, lat2):

 lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2])
 dlon = lon2 - lon1
dlat = lat2 - lat1
a = np.sin(dlat/2.0)**2 + np.cos(lat1)* np.cos(lat2)* np.sin(dlon/2.0)**2
c = 2 * np.arcsin(np.sqrt(a))
km = 6367 * c
return km<feature_engineering>
from pycaret.classification import *
train_df['distance'] = haversine_np(train_df['pickup_longitude'], train_df['pickup_latitude'],
train_df['dropoff_longitude'], train_df['dropoff_latitude'] )<data_type_conversions>
exp = setup(data=train_data, target='Survived', ignore_features = ['PassengerId', 'Name'], silent=True, session_id=42 )
train_df['pickup_datetime'] = pd.to_datetime(train_df['pickup_datetime'] )<feature_engineering>
fold_start_number = 2
fold_end_number = 30 + 1
except_count = 0
for i in range(fold_start_number, fold_end_number, 1):
try:
blend_models(fold=i, verbose=False)
 except:
except_count += 1
save_experiment('TitanicBlendModelsExperiment')
 experiment = load_experiment('TitanicBlendModelsExperiment' )
train_df['year'] = train_df['pickup_datetime'].dt.year
train_df['month'] = train_df['pickup_datetime'].dt.month
train_df['day'] = train_df['pickup_datetime'].dt.day
train_df['hour'] = train_df['pickup_datetime'].dt.hour
train_df['minute'] = train_df['pickup_datetime'].dt.minute<train_model>
accuracy = []
best_fold_number = 0
best_accuracy = 0
best_model = None
for i in range(fold_end_number - fold_start_number - except_count, 0, -1):
fold_num = len(experiment[-i*2+1]['Accuracy'])- 2
accuracy_mean = experiment[-i*2+1]['Accuracy']['Mean']
model = experiment[-i*2]
if best_accuracy < accuracy_mean:
best_fold_number = fold_num
best_accuracy = accuracy_mean
best_model = model
accuracy.append([fold_num, accuracy_mean])
 print('best fold number:' + str(best_fold_number))
print('best accuracy:' + str(best_accuracy))
print('Old size: %d' % len(train_df))
train_df = train_df.dropna(how = 'any', axis = 'rows')
 print('New size: %d' % len(train_df))<feature_engineering>
predictions = predict_model(best_model, data=test_data)
 predictions.head()
cond = True
for col in {'pickup_latitude', 'pickup_longitude', 'dropoff_latitude', 'dropoff_longitude'}:
cond &= abs(train_df[col] - train_df[col].mean())< 5 
<feature_engineering>
submission_data['Survived'] = round(predictions['Label'] ).astype(int)
 submission_data.to_csv('submission.csv',index=False)
 submission_data.head()