Kaggle competition: COVID19 Global Forecasting (Week 2)
train_data = pd.read_csv('.. /input/ghouls-goblins-and-ghosts-boo/train.csv.zip')
 test_data = pd.read_csv('.. /input/ghouls-goblins-and-ghosts-boo/test.csv.zip' )<count_missing_values>
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
train_data.isnull().sum()<prepare_x_and_y>
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)< 2:
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 'China' in state and 'Macau' not in state and 'Hong Kong' not in state and 'Liaoning' not in state or 'Korea, South' in state:
def funexp(x, a, b, c):
return(x/a)**b + c
def funmi(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
p0 = [max(perspective_train), np.median(x), 5]
popt, pcov = curve_fit(funsigmoid, x, y, p0)
 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
X = train_data.drop(['id','type'],axis=1)
 y = train_data['type']<categorify>
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 )