Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<data_type_conversions>
import pandas as pd
import numpy as np
import seaborn as sns
from sklearn.preprocessing import OneHotEncoder, LabelEncoder, StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.feature_selection import RFECV, RFE
from sklearn.model_selection import ParameterGrid, GridSearchCV, KFold, cross_val_score
from sklearn.metrics import classification_report
from sklearn.pipeline import Pipeline
import matplotlib.pyplot as plt
import catboost
train['id_x']=train['Date'].astype(str ).values+'_'+train['State'].astype(str ).values+'_'+train['Country'].astype(str ).values+'_'+train['Type'].astype(str ).values
test['id_x']=test['Date'].astype(str ).values+'_'+test['State'].astype(str ).values+'_'+test['Country'].astype(str ).values+'_'+test['Type'].astype(str ).values
raw = pd.concat([train,test], axis=0, sort=False)
 raw['Date'] = pd.to_datetime(raw["Date"])
 raw.sort_values('Date',inplace=True)
 raw.fillna(0, inplace=True)
 Country_State = raw.Country+'_'+raw.State.astype(str)
 raw['Country_State_id'] = Country_State.astype('category' ).cat.codes
raw['Day']=raw['Date'].astype('category' ).cat.codes+1
raw.set_index('Country_State_id', inplace=True)
 raw.Day=raw.Day.astype(np.int32)
 raw.reset_index(inplace=True)
 features = ['id_x','Day','Id','Country_State_id']
train = train.merge(raw[features], on=['id_x'], how='left')
 test = test.merge(raw[features], on=['id_x'], how='left')
 print(train.shape, test.shape )<prepare_x_and_y>
random_state = 101
os.environ['OMP_NUM_THREADS'] = '1'
gc.enable()
features = ['id_x','Day']
X_train = [np.array(train[train.Country_State_id== x][features])for x in list(train.Country_State_id.unique())]
X_test = [np.array(test[test.Country_State_id== x][features])for x in list(train.Country_State_id.unique())]
y_target_c = [np.array(train[train.Country_State_id == x][['Confirmed']])for x in list(train.Country_State_id.unique())]
y_target_f = [np.array(train[train.Country_State_id == x][['Fatalities']])for x in list(train.Country_State_id.unique())]
poly=PolynomialFeatures(5)
 out_ = pd.DataFrame({'id_x': [], 'Confirmed': [], 'Fatalities': []})
 def fit_model(xtrain, xtest, ytrain, ytrain1, idx)-> np.array:
X = xtrain[idx][:,1]
x_test = xtest[idx][:,1]
Y = ytrain[idx]
Y1 = ytrain1[idx]
X_transf=poly.fit_transform(X.reshape(-1,1))
x_test_transf=poly.fit_transform(x_test.reshape(-1,1))
model = Earth()
model.fit(np.array(X_transf),Y)
 conf_p = model.predict(x_test_transf)
 model.fit(X_transf,Y1)
 conf_f = model.predict(x_test_transf)
 res = pd.DataFrame({'id_x':xtest[idx][:,0] , 'Confirmed': conf_p, 'Fatalities': conf_f})
 return res
with ThreadPool(processes=4)as pool:
args = [(X_train, X_test, y_target_c, y_target_f, idx)for idx in test.Country_State_id.unique() ]
out_ = pd.concat(pool.starmap(fit_model, args))
out_ = test[['id_x']].merge(out_, on='id_x', how='left')
 pool.close()
sub = pd.read_csv(".. /input/covid19-global-forecasting-week-4/submission.csv")
 sub_new = sub[["ForecastId"]]
result = pd.concat([out_.reset_index().Confirmed,out_.reset_index().Fatalities,sub_new],axis=1)
 result.columns = ['ConfirmedCases', 'Fatalities', 'ForecastId']
result = result[['ForecastId','ConfirmedCases', 'Fatalities']]
result.to_csv("submission.csv",index=False)
 result.head()<import_modules>
data_train = pd.read_csv(".. /input/titanic/train.csv" ).set_index("PassengerId")
 data_test = pd.read_csv(".. /input/titanic/test.csv" ).set_index("PassengerId")
 data = pd.concat([data_train, data_test] )
import numpy as np
import pandas as pd 
<load_from_csv>
data["Sex"] = OneHotEncoder(drop='if_binary' ).fit_transform(data[["Sex"]] ).toarray().astype("int" )
train = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/train.csv')
 test = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/test.csv')
 train['CRPS']=train.Country_Region+train.Province_State.fillna('')
 test['CRPS']=test.Country_Region+test.Province_State.fillna('')
 train<feature_engineering>
FEATURES_BASELINE = ["Sex", "Pclass"]
train['LConfirmedCases']=np.log1p(train['ConfirmedCases'])
 train['LFatalities']=np.log1p(train['Fatalities'])
 train['LDConfirmedCases']=train.groupby('CRPS')[['LConfirmedCases']].diff()
train['LDFatalities']=train.groupby('CRPS')[['LFatalities']].diff()
train['LConfirmedCases1']=train.groupby('CRPS')[['LConfirmedCases']].shift(1)
 train['LConfirmedCases2']=train.groupby('CRPS')[['LConfirmedCases']].shift(2)
 train['LConfirmedCases3']=train.groupby('CRPS')[['LConfirmedCases']].shift(3)
 train['LConfirmedCases4']=train.groupby('CRPS')[['LConfirmedCases']].shift(4)
 train['LDConfirmedCases1']=train.groupby('CRPS')[['LDConfirmedCases']].shift(1)
 train['LDConfirmedCases2']=train.groupby('CRPS')[['LDConfirmedCases']].shift(2)
 train['LDConfirmedCases3']=train.groupby('CRPS')[['LDConfirmedCases']].shift(3)
 train['LDConfirmedCases4']=train.groupby('CRPS')[['LDConfirmedCases']].shift(4)
 train['LDConfirmedCasesMA']=(train.LDConfirmedCases+train.LDConfirmedCases1+train.LDConfirmedCases2+train.LDConfirmedCases3+train.LDConfirmedCases4)/5
