Kaggle competition: Homework for Students
<SOS> metric: auc Kaggle data source: homework-for-students<categorify>
sns.set()
%matplotlib inline
filterwarnings('ignore')
 le = preprocessing.LabelEncoder()
countries = X_Train.Country.unique()
df_out = pd.DataFrame({'ForecastId': [], 'ConfirmedCases': [], 'Fatalities': []})
 for country in countries:
states = X_Train.loc[X_Train.Country == country, :].State.unique()
for state in states:
X_Train_CS = X_Train.loc[(X_Train.Country == country)&(X_Train.State == state), ['State', 'Country', 'Date', 'ConfirmedCases', 'Fatalities']]
y1_Train_CS = X_Train_CS.loc[:, 'ConfirmedCases']
y2_Train_CS = X_Train_CS.loc[:, 'Fatalities']
X_Train_CS = X_Train_CS.loc[:, ['State', 'Country', 'Date']]
X_Train_CS.Country = le.fit_transform(X_Train_CS.Country)
 X_Train_CS['State'] = le.fit_transform(X_Train_CS['State'])
 X_Test_CS = X_Test.loc[(X_Test.Country == country)&(X_Test.State == state), ['State', 'Country', 'Date', 'ForecastId']]
X_Test_CS_Id = X_Test_CS.loc[:, 'ForecastId']
X_Test_CS = X_Test_CS.loc[:, ['State', 'Country', 'Date']]
X_Test_CS.Country = le.fit_transform(X_Test_CS.Country)
 X_Test_CS['State'] = le.fit_transform(X_Test_CS['State'])
 model1 = XGBRegressor(n_estimators=1000)
 model1.fit(X_Train_CS, y1_Train_CS)
 y1_pred = model1.predict(X_Test_CS)
 model2 = XGBRegressor(n_estimators=1000)
 model2.fit(X_Train_CS, y2_Train_CS)
 y2_pred = model2.predict(X_Test_CS)
 df = pd.DataFrame({'ForecastId': X_Test_CS_Id, 'ConfirmedCases': y1_pred, 'Fatalities': y2_pred})
 df_out = pd.concat([df_out, df], axis=0)
<save_to_csv>
df_train = pd.read_csv('.. /input/homework-for-students4plus/train.csv', index_col=0, parse_dates=['issue_d'])
 df_test = pd.read_csv('.. /input/homework-for-students4plus/test.csv' , index_col=0, parse_dates=['issue_d'] )
df_out.ForecastId = df_out.ForecastId.astype('int')
 df_out.tail()
df_out.to_csv('submission.csv', index=False )<set_options>
y_train = df_train.loan_condition
X_train = df_train.drop(['loan_condition'], axis=1)
 X_test = df_test
import scipy.optimize as opt
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
from tqdm import tqdm_notebook
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_squared_log_error
import warnings; warnings.filterwarnings('ignore' )<feature_engineering>
X_train['loan_amnt'] = X_train['loan_amnt'].apply(np.log1p)
 X_test['loan_amnt'] = X_test['loan_amnt'].apply(np.log1p )
train = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-2/train.csv')
 train['Date'] = pd.to_datetime(train['Date'])
 first_test_date = np.datetime64('2020-03-19')
 last_train_date = np.datetime64('2020-03-31')
 eval_set = train[train['Date'] > first_test_date]
overlap_days = last_train_date - first_test_date
overlap_days = int(overlap_days.astype('timedelta64[D]')/ np.timedelta64(1, 'D'))
train = train[train['Date'] <= last_train_date]
train<load_from_csv>
X_train['earliest_cr_line_day'] = pd.to_datetime(X_train['earliest_cr_line'], format='%b-%Y')
 X_test['earliest_cr_line_day'] = pd.to_datetime(X_test['earliest_cr_line'] , format='%b-%Y' )
test = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-2/test.csv')
 test['Date'] = pd.to_datetime(test['Date'])
 test<load_from_csv>
