Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<categorify>
import numpy as np
import pandas as pd
from xgboost import XGBClassifier
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score
from sklearn.metrics import accuracy_score
from sklearn.ensemble import RandomForestClassifier,BaggingClassifier,AdaBoostClassifier,ExtraTreesClassifier,GradientBoostingClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression,SGDClassifier,RidgeClassifier,PassiveAggressiveClassifier,Perceptron
from sklearn.neural_network import MLPClassifier
import re
import optuna
from optuna.samplers import TPESampler
from lightgbm import LGBMClassifier
from mlens.ensemble import SuperLearner
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
from sklearn import feature_selection
from sklearn import model_selection
from sklearn import metrics
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler
from sklearn.utils import shuffle
submission=pd.DataFrame(columns=submission.columns)
 l1=LabelEncoder()
l2=LabelEncoder()
l1.fit(train['Country_Region'])
 l2.fit(train['Province_State'] )<categorify>
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv')
 PassengerId = test['PassengerId']
countries=train['Country_Region'].unique()
for country in countries:
country_df=train[train['Country_Region']==country]
provinces=country_df['Province_State'].unique()
for province in provinces:
train_df=country_df[country_df['Province_State']==province]
train_df.pop('Id')
 x=train_df[['Province_State','Country_Region','Day_of_Week','Month','Day','Day_of_Year','Week_of_Year','Quarter']]
x['Country_Region']=l1.transform(x['Country_Region'])
 x['Province_State']=l2.transform(x['Province_State'])
 y1=train_df[['ConfirmedCases']]
y2=train_df[['Fatalities']]
model_1=DecisionTreeClassifier()
model_2=DecisionTreeClassifier()
model_1.fit(x,y1)
 model_2.fit(x,y2)
 test_df=test.query('Province_State==@province & Country_Region==@country')
 test_id=test_df['ForecastId'].values.tolist()
test_df.pop('ForecastId')
 test_x=test_df[['Province_State','Country_Region','Day_of_Week','Month','Day','Day_of_Year','Week_of_Year','Quarter']]
test_x['Country_Region']=l1.transform(test_x['Country_Region'])
 test_x['Province_State']=l2.transform(test_x['Province_State'])
 test_y1=model_1.predict(test_x)
 test_y2=model_2.predict(test_x)
 test_res=pd.DataFrame(columns=submission.columns)
 test_res['ForecastId']=test_id
test_res['ConfirmedCases']=test_y1
test_res['Fatalities']=test_y2
submission=submission.append(test_res )<save_to_csv>
train.isna().sum()
submission
submission.to_csv('submission.csv',index=False )<define_variables>
test.isna().sum()
datapath = '.. /input/covid19-global-forecasting-week-4/'
datapath2 = '.. /input/worldpopulationinfo/'
datapath3 = '.. /input/country-ppp/'
datapath4 = '.. /input/populationandcountryinfo/'
datapath5 = '.. /input/usstateland/'
datapath_week1 = '.. /input/covid19week1/'
add_other = True
CURVE_SMOOTHING = True
USE_NEW = False
normfactor = 1.0
NUM_SHIFT = 25
days_shift =[0,1,2,3,5,7,10,14,18,22,27,32,37,42]
days_shift =[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,16,18,20,22,24,26,28,30,32,34,36]
NUM_MODELS = 10
TRAIN_START_DAY = 1
TARGETS = ["ConfirmedCases", "Fatalities"]
if USE_NEW:
TARGETS = ["NewCases", "NewFatalities"]
loc_group = ["Province_State", "Country_Region"]
if add_other:
base_features = ['ages 0-14', 'ages 15-64', 'ages 64-',
'Population(2020)', 'Density(P/Km²)', 'Med.Age', 'Urban Pop %',
'Apr', 'year', 'Low Temp', 'ppp', 'Season',
'R_Africa','R_China',
'R_Australia and New Zealand', 'R_Eastern Asia', 'R_Europe',
'R_Latin America and the Caribbean', 'R_Northern America', 'R_Oceania',
'R_Southern Asia', 'R_Western Asia',
'I_High income', 'I_Low income', 'I_Lower middle income',
'I_Upper middle income']
base_features = ['ages 64-',
'Urban Pop %',
'Season','Apr','ppp']
if USE_NEW:
base_features =['NewFatalities_ppm','NewCases_ppm'] + base_features
else:
a=1
else:
base_features=[]
def get_shift_features() :
shift_features = []
for s in range(1, NUM_SHIFT+1):
for col in TARGETS:
shift_features.append("prev_{}_{}".format(col, days_shift[s]))
return shift_features
shift_features = get_shift_features()
prev_targets = shift_features[0:2]
def fill_shift_columns(df,targets):
for s in range(1, NUM_SHIFT+1):
for col in targets:
df["prev_{}_{}".format(col, days_shift[s])] = df.groupby(loc_group)[col].shift(days_shift[s])
 return df
def add_seasons(coor_df):
coor_df["Season"] = 0
mask = coor_df["Lat"]>20
coor_df.loc[mask,"Season"] = 1
mask = coor_df["Lat"]<-20
coor_df.loc[mask,"Season"] = -1
return coor_df
def preprocess(df):
df["Date"] = df["Date"].astype("datetime64[ms]")
 df["days"] =(df["Date"] - pd.to_datetime("2020-01-01")).dt.days
for col in loc_group:
df[col].fillna("none", inplace=True)
 return df
def print_rows_with_nan(df,features=False):
if not features:
dyt = df.isna()
dyt2 = dyt.any(axis=1)
 dyt2.sum()
print(df.loc[dyt2])
 else:
dyt = df[features].isna()
dyt2 = dyt.any(axis=1)
 dyt2.sum()
print(df[features].loc[dyt2])
 return
def diff(first, second):
second = set(second)
 return [item for item in first if item not in second]
def has_nan(df,features=False):
if not features:
dyt = df.isnull()
else:
dyt = df[features].isnull()
dyt2 = dyt.any(axis=1)
 return dyt2.sum()
def print_washinton(df,features,values):
print(len(values))
if len(values)== 1:
mask1 = df[features[0]]==values[0]
print(df.loc[mask1,features+["Date","ConfirmedCases","Fatalities"]].tail(50))
elif len(values)== 2:
mask1 = df[features[0]]==values[0]
mask2 = df[features[1]]==values[1]
mask4 = pd.concat(( mask1,mask2), axis=1)
 mask5 = mask4.all(axis=1)
 print(df.loc[mask5,features+["Date","ConfirmedCases","Fatalities"]].tail(50))
