Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<save_to_csv>
%matplotlib inline
submission.to_csv('submission.csv', index=False )<set_options>
titanic_filepath =('.. /input/titanic/train.csv')
 titanic_data= pd.read_csv(titanic_filepath)
 test_filepath =('.. /input/titanic/test.csv')
 test_data=pd.read_csv(test_filepath )
pio.templates.default = "plotly_dark" 
<load_from_csv>
features=['Sex','Fare', 'Pclass','Parch','SibSp']
train_df = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/train.csv')
 test_df = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/test.csv')
 submission = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/submission.csv' )<count_missing_values>
x=titanic_data[features]
test_x=test_data[features]
train_df.isna().sum()<count_missing_values>
y=titanic_data.Survived
test_df.isna().sum()<data_type_conversions>
cleanup_nums = {"Sex": {"male": 1, "female": 2}}
cleanup_nums2 = {"Embarked": {"S": 1, "C": 2, "Q": 3}}
x.head()
all_data = pd.concat([train_df,test_df],axis=0,sort=False)
 all_data['Province_State'].fillna("None", inplace=True)
 all_data['ConfirmedCases'].fillna(0, inplace=True)
 all_data['Fatalities'].fillna(0, inplace=True)
 all_data['Id'].fillna(-1, inplace=True)
 all_data['ForecastId'].fillna(-1, inplace=True)
<categorify>
x=x.fillna(x.mean())
 test_x=test_x.fillna(test_x.mean())
le = LabelEncoder()
all_data['Date'] = pd.to_datetime(all_data['Date'])
 all_data['Day_num'] = le.fit_transform(all_data.Date)
 all_data['Day'] = all_data['Date'].dt.day
all_data['Month'] = all_data['Date'].dt.month
all_data['Year'] = all_data['Date'].dt.year 
<split>
model=XGBClassifier()
model.fit(x,y )
train = all_data[all_data['ForecastId']==-1.0]
test = all_data[all_data['ForecastId']!=-1.0]<categorify>
submission_path =('.. /input/titanic/gender_submission.csv')
 submission= pd.read_csv(submission_path )
train['Province_State'] = le.fit_transform(train['Province_State'])
 train['Country_Region'] = le.fit_transform(train['Country_Region'])
 test['Province_State'] = le.fit_transform(test['Province_State'])
 test['Country_Region'] = le.fit_transform(test['Country_Region'])
 X = train.drop(columns=['Id','ConfirmedCases','Fatalities','Date','ForecastId'],axis=1)
 cases = train.ConfirmedCases
fatalities = train.Fatalities
x_test = test.drop(columns=['Id','ConfirmedCases','Fatalities','Date','ForecastId'],axis=1 )<train_model>
submission['Survived']=model.predict(test_x)
model = XGBRegressor(n_estimators = 1000 , random_state = 0 , max_depth = 15)
 model.fit(X,cases)
 cases_pred = model.predict(x_test)
 model1 = XGBRegressor(n_estimators = 1000 , random_state = 0 , max_depth = 15)
 model1.fit(X,fatalities)
 fatalities_pred = model1.predict(x_test)
<compute_test_metric>
submission['PassengerId']=test_data['PassengerId']
MSE = mean_squared_error(cases.iloc[0:13459],cases_pred)
 RMSE = sqrt(mean_squared_error(cases.iloc[0:13459],cases_pred))
MAE = mean_absolute_error(cases.iloc[0:13459],cases_pred)
 R2 = r2_score(cases.iloc[0:13459],cases_pred)
 print('Mean squared error :', MSE)
 print('Root mean squared error :',RMSE)
 print('Mean absolute error :', MAE)
 print('R squared :',R2 )<save_to_csv>
submission.columns=['PassengerId','Survived']
test_df_predict = test_df.copy()
test_df_predict['Confirmedcase'] = cases_pred
test_df_predict['Fatalities'] = fatalities_pred
test_df_predict = test_df_predict.drop('Province_State',axis=1)
 test_df_predict.to_csv('Forecast_result.csv' )<groupby>
submission.columns=['PassengerId','Survived']
submission.head()
US = test_df_predict[test_df_predict['Country_Region']=='US']
US.groupby('Date')['Confirmedcase','Fatalities'].sum().reset_index()<sort_values>
submission.to_csv('Submission.csv', index=False)