X_train["date_from_earliest_cr_line"] =(X_train["issue_d"] - X_train["earliest_cr_line_day"] ).dt.days
X_test["date_from_earliest_cr_line"] =(X_test["issue_d"] - X_test["earliest_cr_line_day"] ).dt.days
X_train["date_from_earliest_cr_line"] = X_train["date_from_earliest_cr_line"].fillna(X_train["date_from_earliest_cr_line"].median())
 X_test["date_from_earliest_cr_line"] = X_test["date_from_earliest_cr_line"].fillna(X_train["date_from_earliest_cr_line"].median())
 X_train["date_from_earliest_cr_line"] = X_train["date_from_earliest_cr_line"].astype("int")
 X_test["date_from_earliest_cr_line"] = X_test["date_from_earliest_cr_line"].astype("int")
 X_train.drop(['earliest_cr_line_day'],axis=1,inplace=True)
 X_test.drop(['earliest_cr_line_day'],axis=1,inplace=True)
 X_train.dtypes
sub = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-2/submission.csv')
 sub = sub.set_index('ForecastId', drop=True)
 sub<compute_test_metric>
X_train["sub_grade_num"] = [int(i[1])for i in X_train["sub_grade"]]
X_test["sub_grade_num"] = [int(i[1])for i in X_test["sub_grade"]]
def model(parameters, time):
y_pred = parameters[0] *(1 - np.exp(-parameters[1] *(time - parameters[3])))**parameters[2] + np.maximum(time*parameters[4], 0)
 return np.nan_to_num(y_pred ).clip(0, np.inf)
 def residual(parameters, time, data):
y_pred = model(parameters, time)
 return mean_squared_error(data, y_pred )<train_model>
X_train['month'] = X_train["issue_d"].dt.month
X_test['month'] = X_test["issue_d"].dt.month
X_train['month'] = X_train['month'].astype("str")
 X_test['month'] = X_test['month'].astype("str")
 X_train.dtypes
def fitModel(time, data, guess):
params = opt.minimize(residual, guess, args=(time, data), method='Nelder-Mead', tol=1e-7)
 return params.x<train_model>
X_train["zip_code"] = [ i[0:3] for i in X_train["zip_code"]]
X_test["zip_code"] = [ i[0:3] for i in X_test["zip_code"]]
model_params = trainModels(train )<predict_on_test>
df_zip = pd.read_csv(".. /input/homework-for-students4plus/free-zipcode-database.csv")
 df_zip["Zipcode"] = [str(i ).zfill(5)[0:3] for i in df_zip["Zipcode"]]
df_zip.head()
model_predictions = forecast(test, model_params, sub )<define_variables>
del df_zip
gc.collect()
identifier =('Taiwan*', None, 'ConfirmedCases')
 samples_train = train[(train['Country_Region'] == identifier[0])& train['Province_State'].isnull() ][identifier[2]]
samples_eval = eval_set[(eval_set['Country_Region'] == identifier[0])& eval_set['Province_State'].isnull() ][identifier[2]]
if identifier[1] is not None:
samples_train = train[(train['Country_Region'] == identifier[0])&(train['Province_State'] == identifier[1])][identifier[2]]
samples_eval = eval_set[(eval_set['Country_Region'] == identifier[0])&(eval_set['Province_State'] == identifier[1])][identifier[2]]
params = model_params[identifier]['params']
print(params)
 num_samples = model_params[identifier]['num_samples']
start_date = model_params[identifier]['start_date']
predictions = model_predictions[identifier]
plt.scatter(range(num_samples), samples_train)
 plt.scatter(range(num_samples-overlap_days, num_samples-overlap_days+len(samples_eval)) , samples_eval)
 plt.plot(range(start_date, num_samples + len(predictions)) , model(params, range(num_samples-start_date + len(predictions))))<merge>
X_train["zip_code_null"] = X_train["zip_code"].where(X_train["zip_code"].isin(zip_list), "zip_null")
 X_test["zip_code_null"] = X_test["zip_code"].where(X_test["zip_code"].isin(zip_list), "zip_null" )
eval_with_id = pd.merge(eval_set, test, on=['Date', 'Country_Region', 'Province_State'])
 sub = sub.replace([np.inf, -np.inf], np.nan)
 sub = sub.fillna(0)
 merged_eval = pd.merge(eval_with_id, sub, left_on='ForecastId', right_index=True)
 merged_eval<compute_test_metric>