train['LDConfirmedCasesMA1']=train.groupby('CRPS')[['LDConfirmedCasesMA']].shift(1)
 train['LDConfirmedCasesMA2']=train.groupby('CRPS')[['LDConfirmedCasesMA']].shift(2)
 train['LDConfirmedCasesMA3']=train.groupby('CRPS')[['LDConfirmedCasesMA']].shift(3)
 train['LDConfirmedCasesMA4']=train.groupby('CRPS')[['LDConfirmedCasesMA']].shift(4)
 train['LConfirmedCasesMA']=(train.LConfirmedCases+train.LConfirmedCases1+train.LConfirmedCases2+train.LConfirmedCases3+train.LConfirmedCases4)/5
train['LConfirmedCasesMA1']=train.groupby('CRPS')[['LConfirmedCasesMA']].shift(1)
 train['LConfirmedCasesMA2']=train.groupby('CRPS')[['LConfirmedCasesMA']].shift(2)
 train['LConfirmedCasesMA3']=train.groupby('CRPS')[['LConfirmedCasesMA']].shift(3)
 train['LConfirmedCasesMA4']=train.groupby('CRPS')[['LConfirmedCasesMA']].shift(4)
 train['LFatalities1']=train.groupby('CRPS')[['LFatalities']].shift(1)
 train['LFatalities2']=train.groupby('CRPS')[['LFatalities']].shift(2)
 train['LFatalities3']=train.groupby('CRPS')[['LFatalities']].shift(3)
 train['LFatalities4']=train.groupby('CRPS')[['LFatalities']].shift(4)
 train['LDFatalities1']=train.groupby('CRPS')[['LDFatalities']].shift(1)
 train['LDFatalities2']=train.groupby('CRPS')[['LDFatalities']].shift(2)
 train['LDFatalities3']=train.groupby('CRPS')[['LDFatalities']].shift(3)
 train['LDFatalities4']=train.groupby('CRPS')[['LDFatalities']].shift(4)
 train['LDFatalitiesMA']=(train.LDFatalities+train.LDFatalities1+train.LDFatalities2+train.LDFatalities3+train.LDFatalities4)/5
train['LDFatalitiesMA1']=train.groupby('CRPS')[['LDFatalitiesMA']].shift(1)
 train['LDFatalitiesMA2']=train.groupby('CRPS')[['LDFatalitiesMA']].shift(2)
 train['LDFatalitiesMA3']=train.groupby('CRPS')[['LDFatalitiesMA']].shift(3)
 train['LDFatalitiesMA4']=train.groupby('CRPS')[['LDFatalitiesMA']].shift(4)
 train['LFatalitiesMA']=(train.LFatalities+train.LFatalities1+train.LFatalities2+train.LFatalities3+train.LFatalities4)/5
train['LFatalitiesMA1']=train.groupby('CRPS')[['LFatalitiesMA']].shift(1)
 train['LFatalitiesMA2']=train.groupby('CRPS')[['LFatalitiesMA']].shift(2)
 train['LFatalitiesMA3']=train.groupby('CRPS')[['LFatalitiesMA']].shift(3)
 train['LFatalitiesMA4']=train.groupby('CRPS')[['LFatalitiesMA']].shift(4)
 train['serd']=train.groupby('CRPS' ).cumcount()
train.loc[train.ConfirmedCases==0,'days_since_confirmed']=0
train.loc[train.ConfirmedCases>0,'days_since_confirmed']=train[train.ConfirmedCases>0].groupby('CRPS' ).cumcount() 
<choose_model_class>
def title(name):
surname_split = name.split(", ")
 title_split = surname_split[1].split(".")
 title = title_split[0]
return title
lgbm_cc=LGBMRegressor(num_leaves = 85,learning_rate =10**-1.89,n_estimators=100,min_sum_hessian_in_leaf=(10**-4.1),min_child_samples =2,subsample =0.97,subsample_freq=10,
colsample_bytree = 0.68,reg_lambda=10**1.4,random_state=1234,n_jobs=4)
 lgbm_f=LGBMRegressor(num_leaves = 26,learning_rate =10**-1.63,n_estimators=100,min_sum_hessian_in_leaf=(10**-4.04),min_child_samples =14,subsample =0.66,subsample_freq=5,
colsample_bytree = 0.8,reg_lambda=10**1.92,random_state=1234,n_jobs=4)
 oe=OrdinalEncoder()
X=oe.fit_transform(train[['Country_Region','Province_State']].fillna(''))
train['CR']=X[:,0]
train['PS']=X[:,1]
lgbm_cc.fit(train.loc[:,['LDConfirmedCases1','LDConfirmedCases2','LDConfirmedCases3','LDConfirmedCases4',
'LDFatalities1','LDFatalities2','LDFatalities3','LDFatalities4','days_since_confirmed','CR','PS',
'LDConfirmedCasesMA1','LDConfirmedCasesMA2','LDConfirmedCasesMA3','LDConfirmedCasesMA4',
'LDFatalitiesMA1','LDFatalitiesMA2','LDFatalitiesMA3','LDFatalitiesMA4',
'LConfirmedCases1','LConfirmedCases2','LConfirmedCases3','LConfirmedCases4',
'LFatalities1','LFatalities2','LFatalities3','LFatalities4',
'LConfirmedCasesMA1','LConfirmedCasesMA2','LConfirmedCasesMA3','LConfirmedCasesMA4',
'LFatalitiesMA1','LFatalitiesMA2','LFatalitiesMA3','LFatalitiesMA4']],train.LDConfirmedCases,categorical_feature=['CR','PS'])
 lgbm_f.fit(train.loc[:,['LDConfirmedCases1','LDConfirmedCases2','LDConfirmedCases3','LDConfirmedCases4',
'LDFatalities1','LDFatalities2','LDFatalities3','LDFatalities4','days_since_confirmed','CR','PS',
'LDConfirmedCasesMA1','LDConfirmedCasesMA2','LDConfirmedCasesMA3','LDConfirmedCasesMA4',
'LDFatalitiesMA1','LDFatalitiesMA2','LDFatalitiesMA3','LDFatalitiesMA4',
'LConfirmedCases1','LConfirmedCases2','LConfirmedCases3','LConfirmedCases4',
'LFatalities1','LFatalities2','LFatalities3','LFatalities4',
'LConfirmedCasesMA1','LConfirmedCasesMA2','LConfirmedCasesMA3','LConfirmedCasesMA4',
'LFatalitiesMA1','LFatalitiesMA2','LFatalitiesMA3','LFatalitiesMA4','LConfirmedCases','LDConfirmedCases']],train.LDFatalities,categorical_feature=['CR','PS'])
<feature_engineering>
data["Title"] = data.Name.apply(title)
 data['Title'] = data['Title'].replace(['Dona', 'Mlle', 'Ms'], 'Miss')
 data['Title'] = data['Title'].replace(['Lady', 'the Countess', 'Mme'], 'Mrs')
 data['Title'] = data['Title'].replace(['Jonkheer', 'Don', 'Sir', 'Capt', 'Major', 'Col'], 'Mr')
 data['Title'].value_counts()
