Kaggle competition: COVID19 Global Forecasting (Week 4)
<SOS> metric: MCRMSLE Kaggle data source: covid19-global-forecasting-(week-4)<compute_train_metric>
warnings.filterwarnings('ignore')
 %matplotlib inline
n_folds = 5
def get_cv_scores(model, X, y, print_scores=True):
kf = KFold(n_folds, shuffle=True, random_state=42 ).get_n_splits(X)
 rmse = np.sqrt(-cross_val_score(model, X, y, scoring="neg_mean_squared_error", cv = kf))
if print_scores:
print(f'Root mean squared error: {rmse.mean() :.3f}({rmse.std() :.3f})')
 return [rmse]<define_search_model>
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
train = pd.read_csv('.. /input/covid19-global-forecasting-week-4/train.csv')
 test = pd.read_csv('.. /input/covid19-global-forecasting-week-4/test.csv')
 Y1=train['ConfirmedCases']
Y2=train['Fatalities']
lasso_model = make_pipeline(RobustScaler() , Lasso(alpha=0.0005, random_state=1))
rf = RandomForestRegressor()
gbr = GradientBoostingRegressor()
xgb_model = xgb.XGBRegressor()
lgb_model = lgb.LGBMRegressor()
for model in [lasso_model, rf, gbr, xgb_model, lgb_model]:
print(str(model))
get_cv_scores(model, df_train, y_train )<train_model>
train['Complete_Date'] = train['Date'].astype('datetime64[ns]')
 test['Complete_Date'] = test['Date'].astype('datetime64[ns]')
 month = [int(el[5:7])for el in list(train['Date'].values)]
day = [int(el[8:10])for el in list(train['Date'].values)]
month_test = [int(el[5:7])for el in list(test['Date'].values)]
day_test = [int(el[8:10])for el in list(test['Date'].values)]
df_month= pd.DataFrame(month, columns= ['Month'])
 df_day= pd.DataFrame(day, columns= ['Day'])
 df_month_test= pd.DataFrame(month_test, columns= ['Month'])
 df_day_test= pd.DataFrame(day_test, columns= ['Day'])
 train=pd.concat([train, df_month], axis=1)
 test=pd.concat([test, df_month_test], axis=1)
 train=pd.concat([train, df_day], axis=1)
 test=pd.concat([test, df_day_test], axis=1)
 train['Date']=train['Month']*100+train['Day']
test['Date']=test['Month']*100+test['Day']
class WeightedAveragedModels(BaseEstimator, RegressorMixin, TransformerMixin):
def __init__(self, models, weights):
self.models = models
self.weights = weights
assert sum(self.weights)==1
def fit(self, X, y):
self.models_ = [clone(x)for x in self.models]
for model in self.models_:
model.fit(X, y)
 return self
def predict(self, X):
predictions = np.column_stack([
model.predict(X)for model in self.models_
])
 return np.sum(predictions*self.weights, axis=1 )<choose_model_class>
train['Province_State'].fillna('',inplace=True)
 test['Province_State'].fillna('',inplace=True)
 train['Province_State']=train['Province_State'].astype(str)
 test['Province_State']=test['Province_State'].astype(str)
 y= train['Country_Region']+train['Province_State']
y= pd.DataFrame(y, columns= ['Place'])
 y_test= test['Country_Region']+test['Province_State']
y_test= pd.DataFrame(y_test, columns= ['Place'])
 train=pd.concat([train, y], axis=1)
 test=pd.concat([test, y_test], axis=1 )
weighted_average_model = WeightedAveragedModels([gbr, lasso_model, xgb_model], [0.3, 0.45, 0.25])
 get_cv_scores(weighted_average_model, df_train, y_train); 
<compute_test_metric>
Country_df=train["Place"]
ConfirmedCases_df=train["ConfirmedCases"]
Country_df.to_numpy()
ConfirmedCases_df.to_numpy()
Country=Country_df[0]
NbDay = pd.DataFrame(columns=['NbDay'])
 day=0
count=0
for x in train["Month"]:
if(ConfirmedCases_df[count]==0):
NbDay = NbDay.append({'NbDay': int(0)}, ignore_index=True)
 count=count+1
else:
if(Country_df[count]==Country):
day=day+1
NbDay = NbDay.append({'NbDay': int(day)}, ignore_index=True)
 count=count+1
else:
Country=Country_df[count]
day=1
NbDay = NbDay.append({'NbDay': int(day)}, ignore_index=True)
 count=count+1
train=pd.concat([train, NbDay], axis=1)
 NbDay_test_array=np.zeros(test.shape[0])
 i=0
df=test["Place"]
Place_array=df.to_numpy()
for t in test.Date:
place=Place_array[i]
if t==402:
row=train.loc[(train['Place'] == place)&(train['Date'] ==t)]
row=row.to_numpy()
NbDay_test_array[i]= row[0][10]
else:
NbDay_test_array[i]=0
i=i+1
NbDay=pd.DataFrame(NbDay_test_array, columns=['NbDay1'])
 test=pd.concat([test,NbDay], axis=1)
 Country_df=test["Place"]
NbDay_df=test['NbDay1']
Country_df.to_numpy()
day_array=NbDay_df.to_numpy()
Country=Country_df[0]
NbDay = pd.DataFrame(columns=['NbDay'])
 day=0
count=0
for t in test["Date"]:
if(t==402):
day=day_array[count]
NbDay = NbDay.append({'NbDay': int(day)}, ignore_index=True)
 count=count+1
