Kaggle competition: IESB Norte - IGM - Maio 2019
class InferenceConfig(coco.CocoConfig):
GPU_COUNT = 1
IMAGES_PER_GPU = 1
IMAGE_MIN_DIM = 256
IMAGE_MAX_DIM = 256
config = InferenceConfig()
config.display()<load_pretrained>
df2 = pd.read_csv(".. /input/train.csv", parse_dates=[0])
 test = pd.read_csv(".. /input/test.csv", parse_dates=[0] )
model = modellib.MaskRCNN(mode="inference", config=config, model_dir=ROOT_DIR)
 model.load_weights(COCO_MODEL_PATH, by_name=True )<define_variables>
df3 = df2.copy()
test3 = test.copy()
class_names = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',
'bus', 'train', 'truck', 'boat', 'traffic light',
'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird',
'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear',
'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
'kite', 'baseball bat', 'baseball glove', 'skateboard',
'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
'keyboard', 'cell phone', 'microwave', 'oven', 'toaster',
'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
'teddy bear', 'hair drier', 'toothbrush']<define_variables>
df['populacao']= df['populacao'].str.replace(',','', regex=False)
 df['populacao']= df['populacao'].str.replace('(2)','', regex=False)
 df['populacao']= df['populacao'].str.replace('(1)','', regex=False)
 df['populacao']= df['populacao'].str.replace('() ','', regex=False)
 df['populacao']= df.populacao.astype(float )
IMAGE_DIR = "/kaggle/input/test/"<load_from_csv>
df['area']= df['area'].str.replace(',','', regex=False)
 df['area']= df.area.astype(float )
class_lookup_df = pd.read_csv("./challenge-2019-classes-description-segmentable.csv", header=None)
 empty_submission_df = pd.read_csv("input/sample_empty_submission.csv" )<feature_engineering>
df['densidade_dem']= df['densidade_dem'].str.replace(',','', regex=False)
 df['densidade_dem']= df.densidade_dem.astype(float )
class_lookup_df.columns = ["encoded_label","label"]
class_lookup_df['label'] = class_lookup_df['label'].str.lower()
class_lookup_df.head()<categorify>
for col in df.columns:
if df[col].dtype == 'object':
df[col] = df[col].astype('category' ).cat.codes
def encode_binary_mask(mask: np.ndarray)-> t.Text:

 if mask.dtype != np.bool:
raise ValueError("encode_binary_mask expects a binary mask, received dtype == %s" % mask.dtype)
 mask = np.squeeze(mask)
 if len(mask.shape)!= 2:
raise ValueError("encode_binary_mask expects a 2d mask, received shape == %s" % mask.shape)
 mask_to_encode = mask.reshape(mask.shape[0], mask.shape[1], 1)
 mask_to_encode = mask_to_encode.astype(np.uint8)
 mask_to_encode = np.asfortranarray(mask_to_encode)
 encoded_mask = coco_mask.encode(mask_to_encode)[0]["counts"]
binary_str = zlib.compress(encoded_mask, zlib.Z_BEST_COMPRESSION)
 base64_str = base64.b64encode(binary_str)
 return base64_str<define_variables>
col_nan=df.isnull().any(axis=0)
 col_nan
ImageID_list = []
ImageWidth_list = []
ImageHeight_list = []
PredictionString_list = []
for num, row in tqdm(empty_submission_df.iterrows() , total=len(empty_submission_df)) :
filename = row["ImageID"] + ".jpg"
image = skimage.io.imread(os.path.join(IMAGE_DIR, filename))
results = model.detect([image])
 r = results[0]
height = image.shape[0]
width = image.shape[1]
PredictionString = ""
for i in range(len(r["class_ids"])) :
class_id = r["class_ids"][i]
roi = r["rois"][i]
mask = r["masks"][:,:,i]
confidence = r["scores"][i]
encoded_mask = encode_binary_mask(mask)
 labelname = class_names[r['class_ids'][i]]
if class_lookup_df[class_lookup_df["label"] == labelname].shape[0] == 0:
continue
encoded_label = class_lookup_df[class_lookup_df["label"] == labelname]["encoded_label"].item()
PredictionString += encoded_label
PredictionString += " "
PredictionString += str(confidence)
 PredictionString += " "