train['serd']=train.groupby('CRPS' ).cumcount()
trainpred = pd.concat(( train,test[test.Date>train.Date.max() ])).reset_index(drop=True)
 trainpred.sort_values(['Country_Region','Province_State','Date'],inplace=True)
 X=oe.transform(trainpred[['Country_Region','Province_State']].fillna(''))
trainpred['CR']=X[:,0]
trainpred['PS']=X[:,1]
trainpred['serd']=trainpred.groupby('CRPS' ).cumcount()
trainpred.loc[trainpred.ConfirmedCases.isnull() ,'ConfirmedCases']=1
trainpred.loc[trainpred.ConfirmedCases==0,'days_since_confirmed']=0
trainpred.loc[trainpred.ConfirmedCases>0,'days_since_confirmed']=trainpred[trainpred.ConfirmedCases>0].groupby('CRPS' ).cumcount()
trainpred['LConfirmedCases']=np.log1p(trainpred['ConfirmedCases'])
 trainpred['LFatalities']=np.log1p(trainpred['Fatalities'])
 trainpred['LDConfirmedCases']=trainpred.groupby('CRPS')[['LConfirmedCases']].diff()
trainpred['LDFatalities']=trainpred.groupby('CRPS')[['LFatalities']].diff()
for serd in range(train.serd.max() +1,trainpred.serd.max() +1):
print(serd)
 trainpred['LConfirmedCases1']=trainpred.groupby('CRPS')[['LConfirmedCases']].shift(1)
 trainpred['LConfirmedCases2']=trainpred.groupby('CRPS')[['LConfirmedCases']].shift(2)
 trainpred['LConfirmedCases3']=trainpred.groupby('CRPS')[['LConfirmedCases']].shift(3)
 trainpred['LConfirmedCases4']=trainpred.groupby('CRPS')[['LConfirmedCases']].shift(4)
 trainpred['LDConfirmedCases1']=trainpred.groupby('CRPS')[['LDConfirmedCases']].shift(1)
 trainpred['LDConfirmedCases2']=trainpred.groupby('CRPS')[['LDConfirmedCases']].shift(2)
 trainpred['LDConfirmedCases3']=trainpred.groupby('CRPS')[['LDConfirmedCases']].shift(3)
 trainpred['LDConfirmedCases4']=trainpred.groupby('CRPS')[['LDConfirmedCases']].shift(4)
 trainpred['LDConfirmedCasesMA']=(trainpred.LDConfirmedCases+trainpred.LDConfirmedCases1+trainpred.LDConfirmedCases2+trainpred.LDConfirmedCases3+trainpred.LDConfirmedCases4)/5
trainpred['LDConfirmedCasesMA1']=trainpred.groupby('CRPS')[['LDConfirmedCasesMA']].shift(1)
 trainpred['LDConfirmedCasesMA2']=trainpred.groupby('CRPS')[['LDConfirmedCasesMA']].shift(2)
 trainpred['LDConfirmedCasesMA3']=trainpred.groupby('CRPS')[['LDConfirmedCasesMA']].shift(3)
 trainpred['LDConfirmedCasesMA4']=trainpred.groupby('CRPS')[['LDConfirmedCasesMA']].shift(4)
 trainpred['LConfirmedCasesMA']=(trainpred.LConfirmedCases+trainpred.LConfirmedCases1+trainpred.LConfirmedCases2+trainpred.LConfirmedCases3+trainpred.LConfirmedCases4)/5
trainpred['LConfirmedCasesMA1']=trainpred.groupby('CRPS')[['LConfirmedCasesMA']].shift(1)
 trainpred['LConfirmedCasesMA2']=trainpred.groupby('CRPS')[['LConfirmedCasesMA']].shift(2)
 trainpred['LConfirmedCasesMA3']=trainpred.groupby('CRPS')[['LConfirmedCasesMA']].shift(3)
 trainpred['LConfirmedCasesMA4']=trainpred.groupby('CRPS')[['LConfirmedCasesMA']].shift(4)
 trainpred['LFatalities1']=trainpred.groupby('CRPS')[['LFatalities']].shift(1)
 trainpred['LFatalities2']=trainpred.groupby('CRPS')[['LFatalities']].shift(2)
 trainpred['LFatalities3']=trainpred.groupby('CRPS')[['LFatalities']].shift(3)
 trainpred['LFatalities4']=trainpred.groupby('CRPS')[['LFatalities']].shift(4)
 trainpred['LDFatalities1']=trainpred.groupby('CRPS')[['LDFatalities']].shift(1)
 trainpred['LDFatalities2']=trainpred.groupby('CRPS')[['LDFatalities']].shift(2)
 trainpred['LDFatalities3']=trainpred.groupby('CRPS')[['LDFatalities']].shift(3)
 trainpred['LDFatalities4']=trainpred.groupby('CRPS')[['LDFatalities']].shift(4)
 trainpred['LDFatalitiesMA']=(trainpred.LDFatalities+trainpred.LDFatalities1+trainpred.LDFatalities2+trainpred.LDFatalities3+trainpred.LDFatalities4)/5
trainpred['LDFatalitiesMA1']=trainpred.groupby('CRPS')[['LDFatalitiesMA']].shift(1)
 trainpred['LDFatalitiesMA2']=trainpred.groupby('CRPS')[['LDFatalitiesMA']].shift(2)
 trainpred['LDFatalitiesMA3']=trainpred.groupby('CRPS')[['LDFatalitiesMA']].shift(3)
 trainpred['LDFatalitiesMA4']=trainpred.groupby('CRPS')[['LDFatalitiesMA']].shift(4)
 trainpred['LFatalitiesMA']=(trainpred.LFatalities+trainpred.LFatalities1+trainpred.LFatalities2+trainpred.LFatalities3+trainpred.LFatalities4)/5
