Kaggle competition: Titanic - Machine Learning from Disaster
xgb1 = XGBRegressor(n_estimators = 1500 , random_state = 0 , max_depth = 15)
 xgb1.fit(X_train,y_train_fat )<predict_on_test>
%matplotlib inline
y_pred_fat = xgb1.predict(X_test)
 y_pred_fat = np.around(y_pred_fat,decimals = 0)
 y_pred_fat<save_to_csv>
train=pd.read_csv("/kaggle/input/titanic/train.csv")
 test=pd.read_csv('/kaggle/input/titanic/test.csv')
 train.head(10 )
df_out = pd.DataFrame({'ForecastId': [], 'ConfirmedCases': [], 'Fatalities': []})
 soln = pd.DataFrame({'ForecastId': df_test.ForecastId, 'ConfirmedCases': y_pred, 'Fatalities': y_pred_fat})
 df_out = pd.concat([df_out, soln], axis=0)
 df_out.ForecastId = df_out.ForecastId.astype('int')
 df_out.ConfirmedCases = df_out.ConfirmedCases.astype('int')
 df_out.Fatalities = df_out.Fatalities.astype('int')
 df_out.to_csv('submission.csv', index=False )<load_from_csv>
train.isnull().sum()
test = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/test.csv')
 train = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/train.csv')
 sub = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/submission.csv' )<data_type_conversions>
test.isnull().sum()
train['Date'] = pd.to_datetime(train['Date'], infer_datetime_format=True)
 test['Date'] = pd.to_datetime(test['Date'], infer_datetime_format=True )<data_type_conversions>
all_data = train.append(test )
train.loc[:, 'Date'] = train.Date.dt.strftime('%y%m%d')
 train.loc[:, 'Date'] = train['Date'].astype(int)
 test.loc[:, 'Date'] = test.Date.dt.strftime('%y%m%d')
 test.loc[:, 'Date'] = test['Date'].astype(int )<rename_columns>
all_data['Family_Size'] = all_data['Parch'] + all_data['SibSp']
train['Family_Size'] = all_data['Family_Size'][:891]
test['Family_Size'] = all_data['Family_Size'][891:]
train['Province_State'].fillna('nan', inplace=True)
 test['Province_State'].fillna('nan', inplace=True )<import_modules>
all_data['Last_Name'] = all_data['Name'].apply(lambda x: str.split(x, ",")[0])
 all_data['Fare'].fillna(all_data['Fare'].mean() , inplace=True)
 DEFAULT_SURVIVAL_VALUE = 0.5
all_data['Family_Survival'] = DEFAULT_SURVIVAL_VALUE
for grp, grp_df in all_data[['Survived','Name', 'Last_Name', 'Fare', 'Ticket', 'PassengerId',
'SibSp', 'Parch', 'Age', 'Cabin']].groupby(['Last_Name', 'Fare']):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
all_data.loc[all_data['PassengerId'] == passID, 'Family_Survival'] = 1
elif(smin==0.0):
all_data.loc[all_data['PassengerId'] == passID, 'Family_Survival'] = 0
print("Number of passengers with family survival information:",
all_data.loc[all_data['Family_Survival']!=0.5].shape[0] )
from sklearn.preprocessing import LabelEncoder<define_variables>
for _, grp_df in all_data.groupby('Ticket'):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
if(row['Family_Survival'] == 0)|(row['Family_Survival']== 0.5):
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
all_data.loc[all_data['PassengerId'] == passID, 'Family_Survival'] = 1
elif(smin==0.0):
all_data.loc[all_data['PassengerId'] == passID, 'Family_Survival'] = 0
print("Number of passenger with family/group survival information: "
+str(all_data[all_data['Family_Survival']!=0.5].shape[0]))
train['Family_Survival'] = all_data['Family_Survival'][:891]
test['Family_Survival'] = all_data['Family_Survival'][891:]
s =(train.dtypes == 'object')
 object_cols = list(s[s].index )<categorify>
