Kaggle competition: COVID19 Global Forecasting (Week 3)
catb = CatbModel(data, test, target, features, categoricals=categoricals, n_splits=10,
cv_method="StratifiedKFold", group=None, task="classification", scaler=None, verbose=True )<load_from_csv>
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
from datetime import datetime
from sklearn import preprocessing
from xgboost import XGBRegressor
from sklearn.tree import DecisionTreeRegressor
from lightgbm import LGBMRegressor
from catboost import CatBoostRegressor
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
submission_df = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv')
 submission_df['Pred'] = test_preds
submission_df.head()<save_to_csv>
path = '/kaggle/input/covid19-global-forecasting-week-3/'
train = pd.read_csv(path+'train.csv')
 test = pd.read_csv(path+'test.csv' )
submission_df.to_csv('submission.csv', index=False )<set_options>
train.isnull().sum()
%reload_ext autoreload
%autoreload 2
%matplotlib inline<import_modules>
test.isnull().sum()
from fastai import *
from fastai.vision import *
import pandas as pd
from fastai.utils.mem import *<define_variables>
%matplotlib inline
path = Path('/kaggle/input/iwildcam-2019-fgvc6')
 debug =1
if debug:
train_pct=0.04
else:
train_pct=0.5<load_from_csv>
train["Date"] = train["Date"].apply(lambda x: x.replace("-",""))
train["Date"] = train["Date"].astype(int)
 test["Date"] = test["Date"].apply(lambda x: x.replace("-",""))
test["Date"] = test["Date"].astype(int )
train_df = pd.read_csv(path/'train.csv')
 train_df = pd.concat([train_df['id'],train_df['category_id']],axis=1,keys=['id','category_id'])
 train_df.head()<load_from_csv>
EMPTY_VAL = "EMPTY_VAL"
def fillState(state, country):
if state == EMPTY_VAL: return country
return state
test_df = pd.read_csv(path/'test.csv')
 test_df = pd.DataFrame(test_df['id'])
 test_df['predicted'] = 0
test_df.head() 
<init_hyperparams>
train['Province_State'].fillna(EMPTY_VAL, inplace=True)
 train['Province_State'] = train.loc[:, ['Province_State', 'Country_Region']].apply(lambda x : fillState(x['Province_State'], x['Country_Region']), axis=1)
 test['Province_State'].fillna(EMPTY_VAL, inplace=True)
 test['Province_State'] = test.loc[:, ['Province_State', 'Country_Region']].apply(lambda x : fillState(x['Province_State'], x['Country_Region']), axis=1 )
free = gpu_mem_get_free_no_cache()
if free > 8200: bs=64
else: bs=32
print(f"using bs={bs}, have {free}MB of GPU RAM free")
 tfms = get_transforms(max_rotate=20, max_zoom=1.3, max_lighting=0.4, max_warp=0.4,
p_affine=1., p_lighting=1.) 
<create_dataframe>
le = LabelEncoder()
train['Country_Region'] = le.fit_transform(train['Country_Region'])
 train['Province_State'] = le.fit_transform(train['Province_State'])
 test['Country_Region'] = le.fit_transform(test['Country_Region'])
 test['Province_State'] = le.fit_transform(test['Province_State'] )
data = get_data(224, bs, 'zeros' )<choose_model_class>
def build_model_1() :
model = RandomForestRegressor(n_estimators = 100, random_state = 0)
 return model
def build_model_2() :
model = XGBRegressor(n_estimators=1000)
 return model
def build_model_3() :
model = DecisionTreeRegressor(random_state=1)
 return model
def build_model_4() :
model = LogisticRegression()
return model
def build_model_5() :
model = LinearRegression()
return model
def build_model_6() :
model = LGBMRegressor(random_state=5)
 return model
def build_model_7() :
model = LGBMRegressor(iterations=2)
 return model
gc.collect()
wd=1e-1
learn = cnn_learner(data, models.resnet34, metrics=error_rate, bn_final=True, wd=wd)
 learn.model_dir= '/kaggle/working/'<prepare_output>
out = pd.DataFrame({'ForecastId': [], 'ConfirmedCases': [], 'Fatalities': []} )
data = get_data(352,bs)
 learn.data = data 
<train_model>
for country in range(len(countries)) :
country_train = train.loc[train['Country_Region'] == countries[country]]
country_test = test.loc[test['Country_Region'] == countries[country]]
print("Current Country: ", countries[country])
 xtrain = country_train[['Country_Region', 'Province_State', 'Date']].to_numpy()
y1train = country_train[['ConfirmedCases']].to_numpy()
y2train = country_train[['Fatalities']].to_numpy()
xtest = country_test[['Country_Region', 'Province_State', 'Date']].to_numpy()
y1train = y1train.reshape(-1)
 y2train = y2train.reshape(-1)
 model1 = build_model_2()
model1.fit(xtrain, y1train)
 res_cnf_cls = model1.predict(xtest)
 model2 = build_model_2()
model2.fit(xtrain, y2train)
 res_fac = model2.predict(xtest)
 country_test_Id = country_test.loc[:, 'ForecastId']
country_test_Id = country_test_Id.astype(int)
 ans = pd.DataFrame({'ForecastId': country_test_Id, 'ConfirmedCases': res_cnf_cls, 'Fatalities': res_fac})
 out = pd.concat([out, ans], axis=0)
learn.fit_one_cycle(2, max_lr=slice(1e-6,1e-4))
learn.save('352' )<train_model>
out["ForecastId"] = out["ForecastId"].astype(int )
<save_model><EOS> 
out.to_csv('submission.csv', index=False )