Kaggle competition: COVID19 Global Forecasting (Week 4)
content_agg = train_df.groupby('content_id')[target].agg(['sum', 'count','var'])
 task_container_agg = train_df.groupby('task_container_id')[target].agg(['sum', 'count','var'])
 content_agg=content_agg.astype('float32')
 task_container_agg=task_container_agg.astype('float32' )<data_type_conversions>
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.ensemble import ExtraTreesRegressor
from sklearn.metrics import mean_squared_log_error
train_df['task_container_uncor_count'] = train_df['task_container_id'].map(task_container_agg['count']-task_container_agg['sum'] ).astype('int32')
 train_df['task_container_cor_count'] = train_df['task_container_id'].map(task_container_agg['sum'] ).astype('int32')
 train_df['task_container_std'] = train_df['task_container_id'].map(task_container_agg['var'] ).astype('float16')
 train_df['task_container_correctness'] = train_df['task_container_id'].map(task_container_agg['sum'] / task_container_agg['count'])
 train_df.task_container_correctness=train_df.task_container_correctness.astype('float16' )<groupby>
df=pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/train.csv', index_col='Id')
 dtest=pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/test.csv', index_col='ForecastId' )
content_elapsed_time_agg=train_df.groupby('content_id')['prior_question_elapsed_time'].agg(['mean'])
 content_had_explanation_agg=train_df.groupby('content_id')['prior_question_had_explanation'].agg(['mean'] )<train_model>
y1=df['ConfirmedCases']
y2=df['Fatalities']
df.drop('ConfirmedCases', axis=1, inplace=True)
 df.drop('Fatalities', axis=1, inplace=True )
print('start questions data...' )<load_from_csv>
df['check']=1
dtest['check']=2
combo=pd.concat([df,dtest])
 def date_split(date):
d=date.str.split('-', n=1, expand=True)
 return d[1]
combo['MM_DD']= date_split(combo['Date'])
 combo['Province_State']=combo['Province_State'].fillna(0 )
questions_df = pd.read_csv(
 '.. /input/riiid-test-answer-prediction/questions.csv',
usecols=[0, 1,3,4],
dtype={'question_id': 'int16','bundle_id': 'int16', 'part': 'int8','tags': 'str'}
)<groupby>
le=LabelEncoder()
combo['MM_DD']=le.fit_transform(combo['MM_DD'])
 combo=pd.get_dummies(combo)
 df1=combo[combo['check']==1]
dtest1=combo[combo['check']==2]
bundle_agg = questions_df.groupby('bundle_id')['question_id'].agg(['count'] )<data_type_conversions>
df1.drop('check', axis=1, inplace=True)
 dtest1.drop('check', axis=1, inplace=True )
questions_df['content_sub_bundle'] = questions_df['bundle_id'].map(bundle_agg['count'] ).astype('int8' )<set_options>
X_train1, X_valid1, y_train1, y_valid1 = train_test_split(df1, y1, train_size=0.8, test_size=0.2, random_state=0)
 X_train2, X_valid2, y_train2, y_valid2 = train_test_split(df1, y2, train_size=0.8, test_size=0.2, random_state=0 )
questions_df['tags'].fillna('188', inplace=True )<string_transform>
ef= ExtraTreesRegressor(n_estimators=15, random_state=3)
 p2=ef.fit(X_train1, y_train1 ).predict(X_valid1)
 rmsle2=np.sqrt(mean_squared_log_error(y_valid1 , p2))
print(rmsle2 )
def gettags(tags,num):
tags_splits=tags.split(" ")
 result=''
for t in tags_splits:
x=int(t)
 if(x<32*(num+1)and x>=32*num):
result=result+' '+t
return result<categorify>
ef2= ExtraTreesRegressor(n_estimators=29,criterion='friedman_mse', random_state=7)
 p3=ef2.fit(X_train2, y_train2 ).predict(X_valid2)
 rmsle3=np.sqrt(mean_squared_log_error(y_valid2 , p3))
print(rmsle3 )
for num in range(0,6):
questions_df["tags"+str(num)] = questions_df["tags"].apply(lambda row: gettags(row,num))
le = LabelEncoder()
le.fit(np.unique(questions_df['tags'+str(num)].values))
questions_df['tags'+str(num)]=questions_df[['tags'+str(num)]].apply(le.transform )<data_type_conversions>
pre1=ef.fit(df1,y1 ).predict(dtest1)
 pre2=ef2.fit(df1,y2 ).predict(dtest1 )
questions_df_dict = {
'tags0': 'int8',
'tags1': 'int8',
'tags2': 'int8',
'tags3': 'int8',
'tags4': 'int8',
'tags5': 'int8',
}
questions_df = questions_df.astype(questions_df_dict )<drop_column>
output=pd.DataFrame({'ForecastId': dtest.index, 'ConfirmedCases':pre1, 'Fatalities':pre2})
 output.to_csv('submission.csv', index=False )