trainpred['LFatalitiesMA1']=trainpred.groupby('CRPS')[['LFatalitiesMA']].shift(1)
 trainpred['LFatalitiesMA2']=trainpred.groupby('CRPS')[['LFatalitiesMA']].shift(2)
 trainpred['LFatalitiesMA3']=trainpred.groupby('CRPS')[['LFatalitiesMA']].shift(3)
 trainpred['LFatalitiesMA4']=trainpred.groupby('CRPS')[['LFatalitiesMA']].shift(4)
 trainpred.loc[trainpred.serd==serd,'LDConfirmedCases']= lgbm_cc.predict(trainpred.loc[trainpred.serd==serd,['LDConfirmedCases1','LDConfirmedCases2','LDConfirmedCases3','LDConfirmedCases4',
'LDFatalities1','LDFatalities2','LDFatalities3','LDFatalities4','days_since_confirmed','CR','PS',
'LDConfirmedCasesMA1','LDConfirmedCasesMA2','LDConfirmedCasesMA3','LDConfirmedCasesMA4',
'LDFatalitiesMA1','LDFatalitiesMA2','LDFatalitiesMA3','LDFatalitiesMA4',
'LConfirmedCases1','LConfirmedCases2','LConfirmedCases3','LConfirmedCases4',
'LFatalities1','LFatalities2','LFatalities3','LFatalities4',
'LConfirmedCasesMA1','LConfirmedCasesMA2','LConfirmedCasesMA3','LConfirmedCasesMA4',
'LFatalitiesMA1','LFatalitiesMA2','LFatalitiesMA3','LFatalitiesMA4']])
 trainpred.loc[(trainpred.serd==serd)&(trainpred.LDConfirmedCases<0),'LDConfirmedCases']=0
trainpred.loc[trainpred.serd==serd,'LConfirmedCases']=trainpred.loc[trainpred.serd==serd,'LDConfirmedCases']+trainpred.loc[trainpred.serd==serd,'LConfirmedCases1']
trainpred.loc[trainpred.serd==serd,'ConfirmedCases']=np.exp(trainpred.loc[trainpred.serd==serd,'LConfirmedCases'])-1
trainpred.loc[trainpred.serd==serd,'LDFatalities']= lgbm_f.predict(trainpred.loc[trainpred.serd==serd,['LDConfirmedCases1','LDConfirmedCases2','LDConfirmedCases3','LDConfirmedCases4',
'LDFatalities1','LDFatalities2','LDFatalities3','LDFatalities4','days_since_confirmed','CR','PS',
'LDConfirmedCasesMA1','LDConfirmedCasesMA2','LDConfirmedCasesMA3','LDConfirmedCasesMA4',
'LDFatalitiesMA1','LDFatalitiesMA2','LDFatalitiesMA3','LDFatalitiesMA4',
'LConfirmedCases1','LConfirmedCases2','LConfirmedCases3','LConfirmedCases4',
'LFatalities1','LFatalities2','LFatalities3','LFatalities4',
'LConfirmedCasesMA1','LConfirmedCasesMA2','LConfirmedCasesMA3','LConfirmedCasesMA4',
'LFatalitiesMA1','LFatalitiesMA2','LFatalitiesMA3','LFatalitiesMA4','LConfirmedCases','LDConfirmedCases']])
 trainpred.loc[(trainpred.serd==serd)&(trainpred.LDFatalities<0),'LDFatalities']=0
trainpred.loc[trainpred.serd==serd,'LFatalities']=trainpred.loc[trainpred.serd==serd,'LDFatalities']+trainpred.loc[trainpred.serd==serd,'LFatalities1']
trainpred.loc[trainpred.serd==serd,'Fatalities']=np.exp(trainpred.loc[trainpred.serd==serd,'LFatalities'])-1<save_to_csv>
data["Neighbors"] = neighbors(data["Ticket"] )
trainpred.loc[(trainpred.Date<=max(test.Date)) &(trainpred.Date>=min(test.Date)) ,'ForecastId']=test.loc[:,'ForecastId'].values
submission=trainpred.loc[trainpred.Date>=min(test.Date)][['ForecastId','ConfirmedCases','Fatalities']]
submission.ForecastId=submission.ForecastId.astype('int')
 submission.sort_values('ForecastId',inplace=True)
 submission.to_csv('submission.csv',index=False )<load_from_csv>
print("Passengers with Embarked NaNs")
 print(data[pd.isnull(data.Embarked)])
 embarked_nans_fare = data.loc[(data.Pclass == 1)&(data.Neighbors == 1), "Fare"].median()
print(f"Median fare for this type of passengers: {embarked_nans_fare}" )
warnings.filterwarnings("ignore")
 train = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-4/train.csv")
 test = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-4/test.csv" )<feature_engineering>
data.loc[pd.isnull(data.Embarked), "Embarked"] = "C"
train["Province_State"] = train["Province_State"].fillna('')
 test["Province_State"] = test["Province_State"].fillna('')
 train["Month"], train["Day"] = 0, 0
for i in range(len(train)) :
train["Month"][i] =(train["Date"][i] ).split("-")[1]
train["Day"][i] =(train["Date"][i] ).split("-")[2]
test["Month"], test["Day"] = 0, 0
for i in range(len(test)) :
test["Month"][i] =(test["Date"][i] ).split("-")[1]
test["Day"][i] =(test["Date"][i] ).split("-")[2]<drop_column>
print(data[pd.isnull(data.Age)])
 data.loc[(data["Title"] == "Mr")&(data["Age"] < 12), "Title"] = "Master"
data.Age.fillna(data.groupby(['Title', 'Pclass', 'Sex'] ).transform('median' ).Age, inplace=True )
for i in range(len(train)) :
if train["Province_State"][i] != '':
train["Country_Region"][i] = train["Province_State"][i] + "(" + str(train["Country_Region"][i])+ ")"
for i in range(len(test)) :
if test["Province_State"][i] != '':
test["Country_Region"][i] = test["Province_State"][i] + "(" + str(test["Country_Region"][i])+ ")"
train.drop(columns = "Province_State", inplace=True)
 test.drop(columns = "Province_State", inplace=True)
 train.rename(columns = {"Country_Region" : "Country/State"}, inplace=True)
 test.rename(columns = {"Country_Region" : "Country/State"}, inplace=True)