PredictionString += encoded_mask.decode()
PredictionString += " "
ImageID_list.append(row["ImageID"])
 ImageWidth_list.append(width)
 ImageHeight_list.append(height)
 PredictionString_list.append(PredictionString )<create_dataframe>
df['servidores']= np.where(df['servidores'].isna() , round(df['populacao']*df.servidores.mean() /df.populacao.mean() , 0),
df['servidores'] )
results=pd.DataFrame({"ImageID":ImageID_list,
"ImageWidth":ImageWidth_list,
"ImageHeight":ImageHeight_list,
"PredictionString":PredictionString_list
} )<save_to_csv>
df['gasto_pc_educacao'] = np.where(df['gasto_pc_educacao'].isna() , round(df['populacao']*df.gasto_pc_educacao.mean() /df.populacao.mean() , 2),
df['gasto_pc_educacao'] )
results.to_csv("submission.csv", index=False )<set_options>
df['comissionados_por_servidor']= df['comissionados']/df['servidores']
color = sns.color_palette()
py.init_notebook_mode(connected=True)
 init_notebook_mode(connected=True)
 offline.init_notebook_mode()
cf.go_offline()
eng_stopwords = stopwords.words('english')
<load_from_csv>
df['densidade_dem'] = np.where(df['densidade_dem'].isna() , round(df['populacao']/df['area'], 1),
df['densidade_dem'] )
print('Reading data...')
 loading_data = pd.read_csv(bp+'/loading.csv')
 train_data = pd.read_csv(bp+'/train_scores.csv')
 sample_submission = pd.read_csv(bp+'/sample_submission.csv')
 print('Reading data completed' )<sort_values>
df['hab_p_medico'] = np.where(df['hab_p_medico'].isna() , -1, df['hab_p_medico'])
total = train_data.isnull().sum().sort_values(ascending = False)
 percent =(train_data.isnull().sum() /train_data.isnull().count() *100 ).sort_values(ascending = False)
 missing_train_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_train_data.head()<sort_values>
df['perc_pop_econ_ativa'] = np.where(df['perc_pop_econ_ativa'].isna() , -1, df['perc_pop_econ_ativa'] )
total = loading_data.isnull().sum().sort_values(ascending = False)
 percent =(loading_data.isnull().sum() /loading_data.isnull().count() *100 ).sort_values(ascending = False)
 missing_test_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_test_data.head()<merge>
df['participacao_transf_receita'] = np.where(df['participacao_transf_receita'].isna() , -1, df['participacao_transf_receita'] )
merged = train_data.merge(loading_data)
<save_to_csv>
df['gasto_pc_saude'] = np.where(df['gasto_pc_saude'].isna() , -1, df['gasto_pc_saude'] )
sample_submission.to_csv('sample_submission.csv', index=False )<save_to_csv>
df['exp_vida'] = np.where(df['exp_vida'].isna() , df['exp_vida'].mean() , df['exp_vida'] )
sub1 = sample_submission.copy()
for i in range(int(len(sub1)/5)) :
sub1.at[i*5+0, 'Predicted']= 50.034068
sub1.at[i*5+1, 'Predicted']= 51.474692
sub1.at[i*5+2, 'Predicted']= 59.244132
sub1.at[i*5+3, 'Predicted']= 47.325130
sub1.at[i*5+4, 'Predicted']= 51.905658
sub1.to_csv('mean_submission.csv', index=False )<save_to_csv>
df['exp_anos_estudo'] = np.where(df['exp_anos_estudo'].isna() , df['exp_anos_estudo'].mean() , df['exp_anos_estudo'] )
sub = pd.read_csv('.. /input/trends-bob/sub123_6.75.csv')
 sub.to_csv('sub-my.csv', index=False )<import_modules>
col_nan=df.isnull().any(axis=0)
 col_nan
import numpy as np
import pandas as pd
import xgboost
import csv as csv
from xgboost import plot_importance
from matplotlib import pyplot
from sklearn.model_selection import cross_val_score,KFold
from sklearn.cross_validation import train_test_split
from sklearn.metrics import mean_absolute_error
import matplotlib.pyplot as plt
from sklearn.grid_search import GridSearchCV
from scipy.stats import skew
from collections import OrderedDict<load_from_csv>
import seaborn as sns
train_dataset=pd.read_csv('.. /input/train.csv', header=0)
 test_dataset=pd.read_csv('.. /input/test.csv', header=0 )<define_variables>