else:
day=day+1
NbDay = NbDay.append({'NbDay': int(day)}, ignore_index=True)
 count=count+1
test=pd.concat([test,NbDay], axis=1 )
def rmsle(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred))<predict_on_test>
train=train[['Place','Country_Region','NbDay','ConfirmedCases','Fatalities']]
test=test[['Place','Country_Region','NbDay','ForecastId']]
weighted_average_model.fit(df_train.values, y_train)
 weighted_train_pred = weighted_average_model.predict(df_train.values)
 weighted_pred = np.expm1(weighted_average_model.predict(df_test.values))
print(rmsle(y_train, weighted_train_pred))<save_to_csv>
country_array=train['Place'].to_numpy()
def distinct_values(country_array):
liste=[]
liste.append(country_array[0])
 for i in range(1,len(country_array)) :
if country_array[i]!=country_array[i-1]:
liste.append(country_array[i])
 return liste
Countries_liste=distinct_values(country_array)
 len(Countries_liste )
sub = pd.DataFrame()
sub['Id'] = Id_test
sub['SalePrice'] = weighted_pred
sub.to_csv('submission.csv',index=False )<set_options>
def exponentiate_alpha(column,v):
array=column.to_numpy()
string='NbDay'+str(v)
 array=np.power(v,array)
 frame=pd.DataFrame(array, columns=[string])
 return frame
def product(column1,column2,number):
array=column1.to_numpy()
array2=column2.to_numpy()
string='Product'+str(number)
 array=np.multiply(array,array2)
 frame=pd.DataFrame(array, columns=[string])
 return frame
%matplotlib inline 
<load_from_csv>
df1=exponentiate_alpha(train['NbDay'],1.0001 )
raw_train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv',index_col='Id')
 raw_test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv',index_col='Id')
 raw_train.info()
raw_test.info()<create_dataframe>
df2=product(df1,train['NbDay'],1 )
train = raw_train.copy()
test = raw_test.copy()
train_test = pd.concat([train.drop(columns=['SalePrice']),test] )<sort_values>
train['NbDay_exp']=df1
train['Product']=df12
train.corr() ['SalePrice'].sort_values(key=abs,ascending=False )<drop_column>
model = xgboost.XGBRegressor(colsample_bytree=0.4,
gamma=0,
learning_rate=0.07,
max_depth=5,
min_child_weight=1.5,
n_estimators=10000,
reg_alpha=0.75,
reg_lambda=0.45,
subsample=0.6,
seed=42,
objective='reg:squarederror',
eval_metric='rmse' )
train_test = train_test.drop(train[(train['GrLivArea']>4000)&(train['SalePrice']<300000)].index)
 train = train.drop(train[(train['GrLivArea']>4000)&(train['SalePrice']<300000)].index )<sort_values>
ConfirmedCasesPredictions=[]
i=1
for country in Countries_liste:
train_=train[train['Place']==country][['NbDay']]
y_=train[train['Place']==country]['ConfirmedCases']
train_=train_.astype(float)
 test_=test[test['Place']==country][['NbDay']]
test_=test_.astype(float)
 model.fit(train_, y_)
 y_pred = model.predict(test_)
 y_pred = list(y_pred)
 ConfirmedCasesPredictions+=y_pred
i=i+1
train.isnull().sum().sort_values(ascending=False ).head(20 )<categorify>
ConfirmedCases=np.array(ConfirmedCasesPredictions)
 ConfirmedCases=pd.DataFrame(ConfirmedCases, columns=['ConfirmedCases'])
 test['ConfirmedCases']=ConfirmedCases
for col in ['PoolQC','MiscFeature','Alley','Fence','BsmtQual','BsmtCond','BsmtExposure',
'BsmtFinType1','BsmtFinType2','FireplaceQu','GarageType','GarageFinish',
'GarageQual','GarageCond','MasVnrType']:
train_test[col].fillna('None',inplace=True)
 for col in ['Functional','Electrical','KitchenQual','Exterior1st','Exterior2nd','SaleType','MSZoning','Utilities']:
train_test[col].fillna(train_test[col].mode() [0],inplace=True)
 for col in ['GarageYrBlt', 'GarageArea', 'GarageCars','MasVnrArea','BsmtFinSF1',
'BsmtUnfSF','BsmtFinSF2','BsmtHalfBath','BsmtHalfBath','BsmtFullBath','TotalBsmtSF']:
train_test[col].fillna(0,inplace=True )<sort_values>
ConfirmedFatalities=[]
for country in Countries_liste:
train_=train[train['Place']==country][['NbDay','ConfirmedCases']]
y_=train[train['Place']==country]['Fatalities']
train_=train_.astype(float)
 test_=test[test['Place']==country][['NbDay','ConfirmedCases']]
test_=test_.astype(float)
 model.fit(train_, y_)
 y_pred = model.predict(test_)
 y_pred = list(y_pred)
 ConfirmedFatalities+=y_pred
train.corr() ['LotFrontage'].sort_values(key=abs, ascending=False )<prepare_x_and_y>
Fatalities=np.array(ConfirmedFatalities)
 Fatalities=pd.DataFrame(Fatalities, columns=['Fatalities'])
 test['Fatalities']=Fatalities
<categorify><EOS> 
sub = pd.DataFrame()
sub['ForecastId'] = test['ForecastId']
sub['ConfirmedCases'] = test['ConfirmedCases']
sub['Fatalities'] = test['Fatalities']
sub.to_csv('submission.csv', index=False )