<feature_engineering>
print(data[(pd.isnull(data["Fare"])) |(data["Fare"] == 0)])
 data.loc[data["Fare"] == 0, "Fare"] = np.NaN
data["Fare"].fillna(data.groupby(['Embarked', 'Pclass', 'Neighbors'] ).transform('median' ).Fare, inplace=True )
i = 0
for value in train["Country/State"].unique() :
if i < len(train):
j = 1
while(train["Country/State"][i] == value):
train["Day"][i] = j
j += 1; i += 1
if i == len(train):
break
i = 0
for value in test["Country/State"].unique() :
if i < len(test):
j = 72
while(test["Country/State"][i] == value):
test["Day"][i] = j
j += 1; i += 1
if i == len(test):
break
train.rename(columns = {"Day" : "No.of days"}, inplace = True)
 test.rename(columns = {"Day" : "No.of days"}, inplace = True )<feature_engineering>
encoder = LabelEncoder()
data["Embarked"] = encoder.fit_transform(data["Embarked"] )
train["Country/State"].loc[train["Country/State"] == "Taiwan*"] = "Taiwan"
test["Country/State"].loc[test["Country/State"] == "Taiwan*"] = "Taiwan"
train = train.drop(columns = ["Date"])
 test = test.drop(columns = ["Date"])
 countriesorstates = train["Country/State"].unique()
print(len(countriesorstates))
print(countriesorstates )<prepare_x_and_y>
FEATURES_1 = ['Sex', 'Pclass', 'Embarked']
random_picks = random.choices(countriesorstates, k = 9)
 for value in random_picks:
train_temp = train.loc[train["Country/State"] == value]
test_temp = test.loc[test["Country/State"] == value]
train_temp_cc = train_temp["ConfirmedCases"].loc[train["Country/State"] == value]
train_temp_ft = train_temp["Fatalities"].loc[train["Country/State"] == value]
train_temp_X = train_temp.iloc[:, 5]
test_temp_X = test_temp.iloc[:, 3]
x = train_temp_X.to_numpy(dtype = float)
 y_1 = train_temp_cc.to_numpy(dtype = float)
 y_2= train_temp_ft.to_numpy(dtype = float)
 x = x.reshape(-1,)
 plt.plot(x, y_1, color ='red', label = value)
 plt.xlabel("No.of days")
 plt.ylabel('Confirmed cases')
 plt.legend()
plt.title("Place: " + value)
 plt.show()
plt.plot(x, y_2, color ='black', label = value)
 plt.xlabel("No.of days")
 plt.ylabel('Fatalities')
 plt.legend()
plt.title("Place: " + value)
 plt.show()<create_dataframe>
data['Group_Status'] = 0
ticket_grouping = data.groupby("Ticket")
 for _, group in ticket_grouping:
if(len(group)> 1):
for i, row in group.iterrows() :
s_max = group.drop(i)['Survived'].max()
pass_id = row.name
if s_max == 1.0:
data.loc[pass_id, 'Group_Status'] = 1
elif s_max == 0.0:
data.loc[pass_id, 'Group_Status'] = -1
poly_reg_cc = PolynomialFeatures(degree = 4)
 poly_reg_ft = PolynomialFeatures(degree = 4)
 reg_cc = LinearRegression()
reg_ft = LinearRegression()
sub = pd.DataFrame({'ForecastId': [], 'ConfirmedCases': [], 'Fatalities': []})
 for value in countriesorstates:
train_temp = train.loc[train["Country/State"] == value]
test_temp = test.loc[test["Country/State"] == value]
train_temp_cc = train_temp["ConfirmedCases"].loc[train["Country/State"] == value].to_frame()
train_temp_ft = train_temp["Fatalities"].loc[train["Country/State"] == value].to_frame()
train_temp_X = train_temp.iloc[:, 4:6]
test_temp_X = test_temp.iloc[:, 2:4]
sc1 = StandardScaler()
train_temp_X = sc1.fit_transform(train_temp_X)
 test_temp_X = sc1.transform(test_temp_X)
 sc_cc = StandardScaler()
sc_ft = StandardScaler()
train_temp_cc = sc_cc.fit_transform(train_temp_cc)
 train_temp_ft = sc_ft.fit_transform(train_temp_ft)
 X_poly = poly_reg_cc.fit_transform(train_temp_X)
 reg_cc.fit(X_poly, train_temp_cc)
 test_cc = sc_cc.inverse_transform(reg_cc.predict(poly_reg_cc.fit_transform(test_temp_X)))
 X_poly = poly_reg_ft.fit_transform(train_temp_X)
 reg_ft.fit(X_poly, train_temp_ft)
 test_ft = sc_ft.inverse_transform(reg_ft.predict(poly_reg_ft.fit_transform(test_temp_X)))
 a = int(train["No.of days"].loc[train["Country/State"] == "India"].max())
 b = int(a - test_temp["No.of days"].min())
 test_cc[0:b+1] = sc_cc.inverse_transform(train_temp_cc)[(a-b-1):(a)]
test_ft[0:b+1] = sc_ft.inverse_transform(train_temp_ft)[(a-b-1):(a)]
test_cc = test_cc.flatten()
test_ft = test_ft.flatten()
sub_temp = pd.DataFrame({'ForecastId': test_temp["ForecastId"].loc[test["Country/State"] == value],
'ConfirmedCases': test_cc, 'Fatalities': test_ft})
 sub = pd.concat([sub, sub_temp], axis = 0 )<save_to_csv>
FEATURES_2 = ['Sex', 'Pclass', 'Embarked', 'Group_Status']
sub.ForecastId = sub.ForecastId.astype('int')
 for i in range(len(sub)) :
