Kaggle competition: COVID19 Global Forecasting (Week 2)
with open("data_all.pickle", "wb")as f:
pickle.dump(data_all, f )<load_pretrained>
dirname = '/kaggle/input/covid19-global-forecasting-week-2/'
trainname = 'train.csv'
trainpath = dirname + trainname
traindata_raw = pd.read_csv(trainpath)
 traindata = collections.defaultdict(lambda :collections.defaultdict(list))
traindata_country = collections.defaultdict(lambda :collections.defaultdict(lambda :[0,0]))
traindata_state = collections.defaultdict(lambda :collections.defaultdict(lambda :[0,0]))
traindata_country_onlymainland = collections.defaultdict(lambda :collections.defaultdict(lambda :[0,0]))
for i in range(len(traindata_raw)) :
country = traindata_raw['Country_Region'][i]
state = traindata_raw['Province_State'][i]
date = traindata_raw['Date'][i]
cases = traindata_raw['ConfirmedCases'][i]
fata = traindata_raw['Fatalities'][i]
traindata[country][date].append(( state, cases, fata))
traindata_country[country][date][0] += cases
traindata_country[country][date][1] += fata
if state != state:
traindata_country_onlymainland[country][date][0] += cases
traindata_country_onlymainland[country][date][1] += fata
state_ = country + ':' + state if state == state else country + ':'
traindata_state[state_][date][0] += cases
traindata_state[state_][date][1] += fata
with open("data_all.pickle", "rb")as f:
data_all = pickle.load(f )<drop_column>
polynomial = PolynomialFeatures(degree = 5)
 poly_model = LinearRegression()
predictdata_state = collections.defaultdict(lambda :[[],[]])
 for state in traindata_state:
state_history = [[],[]]
for date in traindata_state[state]:
state_history[0].append(traindata_state[state][date][0])
 state_history[1].append(traindata_state[state][date][1])
 i = 0
for perspective in state_history:
perspective_train = [i for i in perspective[:] if i!=0]
perspective = [i for i in perspective if i!=0]
x_all = np.array(range(1, len(perspective)+ 31))
x = x_all[:len(perspective_train)]
y = np.array(perspective_train)
 if len(perspective_train)< 3:
if not perspective_train:
predict_y = np.zeros(len(x_all[len(perspective_train):]))
else:
predict_y = np.array([perspective_train[-1]]*(len(x_all[len(perspective_train):])))
 predictdata_state[state][i] = predict_y
i =(i+1)% 2
continue
if True:
def funmi(x, a, b, c):
return(x/a)**b + c
def funexp(x,a,b,c):
return(a**(x + b)) * c
def funsigmoid(x, a, b, c):
s = a /(1 + np.exp(-(x + b)/ c))
return s
popt, pcov = curve_fit(funsigmoid, x, y, maxfev = 10000)
 predict_x = x_all[len(perspective_train):]
predict_y = [funsigmoid(i, popt[0], popt[1], popt[2])for i in predict_x]
if predict_y[0] < perspective_train[-1]:
predict_y = [i +(perspective_train[-1] - predict_y[0])for i in predict_y]
predict_y = [round(i)for i in predict_y]
predictdata_state[state][i] = predict_y
else:
x = x.reshape(-1,1)
 x_transformed = polynomial.fit_transform(x)
 poly_model.fit(x_transformed, y)
 predict_x = x_all[len(perspective_train):]
predict_y = poly_model.predict(polynomial.fit_transform(np.array(predict_x ).reshape(-1,1)))
 predict_y = [round(i)for i in predict_y]
predictdata_state[state][i] = predict_y
i =(i+1)% 2
print(predictdata_state)
features_to_drop = ["sig_id", "cp_type"]
data_all.drop(features_to_drop, axis = 1, inplace = True)
 try:
targets.drop("sig_id", axis = 1, inplace = True)
 except:
pass
train_df = data_all[: train.shape[0]]
train_df.reset_index(drop = True, inplace = True)
 test_df = data_all[train_df.shape[0]: ]
test_df.reset_index(drop = True, inplace = True )<choose_model_class>
testname = 'test.csv'
testpath = dirname + testname
testdata = pd.read_csv(testpath)
 submname = 'submission.csv'
submpath = dirname + submname
submdata = pd.read_csv(submpath)
 res = []
ind = 0
found = False
for i in range(len(testdata)) :
forecastid = testdata['ForecastId'][i]
state = testdata['Province_State'][i]
country = testdata['Country_Region'][i]
date = testdata['Date'][i]
state_ = country + ':' + state if state == state else country + ':'
if date == '2020-03-19':
found = True
if date == '2020-04-01':
found = False
ind = 0
if found:
res.append(traindata_state[state_][date])
 else:
res.append([int(predictdata_state[state_][0][ind]), int(predictdata_state[state_][1][ind])])
 ind += 1
subm_cases = [i[0] for i in res]
subm_fata = [i[1] for i in res]
submdata['ConfirmedCases'] = subm_cases
submdata['Fatalities'] = subm_fata
submdata.to_csv('submission.csv', index = False )