test = df[df['nota_mat'].isnull() ]
categorical_features=['MSSubClass','MSZoning','Street','Alley','LotShape','LandContour','Utilities',
'LotConfig','LandSlope','Neighborhood','Condition1','Condition2','BldgType','HouseStyle',
'RoofStyle','RoofMatl','Exterior1st','Exterior2nd','MasVnrType','ExterQual','ExterCond',
'Foundation','BsmtQual','BsmtCond','BsmtExposure','BsmtFinType1','BsmtFinType2','Heating',
'HeatingQC','CentralAir','Electrical','KitchenQual','Functional','FireplaceQu',
'GarageType','GarageFinish','GarageQual','GarageCond','PavedDrive','PoolQC','Fence',
'MiscFeature','SaleType','SaleCondition']
every_column_except_y= [col for col in train_dataset.columns if col not in ['SalePrice','Id']]
train_dataset.describe()<feature_engineering>
df = df[~df['nota_mat'].isnull() ]
every_column_non_categorical= [col for col in train_dataset.columns if col not in categorical_features and col not in ['Id'] ]
numeric_feats = train_dataset[every_column_non_categorical].dtypes[train_dataset.dtypes != "object"].index
train_dataset[numeric_feats] = np.log1p(train_dataset[numeric_feats])
 every_column_non_categorical= [col for col in test_dataset.columns if col not in categorical_features and col not in ['Id'] ]
numeric_feats = test_dataset[every_column_non_categorical].dtypes[test_dataset.dtypes != "object"].index
test_dataset[numeric_feats] = np.log1p(test_dataset[numeric_feats] )<categorify>
train, valid = train_test_split(df, random_state=42 )
features_with_nan=['Alley','MasVnrType','BsmtQual','BsmtQual','BsmtCond','BsmtCond','BsmtExposure',
'BsmtFinType1','BsmtFinType2','FireplaceQu','GarageType','GarageFinish']
def ConverNaNToNAString(data, columnList):
for x in columnList:
data[x] =str(data[x])
 ConverNaNToNAString(train_dataset, features_with_nan)
 ConverNaNToNAString(test_dataset, features_with_nan )<categorify>
removed_cols = ['nota_mat', 'municipio', 'hab_p_medico', 'capital', 'comissionados_por_servidor']
def CreateColumnPerValue(data, columnList):
for x in columnList:
values=pd.unique(data[x])
 for v in values:
column_name=x+"_"+str(v)
 data[column_name]=(data[x]==v ).astype(float)
 data.drop(x, axis=1, inplace=True)
<categorify>
feats = [c for c in df.columns if c not in removed_cols]
train_dataset = pd.get_dummies(train_dataset,columns =categorical_features)
 test_dataset = pd.get_dummies(test_dataset,columns =categorical_features)
<choose_model_class>
from sklearn.ensemble import RandomForestClassifier
model = xgboost.XGBRegressor(colsample_bytree=0.4,
gamma=0,
learning_rate=0.07,
max_depth=3,
min_child_weight=1.5,
n_estimators=10000,
reg_alpha=0.75,
reg_lambda=0.45,
subsample=0.6,
seed=42 )<train_model>
rf= RandomForestClassifier(n_estimators=200, min_samples_split=5, max_depth=4, random_state=42 )
every_column_except_y= [col for col in train_dataset.columns if col not in ['SalePrice','Id']]
model.fit(train_dataset[every_column_except_y],train_dataset['SalePrice'])
 OrderedDict(sorted(model.booster().get_fscore().items() , key=lambda t: t[1], reverse=True))<find_best_params>
rf.fit(train[feats], train['nota_mat'] )
most_relevant_features= list(dict(( k, v)for k, v in model.booster().get_fscore().items() if v >= 10 ).keys())
 print(most_relevant_features )<drop_column>
preds = rf.predict(valid[feats] )
train_dataset = train_dataset[train_dataset.GrLivArea < 8.25]
train_dataset = train_dataset[train_dataset.LotArea < 11.5]
train_dataset = train_dataset[train_dataset.SalePrice<13]
train_dataset = train_dataset[train_dataset.SalePrice>10.75]
train_dataset.drop("Id", axis=1, inplace=True )<prepare_x_and_y>
from sklearn.metrics import mean_squared_error
train_x=train_dataset[most_relevant_features]
train_y=train_dataset['SalePrice']
best_xgb_model = xgboost.XGBRegressor(colsample_bytree=0.4,
gamma=0,
learning_rate=0.07,
max_depth=3,
min_child_weight=1.5,
n_estimators=10000,
reg_alpha=0.75,
reg_lambda=0.45,
subsample=0.6,
seed=42)
 best_xgb_model.fit(train_x,train_y )<save_to_csv>