sub["ConfirmedCases"][i] = int(round(sub["ConfirmedCases"][i]))
sub["Fatalities"][i] = int(round(sub["Fatalities"][i]))
sub.to_csv("submission.csv", index = False )<load_from_csv>
FEATURES_3 = ['Sex', 'Pclass', 'Embarked', 'Group_Status', 'Age_Bin']
submission=pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/submission.csv' )<load_from_csv>
data["Fare_per_Person"] = data["Fare"] /(1 + data["Neighbors"])
 bins = pd.qcut(data["Fare_per_Person"], 6, labels=False, retbins=True)
 data["Fare_per_Person_Bin"] = bins[0]
submission=pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/submission.csv' )<load_from_csv>
FEATURES_4 = ['Sex', 'Pclass', 'Embarked', 'Group_Status', 'Age_Bin', 'Fare_per_Person_Bin']
submission = pd.read_csv(".. /input/covid19-global-forecasting-week-4/submission.csv")
 test = pd.read_csv(".. /input/covid19-global-forecasting-week-4/test.csv")
 train = pd.read_csv(".. /input/covid19-global-forecasting-week-4/train.csv")
 train.Province_State.fillna("None", inplace=True)
 display(train.head(5))
display(train.describe())
 print("Number of Country_Region: ", train['Country_Region'].nunique())
 print("Dates go from day", max(train['Date']), "to day", min(train['Date']), ", a total of", train['Date'].nunique() , "days")
 print("Countries with Province/State informed: ", train.loc[train['Province_State']!='None']['Country_Region'].unique() )<groupby>
data["Family_Size"] = data["SibSp"] + data["Parch"]
data["Connections"] = data[["Family_Size", "Neighbors"]].max(axis=1)
 bins = np.array([data["Connections"].min() , 1, 4, data["Connections"].max() ])
 data["Connections_Bin"] = np.digitize(data["Connections"], bins )
confirmed_total_date_Italy = train[train['Country_Region']=='Italy'].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_total_date_Italy = train[train['Country_Region']=='Italy'].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_Italy = confirmed_total_date_Italy.join(fatalities_total_date_Italy)
 confirmed_total_date_Spain = train[train['Country_Region']=='Spain'].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_total_date_Spain = train[train['Country_Region']=='Spain'].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_Spain = confirmed_total_date_Spain.join(fatalities_total_date_Spain)
 confirmed_total_date_UK = train[train['Country_Region']=='United Kingdom'].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_total_date_UK = train[train['Country_Region']=='United Kingdom'].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_UK = confirmed_total_date_UK.join(fatalities_total_date_UK)
 confirmed_total_date_Australia = train[train['Country_Region']=='Australia'].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_total_date_Australia = train[train['Country_Region']=='Australia'].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_Australia = confirmed_total_date_Australia.join(fatalities_total_date_Australia)
 confirmed_total_date_Singapore = train[train['Country_Region']=='Singapore'].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_total_date_Singapore = train[train['Country_Region']=='Singapore'].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_Singapore = confirmed_total_date_Singapore.join(fatalities_total_date_Singapore)
 confirmed_total_date_India = train[train['Country_Region']=='India'].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_total_date_India = train[train['Country_Region']=='India'].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_India = confirmed_total_date_India.join(fatalities_total_date_India)
 confirmed_total_date_US = train[train['Country_Region']=='US'].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_total_date_US = train[train['Country_Region']=='US'].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_US = confirmed_total_date_US.join(fatalities_total_date_US)
 plt.figure(figsize=(17,10))
plt.subplot(3, 2, 1)
 total_date_Italy.plot(ax=plt.gca() , title='Italy')
 plt.ylabel("Confirmed infection cases", size=13)
 plt.subplot(3, 2, 2)
 total_date_Spain.plot(ax=plt.gca() , title='Spain')
 plt.subplot(3, 2, 3)
 total_date_UK.plot(ax=plt.gca() , title='United Kingdom')
 plt.ylabel("Confirmed infection cases", size=13)
 plt.subplot(3, 2, 4)
 total_date_India.plot(ax=plt.gca() , title='India')
 plt.subplot(3, 2, 5)
 total_date_Australia.plot(ax=plt.gca() , title='Australia')
 plt.ylabel("Confirmed infection cases", size=13)
 plt.subplot(3, 2, 6)
 total_date_US.plot(ax=plt.gca() , title='United States')
 plt.ylabel("Confirmed infection cases", size=13 )<feature_engineering>
FEATURES_5 = ['Sex', 'Pclass', 'Embarked', 'Group_Status', 'Age_Bin', 'Fare_per_Person_Bin', "Connections_Bin"]
pop_India=1377011281.
 pop_US=330578810.
 pop_spain = 46749696.
 pop_italy = 60486683.
 pop_UK = 67784927.
 pop_singapore = 5837230.
 total_date_India.ConfirmedCases = total_date_India.ConfirmedCases/pop_India*100.
 total_date_India.Fatalities = total_date_India.ConfirmedCases/pop_India*100.
 total_date_US.ConfirmedCases = total_date_US.ConfirmedCases/pop_US*100.
 total_date_US.Fatalities = total_date_US.ConfirmedCases/pop_US*100.
 total_date_Italy.ConfirmedCases = total_date_Italy.ConfirmedCases/pop_italy*100.
 total_date_Italy.Fatalities = total_date_Italy.ConfirmedCases/pop_italy*100.
 total_date_Spain.ConfirmedCases = total_date_Spain.ConfirmedCases/pop_spain*100.
 total_date_Spain.Fatalities = total_date_Spain.ConfirmedCases/pop_spain*100.
 total_date_UK.ConfirmedCases = total_date_UK.ConfirmedCases/pop_UK*100.
 total_date_UK.Fatalities = total_date_UK.ConfirmedCases/pop_UK*100.
 total_date_Singapore.ConfirmedCases = total_date_Singapore.ConfirmedCases/pop_singapore*100.
 total_date_Singapore.Fatalities = total_date_Singapore.ConfirmedCases/pop_singapore*100.
 plt.figure(figsize=(15,10))