all_data['Ticket_Frequency'] = all_data.groupby('Ticket')['Ticket'].transform('count')
 train['Ticket_Frequency'] = all_data['Ticket_Frequency'][:891]
test['Ticket_Frequency'] = all_data['Ticket_Frequency'][891:]
label_encoder1 = LabelEncoder()
label_encoder2 = LabelEncoder()
train['Province_State'] = label_encoder1.fit_transform(train['Province_State'])
 test['Province_State'] = label_encoder1.transform(test['Province_State'])
 train['Country_Region'] = label_encoder2.fit_transform(train['Country_Region'])
 test['Country_Region'] = label_encoder2.transform(test['Country_Region'])
<import_modules>
all_data["Fare"] = all_data["Fare"].fillna(test["Fare"].median())
 all_data["Fare_per_one"]=all_data['Fare']/all_data['Ticket_Frequency']
all_data['FareBin'] = pd.qcut(all_data['Fare_per_one'], 16)
 label = LabelEncoder()
all_data['FareBin_Code'] = label.fit_transform(all_data['FareBin'])
 train['FareBin_Code'] = all_data['FareBin_Code'][:891]
test['FareBin_Code'] = all_data['FareBin_Code'][891:]
train.drop(['Fare'], 1, inplace=True)
 test.drop(['Fare'], 1, inplace=True )
from xgboost import XGBRegressor
from sklearn.model_selection import GridSearchCV, train_test_split
from sklearn.metrics import mean_squared_log_error<prepare_x_and_y>
all_data['AgeBin'] = pd.qcut(all_data['Age'], 6)
 label = LabelEncoder()
all_data['AgeBin_Code'] = label.fit_transform(all_data['AgeBin'])
 train['AgeBin_Code'] = all_data['AgeBin_Code'][:891]
test['AgeBin_Code'] = all_data['AgeBin_Code'][891:]
train.drop(['Age'], 1, inplace=True)
 test.drop(['Age'], 1, inplace=True )
X_train = train[['Province_State','Country_Region','Date']]
X_train_with_confirmed = train[['Province_State','Country_Region','Date','ConfirmedCases']]
y_train_full = train[['ConfirmedCases', 'Fatalities']]
y_train_confirmed = train['ConfirmedCases']
y_train_fatal = train['Fatalities']<create_dataframe>
train['Sex'].replace(['male','female'],[0,1],inplace=True)
 test['Sex'].replace(['male','female'],[0,1],inplace=True)
 train.drop(['Name', 'PassengerId', 'SibSp', 'Parch', 'Ticket', 'Cabin',
'Embarked'], axis = 1, inplace = True)
 test.drop(['Name','PassengerId', 'SibSp', 'Parch', 'Ticket', 'Cabin',
'Embarked'], axis = 1, inplace = True )
sub1 = pd.DataFrame()
sub1['ForecastID'] = test['ForecastId']<drop_column>
y = train['Survived']
train_df = train.drop('Survived', 1)
 test_df = test.copy()
del test['ForecastId']<split>
my_train, my_test, my_y, my_res = train_test_split(train_df, y, test_size=0.1, random_state=42 )
<choose_model_class>
model_try = RandomForestClassifier(n_estimators=300, max_depth=5, random_state=42)
 model_try.fit(my_train, my_y)
 preds = model_try.predict(my_test)
 print(accuracy_score(my_res, preds))
xgb = XGBRegressor(
 n_estimators = 500,
max_depth = 20,
learning_rate = 0.1,
)<train_model>
test = pd.read_csv('/kaggle/input/titanic/test.csv' )
<predict_on_test><EOS> 
model = RandomForestClassifier(n_estimators=500, max_depth=5, random_state=42)
 model.fit(train_df, y)
 predictions = model.predict(test_df)
 output = pd.DataFrame({'PassengerId': test.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print(predictions )