from sklearn.metrics import mean_squared_error
test_dataset['Prediction'] = np.expm1(best_xgb_model.predict(test_dataset[most_relevant_features]))
filename = 'submission.csv'
pd.DataFrame({'Id': test_dataset.Id, 'SalePrice': test_dataset.Prediction} ).to_csv(filename, index=False )<count_values>
mean_squared_error(valid['nota_mat'], preds )
print(test_dataset['Prediction'].head())
 print(test_dataset['Prediction'].count() )<load_from_csv>
accuracy_score(valid['nota_mat'], preds )
%matplotlib inline
NUMBER_DAYS = 100
NUMBER_FAMILIES = 6000
data = pd.read_csv('/kaggle/input/santa-2019-revenge-of-the-accountants/family_data.csv')
 submission = pd.read_csv('/kaggle/input/baseline/submission.csv')
 assigned_days = submission['assigned_day'].values
columns = data.columns[1:11]
DESIRED = data[columns].values
COST_PER_FAMILY = [0,50,50,100,200,200,300,300,400,500]
COST_PER_FAMILY_MEMBER = [0, 0, 9, 9, 9, 18, 18, 36, 36,235]
N_PEOPLE = data['n_people'].values
def get_daily_occupancy(assigned_days):
daily_occupancy = np.zeros(100, int)
 for fid, assigned_day in enumerate(assigned_days):
daily_occupancy[assigned_day-1] += N_PEOPLE[fid]
return daily_occupancy
def cost_function(prediction):
N_DAYS = 100
MAX_OCCUPANCY = 300
MIN_OCCUPANCY = 125
penalty = 0
days = list(range(N_DAYS,0,-1))
tmp = pd.read_csv('/kaggle/input/santa-2019-revenge-of-the-accountants/family_data.csv', index_col='family_id')
 family_size_dict = tmp[['n_people']].to_dict() ['n_people']
cols = [f'choice_{i}' for i in range(10)]
choice_dict = tmp[cols].to_dict()
daily_occupancy = {k:0 for k in days}
for f, d in enumerate(prediction):
n = family_size_dict[f]
choice_0 = choice_dict['choice_0'][f]
choice_1 = choice_dict['choice_1'][f]
choice_2 = choice_dict['choice_2'][f]
choice_3 = choice_dict['choice_3'][f]
choice_4 = choice_dict['choice_4'][f]
choice_5 = choice_dict['choice_5'][f]
choice_6 = choice_dict['choice_6'][f]
choice_7 = choice_dict['choice_7'][f]
choice_8 = choice_dict['choice_8'][f]
choice_9 = choice_dict['choice_9'][f]
daily_occupancy[d] += n
if d == choice_0:
penalty += 0
elif d == choice_1:
penalty += 50
elif d == choice_2:
penalty += 50 + 9 * n
elif d == choice_3:
penalty += 100 + 9 * n
elif d == choice_4:
penalty += 200 + 9 * n
elif d == choice_5:
penalty += 200 + 18 * n
elif d == choice_6:
penalty += 300 + 18 * n
elif d == choice_7:
penalty += 300 + 36 * n
elif d == choice_8:
penalty += 400 + 36 * n
elif d == choice_9:
penalty += 500 + 36 * n + 199 * n
else:
penalty += 500 + 36 * n + 398 * n
for _, v in daily_occupancy.items() :
if v > MAX_OCCUPANCY or v < MIN_OCCUPANCY:
penalty += 100000000
accounting_costs = 0
for j in range(1,6):
for day in days[0:]:
today_count = daily_occupancy[day]
diff = abs(daily_occupancy[day] - daily_occupancy[min(100, day+j)])
 accounting_costs += max(0,(today_count-125.0)/ 400.0 * today_count**(0.5 + diff / 50.0)) /j/j