plt.subplot(3, 2, 1)
 total_date_India.ConfirmedCases.plot(ax=plt.gca() , title='India')
 plt.ylabel("Fraction of population infected")
 plt.ylim(0, 0.05)
 plt.subplot(3, 2, 2)
 total_date_US.ConfirmedCases.plot(ax=plt.gca() , title='US')
 plt.ylim(0, 0.05)
 plt.subplot(3, 2, 3)
 total_date_Spain.ConfirmedCases.plot(ax=plt.gca() , title='Spain')
 plt.ylabel("Fraction of population infected")
 plt.ylim(0, 0.05)
 plt.subplot(3, 2, 4)
 total_date_Italy.ConfirmedCases.plot(ax=plt.gca() , title='Italy')
 plt.ylim(0, 0.05)
 plt.subplot(3, 2, 5)
 total_date_UK.ConfirmedCases.plot(ax=plt.gca() , title='United Kingdom')
 plt.ylabel("Fraction of population infected")
 plt.ylim(0, 0.05)
 plt.subplot(3, 2, 6)
 total_date_Singapore.ConfirmedCases.plot(ax=plt.gca() , title='Singapore')
 plt.ylim(0, 0.05 )<merge>
def model(X, y, features, random_state, parameters=None):
model = RandomForestClassifier(random_state=random_state,
n_estimators=500,
min_samples_split=0.05)
 if parameters is not None:
model.set_params(**parameters)
 model.fit(X[features], y)
 score_cv = cross_val_score(model, X[features], y, cv=cv ).mean()
print(f"Model CV score is {score_cv:.5f}")
 return model
confirmed_total_date_India = train[(train['Country_Region']=='India')& train['ConfirmedCases']!=0].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_total_date_India = train[(train['Country_Region']=='India')& train['ConfirmedCases']!=0].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_India = confirmed_total_date_India.join(fatalities_total_date_India)
 confirmed_total_date_US = train[(train['Country_Region']=='US')& train['ConfirmedCases']!=0].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_total_date_US = train[(train['Country_Region']=='US')& train['ConfirmedCases']!=0].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_US = confirmed_total_date_US.join(fatalities_total_date_US)
 confirmed_total_date_Italy = train[(train['Country_Region']=='Italy')& train['ConfirmedCases']!=0].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_total_date_Italy = train[(train['Country_Region']=='Italy')& train['ConfirmedCases']!=0].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_Italy = confirmed_total_date_Italy.join(fatalities_total_date_Italy)
 confirmed_total_date_Spain = train[(train['Country_Region']=='Spain')&(train['ConfirmedCases']!=0)].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_total_date_Spain = train[(train['Country_Region']=='Spain')&(train['ConfirmedCases']!=0)].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_Spain = confirmed_total_date_Spain.join(fatalities_total_date_Spain)
 confirmed_total_date_UK = train[(train['Country_Region']=='United Kingdom')&(train['ConfirmedCases']!=0)].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_total_date_UK = train[(train['Country_Region']=='United Kingdom')&(train['ConfirmedCases']!=0)].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_UK = confirmed_total_date_UK.join(fatalities_total_date_UK)
 confirmed_total_date_Australia = train[(train['Country_Region']=='Australia')&(train['ConfirmedCases']!=0)].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_total_date_Australia = train[(train['Country_Region']=='Australia')&(train['ConfirmedCases']!=0)].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_Australia = confirmed_total_date_Australia.join(fatalities_total_date_Australia)
 confirmed_total_date_Singapore = train[(train['Country_Region']=='Singapore')&(train['ConfirmedCases']!=0)].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_total_date_Singapore = train[(train['Country_Region']=='Singapore')&(train['ConfirmedCases']!=0)].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_Singapore = confirmed_total_date_Singapore.join(fatalities_total_date_Singapore)
 India = [i for i in total_date_India.ConfirmedCases['sum'].values]
India_30 = India[0:70]
US = [i for i in total_date_US.ConfirmedCases['sum'].values]
US_30 = US[0:70]
italy = [i for i in total_date_Italy.ConfirmedCases['sum'].values]
italy_30 = italy[0:70]
spain = [i for i in total_date_Spain.ConfirmedCases['sum'].values]
spain_30 = spain[0:70]
UK = [i for i in total_date_UK.ConfirmedCases['sum'].values]
UK_30 = UK[0:70]
singapore = [i for i in total_date_Singapore.ConfirmedCases['sum'].values]
singapore_30 = singapore[0:70]
plt.figure(figsize=(12,6))
plt.plot(India_30)
 plt.plot(US_30)
 plt.plot(italy_30)
 plt.plot(spain_30)
 plt.plot(UK_30)
 plt.plot(singapore_30)
 plt.legend(["India","US", "Italy", "Spain", "UK", "Singapore"], loc='upper left')
 plt.title("COVID-19 infections from the first| confirmed case", size=15)
 plt.xlabel("Days", size=13)
 plt.ylabel("Infected cases", size=13)
 plt.ylim(0, 300000)
 plt.show()<categorify>
X_train = data.loc[data_train.index].drop("Survived", axis=1)
 y_train = data.loc[data_train.index, "Survived"]
X_test = data.loc[data_test.index].drop("Survived", axis=1)
 cv=KFold(10, shuffle=True, random_state=random_state )
le = preprocessing.LabelEncoder() 
<sort_values>
MODEL_BASELINE = model(X_train, y_train, FEATURES_BASELINE, random_state)
df = train.fillna('NA' ).groupby(['Country_Region','Province_State','Date'])['ConfirmedCases'].sum() \ 
.groupby(['Country_Region','Province_State'] ).max().sort_values() \ 
.groupby(['Country_Region'] ).sum().sort_values(ascending = False)
 top20_countries = pd.DataFrame(df ).head(20)
 top20_countries<data_type_conversions>
MODEL_1 = model(X_train, y_train, FEATURES_1, random_state)
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>
MODEL_2 = model(X_train, y_train, FEATURES_2, random_state)
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 )<feature_engineering>
MODEL_3 = model(X_train, y_train, FEATURES_3, random_state)
train['Province_State'] = np.where(train['Province_State'] == 'nan',train['Country_Region'],train['Province_State'])
 test['Province_State'] = np.where(test['Province_State'] == 'nan',test['Country_Region'],test['Province_State'] )<data_type_conversions>
MODEL_4 = model(X_train, y_train, FEATURES_4, random_state)
convert_dict = {'Province_State': str}
train = train.astype(convert_dict)
 test = test.astype(convert_dict )<load_from_csv>