else:
mask1 = df[features[0]]==values[0]
mask2 = df[features[1]]==values[1]
mask3 = df[features[2]]==values[2]
mask4 = pd.concat(( mask1,mask2,mask3), axis=1)
 mask5 = mask4.all(axis=1)
 print(df.loc[mask5,["Date"]])
 return
def find_anomalities(df):
df[['Cases_test']] = df.groupby([ 'Country_Region', 'Province_State'])\
[['ConfirmedCases']].shift(1)
 df[['Fatality_test']] = df.groupby(['Country_Region', 'Province_State'])\
[['Fatalities']].shift(1)
 mask1 = df['ConfirmedCases'] < df['Cases_test']
mask2 = df['Fatalities'] < df['Fatality_test']
print("ANOMALITIES ConfirmedCases")
 print(df.loc[mask1])
 print("ANOMALITIES Fatalities")
 print(df.loc[mask2])
 print("SLUT")
 return
def correct_anomalities(df):
df[['Cases_testm']] = df.groupby([ 'Country_Region', 'Province_State'])\
[['ConfirmedCases']].shift(1)
 df[['Fatalities_testm']] = df.groupby(['Country_Region', 'Province_State'])\
[['Fatalities']].shift(1)
 df[['Cases_testp']] = df.groupby([ 'Country_Region', 'Province_State'])\
[['ConfirmedCases']].shift(-1)
 df[['Fatalities_testp']] = df.groupby(['Country_Region', 'Province_State'])\
[['Fatalities']].shift(-1)
 print("CORRECTING ConfirmedCases")
 mask1 = df['ConfirmedCases'] < df['Cases_testm']
mask2 = df['Cases_testp'] >= df['Cases_testm']
mask3 = pd.concat(( mask1,mask2),axis=1)
 mask4 = mask3.all(axis=1)
 df.loc[mask4,'ConfirmedCases'] = 0.5*(df.loc[mask4,'Cases_testp']+df.loc[mask4,'Cases_testm'])
 print(df.loc[mask4,['Country_Region', 'Province_State','Cases_testm','ConfirmedCases','Cases_testp']])
 mask1 = df['ConfirmedCases'] > df['Cases_testm']
mask2 = df['Cases_testp'] == df['Cases_testm']
mask3 = pd.concat(( mask1,mask2),axis=1)
 mask4a = mask3.all(axis=1)
 df.loc[mask4a,'ConfirmedCases'] = 0.5*(df.loc[mask4a,'Cases_testp']+df.loc[mask4a,'Cases_testm'])
 print(df.loc[mask4a,['Country_Region', 'Province_State','Cases_testm','ConfirmedCases','Cases_testp']])
 print("CORRECTED ConfirmedCases:", mask4.sum() ,"+", mask4a.sum())
 print("CORRECTING Fatalities")
 mask1 = df['Fatalities'] < df['Fatalities_testm']
mask2 = df['Fatalities_testp'] >= df['Fatalities_testm']
mask3 = pd.concat(( mask1,mask2),axis=1)
 mask4 = mask3.all(axis=1)
 df.loc[mask4,'Fatalities'] = 0.5*(df.loc[mask4,'Fatalities_testp']+df.loc[mask4,'Fatalities_testm'])
 print(df.loc[mask4,['Country_Region', 'Province_State','Fatalities_testm','Fatalities','Fatalities_testp']])
 mask1 = df['Fatalities'] > df['Fatalities_testm']
mask2 = df['Fatalities_testp'] == df['Fatalities_testm']
mask3 = pd.concat(( mask1,mask2),axis=1)
 mask4a = mask3.all(axis=1)
 df.loc[mask4a,'Fatalities'] = 0.5*(df.loc[mask4a,'Fatalities_testp']+df.loc[mask4a,'Fatalities_testm'])
 print(df.loc[mask4a,['Country_Region', 'Province_State','Fatalities_testm','Fatalities','Fatalities_testp']])
 print("CORRECTED Fatalities:",mask4.sum() ,"+",mask4a.sum())
 print("CORRECTING Series(in Mix)")
 cases = 1
sumcases = 0
while cases > 0:
mask1 = df['ConfirmedCases'] < df['Cases_testm']
df.loc[mask1,'ConfirmedCases'] = df.loc[mask1,'Cases_testm']
print("Cases",df.loc[mask1,['Country_Region', 'Province_State','Cases_testm','ConfirmedCases']])
 mask2 = df['Fatalities'] < df['Fatalities_testm']
df.loc[mask2,'Fatalities'] = df.loc[mask2,'Fatalities_testm']
print("Fatas",df.loc[mask2,['Country_Region', 'Province_State','Fatalities_testm','Fatalities']])
 cases = mask1.sum() +mask2.sum()
sumcases+=cases
print
df[['Cases_testm']] = df.groupby([ 'Country_Region', 'Province_State'])\
[['ConfirmedCases']].shift(1)
 df[['Fatalities_testm']] = df.groupby(['Country_Region', 'Province_State'])\
[['Fatalities']].shift(1)
 df.drop([ 'Cases_testm', 'Fatalities_testm', 'Cases_testp','Fatalities_testp'],axis=1,inplace=True)
 print("Corrected in Series in all",sumcases)
 return<load_from_csv>
df = pd.concat([train, test])
 df = df.reset_index(drop=True)
 df['FamilySize'] = df['SibSp'] + df['Parch'] + 1