date_col = ['issue_d']
lang_col = ['emp_title','title']
cats_col = []
for col in X_train.columns:
if X_train[col].dtype == 'object':
cats_col.append(col)
 cats_col = list(set(cats_col)- set(lang_col))
num_col = list(X_train.columns)
 num_col = list(set(num_col)- set(date_col))
num_col = list(set(num_col)- set(lang_col))
num_col = list(set(num_col)- set(cats_col))
print("date_col : " + str(len(date_col)))
 print("lang_col : " + str(len(lang_col)))
 print("cats_col : " + str(len(cats_col)))
 print("num_col : " + str(len(num_col)))
 print("total : " + str(len(X_train.columns)) )
score_confirmed = np.sqrt(mean_squared_log_error(merged_eval['ConfirmedCases_x'].values, merged_eval['ConfirmedCases_y'].values))
score_fatality = np.sqrt(mean_squared_log_error(merged_eval['Fatalities_x'].values, merged_eval['Fatalities_y'].values))
print(f'Confirmed Cases Score: {score_confirmed}
Fatality Score: {score_fatality}
Average Score: {np.mean([score_confirmed, score_fatality])}' )<save_to_csv>
X_train_date = X_train[date_col].copy()
X_test_date = X_test[date_col].copy()
X_train_lang = X_train[lang_col].copy()
X_test_lang = X_test[lang_col].copy()
X_train_cats = X_train[cats_col].copy()
X_test_cats = X_test[cats_col].copy()
X_train_num = X_train[num_col].copy()
X_test_num = X_test[num_col].copy()
sub.to_csv('submission.csv')
 sub<load_from_csv>
X_train_num.fillna(X_train_num.median() ,axis=0, inplace=True)
 X_test_num.fillna(X_train_num.median() ,axis=0, inplace=True )
def p2f(x):
try:
return float(x.strip('%')) /100
except:
return None
def s2f(x):
try:
return float(x)
 except:
return None
pop_set = pd.read_csv('/kaggle/input/population-by-country-2020/population_by_country_2020.csv', converters={'Med.Age': s2f, 'Urban Pop %': p2f})
 pop_set.rename(columns={ pop_set.columns[0]: "Country" , pop_set.columns[1]: "Population" , pop_set.columns[4]: "Density", pop_set.columns[8]: 'Median Age', pop_set.columns[9]: "Urban Pop"}, inplace = True)
 pop_set = pop_set.iloc[:, [0, 1, 4, 8, 9]]
pop_set = pop_set.replace(regex={
'Czech Republic(Czechia)': 'Czechia',
"Côte d'Ivoire": "Cote d'Ivoire",
'St.Vincent & Grenadines': 'Saint Vincent and the Grenadines',
'Saint Kitts & Nevis': 'Saint Kitts and Nevis',
'Taiwan': 'Taiwan*',
'South Korea': 'Korea, South',
'United States': 'US'})
 pop_set<load_from_csv>
target = 'loan_condition'
X_temp = pd.concat([X_train_cats, y_train], axis=1)
 for col in cats_col:
print(col)
 summary = X_temp.groupby([col])[target].mean()
X_test_cats[col] = X_test_cats[col].map(summary)
 skf = StratifiedKFold(n_splits=5, random_state=71, shuffle=True)
 enc_train = pd.Series(np.zeros(len(X_train)) , index=X_train.index)
 for i,(train_ix, val_ix)in enumerate(( skf.split(X_train, y_train))):
X_train_, _ = X_temp.iloc[train_ix], y_train.iloc[train_ix]
X_val, _ = X_temp.iloc[val_ix], y_train.iloc[val_ix]
summary = X_train_.groupby([col])[target].mean()
enc_train.iloc[val_ix] = X_val[col].map(summary)
 X_train_cats[col] = enc_train
freedom_set = pd.read_csv('/kaggle/input/cato-2017-human-freedom-index/cato_2017_hfi_by_year_summary.csv')
 freedom_set = freedom_set[freedom_set['Year'] == 2015]
freedom_set.rename(columns={ freedom_set.columns[2]: "Country" , freedom_set.columns[3]: "Personal Freedom" , freedom_set.columns[4]: "Economic Freedom", freedom_set.columns[5]: 'Human Freedom'}, inplace = True)
 freedom_set = freedom_set.iloc[:, [2, 3, 4, 5]]
freedom_set = freedom_set.replace(regex={
'Czech Republic': 'Czechia',
'Taiwan': 'Taiwan*',
'Korea, Republic of': 'Korea, South',
'United States': 'US'})
 freedom_set<load_from_csv>
X_train_cats.fillna(X_train_cats.median() ,axis=0, inplace=True)
 X_test_cats.fillna(X_train_cats.median() ,axis=0, inplace=True )
country_set = pd.read_csv('/kaggle/input/countries-of-the-world/countries of the world.csv', decimal=",")
 country_set.rename(columns={ country_set.columns[7]: "Infant Mortality" , country_set.columns[8]: "GDP" , country_set.columns[9]: "Literacy"}, inplace = True)
 country_set = country_set.iloc[:, [0,7,8,9]]
country_set['Country'] = country_set['Country'].str.strip()
country_set = country_set.replace(regex={
'Czech Republic': 'Czechia',
'Taiwan': 'Taiwan*',
'Korea, Republic of': 'Korea, South',
'United States': 'US'})
 country_set<normalization>
tfidf = TfidfVectorizer(max_features=1000, analyzer='word', ngram_range=(1, 2))
train = []
test = []
for col in lang_col:
train.append(tfidf.fit_transform(X_train_lang[col].fillna('
test.append(tfidf.transform(X_test_lang[col].fillna('
def analyzeParams(model_params, index, measurement, external_set, external_column, threshold= 1000, label=False, xmin=None, xmax=None, ymin=None, ymax=None):
external_vals = []
params_vals = []
names = []
for identifier in model_params:
if identifier[0] not in ['US', 'China'] and identifier[2] == measurement and model_params[identifier]['max_value'] > threshold:
try:
val = external_set.loc[external_set['Country'] == identifier[0], external_column].values[0]
if np.isnan(val):
continue
external_vals.append(val)
 if identifier[1] is None:
names.append(identifier[0])
 else:
names.append(identifier[1] + ', ' + identifier[0])
 params = model_params[identifier]['params']
params_vals.append(params[index])
 except:
continue
R = np.corrcoef(external_vals, params_vals)
 plt.figure(figsize=(8, 6))
plt.scatter(external_vals, params_vals)
 plt.xlim(xmin, xmax)
 plt.ylim(ymin, ymax)
 plt.title("Correlation R = " + str(R[0, 1]))
if label:
for i, name in enumerate(names):
if(xmax is None or external_vals[i] <= xmax)and(xmin is None or external_vals[i] >= xmin)and(ymax is None or yparams_vals[i] <= ymax)and(ymin is None or params_vals[i] >= ymin):
plt.text(external_vals[i], params_vals[i], name, size=12)
<compute_test_metric>
X_train = pd.concat([X_train_date,X_train_cats, X_train_num], axis=1)
 X_test = pd.concat([X_test_cats , X_test_num], axis=1)
 df_concat = pd.concat([X_train,y_train],axis=1 )
analyzeParams(model_params, 1, 'ConfirmedCases', pop_set, 'Density', 500, True )<compute_test_metric>
train_index_date = df_concat.set_index("issue_d")[:"2015-06-01"].reset_index().index
val_index_date = df_concat.set_index("issue_d")["2015-07-01":].reset_index().index
analyzeParams(model_params, 1, 'Fatalities', pop_set, 'Median Age', 10, True )<compute_test_metric>
X_inner_train = df_concat.drop(["issue_d","loan_condition"],axis=1)
 y_inner_train = df_concat["loan_condition"]
analyzeParams(model_params, 1, 'ConfirmedCases', pop_set, 'Median Age', 500, True )<compute_test_metric>
for i,col in enumerate(lang_col):
X_inner_train = sp.sparse.hstack([X_inner_train, train[i]])
 X_test = sp.sparse.hstack([X_test, test[i]])
 X_inner_train = X_inner_train.tocsr()
X_test = X_test.tocsr()
analyzeParams(model_params, 1, 'ConfirmedCases', freedom_set, 'Economic Freedom', 500, True )<compute_test_metric>
del X_train_date, X_test_date, X_train_lang, X_test_lang, X_train_cats, X_test_cats, X_train_num, X_test_num
gc.collect()
analyzeParams(model_params, 1, 'ConfirmedCases', freedom_set, 'Personal Freedom', 500, True )<compute_test_metric>
def objectives(trial):
params = {
'random_seed' : 71,
'num_leaves' : trial.suggest_int('num_leaves', 2, 256),
'min_child_samples': int(trial.suggest_loguniform('min_child_samples', 100, 10000)) ,
'min_child_weight' : trial.suggest_loguniform('min_child_weight', 0.1, 2000),
'subsample' : trial.suggest_uniform('subsample', 0.8, 1),
'colsample_bytree' : trial.suggest_uniform('colsample_bytree', 0.5, 1),
'learning_rate' : trial.suggest_loguniform('learning_rate', 0.025, 0.5),
'min_data_in_leaf' : int(trial.suggest_loguniform('min_data_in_leaf', 1, 1000)) ,
}
scores = []
skf = StratifiedKFold(n_splits=5, random_state=71, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(skf.split(X_inner_train, y_inner_train)) :
X_train_, y_train_ = X_inner_train[train_ix,:], y_inner_train.values[train_ix]
X_val, y_val = X_inner_train[test_ix,:], y_inner_train.values[test_ix]
model = lgb.LGBMRegressor(**params)
 model.fit(X_train_, y_train_,
eval_set=(X_val,y_val),
early_stopping_rounds=20,
eval_metric='auc',
verbose=False)
 y_inner_pred = model.predict(X_val)
 score = roc_auc_score(y_val,y_inner_pred)
 scores.append(score)
 return np.mean(scores )
analyzeParams(model_params, 1, 'ConfirmedCases', freedom_set, 'Human Freedom', 500, True )<compute_test_metric>
%%time
opt = optuna.create_study(direction='maximize',sampler=optuna.samplers.RandomSampler(seed=71))
opt.optimize(objectives, n_trials=100 )
analyzeParams(model_params, 1, 'Fatalities', country_set, 'Infant Mortality', 10, True )<compute_test_metric>
trial = opt.best_trial
params_best = dict(trial.params.items())
 params_best['random_seed'] = 71
params_best["num_leaves"] = int(params_best["num_leaves"])
 params_best["min_data_in_leaf"] = int(params_best["min_data_in_leaf"] )
analyzeParams(model_params, 1, 'ConfirmedCases', country_set, 'Literacy', 500, True )<compute_test_metric>
params_best2 = dict(trial.params.items())
 params_best2['random_seed'] = 123
params_best2["num_leaves"] = int(params_best["num_leaves"])
 params_best2["min_data_in_leaf"] = int(params_best["min_data_in_leaf"] )
analyzeParams(model_params, 1, 'ConfirmedCases', country_set, 'GDP', 500, True )<import_modules>
list_params_best_skf = [params_best,params_best2]
plotly.offline.init_notebook_mode()
%matplotlib inline
def RMSLE(pred,actual):
return np.sqrt(np.mean(np.power(( np.log(pred+1)-np.log(actual+1)) ,2)) )<load_from_csv>
def objectives(trial):
params = {
'random_seed' : 71,
'num_leaves' : trial.suggest_int('num_leaves', 2, 256),
'min_child_samples': int(trial.suggest_loguniform('min_child_samples', 100, 10000)) ,
'min_child_weight' : trial.suggest_loguniform('min_child_weight', 0.1, 2000),
'subsample' : trial.suggest_uniform('subsample', 0.8, 1),
'colsample_bytree' : trial.suggest_uniform('colsample_bytree', 0.5, 1),
'learning_rate' : trial.suggest_loguniform('learning_rate', 0.025, 0.5),
'min_data_in_leaf' : int(trial.suggest_loguniform('min_data_in_leaf', 1, 1000)) ,
}
X_train_, y_train_ = X_inner_train[train_index_date,:], y_inner_train.values[train_index_date]
X_val, y_val = X_inner_train[val_index_date,:], y_inner_train.values[val_index_date]
model = lgb.LGBMRegressor(**params)
 model.fit(X_train_, y_train_,
eval_set=(X_val,y_val),
early_stopping_rounds=20,
eval_metric='auc',
verbose=False)
 y_inner_pred = model.predict(X_val)
 score = roc_auc_score(y_val,y_inner_pred)
 return score
pd.set_option('mode.chained_assignment', None)
 test = pd.read_csv(".. /input/covid19-global-forecasting-week-2/test.csv")
 train = pd.read_csv(".. /input/covid19-global-forecasting-week-2/train.csv")
 train['Province_State'].fillna('', inplace=True)
 test['Province_State'].fillna('', inplace=True)
 train['Date'] = pd.to_datetime(train['Date'])
 test['Date'] = pd.to_datetime(test['Date'])
 train = train.sort_values(['Country_Region','Province_State','Date'])
 test = test.sort_values(['Country_Region','Province_State','Date'] )<feature_engineering>
%%time
opt = optuna.create_study(direction='maximize',sampler=optuna.samplers.RandomSampler(seed=71))
opt.optimize(objectives, n_trials=100 )
FirstDate = train.groupby('Country_Region' ).min() ['Date'].unique() [0]
train['Last Confirm'] = train['ConfirmedCases'].shift(1)
 while train[(train['Last Confirm'] > train['ConfirmedCases'])&(train['Date'] > FirstDate)].shape[0] > 0:
train['Last Confirm'] = train['ConfirmedCases'].shift(1)
 train['Last Fatalities'] = train['Fatalities'].shift(1)
 train.loc[(train['Last Confirm'] > train['ConfirmedCases'])&(train['Date'] > FirstDate),'ConfirmedCases'] = train.loc[(train['Last Confirm'] > train['ConfirmedCases'])&(train['Date'] > FirstDate),'Last Confirm']
train.loc[(train['Last Fatalities'] > train['Fatalities'])&(train['Date'] > FirstDate),'Fatalities'] = train.loc[(train['Last Fatalities'] > train['Fatalities'])&(train['Date'] > FirstDate),'Last Fatalities']
train['Last Confirm'] = train['ConfirmedCases'].shift(1)
 train['Last Fatalities'] = train['Fatalities'].shift(1 )<feature_engineering>
seed_i = [71,123,51,17,91]
list_params_best_tss = []
for i in range(5):
trial = opt.best_trial
params_best = dict(trial.params.items())
 params_best['random_seed'] = seed_i[i]
params_best["num_leaves"] = int(params_best["num_leaves"])
 params_best["min_data_in_leaf"] = int(params_best["min_data_in_leaf"])
 list_params_best_tss.append(params_best )
feature_day = [1,20,50,100,200,500,1000]
def CreateInput(data):
feature = []
for day in feature_day:
data.loc[:,'Number day from ' + str(day)+ ' case'] = 0
if(train[(train['Country_Region'] == country)&(train['Province_State'] == province)&(train['ConfirmedCases'] < day)]['Date'].count() > 0):
fromday = train[(train['Country_Region'] == country)&(train['Province_State'] == province)&(train['ConfirmedCases'] < day)]['Date'].max()
else:
fromday = train[(train['Country_Region'] == country)&(train['Province_State'] == province)]['Date'].min()
for i in range(0, len(data)) :
if(data['Date'].iloc[i] > fromday):
day_denta = data['Date'].iloc[i] - fromday
data['Number day from ' + str(day)+ ' case'].iloc[i] = day_denta.days
feature = feature + ['Number day from ' + str(day)+ ' case']
return data[feature]
pred_data_all = pd.DataFrame()
with tqdm(total=len(train['Country_Region'].unique())) as pbar:
for country in train['Country_Region'].unique() :
for province in train[(train['Country_Region'] == country)]['Province_State'].unique() :
df_train = train[(train['Country_Region'] == country)&(train['Province_State'] == province)]
df_test = test[(test['Country_Region'] == country)&(test['Province_State'] == province)]
X_train = CreateInput(df_train)
 y_train_confirmed = df_train['ConfirmedCases'].ravel()
y_train_fatalities = df_train['Fatalities'].ravel()
X_pred = CreateInput(df_test)
 feature_use = X_pred.columns[0]
for i in range(X_pred.shape[1] - 1,0,-1):
if(X_pred.iloc[0,i] > 0):
feature_use = X_pred.columns[i]
break
idx = X_train[X_train[feature_use] == 0].shape[0]
adjusted_X_train = X_train[idx:][feature_use].values.reshape(-1, 1)
 adjusted_y_train_confirmed = y_train_confirmed[idx:]
adjusted_y_train_fatalities = y_train_fatalities[idx:]
adjusted_X_pred = X_pred[feature_use].values.reshape(-1, 1)
 model = make_pipeline(PolynomialFeatures(2), BayesianRidge())
 model.fit(adjusted_X_train,adjusted_y_train_confirmed)
 y_hat_confirmed = model.predict(adjusted_X_pred)
 model.fit(adjusted_X_train,adjusted_y_train_fatalities)
 y_hat_fatalities = model.predict(adjusted_X_pred)
 pred_data = test[(test['Country_Region'] == country)&(test['Province_State'] == province)]
pred_data['ConfirmedCases_hat'] = y_hat_confirmed
pred_data['Fatalities_hat'] = y_hat_fatalities
pred_data_all = pred_data_all.append(pred_data)
 pbar.update(1)
 df_val = pd.merge(pred_data_all,train[['Date','Country_Region','Province_State','ConfirmedCases','Fatalities']],on=['Date','Country_Region','Province_State'], how='left')
 df_val.loc[df_val['Fatalities_hat'] < 0,'Fatalities_hat'] = 0
df_val.loc[df_val['ConfirmedCases_hat'] < 0,'ConfirmedCases_hat'] = 0
df_val_1 = df_val.copy()<compute_test_metric>
submission = pd.read_csv('.. /input/homework-for-students4plus/sample_submission.csv', index_col=0 )
RMSLE(df_val[(df_val['ConfirmedCases'].isnull() == False)]['ConfirmedCases'].values,df_val[(df_val['ConfirmedCases'].isnull() == False)]['ConfirmedCases_hat'].values )<compute_test_metric>
y_pred_test = np.zeros(len(submission))
for i,(train_ix, test_ix)in enumerate(skf.split(X_inner_train, y_inner_train)) :
print(i)
 X_train_, y_train_ = X_inner_train[train_ix,:], y_inner_train.values[train_ix]
X_val, y_val = X_inner_train[test_ix,:], y_inner_train.values[test_ix]
for j in list_params_best_skf:
params_best = j
model = lgb.LGBMRegressor(**params_best)
 model.fit(X_train_, y_train_,
eval_set=(X_val,y_val),
early_stopping_rounds=20,
eval_metric='auc',
verbose=False)
 y_inner_pred = model.predict(X_test)
 y_pred_test += y_inner_pred
RMSLE(df_val[(df_val['Fatalities'].isnull() == False)]['Fatalities'].values,df_val[(df_val['Fatalities'].isnull() == False)]['Fatalities_hat'].values )<groupby>
X_train_, y_train_ = X_inner_train[train_index_date,:], y_inner_train.values[train_index_date]
X_val, y_val = X_inner_train[val_index_date,:], y_inner_train.values[val_index_date]
for index,j in enumerate(list_params_best_tss):
print(index)
 params_best = j
model = lgb.LGBMRegressor(**params_best)
 model.fit(X_train_, y_train_,
eval_set=(X_val,y_val),
early_stopping_rounds=20,
eval_metric='auc',
verbose=False)
 y_inner_pred = model.predict(X_test)
 y_pred_test += y_inner_pred
country = "Vietnam"
df_val = df_val_1
df_val[df_val['Country_Region'] == country].groupby(['Date','Country_Region'] ).sum().reset_index()<save_model>
y_pred = y_pred_test / 20
y_pred
<feature_engineering><EOS> 
submission = pd.read_csv('.. /input/homework-for-students4plus/sample_submission.csv', index_col=0)
 submission.loan_condition = y_pred
submission.to_csv('submission.csv' )