Kaggle competition: Titanic - Machine Learning from Disaster
train['country_region'].value_counts()<filter>
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_data = pd.read_csv('/kaggle/input/titanic/test.csv' )
train[train['country_region']=='Singapore']<categorify>
full_train = train_data.drop(['Name', 'Parch', 'Ticket'], axis = 1)
 full_train.head()
train['province_state'] = train['province_state'].fillna(value = 'nil')
 train.head()<data_type_conversions>
full_train = full_train[pd.notnull(full_train['Embarked'])]
test['province_state'] = test['province_state'].fillna(value = 'nil')
 test.head()<remove_duplicates>
full_train['Cabin'] = full_train['Cabin'].where(full_train['Cabin'].isna() , 1)
 full_train['Cabin'] = full_train['Cabin'].fillna(0)
 full_train['Cabin'].head()
ps_cr_unique = train[['province_state', 'country_region']].drop_duplicates()
ps_cr_unique<define_variables>
s =(full_train.dtypes == 'object')
 object_cols = list(s[s].index)
 OH_encoder = OneHotEncoder(handle_unknown='ignore', sparse=False)
 OH_cols = pd.DataFrame(OH_encoder.fit_transform(full_train[object_cols]))
OH_cols.index = full_train.index
num = full_train.drop(object_cols, axis=1)
 OH = pd.concat([num, OH_cols], axis=1)
 OH.columns
date_max_train = train[(train['province_state']=='nil')& 
(train['country_region']=='Singapore')]['date'].max()
date_max_test = test[(test['province_state']=='nil')& 
(test['country_region']=='Singapore')]['date'].max()
pred_days =(date_max_test - date_max_train ).days
print(date_max_train, date_max_test, pred_days )<define_variables>
X = OH.drop('Survived', axis=1)
 y = OH['Survived']
X_train, X_valid, y_train, y_valid = train_test_split(X, y,
train_size=0.8, test_size=0.2,
random_state=0 )
ps = 'nil'
cr = 'Singapore'<filter>
model = RandomForestRegressor(n_estimators=1500, random_state=0)
 model.fit(X_train, y_train)
 preds = model.predict(X_valid)
 mae = mean_absolute_error(y_valid, preds)
 print(mae )
train_sgp = train[(train['province_state']==ps)&(train['country_region']==cr)]
train_sgp[-5:]<predict_on_test>
test_data = test_data.drop(['Name', 'Parch','Ticket'], axis = 1)
 test_data['Cabin'] = test_data['Cabin'].where(test_data['Cabin'].isna() , 1)
 test_data['Cabin'] = test_data['Cabin'].fillna(0)
 OH_cols = pd.DataFrame(OH_encoder.fit_transform(test_data[object_cols]))
OH_cols.index = test_data.index
num = test_data.drop(object_cols, axis=1)
 OH_test_data = pd.concat([num, OH_cols], axis=1)
 OH_test_data.fillna(OH_test_data.mean() , inplace = True )
preds = get_preds_lin_reg(train_sgp['confirmedcases'][-N:], 0, pred_days)
 preds<prepare_output>
prediction = model.predict(OH_test_data)
 test_data['Survived'] = prediction
submission = test_data[['PassengerId','Survived']]
submission['Survived'].values[submission['Survived'] >= 0.7] = 1
submission['Survived'].values[submission['Survived'] < 0.7] = 0
submission.Survived = submission.Survived.astype(int)
 submission.to_csv('titanic_randomforest.csv', index=False)
 submission.info()