df['IsAlone'] = 0
df.loc[df['FamilySize'] == 1, 'IsAlone'] = 1
df['Fare'] = df['Fare'].fillna(df['Fare'].median())
 df['Has_Cabin'] = df["Cabin"].apply(lambda x: 0 if type(x)== float else 1)
 df['Title'] = df['Name'].apply(get_title)
 mapping = {'Mlle': 'Miss', 'Major': 'Mr', 'Col': 'Mr', 'Sir': 'Mr', 'Don': 'Mr', 'Mme': 'Miss',
'Jonkheer': 'Mr', 'Lady': 'Mrs', 'Capt': 'Mr', 'Countess': 'Mrs', 'Ms': 'Miss', 'Dona': 'Mrs'}
df['Title'] = df['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 df['Title'] = df['Title'].replace('Mlle', 'Miss')
 df['Title'] = df['Title'].replace('Ms', 'Miss')
 df['Title'] = df['Title'].replace('Mme', 'Mrs')
 df['Sex'] = df['Sex'].map({'female': 0, 'male': 1} ).astype(int)
 title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
df['Title'] = df['Title'].map(title_mapping)
 df.loc[ df['Fare'] <= 7.91, 'Fare'] = 0
df.loc[(df['Fare'] > 7.91)&(df['Fare'] <= 14.454), 'Fare'] = 1
df.loc[(df['Fare'] > 14.454)&(df['Fare'] <= 31), 'Fare'] = 2
df.loc[ df['Fare'] > 31, 'Fare'] = 3
df['Fare'] = df['Fare'].astype(int )
population_by_age_df = pd.read_csv(datapath4 + "population_age_info.csv")
 population_by_age_df.drop('ID',axis=1,inplace=True)
 population_by_age_df[['ages 0-14', 'ages 15-64','Density(P/Km²)','Med.Age', 'ages 64-','Urban Pop %']] = \
population_by_age_df[['ages 0-14', 'ages 15-64','Density(P/Km²)','Med.Age', 'ages 64-','Urban Pop %']].apply(lambda x: x.fillna(x.mean()))
translations = [["Brunei Darussalam","Brunei"],
["Myanmar","Bruma"],
["Gambia, The","Gambia"],
["Egypt, Arab Rep.","Egypt"],
["Congo, Rep.","Congo(Brazzaville)"],
["Congo, Dem.Rep.","Congo(Kinshasa)"],
["Iran, Islamic Rep.","Iran"],
["Korea, Rep.","Korea, South"],
["Russian Federation","Russia"],
["Syrian Arab Republic","Syria"],
["Venezuela, RB","Venezuela"],
["Slovak Republic","Slovakia"]]
for post in translations:
mask = population_by_age_df["Country_Region"] == post[0]
population_by_age_df.loc[mask,"Country_Region"] = post[1]
usstates_land = pd.read_csv(datapath5 + "us-state-land.csv", sep='\t')
 usstates_population = pd.read_csv(datapath2 + "us-state-population.csv", sep='\t')
 usstates_info = pd.merge(usstates_land,usstates_population, on=['Province_State'], how='left')
 usstates_info['Country_Region'] = 'US'
usstates_info["Province_State"].replace('_',' ', regex=True,inplace=True)
 supp_info = pd.read_csv(datapath2 + "population_info_supplement.csv")
 supp_info["Country_Region"].replace(':',',', regex=True,inplace=True)
 CountryRegion = pd.read_csv(datapath2+'CountryRegion.csv')
 CountryRegion.drop('ID',axis=1,inplace=True)
 state_temperatures = pd.read_csv(datapath4+'state_temperature_info.csv')
 state_temperatures.drop('ID',axis=1,inplace=True)
 state_temperatures["Province_State"].fillna("", inplace=True)
 ppp_tabel = pd.read_csv(datapath3 + 'Country_PPP.csv', sep='\s+')
 ppp_tabel.drop('Id', 1,inplace=True)
 ppp_tabel = ppp_tabel.append({'Country' : 'Burma' , 'ppp' : 8000} , ignore_index=True)
 ppp_tabel = ppp_tabel.append({'Country' : 'MS_Zaandam' , 'ppp' : 40000} , ignore_index=True)
 ppp_tabel = ppp_tabel.append({'Country' : 'West_Bank_and_Gaza' , 'ppp' : 20000} , ignore_index=True)
 ppp_tabel["Country"].replace('_',' ', regex=True,inplace=True)
 ppp_tabel["Country"].replace('United States','US', regex=True,inplace=True)
 ppp_tabel.rename(columns={'Country':'Country_Region'},inplace=True)
 ppp_tabel.sort_values('Country_Region',inplace=True)
 coor_df = pd.read_csv(datapath_week1 + "train.csv" ).rename(columns={"Country/Region": "Country_Region","Province/State":"Province_State"})
 coor_df["Province_State"].fillna("", inplace=True)
 coor_df = coor_df[coor_df["Country_Region"].notnull() ]
coor_df = coor_df.groupby(["Country_Region","Province_State"])[["Lat", "Long"]].mean().reset_index()
coor_df = add_seasons(coor_df)
 coor_country = coor_df.groupby(["Country_Region"])[["Lat", "Long"]].mean().reset_index()
coor_country = add_seasons(coor_country)
 coor_country.rename(columns={'Season':'Season2'},inplace=True)
 coor_df = pd.merge(coor_df,coor_country, on=['Country_Region'], how='left')
<merge>
df['Title2'] = df['Name'].apply(get_title)
 mapping = {'Mlle': 'Miss', 'Major': 'Mr', 'Col': 'Mr', 'Sir': 'Mr', 'Don': 'Mr', 'Mme': 'Miss',
'Jonkheer': 'Mr', 'Lady': 'Mrs', 'Capt': 'Mr', 'Countess': 'Mrs', 'Ms': 'Miss', 'Dona': 'Mrs'}
df.replace({'Title2': mapping}, inplace=True)
 titles = ['Dr', 'Master', 'Miss', 'Mr', 'Mrs', 'Rev']
for title in titles:
age_to_impute = df.groupby('Title2')['Age'].median() [titles.index(title)]
df.loc[(df['Age'].isnull())&(df['Title2'] == title), 'Age'] = age_to_impute
df.drop('Title2', axis = 1, inplace = True )
def add_other_info(df,istest_df,usstates_info,supp_info,population_by_age_df,CountryRegion,state_temperatures,ppp_tabel,coor_df):
df = pd.merge(df, usstates_info, on=['Country_Region','Province_State'], how='left')
 df = pd.merge(df, population_by_age_df, on=['Country_Region'], how='left')
 df[['ages 0-14', 'ages 15-64','Density(P/Km²)','Med.Age', 'ages 64-','Urban Pop %']] = \
df[['ages 0-14', 'ages 15-64','Density(P/Km²)','Med.Age', 'ages 64-','Urban Pop %']].apply(lambda x: x.fillna(x.mean()))
print(df[df['IncomeGroup'].isnull() ]["Country_Region"].unique())
 df = pd.merge(df, CountryRegion, on=['Country_Region'], how='left')
 mask = df['Country_Region']=='US'
df.loc[mask,'Population(2020)'] = df.loc[mask,'population']
df.loc[mask,'Density(P/Km²)'] = df.loc[mask,'population']/df.loc[mask,'land']
df.drop(["population","land"],axis=1,inplace=True)
 df = pd.merge(df, supp_info, on=['Country_Region'], how='left')
 mask = df['Population(2020)'].isnull()
df.loc[mask,'Population(2020)'] = df.loc[mask,'population']
df.loc[mask,'Density(P/Km²)'] = df.loc[mask,'population']/df.loc[mask,'land']
df.drop(["population","land"],axis=1,inplace=True)
 df = pd.merge(df, state_temperatures, on=['Country_Region','Province_State'], how='left')
 df[['Jan', 'Feb', 'Mar', 'Apr', 'May',
'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'year', 'Low Temp']] = \
df[['Jan', 'Feb', 'Mar', 'Apr', 'May',
'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'year', 'Low Temp']].apply(lambda x: x.fillna(x.mean()))
df.drop(['May','Jun', 'Jul', 'Aug', 'Sep', 'Oct','Nov', 'Dec'],axis=1,inplace=True)
 df.drop(['Jan', 'Feb', 'Mar'],axis=1,inplace=True)
 df = pd.merge(df,ppp_tabel, on=['Country_Region'], how='left')
 df = pd.merge(df,coor_df[["Country_Region",'Province_State',"Season","Season2"]],
on=["Country_Region",'Province_State'], how="left")
 mask = df['Season'].isna()
df.loc[mask,["Season"]]= df.loc[mask,"Season2"]
df["Season"].fillna(1, inplace=True)
 mask = df['Country_Region']== 'China'
df.loc[mask,["sub-region"]]= 'China'
onehot = pd.get_dummies(df['sub-region'],prefix='R')
 onehot2 = pd.get_dummies(df['IncomeGroup'],prefix='I')
 df.drop(["Season2","sub-region",'IncomeGroup'],axis=1,inplace= True)
 df = pd.concat([df,onehot],axis=1,ignore_index=False)
 df = pd.concat([df,onehot2],axis=1,ignore_index=False)
 if istest_df:
if USE_NEW:
df["NewCases_ppm"] = np.log1p(1000000/df["Population(2020)"])
 df["NewFatalities_ppm"] = np.log1p(1000000/df["Population(2020)"])
 else:
df["ConfirmedCases_ppm"] = np.log1p(1000000/df["Population(2020)"])
 df["Fatalities_ppm"] = np.log1p(1000000/df["Population(2020)"])
 else:
if USE_NEW:
df["NewCases_ppm"] = np.log1p(df["NewCases"]*1000000/df["Population(2020)"])
 df["NewFatalities_ppm"] = np.log1p(df["NewFatalities"]*1000000/df["Population(2020)"])
 else:
df["ConfirmedCases_ppm"] = np.log1p(df["ConfirmedCases"]*1000000/df["Population(2020)"])
 df["Fatalities_ppm"] = np.log1p(df["Fatalities"]*1000000/df["Population(2020)"])
 df['ages 0-14'] = df['ages 0-14']/100.
 df['ages 15-64'] = df['ages 15-64']/100.
 df['ages 64-'] = df['ages 64-']/100.
 df['Population(2020)'] = np.log1p(df['Population(2020)'])
 df['Density(P/Km²)'] = np.log1p(df['Density(P/Km²)'])
 df['Med.Age'] = df['Med.Age']/50.
 df['Urban Pop %'] = df['Urban Pop %']/100.
 df['Apr'] = df['Apr']/30.
 df['year'] = df['year']/30.
 df['Low Temp'] = df['Low Temp']/30.
 df['ppp'] = np.log1p(df['ppp'])
 return df 
<load_from_csv>
df['Last_Name'] = df['Name'].apply(lambda x: str.split(x, ",")[0])
 DEFAULT_SURVIVAL_VALUE = 0.5
df['Family_Survival'] = DEFAULT_SURVIVAL_VALUE
for grp, grp_df in df[['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):
df.loc[df['PassengerId'] == passID, 'Family_Survival'] = 1
elif(smin==0.0):
df.loc[df['PassengerId'] == passID, 'Family_Survival'] = 0
print("Number of passengers with family survival information:",
df.loc[df['Family_Survival']!=0.5].shape[0] )
df = pd.read_csv(datapath + "train.csv")
 sub_df = pd.read_csv(datapath + "test.csv")
 df['Province_State'].fillna('', inplace=True)
 sub_df['Province_State'].fillna('', inplace=True)
 gem_targets = df[['Country_Region','Province_State','Date']+TARGETS]
gem_targets["Date"] = gem_targets["Date"].astype("datetime64[ms]")
 combi= df.groupby(["Country_Region","Province_State"] ).size().reset_index()
combi =combi[["Country_Region","Province_State"]].values.tolist()
mask1 = df["Country_Region"]=='Guyana'
mask2 = df["Date"] > '2020-03-21'
mask3 = df["Date"] < '2020-03-28'
mask4 = pd.concat(( mask1,mask2,mask3),axis=1)
 mask5 = mask4.all(axis=1)
 df.loc[mask5,"ConfirmedCases"] = 7.0
mask1 = df["Province_State"]=='Northern Territory'
mask2 = df["Date"] > '2020-03-05'
mask3 = df["Date"] < '2020-03-10'
mask4 = pd.concat(( mask1,mask2,mask3),axis=1)
 mask5 = mask4.all(axis=1)
 df.loc[mask5,"ConfirmedCases"] = 1.0
mask1 = df["Country_Region"]=='Philippines'
mask2 = df["Date"] > '2020-03-17'
mask3 = df["Date"] < '2020-03-19'
mask4 = pd.concat(( mask1,mask2,mask3),axis=1)
 mask5 = mask4.all(axis=1)
 df.loc[mask5,"Fatalities"] = 14.0
correct_anomalities(df)
 if USE_NEW:
df[['NewCases','NewFatalities']] = df.groupby(['Country_Region', 'Province_State'])\
[['ConfirmedCases','Fatalities']].transform(lambda x: x.diff())
 mask = df['NewCases'].isnull()
df.loc[mask,['NewCases']] = 0
df.loc[mask,['NewFatalities']] = 0
provins = 'Virgin Islands'
print("testprint for",provins)
 print(df[df['Province_State']== provins][['NewCases','NewFatalities']])
 def row_count(filename):
with open(filename)as in_file:
return sum(1 for _ in in_file)
 df = preprocess(df)
 sub_df = preprocess(sub_df)
 GIVEN_FIRST_DATE = df["Date"].min()
GIVEN_LAST_DATE = df["Date"].max()
SUBMISSION_FIRST_DATE = sub_df["Date"].min()
ESTIMATE_FIRST_DATE = df["Date"].max() + timedelta(days=1)
 ESTIMATE_LAST_DATE = sub_df["Date"].max()
ESTIMATE_DAYS =(ESTIMATE_LAST_DATE - ESTIMATE_FIRST_DATE ).days +1
if add_other:
df = add_other_info(df,False,usstates_info,supp_info,population_by_age_df,CountryRegion,state_temperatures,ppp_tabel,coor_df)
 sub_df = add_other_info(sub_df,True,usstates_info,supp_info,population_by_age_df,CountryRegion,state_temperatures,ppp_tabel,coor_df)
 print("SUB_DF COLUMNS NYLAVET:")
 print(sub_df.columns)
<categorify>
for _, grp_df in df.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):
df.loc[df['PassengerId'] == passID, 'Family_Survival'] = 1
elif(smin==0.0):
df.loc[df['PassengerId'] == passID, 'Family_Survival'] = 0
print("Number of passenger with family/group survival information: "
+str(df[df['Family_Survival']!=0.5].shape[0]))
if CURVE_SMOOTHING:
df['Cases_m'] = df.groupby(['Country_Region', 'Province_State'])[['ConfirmedCases']].transform(lambda x: x.shift(1))
df['Cases_p'] = df.groupby(['Country_Region', 'Province_State'])[['ConfirmedCases']].transform(lambda x: x.shift(-1))
df['Cases_ave'] = 0.5*(df['ConfirmedCases']+0.5*(df['Cases_p']+df['Cases_m']))
case_cols = ['ConfirmedCases','Cases_m','Cases_p','Cases_ave']
df['Fatalities_m'] = df.groupby(['Country_Region', 'Province_State'])[['Fatalities']].transform(lambda x: x.shift(1))
df['Fatalities_p'] = df.groupby(['Country_Region', 'Province_State'])[['Fatalities']].transform(lambda x: x.shift(-1))
df['Fatalities_ave'] = 0.5*(df['Fatalities']+0.5*(df['Fatalities_p']+df['Fatalities_m']))
fata_cols = ['Fatalities','Fatalities_m','Fatalities_p','Fatalities_ave']
if USE_NEW:
df[['NewCases','NewFatalities']] = df.groupby(['Country_Region', 'Province_State'])\
[['ConfirmedCases','Fatalities']].transform(lambda x: x.diff())
 df['NewCases_m'] = df.groupby(['Country_Region', 'Province_State'])[['NewCases']].transform(lambda x: x.shift(1))
df['NewCases_p'] = df.groupby(['Country_Region', 'Province_State'])[['NewCases']].transform(lambda x: x.shift(-1))
df['NewCases_m2'] = df.groupby(['Country_Region', 'Province_State'])[['NewCases']].transform(lambda x: x.shift(2))
df['NewCases_p2'] = df.groupby(['Country_Region', 'Province_State'])[['NewCases']].transform(lambda x: x.shift(-2))
df['NewCases_ave'] = 0.2*(df['NewCases']+df['NewCases_p']+df['NewCases_m']+df['NewCases_p2']+df['NewCases_m2'])
 df['NewFatalities_m'] = df.groupby(['Country_Region', 'Province_State'])[['NewFatalities']].transform(lambda x: x.shift(1))
df['NewFatalities_p'] = df.groupby(['Country_Region', 'Province_State'])[['NewFatalities']].transform(lambda x: x.shift(-1))
df['NewFatalities_m2'] = df.groupby(['Country_Region', 'Province_State'])[['NewFatalities']].transform(lambda x: x.shift(2))
df['NewFatalities_p2'] = df.groupby(['Country_Region', 'Province_State'])[['NewFatalities']].transform(lambda x: x.shift(-2))
df['NewFatalities_ave'] =0.2*(df['NewFatalities']+df['NewFatalities_p']+df['NewFatalities_m']+df['NewFatalities_p2']+df['NewFatalities_m2'])
 date_max = df["Date"].max()
mask = df["Date"] ==date_max
mask2 = df["Date"]==date_max - timedelta(days=1)
 df.loc[mask,'Cases_ave'] = 0.75*df.loc[mask,'ConfirmedCases']+0.25*df.loc[mask,'Cases_m']
df.loc[mask,'Fatalities_ave'] = 0.75*df.loc[mask,'Fatalities'] +0.25*df.loc[mask,'Fatalities_m']
df.loc[mask,'Cases_ave'] = 0.75*df.loc[mask,'ConfirmedCases']+0.25*df.loc[mask,'Cases_m']
df.loc[mask,'Fatalities_ave'] = 0.75*df.loc[mask,'Fatalities'] +0.25*df.loc[mask,'Fatalities_m']
df.drop(['Cases_m', 'Cases_p', 'Fatalities_m','Fatalities_p','ConfirmedCases','Fatalities'],axis=1,inplace=True)
 df.rename(columns={'Cases_ave':'ConfirmedCases','Fatalities_ave':'Fatalities'},inplace=True)
 if USE_NEW:
df.loc[mask2,'NewCases_ave'] = 0.3*df.loc[mask2,'NewCases_p']+0.3*df.loc[mask2,'NewCases']+\
0.3*df.loc[mask2,'NewCases_m']+0.1*df.loc[mask2,'NewCases_m2']
df.loc[mask2,'NewFatalities_ave'] = 0.3*df.loc[mask2,'NewFatalities_p']+0.3*df.loc[mask2,'NewFatalities']+\
0.3*df.loc[mask2,'NewFatalities_m']+0.1*df.loc[mask2,'NewFatalities_m2']
df.loc[mask,'NewCases_ave'] = 0.55*df.loc[mask,'NewCases'] +0.3*df.loc[mask,'NewCases_m']+\
0.2*df.loc[mask,'NewCases_m2']
df.loc[mask,'NewFatalities_ave'] = 0.5*df.loc[mask,'NewFatalities'] +0.3*df.loc[mask,'NewFatalities_m']+\
0.2*df.loc[mask,'NewFatalities_m2']
df.drop(['NewCases_m', 'NewCases_p', 'NewFatalities_m','NewFatalities_p','NewCases','NewFatalities'],axis=1,inplace=True)
 df.drop(['NewCases_m2', 'NewCases_p2', 'NewFatalities_m2','NewFatalities_p2'],axis=1,inplace=True)
 df.rename(columns={'NewCases_ave':'NewCases','NewFatalities_ave':'NewFatalities'},inplace=True)
 if add_other:
df["NewCases_ppm"] = np.log1p(df["NewCases"]*1000000/df["Population(2020)"])
 df["NewFatalities_ppm"] = np.log1p(df["NewFatalities"]*1000000/df["Population(2020)"])
 df.fillna(0, inplace=True)
 sub_df.fillna(0,inplace =True)
 DEFAULT_VALUE = 0
if df is None:
df = DEFAULT_VALUE
if sub_df is None:
sub_df = DEFAULT_VALUE
if has_nan(df,base_features):
print("DYT-DYYYYYT: der er NAN")
 print_rows_with_nan(df)
 else:
print("HURRA, HURRA")
 if has_nan(df)>0:
print("DOOOOOOOOOT: der er NAN")
 print_rows_with_nan(df)
 print("DOT DONE")
 else:
print("HIP HIP")
<feature_engineering>
df.loc[ df['Age'] <= 16, 'Age'] = 0
df.loc[(df['Age'] > 16)&(df['Age'] <= 32), 'Age'] = 1
df.loc[(df['Age'] > 32)&(df['Age'] <= 48), 'Age'] = 2
df.loc[(df['Age'] > 48)&(df['Age'] <= 64), 'Age'] = 3
df.loc[ df['Age'] > 64, 'Age'] = 4 ;
df = df.drop(['PassengerId','Cabin','Name','SibSp','Parch','Embarked','Ticket','Last_Name'], axis=1 )
df = df[df["Date"] >= df["Date"].min() + timedelta(days=days_shift[NUM_SHIFT])].copy()
for col in TARGETS:
df[col] = np.log1p(df[col])/normfactor
df = df[df['days']>TRAIN_START_DAY] 
<choose_model_class>
df.isna().sum()
def nn_block(input_layer, size, dropout_rate, activation):
out_layer = KL.Dense(size, activation=None )(input_layer)
 out_layer = KL.Activation(activation )(out_layer)
 out_layer = KL.Dropout(dropout_rate )(out_layer)
 return out_layer
def get_model(feature_length,target_length,):
inp = KL.Input(shape=(feature_length,))
hidden_layer = nn_block(inp, 64, 0.0, "relu")
 gate_layer = nn_block(hidden_layer, 32, 0.0, "sigmoid")
 hidden_layer = nn_block(hidden_layer, 64, 0.0, "relu")
 hidden_layer = nn_block(hidden_layer, 32, 0.0, "relu")
 hidden_layer = KL.multiply([hidden_layer, gate_layer])
 out = KL.Dense(target_length, activation="linear" )(hidden_layer)
 model = tf.keras.models.Model(inputs=[inp], outputs=out)
 return model
def get_input(df,features):
return [df[features]]
def train_models(df,features,targets,save=False):
print()
print("TRAINING.Der regnes på denne model:")
 get_model(len(features),len(targets)).summary()
models = []
for i in range(NUM_MODELS):
print("PHS1")
 model = get_model(len(features),len(targets))
print("PHS2")
 model.compile(loss="mean_squared_error", optimizer=Nadam(lr=1e-4))
print("PHS3 - Targets:",targets)
 hist = model.fit(get_input(df,features), df[targets],
batch_size=2048, epochs=500, verbose=0, shuffle=True)
 print("PHS4")
 if save:
print("PHS5")
 model.save_weights("model{}.h5".format(i))
print("PHS6")
 models.append(model)
 print("PHS7")
 return models 
<compute_train_metric>
train = df[df['Survived'].notnull() ]
test = df[df['Survived'].isnull() ]
test = test.drop(['Survived'], axis=1 )
def rmse(y_true, y_pred):
return np.sqrt(mean_squared_error(y_true, y_pred))
def evaluate(df,targets):
error = 0
for col in targets:
error += rmse(df[col].values, df["pred_{}".format(col)].values)
 return np.round(error/len(targets), 5)
 def predict_one(df,features,prev_targets, models):
pred = np.zeros(( df.shape[0], 2))
for model in models:
pred += model.predict(get_input(df,features)) /len(models)
 if np.isnan(np.sum(pred)) :
print("DYYYYYYYYYYYYT: der er NAN")
 print(pred)
 pred = np.maximum(pred, df[prev_targets].values)
 pred[:, 0] = np.log1p(np.expm1(normfactor*pred[:, 0])+ 0.1)/normfactor
pred[:, 1] = np.log1p(np.expm1(normfactor*pred[:, 1])+ 0.01)/normfactor
if np.isnan(np.sum(pred)) :
print("DYT DYT DYT: der er NAN")
 return np.clip(pred, None, 15)
 def predict(test_df,features,targets,prev_targets,first_day, num_days, models, val=False):
for d in range(0, num_days):
print("DAY NO.",d)
 test_df = fill_shift_columns(test_df,targets)
 date = first_day + timedelta(days=d)
 temp_df = test_df.loc[test_df["Date"] == date].copy()
y_pred = predict_one(temp_df,features,prev_targets, models)
 for i, col in enumerate(targets):
test_df.loc[test_df["Date"] == date, col] = y_pred[:, i]
if add_other:
if USE_NEW:
test_df.loc[test_df["Date"] == date,"NewCases_ppm"] = \
np.log1p(np.expm1(y_pred[:, 0])*1000000/ \
np.expm1(test_df.loc[test_df["Date"] == date,"Population(2020)"]))
test_df.loc[test_df["Date"] == date,"NewFatalities_ppm"] = \
np.log1p(np.expm1(y_pred[:, 1])*1000000/ \
np.expm1(test_df.loc[test_df["Date"] == date,"Population(2020)"]))
else:
test_df.loc[test_df["Date"] == date,"ConfirmedCases_ppm"] = \
np.log1p(np.expm1(y_pred[:, 0])*1000000/ \
np.expm1(test_df.loc[test_df["Date"] == date,"Population(2020)"]))
test_df.loc[test_df["Date"] == date,"Fatalities_ppm"] = \
np.log1p(np.expm1(y_pred[:, 1])*1000000/ \
np.expm1(test_df.loc[test_df["Date"] == date,"Population(2020)"]))
if val:
print(evaluate(test_df[test_df["Date"] == date]))
return test_df 
<load_pretrained>
x = train.copy()
y = x.pop('Survived')
 x_test = test.copy()
x = x.values
y = y.values
x_test = x_test.values
std_scaler = StandardScaler()
x = std_scaler.fit_transform(x)
 xf_test = std_scaler.transform(x_test)
 x, x_val, y, y_val = train_test_split(x, y,test_size=0.2, shuffle=False )
all_features = base_features + shift_features
df = fill_shift_columns(df,TARGETS)
 df[all_features] = df[all_features].fillna(0)
 print("Kolonner i modelller",all_features)
 print("BEFORE TRAINING")
 print(df[(df['Country_Region']=='Germany')&(df['days'] >75)])
 final_models = train_models(df,all_features,TARGETS, save=True )<predict_on_test>
class Optimizer:
def __init__(self, metric, trials=30):
self.metric = metric
self.trials = trials
self.sampler = TPESampler()
def objective(self, trial):
model = create_model(trial)
 model.fit(x, y)
 preds = model.predict(x_val)
 if self.metric == 'acc':
return accuracy_score(y_val, preds)
 else:
return f1_score(y_val, preds)
 def optimize(self):
study = optuna.create_study(direction="maximize", sampler=self.sampler)
 study.optimize(self.objective, n_trials=self.trials)
 return study.best_params
full_df_pred= predict(full_df,all_features,TARGETS,prev_targets,ESTIMATE_FIRST_DATE,ESTIMATE_DAYS, final_models)
 for col in TARGETS:
full_df_pred[col] = np.expm1(full_df_pred[col])
<merge>
def create_model(trial):
max_depth = trial.suggest_int("max_depth", 2, 6)
 n_estimators = trial.suggest_int("n_estimators", 2, 150)
 min_samples_leaf = trial.suggest_int("min_samples_leaf", 1, 10)
 model = RandomForestClassifier(
 min_samples_leaf=min_samples_leaf,
n_estimators=n_estimators,
max_depth=max_depth, 
)
 return model
optimizer = Optimizer('f1')
 rf_f1_params = optimizer.optimize()
optimizer = Optimizer('acc')
 rf_acc_params = optimizer.optimize()
def create_model(trial):
max_depth = trial.suggest_int("max_depth", 2, 6)
 n_estimators = trial.suggest_int("n_estimators", 1, 150)
 learning_rate = trial.suggest_uniform('learning_rate', 0.0000001, 1)
 gamma = trial.suggest_uniform('gamma', 0.0000001, 1)
 subsample = trial.suggest_uniform('subsample', 0.0001, 1.0)
 model = XGBClassifier(
 learning_rate=learning_rate,
n_estimators=n_estimators,
max_depth=max_depth,
gamma=gamma,
subsample=subsample, 
)
 return model
optimizer = Optimizer('f1')
 xgb_f1_params = optimizer.optimize()
optimizer = Optimizer('acc')
 xgb_acc_params = optimizer.optimize()
def create_model(trial):
max_depth = trial.suggest_int("max_depth", 2, 6)
 n_estimators = trial.suggest_int("n_estimators", 1, 150)
 learning_rate = trial.suggest_uniform('learning_rate', 0.0000001, 1)
 num_leaves = trial.suggest_int("num_leaves", 2, 3000)
 min_child_samples = trial.suggest_int('min_child_samples', 3, 200)
 model = LGBMClassifier(
 learning_rate=learning_rate,
n_estimators=n_estimators,
max_depth=max_depth,
num_leaves=num_leaves,
min_child_samples=min_child_samples, 
)
 return model
optimizer = Optimizer('f1')
 lgb_f1_params = optimizer.optimize()
optimizer = Optimizer('acc')
 lgb_acc_params = optimizer.optimize()
def create_model(trial):
max_depth = trial.suggest_int("max_depth", 2, 6)
 min_samples_split = trial.suggest_int('min_samples_split', 2, 16)
 min_weight_fraction_leaf = trial.suggest_uniform('min_weight_fraction_leaf', 0.0, 0.5)
 min_samples_leaf = trial.suggest_int('min_samples_leaf', 1, 10)
 model = DecisionTreeClassifier(
 min_samples_split=min_samples_split,
min_weight_fraction_leaf=min_weight_fraction_leaf,
max_depth=max_depth,
min_samples_leaf=min_samples_leaf, 
)
 return model
optimizer = Optimizer('f1')
 dt_f1_params = optimizer.optimize()
optimizer = Optimizer('acc')
 dt_acc_params = optimizer.optimize()
def create_model(trial):
n_estimators = trial.suggest_int('n_estimators', 2, 200)
 max_samples = trial.suggest_int('max_samples', 1, 100)
 model = BaggingClassifier(
 n_estimators=n_estimators,
max_samples=max_samples, 
)
 return model
optimizer = Optimizer('f1')
 bc_f1_params = optimizer.optimize()
optimizer = Optimizer('acc')
 bc_acc_params = optimizer.optimize()
def create_model(trial):
n_neighbors = trial.suggest_int("n_neighbors", 2, 25)
 model = KNeighborsClassifier(n_neighbors=n_neighbors)
 return model
optimizer = Optimizer('f1')
 knn_f1_params = optimizer.optimize()
optimizer = Optimizer('acc')
 knn_acc_params = optimizer.optimize()
def create_model(trial):
n_estimators = trial.suggest_int("n_estimators", 2, 150)
 learning_rate = trial.suggest_uniform('learning_rate', 0.0005, 1.0)
 model = AdaBoostClassifier(
 n_estimators=n_estimators,
learning_rate=learning_rate, 
)
 return model
optimizer = Optimizer('f1')
 abc_f1_params = optimizer.optimize()
optimizer = Optimizer('acc')
 abc_acc_params = optimizer.optimize()
def create_model(trial):
n_estimators = trial.suggest_int("n_estimators", 2, 150)
 max_depth = trial.suggest_int("max_depth", 2, 6)
 model = ExtraTreesClassifier(
 n_estimators=n_estimators,
max_depth=max_depth,
random_state=0 
)
 return model
optimizer = Optimizer('f1')
 et_f1_params = optimizer.optimize()
optimizer = Optimizer('acc')
 et_acc_params = optimizer.optimize()
gem_targets = gem_targets[gem_targets["Date"]>=SUBMISSION_FIRST_DATE]
print(gem_targets.head(15))
values_to_submit = full_df_pred[full_df_pred["Date"]>=ESTIMATE_FIRST_DATE]
values_to_submit = values_to_submit[['Date','Country_Region','Province_State','ConfirmedCases', 'Fatalities']]
print(values_to_submit.head(15))
values_to_submit = gem_targets.append(values_to_submit, sort=False)
 print(values_to_submit.tail(10))
print(sub_df[['Date','Country_Region','Province_State','ForecastId']])
 values_to_submit = pd.merge(values_to_submit,sub_df[['Date','Country_Region','Province_State','ForecastId']], on=['Date','Country_Region','Province_State'], how='left')
 print(values_to_submit.head(20))
print(values_to_submit.tail(20))
sub2 = values_to_submit[["ForecastId"] + TARGETS]
sub2["ForecastId"] = sub2["ForecastId"].astype(np.int16)
<save_to_csv>
mdict = {
'RF': RandomForestClassifier() ,
'XGB': XGBClassifier() ,
'LGBM': LGBMClassifier() ,
'DT': DecisionTreeClassifier() ,
'KNN': KNeighborsClassifier() ,
'BC': BaggingClassifier() ,
'OARF': RandomForestClassifier(**rf_acc_params),
'OFRF': RandomForestClassifier(**rf_f1_params),
'OAXGB': XGBClassifier(**xgb_acc_params),
'OFXGB': XGBClassifier(**xgb_f1_params),
'OALGBM': LGBMClassifier(**lgb_acc_params),
'OFLGBM': LGBMClassifier(**lgb_f1_params),
'OADT': DecisionTreeClassifier(**dt_acc_params),
'OFDT': DecisionTreeClassifier(**dt_f1_params),
'OAKNN': KNeighborsClassifier(**knn_acc_params),
'OFKNN': KNeighborsClassifier(**knn_f1_params),
'OABC': BaggingClassifier(**bc_acc_params),
'OFBC': BaggingClassifier(**bc_f1_params),
'OAABC': AdaBoostClassifier(**abc_acc_params),
'OFABC': AdaBoostClassifier(**abc_f1_params),
'OAET': ExtraTreesClassifier(**et_acc_params),
'OFET': ExtraTreesClassifier(**et_f1_params),
'LR': LogisticRegression(max_iter=1000),
'ABC': AdaBoostClassifier() ,
'SGD': SGDClassifier() ,
'ET': ExtraTreesClassifier() ,
'GB': GradientBoostingClassifier() ,
'RDG': RidgeClassifier() ,
'PCP': Perceptron(max_iter=1500),
'PAC': PassiveAggressiveClassifier()
}
sub2.sort_values("ForecastId", inplace=True)
 sub2.to_csv("submission.csv", index=False)
<merge>
def create_model(trial):
model_names = list()
models_list = [
'RF', 'XGB', 'LGBM', 'DT',
'KNN', 'BC', 'OARF', 'OFRF',
'OAXGB', 'OFXGB', 'OALGBM',
'OFLGBM', 'OADT', 'OFDT',
'OAKNN', 'OFKNN', 'OABC',
'OFBC', 'OAABC', 'OFABC',
'OAET', 'OFET', 'LR',
'ABC', 'SGD', 'ET',
'GB', 'RDG',
'PCP', 'PAC'
]
head_list = [
'RF',
'XGB',
'LGBM',
'DT',
'KNN',
'BC',
'LR',
'ABC',
'SGD',
'ET',
'GB',
'RDG',
'PCP',
'PAC'
]
n_models = trial.suggest_int("n_models", 2, 6)
 for i in range(n_models):
model_item = trial.suggest_categorical('model_{}'.format(i), models_list)
 if model_item not in model_names:
model_names.append(model_item)
 folds = trial.suggest_int("folds", 2, 6)
 model = SuperLearner(
 folds=folds, 
)
 models = [
mdict[item] for item in model_names
]
model.add(models)
 head = trial.suggest_categorical('head', head_list)
 model.add_meta(
 mdict[head] 
)
 return model
def objective(trial):
model = create_model(trial)
 model.fit(x, y)
 preds = model.predict(x_val)
 score = accuracy_score(y_val, preds)
 return score
study = optuna.create_study(
 direction="maximize",
sampler= TPESampler() 
)
 study.optimize(
 objective,
n_trials=50
)
full_df_pred['Cases_Estimate'] = full_df_pred['ConfirmedCases']
full_df_pred['Fatalities_Estimate'] = full_df_pred['Fatalities']
full_df_pred2 = full_df_pred[['Date','Country_Region','Province_State']+TARGETS]
full_df2 = full_df[['Date','Country_Region','Province_State']+TARGETS]
full_df3 = pd.merge(full_df2,full_df_pred[['Date','Country_Region','Province_State','Cases_Estimate','Fatalities_Estimate']], on=['Date','Country_Region','Province_State'], how='left')
 mask = full_df3['Cases_Estimate'].notna()
full_df3.loc[mask,'CasesConfirmed'] = full_df3.loc[mask,'Cases_Estimate']
full_df3.loc[mask,'Fatalities'] = full_df3.loc[mask,'Fatalities_Estimate']
split_on = 'Country_Region'
split_values = full_df3[split_on].unique()
kolonne = 'CasesConfirmed'
for imin in range(0,175,25):
plt.figure(figsize=(30,30))
imax = imin+25
for i in range(imin,imax):
plt.subplot(5,5,i-imin+1)
 idx = i
df_interest = full_df3[full_df3[split_on]==split_values[idx]].reset_index(drop=True)
 tmp = df_interest[kolonne].values
sns.lineplot(x=df_interest['Date'], y=tmp, label='pred')
 df_interest2 = full_df3[(full_df3[split_on]==split_values[idx])&(full_df3['Date']<=GIVEN_LAST_DATE)].reset_index(drop=True)
 sns.lineplot(x=df_interest2['Date'].values, y=df_interest2[kolonne].values, label='true')
 plt.title(split_on+' '+str(split_values[idx]))
plt.show() 
<load_from_csv>
model = SuperLearner(
 folds=folds, 
)
 models = [
mdict[item] for item in result
]
model.add(models)
 model.add_meta(mdict[head])
 xf = train.copy()
yf = xf.pop('Survived')
 xf = xf.values
xf = std_scaler.fit_transform(xf)
 yf = yf.values
model.fit(xf, yf )
train_data = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/train.csv')
 test_data = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/test.csv')
 submission_csv = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/submission.csv' )<data_type_conversions>
preds = model.predict(xf_test ).astype(int)
 output = pd.DataFrame({ 'PassengerId': PassengerId,
'Survived': preds })
 output.to_csv('boosted_tree.csv', index=False)