MODEL_5 = model(X_train, y_train, FEATURES_5, random_state)
submission = pd.read_csv(".. /input/covid19-global-forecasting-week-4/submission.csv")
 test = pd.read_csv(".. /input/covid19-global-forecasting-week-4/test.csv")
 train = pd.read_csv(".. /input/covid19-global-forecasting-week-4/train.csv" )<count_missing_values>
FEATURES = ['Sex', 'Pclass', 'Embarked', 'Group_Status', 'Age_Bin', 'Fare_per_Person_Bin', "Connections_Bin"]
train.isna().sum()<count_missing_values>
params = {
'n_estimators': [500, 1000],
'min_samples_split': [2, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.10]
}
results = pd.DataFrame()
gs = GridSearchCV(estimator=RandomForestClassifier(random_state), param_grid=params, cv=cv, verbose=1, n_jobs=-1)
 gs.fit(X_train[FEATURES], y_train)
 results = pd.DataFrame(gs.cv_results_)
 table = pd.pivot_table(results, values="mean_test_score", index="param_min_samples_split", columns="param_n_estimators")
 sns.heatmap(table, annot=True, cmap="YlGnBu")
test.isna().sum()<data_type_conversions>
params = {
'n_estimators': [500, 1000],
'max_depth': [3, 4, 5, 6, 7, None],
}
results = pd.DataFrame()
gs = GridSearchCV(estimator=RandomForestClassifier(random_state), param_grid=params, cv=cv, verbose=1, n_jobs=-1)
 gs.fit(X_train[FEATURES], y_train)
 results = pd.DataFrame(gs.cv_results_)
 table = pd.pivot_table(results, values="mean_test_score", index="param_max_depth", columns="param_n_estimators")
 sns.heatmap(table, annot=True, cmap="YlGnBu")
train['Province_State'].fillna("",inplace = True)
 test['Province_State'].fillna("",inplace = True )<drop_column>
model = RandomForestClassifier(random_state=random_state)
 pipe = Pipeline([("rfe", RFE(model, verbose=0)) ,("rf", model)])
 params = {
'rfe__n_features_to_select': [1,2,3,4,5,6,7],
'rf__n_estimators': [500],
'rf__min_samples_split': [2, 0.01],
'rf__max_depth': [5, 6, None]
}
grid = GridSearchCV(pipe, param_grid=params, cv=cv, verbose=0, n_jobs=-1)
 grid.fit(X_train[FEATURES], y_train)
 print(gs.best_params_)
 grid.cv_results_
train['Country_Region'] = train['Country_Region'] + ' ' + train['Province_State']
test['Country_Region'] = test['Country_Region'] + ' ' + test['Province_State']
del train['Province_State']
del test['Province_State']<feature_engineering>
MODEL_6 = grid.best_estimator_
print(f"Model CV score is {grid.best_score_:.5f}")
def split_date(date):
date = date.split('-')
 date[0] = int(date[0])
 if(date[1][0] == '0'):
date[1] = int(date[1][1])
 else:
date[1] = int(date[1])
 if(date[2][0] == '0'):
date[2] = int(date[2][1])
 else:
date[2] = int(date[2])
 return date
train.Date = train.Date.apply(split_date)
 test.Date = test.Date.apply(split_date )<feature_engineering>
MODEL_7 = catboost.CatBoostClassifier(one_hot_max_size=4, iterations=1000, random_seed=random_state, verbose=False)
 MODEL_7.fit(X_train[FEATURES_4], y_train)
 score_cv = cross_val_score(MODEL_7, X_train[FEATURES_4], y_train, cv=cv ).mean()
print(f"Model CV score is {score_cv:.5f}")
year = []
month = []
day = []
for i in train.Date:
year.append(i[0])
 month.append(i[1])
 day.append(i[2] )<feature_engineering>
model = KNeighborsClassifier()
pipe = Pipeline([("scaler", StandardScaler()),("knn", model)])
 params = {
'knn__n_neighbors': [3, 9, 15, 20, 21, 22, 25, 30],
'knn__weights': ['uniform', 'distance']
}
knn_grid = GridSearchCV(pipe, param_grid=params, cv=cv, verbose=0, n_jobs=-1)
 knn_grid.fit(X_train[FEATURES], y_train)
 knn_grid.cv_results_
train['Year'] = year
train['Month'] = month
train['Day'] = day
del train['Date']<feature_engineering>
MODEL_8 = knn_grid.best_estimator_
print(f"Model CV score is {knn_grid.best_score_:.5f}")
year = []
month = []
day = []
for i in test.Date:
year.append(i[0])
 month.append(i[1])
 day.append(i[2] )<feature_engineering>
MODEL_4.fit(X_train[FEATURES_4], y_train)
 prediction_4 = MODEL_4.predict_proba(X_test[FEATURES_4])[:,1]
submit = pd.DataFrame({"PassengerId": X_test.index,"Survived": np.round(prediction_4, 0 ).astype(int)})
 submit.to_csv("MODEL_4.csv",index=False )
test['Year'] = year
test['Month'] = month
test['Day'] = day
del test['Date']
del train['Id']
del test['ForecastId']<drop_column>
MODEL_6.fit(X_train[FEATURES], y_train)
 prediction_6 = MODEL_6.predict_proba(X_test[FEATURES])[:,1]
submit = pd.DataFrame({"PassengerId": X_test.index,"Survived": np.round(prediction_6, 0 ).astype(int)})
 submit.to_csv("MODEL_6.csv",index=False )
del train['Year']
del test['Year']<data_type_conversions>
MODEL_7.fit(X_train[FEATURES_4], y_train)
 prediction_7 = MODEL_7.predict_proba(X_test[FEATURES_4])[:,1]
submit = pd.DataFrame({"PassengerId": data_test.index,"Survived": np.round(prediction_7, 0 ).astype(int)})
 submit.to_csv("MODEL_7.csv",index=False )
train['ConfirmedCases'] = train['ConfirmedCases'].apply(int)
 train['Fatalities'] = train['Fatalities'].apply(int )<drop_column>
MODEL_8.fit(X_train[FEATURES], y_train)
 prediction_8 = MODEL_8.predict_proba(X_test[FEATURES])[:,1]
submit = pd.DataFrame({"PassengerId": data_test.index,"Survived": np.round(prediction_8, 0 ).astype(int)})
 submit.to_csv("MODEL_8.csv",index=False )