return penalty, accounting_costs, penalty + accounting_costs<define_variables>
preds_test = rf.predict(test[feats] )
MAX_BEST_CHOICE = 5
NUM_SWAP = 3000
NUM_SECONDS = 1800
NUM_THREADS = 4
for _ in range(20):
solver = pywraplp.Solver('Optimization preference cost', pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
 daily_occupancy = get_daily_occupancy(assigned_days ).astype(float)
 fids = np.random.choice(range(NUMBER_FAMILIES), NUM_SWAP, replace=False)
 PCOSTM, B = {}, {}
for fid in range(NUMBER_FAMILIES):
if fid in fids:
for i in range(MAX_BEST_CHOICE):
PCOSTM[fid, DESIRED[fid][i]-1] = COST_PER_FAMILY[i] + N_PEOPLE[fid] * COST_PER_FAMILY_MEMBER[i]
B[ fid, DESIRED[fid][i]-1] = solver.BoolVar('')
 else:
daily_occupancy[assigned_days[fid]-1] -= N_PEOPLE[fid]
solver.set_time_limit(NUM_SECONDS*NUM_THREADS*1000)
 solver.SetNumThreads(NUM_THREADS)
 for day in range(NUMBER_DAYS):
if daily_occupancy[day]:
solver.Add(solver.Sum([N_PEOPLE[fid] * B[fid, day] for fid in range(NUMBER_FAMILIES)if(fid,day)in B])== daily_occupancy[day])
 for fid in fids:
solver.Add(solver.Sum(B[fid, day] for day in range(NUMBER_DAYS)if(fid, day)in B)== 1)
 solver.Minimize(solver.Sum(PCOSTM[fid, day] * B[fid, day] for fid, day in B))
sol = solver.Solve()
status = ['OPTIMAL', 'FEASIBLE', 'INFEASIBLE', 'UNBOUNDED', 'ABNORMAL', 'MODEL_INVALID', 'NOT_SOLVED']
if status[sol] in ['OPTIMAL', 'FEASIBLE']:
tmp = assigned_days.copy()
for fid, day in B:
if B[fid, day].solution_value() > 0.5:
tmp[fid] = day+1
if cost_function(tmp)[2] < cost_function(assigned_days)[2]:
assigned_days = tmp
submission['assigned_day'] = assigned_days
submission.to_csv('submission.csv', index=False)
 print('Result:', status[sol], cost_function(tmp))
else:
print('Result:', status[sol] )<set_options>
test['nota_mat'] = preds_test
pd.options.display.max_rows = 500
pd.options.display.max_columns = 500
%matplotlib inline
def rmse(yt, yp):
return np.sqrt(np.mean(( yt-yp)**2))
class CovidModel:
def __init__(self):
pass
def predict_first_day(self, date):
return None
def predict_next_day(self, yesterday_pred_df):
return None
class CovidModelAhmet(CovidModel):
def preprocess(self, df, meta_df):
df["Date"] = pd.to_datetime(df['Date'])
 df = df.merge(meta_df, on=self.loc_group, how="left")
 df["lat"] =(df["lat"] // 30 ).astype(np.float32 ).fillna(0)
 df["lon"] =(df["lon"] // 60 ).astype(np.float32 ).fillna(0)
 df["population"] = np.log1p(df["population"] ).fillna(-1)
 df["area"] = np.log1p(df["area"] ).fillna(-1)
 for col in self.loc_group:
df[col].fillna("", inplace=True)
 df['day'] = df.Date.dt.dayofyear
df['geo'] = ['_'.join(x)for x in zip(df['Country_Region'], df['Province_State'])]
return df
def get_model(self):
def nn_block(input_layer, size, dropout_rate, activation):
out_layer = KL.Dense(size, activation=None )(input_layer)
 out_layer = KL.Activation(activation )(out_layer)
 out_layer = KL.Dropout(dropout_rate )(out_layer)
 return out_layer
ts_inp = KL.Input(shape=(len(self.ts_features),))
global_inp = KL.Input(shape=(len(self.global_features),))
inp = KL.concatenate([global_inp, ts_inp])
 hidden_layer = nn_block(inp, 64, 0.0, "relu")
 gate_layer = nn_block(hidden_layer, 32, 0.0, "sigmoid")
 hidden_layer = nn_block(hidden_layer, 32, 0.0, "relu")
 hidden_layer = KL.multiply([hidden_layer, gate_layer])
 out = KL.Dense(len(self.TARGETS), activation="linear" )(hidden_layer)
 model = tf.keras.models.Model(inputs=[global_inp, ts_inp], outputs=out)
 return model
def get_input(self, df):
return [df[self.global_features], df[self.ts_features]]
def train_models(self, df, num_models=20, save=False):
def custom_loss(y_true, y_pred):
return K.sum(K.sqrt(K.sum(K.square(y_true - y_pred), axis=0, keepdims=True)))/len(self.TARGETS)
 models = []
for i in range(num_models):
model = self.get_model()
model.compile(loss=custom_loss, optimizer=Nadam(lr=1e-4))
hist = model.fit(self.get_input(df), df[self.TARGETS],
batch_size=2048, epochs=200, verbose=0, shuffle=True)
 if save:
model.save_weights("model{}.h5".format(i))
models.append(model)
 return models
def predict_one(self, df):
pred = np.zeros(( df.shape[0], 2))
for model in self.models:
pred += model.predict(self.get_input(df)) /len(self.models)
 pred = np.maximum(pred, df[self.prev_targets].values)
 pred[:, 0] = np.log1p(np.expm1(pred[:, 0])+ 0.1)
 pred[:, 1] = np.log1p(np.expm1(pred[:, 1])+ 0.01)
 return np.clip(pred, None, 15)
 def __init__(self):
df = pd.read_csv(".. /input/covid19-global-forecasting-week-3/train.csv")
 sub_df = pd.read_csv(".. /input/covid19-global-forecasting-week-3/test.csv")
 meta_df = pd.read_csv(".. /input/covid19-forecasting-metadata/region_metadata.csv")
 self.loc_group = ["Province_State", "Country_Region"]
df = self.preprocess(df, meta_df)
 sub_df = self.preprocess(sub_df, meta_df)
 df = df.merge(sub_df[["ForecastId", "Date", "geo"]], how="left", on=["Date", "geo"])
 df = df.append(sub_df[sub_df["Date"] > df["Date"].max() ], sort=False)
 df["day"] = df["day"] - df["day"].min()
self.TARGETS = ["ConfirmedCases", "Fatalities"]
self.prev_targets = ['prev_ConfirmedCases_1', 'prev_Fatalities_1']
for col in self.TARGETS:
df[col] = np.log1p(df[col])
 self.NUM_SHIFT = 7
self.global_features = ["lat", "lon", "population", "area"]
self.ts_features = []
for s in range(1, self.NUM_SHIFT+1):
for col in self.TARGETS:
df["prev_{}_{}".format(col, s)] = df.groupby(self.loc_group)[col].shift(s)
 self.ts_features.append("prev_{}_{}".format(col, s))
self.df = df[df["Date"] >= df["Date"].min() + timedelta(days=self.NUM_SHIFT)].copy()
def predict_first_day(self, day):
self.models = self.train_models(self.df[self.df["day"] < day])
 temp_df = self.df.loc[self.df["day"] == day].copy()
y_pred = self.predict_one(temp_df)
 self.y_prevs = [None]*self.NUM_SHIFT
for i in range(1, self.NUM_SHIFT):
self.y_prevs[i] = temp_df[['prev_ConfirmedCases_{}'.format(i), 'prev_Fatalities_{}'.format(i)]].values
temp_df[self.TARGETS] = y_pred
self.day = day
return temp_df[["geo", "day"] + self.TARGETS]
def predict_next_day(self, yesterday_pred_df):
self.day = self.day + 1
temp_df = self.df.loc[self.df["day"] == self.day].copy()
yesterday_pred_df = temp_df[["geo"]].merge(yesterday_pred_df[["geo"] + self.TARGETS], on="geo", how="left")
 temp_df[self.prev_targets] = yesterday_pred_df[self.TARGETS].values
for i in range(2, self.NUM_SHIFT+1):
temp_df[['prev_ConfirmedCases_{}'.format(i), 'prev_Fatalities_{}'.format(i)]] = self.y_prevs[i-1]
y_pred, self.y_prevs = self.predict_one(temp_df), [None, temp_df[self.prev_targets].values] + self.y_prevs[1:-1]
temp_df[self.TARGETS] = y_pred
return temp_df[["geo", "day"] + self.TARGETS] 
<load_from_csv>
test[['codigo_mun', 'nota_mat']].to_csv('rf2.csv', index=False )