Kaggle competition: Bike Sharing Demand
<SOS> metric: RMSLE Kaggle data source: bike-sharing-demand<feature_engineering>
import pandas as pd
import numpy as np
train_df['Date_day'] = train_df["Date_date"].apply(lambda x: x.day)
 train_df['Date_month'] = train_df["Date_date"].apply(lambda x: x.month)
 train_df['Date_weekofyear'] = train_df["Date_date"].apply(lambda x: x.week)
 train_df['Date_dayofweek'] = train_df["Date_date"].apply(lambda x: x.dayofweek)
 train_df['Date_dayofyear'] = train_df["Date_date"].apply(lambda x: x.dayofyear )<feature_engineering>
df = pd.read_csv('.. /input/train.csv', parse_dates=[0])
 test = pd.read_csv('.. /input/test.csv', parse_dates=[0] )
test_df['Date_day'] = test_df["Date_date"].apply(lambda x: x.day)
 test_df['Date_month'] = test_df["Date_date"].apply(lambda x: x.month)
 test_df['Date_weekofyear'] = test_df["Date_date"].apply(lambda x: x.week)
 test_df['Date_dayofweek'] = test_df["Date_date"].apply(lambda x: x.dayofweek)
 test_df['Date_dayofyear'] = test_df["Date_date"].apply(lambda x: x.dayofyear )<data_type_conversions>
df['count'] = np.log(df['count'] + 1 )
train_df["Date"] = train_df["Date"].apply(lambda x: x.replace("-",""))
train_df["Date"] = train_df["Date"].astype(int )<data_type_conversions>
df.rename(columns={'count':'rentals'}, inplace=True )
test_df["Date"] = test_df["Date"].apply(lambda x: x.replace("-",""))
test_df["Date"] = test_df["Date"].astype(int )<load_from_csv>
df = df.append(test,sort=False )
pop_df = pd.read_csv('/kaggle/input/additional/population.csv')
 gdp_df = pd.read_csv('/kaggle/input/additional/GDP.csv')
 age_df = pd.read_csv('/kaggle/input/additional/median_age.csv')
 hosbed_df = pd.read_csv('/kaggle/input/additional/hospital_beds.csv')
 deathrate_df = pd.read_csv('/kaggle/input/additional/death_rate.csv' )<rename_columns>
df['year'] = df['datetime'].dt.year
df['month'] = df['datetime'].dt.month
df['day'] = df['datetime'].dt.day
df['dayofweek'] = df['datetime'].dt.dayofweek
df['hour'] = df['datetime'].dt.hour
pop_df = pop_df.rename(columns={'name': 'Country', 'pop2019': 'Population', 'area': 'Area'})
 pop_df = pop_df[['Country', 'Population', 'Area', 'Density']]
gdp_df = gdp_df.rename(columns={'Country Name': 'Country', '2018': 'GDP'})
 age_df = age_df.rename(columns={'Place': 'Country', 'Median': 'Median_Age'})
 age_df = age_df[['Country', 'Median_Age']]
hosbed_df = hosbed_df.rename(columns={'Country Name': 'Country', 'bed': 'Hospital_Beds'})
 hosbed_df = hosbed_df[['Country', 'Hospital_Beds']]
deathrate_df = deathrate_df.rename(columns={'name': 'Country', 'Rate': 'Death_Rate'} )<merge>
df.sort_values('datetime', inplace=True )
for df in [pop_df, gdp_df, age_df, hosbed_df, deathrate_df]:
train_df = train_df.merge(df, left_on='Country/Region', right_on='Country', how='left' )<drop_column>
df['rolling_temp'] = df['temp'].rolling(4, min_periods=1 ).mean()
train_df = train_df.drop(columns=['Id', 'Province/State', 'Country_x', 'Country_y',
'Country_x', 'Country_y', 'Country'] )<correct_missing_values>
df.year.value_counts()
train_df = train_df.fillna(train_df.mean() )<count_missing_values>
test = df[df['rentals'].isnull() ]
for col in train_df.columns:
count_na = train_df[col].isna().sum()
fill_rate = 1 - count_na / len(train_df)
 print(col + ': ' + str(fill_rate))<merge>
df = df[~df['rentals'].isnull() ]
df_tmp = train_df.copy()
df_tmp = df_tmp.drop_duplicates(subset='Country/Region', keep='first')
 df_tmp = df_tmp[['Country/Region', 'Population', 'Area', 'Density', 'GDP', 'Median_Age', 'Hospital_Beds',
'Death_Rate']]
test_df = test_df.merge(df_tmp, on='Country/Region', how='left' )<define_variables>
from sklearn.model_selection import train_test_split
cols_1 = ['Country_id', 'Lat', 'Long', 'Date_day', 'Date_month', 'Date_weekofyear', 'Date_dayofyear']
cols_2 = ['Country_id', 'Lat', 'Long', 'Date_day', 'Date_month', 'Date_weekofyear', 'Date_dayofyear',
'Population', 'Density', 'GDP', 'Median_Age', 'Hospital_Beds', 'Death_Rate']<prepare_x_and_y>
from sklearn.model_selection import train_test_split
x1 = train_df[cols_2]
x2 = train_df[cols_2]
y1 = train_df['ConfirmedCases']
y2 = train_df['Fatalities']
x_test_1 = test_df[cols_2]
x_test_2 = test_df[cols_2]<import_modules>
train, valid = train_test_split(df, random_state=42 )
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor<choose_model_class>
removed_cols = ['rentals', 'casual', 'registered', 'datetime']
dt_r = DecisionTreeRegressor(random_state = 0)
 rf = RandomForestRegressor(random_state = 0 )<predict_on_test>
feats = [c for c in df.columns if c not in removed_cols]
dt_r.fit(x1,y1)
 pred1 = dt_r.predict(x_test_1)
 pred1 = pd.DataFrame(pred1)
 pred1.columns = ["ConfirmedCases_prediction"]<predict_on_test>
from sklearn.tree import DecisionTreeRegressor
dt_r.fit(x2,y2)
 pred2 = dt_r.predict(x_test_2)
 pred2 = pd.DataFrame(pred2)
 pred2.columns = ["Death_prediction"]<load_from_csv>
dt = DecisionTreeRegressor(random_state=42, max_depth=2 )
sub_df = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-1/submission.csv" )<data_type_conversions>
dt.fit(train[feats], train['rentals'] )
sub_new_df = sub_df[["ForecastId"]]
OP = pd.concat([pred1,pred2,sub_new_df],axis=1)
 OP.columns = ['ConfirmedCases', 'Fatalities', 'ForecastId']
OP = OP[['ForecastId','ConfirmedCases', 'Fatalities']]
OP["ConfirmedCases"] = OP["ConfirmedCases"].astype(int)
 OP["Fatalities"] = OP["Fatalities"].astype(int )<save_to_csv>
from fastai.structured import draw_tree
OP.to_csv("submission.csv",index=False )<feature_engineering>
from sklearn.ensemble import RandomForestRegressor
def create_time_features(df):

 df['date'] = df.index
df['hour'] = df['date'].dt.hour
df['dayofweek'] = df['date'].dt.dayofweek
df['quarter'] = df['date'].dt.quarter
df['month'] = df['date'].dt.month
df['year'] = df['date'].dt.year
df['dayofyear'] = df['date'].dt.dayofyear
df['dayofmonth'] = df['date'].dt.day
df['weekofyear'] = df['date'].dt.weekofyear
X = df[['hour','dayofweek','quarter','month','year',
'dayofyear','dayofmonth','weekofyear']]
return X
def get_wg(country = 'US', state = 'New York'):
booll =(train['Country/Region']==country)&(train['Province/State']==state)&(train['ConfirmedCases']>0)
 narf = train[ booll ].copy().reset_index()
narf['ts'] = narf.index+1
if narf.shape[0] == 0:
return
r = 8.171
a = 4449661.60968839
c = 4.83006577124696
hc =.998
r,a,c,hc = get_params(booll
, [r*.75, 8.171, r*1.25]
, [a*.75, 4449661.60968839, a*1.25]
, [c*.75, 4.83006577124696, c*1.25]
, [hc*.75,.998,.999])
 narf['pt'] = narf.ts.apply(lambda x:(1-(a/(a+x**c)) **r)*(1-hc))
temp = narf.copy()
temp['ts'] = temp.index
temp = temp[['ts','ConfirmedCases','pt']]
temp.columns = ['ts','incr', 'ptr']
if temp.shape[0] == 0:
return
narf = pd.merge(narf, temp, on='ts', how='left')
 narf['incr'] = narf['incr'] - narf['ConfirmedCases']
narf['ptr'] = narf['ptr'] - narf['pt']
index = narf.shape[0] - 1
pop = narf.iloc[index]['pop']
if np.isnan(pop):
return
cc = narf.iloc[index]['ConfirmedCases']
dfj = narf.iloc[0]['day_from_jan_first']
pt =(pop-cc)* np.log(1-narf.iloc[index]['pt'])
 narf['LL'] = narf.apply(lambda x: np.log(x.ptr)* x.incr, axis=1)
 narf.loc[index,'LL'] = pt
LL = np.sum(narf.LL)
 bool2 =(test['Country/Region']==country)&(test['Province/State']==state)
 narft = test[bool2].copy().reset_index()
narft['ts'] = narft.day_from_jan_first - dfj
narft['pt'] = narft.ts.apply(lambda x:(1-(a/(a+x**c)) **r)*(1-hc))
narft['wg'] = narft.pt * pop
test.loc[bool2, 'wg'] = narft.wg.values
print('done')
 def get_params(booll, rl = [1], al = [1], cl = [1], hcl = [.5]):
r_s,a_s,c_s,hc_s = rl[0],al[0],cl[0], hcl[0]
LL = float('-inf')
 for r in rl:
for a in al:
for c in cl:
for hc in hcl:
narf = train[ booll ].copy().reset_index()
narf['ts'] = narf.index+1
if narf.shape[0] == 0:
return r_s, a_s, c_s, hc_s
narf['pt'] = narf.ts.apply(lambda x:(1-(a/(a+x**c)) **r)*(1-hc))
temp = narf.copy()
temp['ts'] = temp.index
temp = temp[['ts','ConfirmedCases','pt']]
temp.columns = ['ts','incr', 'ptr']
if temp.shape[0] == 0:
return r_s, a_s, c_s, hc_s
narf = pd.merge(narf, temp, on='ts', how='left')
 narf['incr'] = narf['incr'] - narf['ConfirmedCases']
narf['ptr'] = narf['ptr'] - narf['pt']
index = narf.shape[0] - 1
pop = narf.iloc[index]['pop']
if np.isnan(pop):
return r_s, a_s, c_s, hc_s
cc = narf.iloc[index]['ConfirmedCases']
dfj = narf.iloc[0]['day_from_jan_first']
pt =(pop-cc)* np.log(1-narf.iloc[index]['pt'])
 narf['LL'] = narf.apply(lambda x: np.log(x.ptr)* x.incr, axis=1)
 narf.loc[index,'LL'] = pt
ll = np.sum(narf.LL)
 if ll > LL:
r_s, a_s, c_s, hc_s = r, a, c, hc
LL = ll
return r_s, a_s, c_s, hc_s<load_from_csv>
from sklearn.ensemble import RandomForestRegressor
PATH = '/kaggle/input/covid19-global-forecasting-week-1/'
train = pd.read_csv(PATH + 'train.csv')
 test = pd.read_csv(PATH + 'test.csv')
 dfp = pd.read_csv('/kaggle/input/population/' + 'population.csv')
 print(train.shape, dfp.shape )<merge>
rf = RandomForestRegressor(random_state=42, n_jobs=-1 )
train.loc[train['Province/State'].isnull() , 'Province/State'] = 'NARF'
test.loc[test['Province/State'].isnull() , 'Province/State'] = 'NARF'
n = train.shape[0]
train = pd.merge(train, dfp, on=['Country/Region','Province/State'], how='left')
 assert train.shape[0] == n
n = test.shape[0]
test = pd.merge(test, dfp, on=['Country/Region','Province/State'], how='left')
 assert test.shape[0] == n
train.loc[train['pop'].isnull() ,'pop'] = 0
test.loc[test['pop'].isnull() ,'pop'] = 0
mo = train['Date'].apply(lambda x: x[5:7])
 da = train['Date'].apply(lambda x: x[8:10])
 train['day_from_jan_first'] =(da.apply(int)
 + 31*(mo=='02')
 + 60*(mo=='03')
 + 91*(mo=='04')
)
 mo = test['Date'].apply(lambda x: x[5:7])
 da = test['Date'].apply(lambda x: x[8:10])
 test['day_from_jan_first'] =(da.apply(int)
 + 31*(mo=='02')
 + 60*(mo=='03')
 + 91*(mo=='04')
)
 print('done' )<feature_engineering>
rf.fit(train[feats], train['rentals'] )
test['wg'] = np.NaN
for country in test['Country/Region'].unique() :
booll = test['Country/Region']==country
for state in test[booll]['Province/State'].unique() :
print(country, state)
 get_wg(country, state)
 print('done', train.shape )<prepare_output>
preds = rf.predict(valid[feats] )
client = bigquery.Client()
dataset_ref = client.dataset("noaa_gsod", project="bigquery-public-data")
 dataset = client.get_dataset(dataset_ref)
 tables = list(client.list_tables(dataset))
table_ref = dataset_ref.table("stations")
 table = client.get_table(table_ref)
 stations_df = client.list_rows(table ).to_dataframe()
table_ref = dataset_ref.table("gsod2020")
 table = client.get_table(table_ref)
 twenty_twenty_df = client.list_rows(table ).to_dataframe()
stations_df['STN'] = stations_df['usaf'] + '-' + stations_df['wban']
twenty_twenty_df['STN'] = twenty_twenty_df['stn'] + '-' + twenty_twenty_df['wban']
cols_1 = ['STN', 'mo', 'da', 'temp', 'min', 'max', 'stp', 'wdsp', 'prcp', 'fog']
cols_2 = ['STN', 'country', 'state', 'call', 'lat', 'lon', 'elev']
weather_df = twenty_twenty_df[cols_1].join(stations_df[cols_2].set_index('STN'), on='STN')
 weather_df.tail(10 )<feature_engineering>
from sklearn.metrics import mean_squared_error
weather_df['day_from_jan_first'] =(weather_df['da'].apply(int)
 + 31*(weather_df['mo']=='02')
 + 60*(weather_df['mo']=='03')
 + 91*(weather_df['mo']=='04')
)
 C = []
for j in train.index:
df = train.iloc[j:(j+1)]
mat = cdist(df[['Lat','Long', 'day_from_jan_first']],
weather_df[['lat','lon', 'day_from_jan_first']],
metric='euclidean')
 new_df = pd.DataFrame(mat, index=df.Id, columns=weather_df.index)
 arr = new_df.values
new_close = np.where(arr == np.nanmin(arr, axis=1)[:,None],new_df.columns,False)
 L = [i[i.astype(bool)].tolist() [0] for i in new_close]
C.append(L[0])
 train['closest_station'] = C
train = train.set_index('closest_station' ).join(weather_df[['temp', 'min', 'max', 'stp', 'wdsp', 'prcp', 'fog']], ).reset_index().drop(['index'], axis=1)
 train.sort_values(by=['Id'], inplace=True)
 train.head()<feature_engineering>
from sklearn.metrics import mean_squared_error
weather_df['day_from_jan_first'] =(weather_df['da'].apply(int)
 + 31*(weather_df['mo']=='02')
 + 60*(weather_df['mo']=='03')
 + 91*(weather_df['mo']=='04')
)
 C = []
for j in test.index:
df = test.iloc[j:(j+1)]
mat = cdist(df[['Lat','Long', 'day_from_jan_first']],
weather_df[['lat','lon', 'day_from_jan_first']],
metric='euclidean')
 new_df = pd.DataFrame(mat, index=df.ForecastId, columns=weather_df.index)
 arr = new_df.values
new_close = np.where(arr == np.nanmin(arr, axis=1)[:,None],new_df.columns,False)
 L = [i[i.astype(bool)].tolist() [0] for i in new_close]
C.append(L[0])
 test['closest_station'] = C
test = test.set_index('closest_station' ).join(weather_df[['temp', 'min', 'max', 'stp', 'wdsp', 'prcp', 'fog']], ).reset_index().drop(['index'], axis=1)
 test.sort_values(by=['ForecastId'], inplace=True)
 test.head()<data_type_conversions>
mean_squared_error(valid['rentals'], preds)**(1/2 )
train["wdsp"] = pd.to_numeric(train["wdsp"])
 test["wdsp"] = pd.to_numeric(test["wdsp"])
 train["fog"] = pd.to_numeric(train["fog"])
 test["fog"] = pd.to_numeric(test["fog"])
 print('done' )<feature_engineering>
train_preds = rf.predict(train[feats] )
train.loc[train['Province/State']=='NARF', 'Province/State'] = np.NaN
test.loc[ test['Province/State']=='NARF', 'Province/State'] = np.NaN<data_type_conversions>
train_preds = rf.predict(train[feats] )
X_train = train.drop(["ConfirmedCases"], axis=1)
 countries = X_train["Country/Region"]
X_train = X_train.drop(["Id"], axis=1)
 X_test = test.drop(["ForecastId"], axis=1)
 X_train['Date']= pd.to_datetime(X_train['Date'])
 X_test['Date']= pd.to_datetime(X_test['Date'])
 X_train = X_train.set_index(['Date'])
 X_test = X_test.set_index(['Date'])
 create_time_features(X_train)
 create_time_features(X_test)
 X_train.drop("date", axis=1, inplace=True)
 X_test.drop("date", axis=1, inplace=True)
 print(X_train.shape, X_test.shape)
 X_train.drop(["Fatalities"], axis=1, inplace=True)
 print('done', X_train.shape)

print('done', X_train.shape )<categorify>
mean_squared_error(train['rentals'], train_preds)**(1/2 )
X_train = pd.concat([X_train,pd.get_dummies(X_train['Province/State'], prefix='ps')],axis=1)
 X_train.drop(['Province/State'],axis=1, inplace=True)
 X_test = pd.concat([X_test,pd.get_dummies(X_test['Province/State'], prefix='ps')],axis=1)
 X_test.drop(['Province/State'],axis=1, inplace=True)
 X_train = pd.concat([X_train,pd.get_dummies(X_train['Country/Region'], prefix='cr')],axis=1)
 X_train.drop(['Country/Region'],axis=1, inplace=True)
 X_test = pd.concat([X_test,pd.get_dummies(X_test['Country/Region'], prefix='cr')],axis=1)
 X_test.drop(['Country/Region'],axis=1, inplace=True)
 print(X_train.shape, X_test.shape)
<prepare_x_and_y>
test['count'] = np.exp(rf.predict(test[feats]))
y_train = train["Fatalities"]
y_train_cc = train["ConfirmedCases"]<init_hyperparams>
test[['datetime', 'count']].to_csv('rf.csv', index=False )
X_TRAIN = X_train.values
params_xgb = {}
params_xgb['n_estimators'] = 1100
params_xgb['max_depth'] = 9
params_xgb['seed'] = 2020
params_xgb['colsample_bylevel'] = 1
params_xgb['colsample_bytree'] = 1
params_xgb['learning_rate'] = 0.300000012
params_xgb['reg_alpha'] = 0
params_xgb['reg_lambda'] = 1
params_xgb['subsample'] = 1
isTraining = False
if isTraining:
kf = KFold(n_splits = 5, shuffle = True, random_state=2020)
 acc = []
for tr_idx, val_idx in kf.split(X_TRAIN, y_train_cc):
X_tr, X_vl = X_TRAIN[tr_idx], X_TRAIN[val_idx, :]
y_tr, y_vl = y_train_cc[tr_idx], y_train_cc[val_idx]
print(X_tr.shape)
 model_xgb_cc = xgb.XGBRegressor(**params_xgb)
 model_xgb_cc.fit(X_tr, y_tr, verbose=True)
 y_hat = model_xgb_cc.predict(X_vl)
 print('xgb mae :', mean_absolute_error(y_vl, y_hat))
acc.append(mean_absolute_error(y_vl, y_hat))
print('done', np.mean(acc)) 
<compute_test_metric>
rf = RandomForestRegressor(random_state=42, n_jobs=-1, n_estimators=200 )
print('done', np.mean(acc))<train_model>
rf.fit(train[feats], train['rentals'] )
params_xgb = {}
params_xgb['n_estimators'] = 1100
params_xgb['max_depth'] = 9
params_xgb['seed'] = 2020
params_xgb['colsample_bylevel'] = 1
params_xgb['colsample_bytree'] = 1
params_xgb['learning_rate'] = 0.300000012
params_xgb['reg_alpha'] = 0
params_xgb['reg_lambda'] = 1
params_xgb['subsample'] = 1
model_xgb_f = xgb.XGBRegressor(**params_xgb)
 model_xgb_f.fit(X_train, y_train, verbose=True)
 y_hat_xgb_f = model_xgb_f.predict(X_test.drop('wg',axis=1))
print(np.mean(y_hat_xgb_f))<train_model>
preds = rf.predict(valid[feats] )
params_xgb = {}
params_xgb['n_estimators'] = 1100
params_xgb['max_depth'] = 9
params_xgb['seed'] = 2020
params_xgb['colsample_bylevel'] = 1
params_xgb['colsample_bytree'] = 1
params_xgb['learning_rate'] = 0.300000012
params_xgb['reg_alpha'] = 0
params_xgb['reg_lambda'] = 1
params_xgb['subsample'] = 1
model_xgb_cc = xgb.XGBRegressor(**params_xgb)
 model_xgb_cc.fit(X_train, y_train_cc, verbose=True)
 y_hat_xgb_cc = model_xgb_cc.predict(X_test.drop('wg',axis=1))
print(np.mean(y_hat_xgb_cc))<feature_engineering>
mean_squared_error(valid['rentals'], preds)**(1/2 )
test['y_hat_cc'] = y_hat_xgb_cc
test.loc[test.wg.isnull() ,'wg'] = test[test.wg.isnull() ].y_hat_cc
test['y_hat_ens'] =.75 * test.y_hat_cc +.25 * test.wg
print('done' )<data_type_conversions>
train_preds = rf.predict(train[feats] )
test['y_hat_ens'] = test.y_hat_ens.astype(float )<load_from_csv>
train_preds = rf.predict(train[feats] )
submissionOrig = pd.read_csv(".. /input/covid19-global-forecasting-week-1/submission.csv")
 submissionOrig["ConfirmedCases"]= pd.Series(test.y_hat_ens)
 submissionOrig["Fatalities"] = pd.Series(y_hat_xgb_f )<save_to_csv>
mean_squared_error(train['rentals'], train_preds)**(1/2 )
submissionOrig.to_csv('submission.csv',index=False )<set_options>
test['count'] = np.exp(rf.predict(test[feats]))
sns.set(context="notebook", style="darkgrid", palette="deep", font="sans-serif", font_scale=1, color_codes=True)
<load_from_csv>
test[['datetime', 'count']].to_csv('rf_opt.csv', index=False )
data= pd.read_csv(".. /input/covid19-global-forecasting-week-1/train.csv")
 test = pd.read_csv(".. /input/covid19-global-forecasting-week-1/test.csv" )<feature_engineering>
rf = RandomForestRegressor(random_state=42, n_jobs=-1, n_estimators=200, oob_score=True )
data['Province/State']=data['Province/State'].fillna('' )<feature_engineering>
rf.fit(df[feats], df['rentals'] )
test['Province/State']=test['Province/State'].fillna('' )<count_values>
mean_squared_error(df['rentals'], rf.oob_prediction_)**(1/2 )
data['Province/State'].value_counts()<count_values>
rf.oob_score_
test['Province/State'].value_counts()<import_modules>
from sklearn.metrics import r2_score
from sklearn.preprocessing import LabelEncoder<categorify>
r2_score(df['rentals'], rf.oob_prediction_ )
le_state=LabelEncoder()<train_model>
r2_score(df['rentals'], df['rentals'] )
le_state=le_state.fit(data['Province/State'] )<categorify>
r2_score(df['rentals'], np.full(rf.oob_prediction_.shape[0], df['rentals'].mean()))
data['Province/State']=le_state.transform(data['Province/State'])
 test['Province/State']=le_state.transform(test['Province/State'] )<categorify>
r2_score(df['rentals'], np.full(rf.oob_prediction_.shape[0], 10000))
le_country=LabelEncoder()<train_model>
test['count'] = np.exp(rf.predict(test[feats]))
le_country=le_country.fit(data['Country/Region'] )<categorify>
test[['datetime', 'count']].to_csv('rf_full.csv', index=False )
data['Country/Region']=le_country.transform(data['Country/Region'])
 test['Country/Region']=le_country.transform(test['Country/Region'] )<data_type_conversions>
train, valid = df[df['day'] <= 15], df[df['day'] > 15]
datetime_str = '01/22/20 00:00:00'
datetime_object = datetime.strptime(datetime_str, '%m/%d/%y %H:%M:%S')
<data_type_conversions>
from sklearn.ensemble import RandomForestRegressor
data['days']=pd.to_datetime(data['Date'] ).sub(datetime_object)/np.timedelta64(1, 'D' )<data_type_conversions>
from sklearn.ensemble import RandomForestRegressor
test['days']=pd.to_datetime(test['Date'] ).sub(datetime_object)/np.timedelta64(1, 'D' )<import_modules>
rf = RandomForestRegressor(random_state=42, n_jobs=-1, n_estimators=200, min_samples_split=4, max_features=0.9, max_depth=17, oob_score=True )
import xgboost as xgb<count_missing_values>
rf.fit(train[feats], train['rentals'] )
data.isna().sum(axis=0)/data.shape[0]<count_missing_values>
preds = rf.predict(valid[feats] )
test.isna().sum(axis=0 )<prepare_x_and_y>
from sklearn.metrics import mean_squared_error
x = data[['Province/State','Country/Region','Lat', 'Long', 'days']]
y1 = data['ConfirmedCases']
y2 = data['Fatalities']
x_test_set = test[['Province/State','Country/Region','Lat', 'Long', 'days']] 
<compute_test_metric>
from sklearn.metrics import mean_squared_error
def rmsle(y, y_pred):
assert len(y)== len(y_pred)
 terms_to_sum = [(math.log(y_pred[i] + 1)- math.log(y[i] + 1)) ** 2.0 for i,pred in enumerate(y_pred)]
return(sum(terms_to_sum)*(1.0/len(y)))** 0.5<choose_model_class>
mean_squared_error(valid['rentals'], preds)**(1/2 )
model=xgb.XGBRegressor(base_score=0.5, booster=None, colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0.1, gpu_id=-1,
importance_type='gain', interaction_constraints=None,
learning_rate=0.1, max_delta_step=0, max_depth=20,
min_child_weight=1, monotone_constraints=None,
n_estimators=2048, n_jobs=0, num_parallel_tree=10,
objective='reg:squarederror', random_state=0, reg_alpha=0,
reg_lambda=1, scale_pos_weight=1, subsample=1, tree_method=None,
validate_parameters=False, verbosity=None )<train_model>
feats = [c for c in feats if c not in ['day']]
model.fit(x,y1 )<predict_on_test>
rf = RandomForestRegressor(random_state=42, n_jobs=-1, n_estimators=200, min_samples_split=4, max_features=0.9, max_depth=17, oob_score=True )
pred1 = model.predict(x_test_set )<define_variables>
rf.fit(df[feats], df['rentals'] )
pred1=np.array([int(round(p)) for p in pred1] )<filter>
preds = rf.predict(df[feats] )
pred1[np.array(pred1)<0]=0<predict_on_test>
mean_squared_error(df['rentals'], rf.oob_prediction_)**(1/2 )
model.fit(x,y2)
 pred2 = model.predict(x_test_set)
 pred2=np.array([int(round(p)) for p in pred2])
 pred2[pred2<0]=0<load_from_csv>
train_preds = rf.predict(df[feats] )
Sub = pd.read_csv(".. /input/covid19-global-forecasting-week-1/submission.csv")
 Sub.columns
sub_new = Sub["ForecastId"]<create_dataframe>
mean_squared_error(df['rentals'], train_preds)**(1/2 )
OP = pd.DataFrame()
OP['ForecastId']=Sub["ForecastId"]
OP['ConfirmedCases']=pred1
OP['Fatalities']=pred2 
<data_type_conversions>
rf.predict(test[feats] )
OP["ConfirmedCases"] = OP["ConfirmedCases"].astype(int)
 OP["Fatalities"] = OP["Fatalities"].astype(int)
<save_to_csv>
test['count'] = np.exp(rf.predict(test[feats]))
OP.to_csv("submission.csv",index=False )<set_options>
test[['datetime', 'count']].to_csv('rf_full_wo_day.csv', index=False )
warnings.filterwarnings("ignore")
 sys.path.append('.. /input/iterative-stratification/iterative-stratification-master')
 np.random.seed(42)
 tf.random.set_seed(42)
 print("Tensorflow version " + tf.__version__)
 AUTO = tf.data.experimental.AUTOTUNE<set_options>
feats = [c for c in feats if c not in ['month', 'holiday']]
MIXED_PRECISION = False
XLA_ACCELERATE = True
if MIXED_PRECISION:
if tpu: policy = tf.keras.mixed_precision.experimental.Policy('mixed_bfloat16')
 else: policy = tf.keras.mixed_precision.experimental.Policy('mixed_float16')
 mixed_precision.set_policy(policy)
 print('Mixed precision enabled')
 if XLA_ACCELERATE:
tf.config.optimizer.set_jit(True)
 print('Accelerated Linear Algebra enabled' )<load_from_csv>
rf = RandomForestRegressor(random_state=42, n_jobs=-1, n_estimators=200, min_samples_split=4, max_features=0.9, max_depth=17, oob_score=True )
train_features = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_targets = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 sub = pd.read_csv('.. /input/lish-moa/sample_submission.csv')
 cols = [c for c in sub.columns.values if c != 'sig_id']<compute_test_metric>
rf.fit(df[feats], df['rentals'] )
def preprocess(df):
df.loc[:, 'cp_type'] = df.loc[:, 'cp_type'].map({'trt_cp': 0, 'ctl_vehicle': 1})
 df.loc[:, 'cp_dose'] = df.loc[:, 'cp_dose'].map({'D1': 0, 'D2': 1})
 del df['sig_id']
return df
def log_loss_metric(y_true, y_pred):
metrics = []
for _target in train_targets.columns:
metrics.append(log_loss(y_true.loc[:, _target], y_pred.loc[:, _target].astype(float), labels = [0,1]))
return np.mean(metrics)
 train = preprocess(train_features)
 test = preprocess(test_features)
 del train_targets['sig_id']<define_variables>
preds = rf.predict(df[feats] )
top_feats = [ 0, 1, 2, 3, 5, 6, 8, 9, 10, 11, 12, 14, 15,
16, 18, 19, 20, 21, 23, 24, 25, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 39, 40, 41, 42, 44, 45, 46,
48, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
63, 64, 65, 66, 68, 69, 70, 71, 72, 73, 74, 75, 76,
78, 79, 80, 81, 82, 83, 84, 86, 87, 88, 89, 90, 92,
93, 94, 95, 96, 97, 99, 100, 101, 103, 104, 105, 106, 107,
108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120,
121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 132, 133, 134,
135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147,
149, 150, 151, 152, 153, 154, 155, 157, 159, 160, 161, 163, 164,
165, 166, 167, 168, 169, 170, 172, 173, 175, 176, 177, 178, 180,
181, 182, 183, 184, 186, 187, 188, 189, 190, 191, 192, 193, 195,
197, 198, 199, 202, 203, 205, 206, 208, 209, 210, 211, 212, 213,
214, 215, 218, 219, 220, 221, 222, 224, 225, 227, 228, 229, 230,
231, 232, 233, 234, 236, 238, 239, 240, 241, 242, 243, 244, 245,
246, 248, 249, 250, 251, 253, 254, 255, 256, 257, 258, 259, 260,
261, 263, 265, 266, 268, 270, 271, 272, 273, 275, 276, 277, 279,
282, 283, 286, 287, 288, 289, 290, 294, 295, 296, 297, 299, 300,
301, 302, 303, 304, 305, 306, 308, 309, 310, 311, 312, 313, 315,
316, 317, 320, 321, 322, 324, 325, 326, 327, 328, 329, 330, 331,
332, 333, 334, 335, 338, 339, 340, 341, 343, 344, 345, 346, 347,
349, 350, 351, 352, 353, 355, 356, 357, 358, 359, 360, 361, 362,
363, 364, 365, 366, 368, 369, 370, 371, 372, 374, 375, 376, 377,
378, 379, 380, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391,
392, 393, 394, 395, 397, 398, 399, 400, 401, 403, 405, 406, 407,
408, 410, 411, 412, 413, 414, 415, 417, 418, 419, 420, 421, 422,
423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435,
436, 437, 438, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450,
452, 453, 454, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465,
466, 468, 469, 471, 472, 473, 474, 475, 476, 477, 478, 479, 482,
483, 485, 486, 487, 488, 489, 491, 492, 494, 495, 496, 500, 501,
502, 503, 505, 506, 507, 509, 510, 511, 512, 513, 514, 516, 517,
518, 519, 521, 523, 525, 526, 527, 528, 529, 530, 531, 532, 533,
534, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545, 546, 547,
549, 550, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 563,
564, 565, 566, 567, 569, 570, 571, 572, 573, 574, 575, 577, 580,
581, 582, 583, 586, 587, 590, 591, 592, 593, 595, 596, 597, 598,
599, 600, 601, 602, 603, 605, 607, 608, 609, 611, 612, 613, 614,
615, 616, 617, 619, 622, 623, 625, 627, 630, 631, 632, 633, 634,
635, 637, 638, 639, 642, 643, 644, 645, 646, 647, 649, 650, 651,
652, 654, 655, 658, 659, 660, 661, 662, 663, 664, 666, 667, 668,
669, 670, 672, 674, 675, 676, 677, 678, 680, 681, 682, 684, 685,
686, 687, 688, 689, 691, 692, 694, 695, 696, 697, 699, 700, 701,
702, 703, 704, 705, 707, 708, 709, 711, 712, 713, 714, 715, 716,
717, 723, 725, 727, 728, 729, 730, 731, 732, 734, 736, 737, 738,
739, 740, 741, 742, 743, 744, 745, 746, 747, 748, 749, 750, 751,
752, 753, 754, 755, 756, 758, 759, 760, 761, 762, 763, 764, 765,
766, 767, 769, 770, 771, 772, 774, 775, 780, 781, 782, 783, 784,
785, 787, 788, 790, 793, 795, 797, 799, 800, 801, 805, 808, 809,
811, 812, 813, 816, 819, 820, 821, 822, 823, 825, 826, 827, 829,
831, 832, 833, 834, 835, 837, 838, 839, 840, 841, 842, 844, 845,
846, 847, 848, 850, 851, 852, 854, 855, 856, 858, 860, 861, 862,
864, 867, 868, 870, 871, 873, 874]
print(len(top_feats))<choose_model_class>
mean_squared_error(df['rentals'], rf.oob_prediction_)**(1/2 )
def create_model(num_columns, hidden_units, dropout_rate, learning_rate):
inp1 = tf.keras.layers.Input(shape =(num_columns,))
x1 = tf.keras.layers.BatchNormalization()(inp1)
 for i, units in enumerate(hidden_units[0]):
x1 = tfa.layers.WeightNormalization(tf.keras.layers.Dense(units, activation = 'elu'))(x1)
 x1 = tf.keras.layers.Dropout(dropout_rate[0] )(x1)
 x1 = tf.keras.layers.BatchNormalization()(x1)
 inp2 = tf.keras.layers.Input(shape =(num_columns,))
x2 = tf.keras.layers.BatchNormalization()(inp2)
 for i, units in enumerate(hidden_units[1]):
x2 = tfa.layers.WeightNormalization(tf.keras.layers.Dense(units, activation = 'elu'))(x2)
 x2 = tf.keras.layers.Dropout(dropout_rate[1] )(x2)
 x2 = tf.keras.layers.BatchNormalization()(x2)
 inp3 = tf.keras.layers.Input(shape =(num_columns,))
x3 = tf.keras.layers.BatchNormalization()(inp3)
 for i, units in enumerate(hidden_units[2]):
x3 = tfa.layers.WeightNormalization(tf.keras.layers.Dense(units, activation = 'elu'))(x3)
 x3 = tf.keras.layers.Dropout(dropout_rate[2] )(x3)
 x3 = tf.keras.layers.BatchNormalization()(x3)
 inp4 = tf.keras.layers.Input(shape =(num_columns,))
x4 = tf.keras.layers.BatchNormalization()(inp4)
 for i, units in enumerate(hidden_units[3]):
x4 = tfa.layers.WeightNormalization(tf.keras.layers.Dense(units, activation = 'elu'))(x4)
 x4 = tf.keras.layers.Dropout(dropout_rate[3] )(x4)
 x4 = tf.keras.layers.BatchNormalization()(x4)
 x = tf.keras.layers.Concatenate()([x1, x2, x3, x4])
 x = tf.keras.layers.Dropout(dropout_rate[4] )(x)
 x = tf.keras.layers.BatchNormalization()(x)
 for units in hidden_units[4]:
x = tfa.layers.WeightNormalization(tf.keras.layers.Dense(units, activation = 'elu'))(x)
 x = tf.keras.layers.Dropout(dropout_rate[5] )(x)
 x = tf.keras.layers.BatchNormalization()(x)
 out = tfa.layers.WeightNormalization(tf.keras.layers.Dense(206, activation = 'sigmoid'))(x)
 model = tf.keras.models.Model(inputs = [inp1, inp2, inp3, inp4], outputs = out)
 model.compile(optimizer = tfa.optimizers.Lookahead(tf.optimizers.Adam(learning_rate), sync_period = 10),
loss = 'binary_crossentropy')
 return model<define_search_space>
train_preds = rf.predict(df[feats] )
hidden_units = [[2048, 512, 2048],
[512, 1024, 512],
[512, 1024, 2048, 1024, 512],
[1024, 2048, 1024],
[1024, 1024]]
dropout_rate = [0.4, 0.3, 0.45, 0.3, 0.35, 0.4]
size = int(np.ceil(0.5 * len(top_feats)))
 res = train_targets.copy()
sub.loc[:, train_targets.columns] = 0
res.loc[:, train_targets.columns] = 0
N_STARTS = 3
for seed in range(N_STARTS):
split_cols = []
for _ in range(4):
split_cols.append(np.random.choice(top_feats, size))
for n,(tr, te)in enumerate(MultilabelStratifiedKFold(n_splits = 5, random_state = seed, shuffle = True ).split(train_targets, train_targets)) :
start_time = time()
x_tr = [
train.values[tr][:, split_cols[0]],
train.values[tr][:, split_cols[1]],
train.values[tr][:, split_cols[2]],
train.values[tr][:, split_cols[3]]
]
x_val = [
train.values[te][:, split_cols[0]],
train.values[te][:, split_cols[1]],
train.values[te][:, split_cols[2]],
train.values[te][:, split_cols[3]]
]
y_tr, y_val = train_targets.astype(float ).values[tr], train_targets.astype(float ).values[te]
x_tt = [
test_features.values[:, split_cols[0]],
test_features.values[:, split_cols[1]],
test_features.values[:, split_cols[2]],
test_features.values[:, split_cols[3]]
]
model = create_model(size, hidden_units, dropout_rate, 1e-3)
 rlr = ReduceLROnPlateau(monitor = 'val_loss', factor = 0.1, patience = 3, verbose = 0,
min_delta = 1e-4, min_lr = 1e-5, mode = 'min')
 ckp = ModelCheckpoint(f'split_nn.hdf5', monitor = 'val_loss', verbose = 0,
save_best_only = True, save_weights_only = True, mode = 'min')
 es = EarlyStopping(monitor = 'val_loss', min_delta = 1e-4, patience = 10, mode = 'min',
baseline = None, restore_best_weights = True, verbose = 0)
 history = model.fit(x_tr, y_tr, validation_data =(x_val, y_val),
epochs = 100, batch_size = 128, callbacks = [rlr, ckp, es], verbose = 0)
 hist = pd.DataFrame(history.history)
 model.load_weights(f'split_nn.hdf5')
 sub.loc[:, train_targets.columns] += model.predict(x_tt, batch_size = 128)
 res.loc[te, train_targets.columns] += model.predict(x_val, batch_size = 128)
 print(f'[{str(datetime.timedelta(seconds = time() - start_time)) [2:7]}] Split NN: Seed {seed}, Fold {n}:', hist['val_loss'].min())
 K.clear_session()
del model, history, hist
x = gc.collect()
sub.loc[:, train_targets.columns] /=(( n + 1)* N_STARTS)
 res.loc[:, train_targets.columns] /= N_STARTS<compute_test_metric>
mean_squared_error(df['rentals'], train_preds)**(1/2 )
print(f'Split NN OOF Metric: {log_loss_metric(train_targets, res)}')
 res.loc[train['cp_type'] == 1, train_targets.columns] = 0
sub.loc[test['cp_type'] == 1, train_targets.columns] = 0
print(f'Split NN OOF Metric with postprocessing: {log_loss_metric(train_targets, res)}' )<save_to_csv>
rf.predict(test[feats] )
sub.to_csv('submission.csv', index = False )<import_modules>
test['count'] = np.exp(rf.predict(test[feats]))
import numpy as np
import pandas as pd
import os
import math
import matplotlib.pyplot as plt
from sklearn.model_selection import KFold
from sklearn.metrics import log_loss
import tensorflow as tf
import tensorflow_addons as tfa
import tensorflow.keras.backend as K
import tensorflow.keras.layers as L
import tensorflow.keras.models as M<load_from_csv>
test[['datetime', 'count']].to_csv('rf_full_wo_month.csv', index=False )
test_df = pd.read_csv('.. /input/lish-moa/test_features.csv')
 train_df = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_target_df = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 sub = pd.read_csv('.. /input/lish-moa/sample_submission.csv')
 target_cols = train_target_df.columns[1:]<define_variables>
def cv(df, test, k, feats, y_name):
preds, score, fis = [], [], []
chunk = df.shape[0] // k
for i in range(k):
if i + 1 < k:
valid = df.iloc[i*chunk:(i+1)*chunk]
train = df.iloc[: i*chunk].append(df.iloc[(i+1)*chunk:])
 else:
valid = df.iloc[i*chunk:]
train = df.iloc[: i*chunk]
rf = RandomForestRegressor(random_state=42, n_jobs=-1, n_estimators=200, min_samples_split=4, max_features=0.9, max_depth=17, oob_score=True)
 rf.fit(train[feats], train[y_name])
 score.append(mean_squared_error(valid[y_name], rf.predict(valid[feats])) **(1/2))
preds.append(rf.predict(test[feats]))
fis.append(rf.feature_importances_)
 print(i, 'OK')
 return pd.Series(score), pd.Series(preds ).mean() , fis
SEED = 9876
EPOCHS = 30
BATCH_SIZE = 128
FOLDS = 5
REPEATS = 2
LR = 0.0005
N_TARGETS = len(target_cols )<define_variables>
score, preds, fis = cv(df, test, 20, feats, 'rentals' )
def seed_everything(seed):
np.random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 tf.random.set_seed(seed )<compute_test_metric>
test['count'] = np.exp(preds )
def multi_log_loss(y_true, y_pred):
losses = []
for col in y_true.columns:
losses.append(log_loss(y_true.loc[:, col], y_pred.loc[:, col]))
return np.mean(losses )<data_type_conversions>
test[['datetime', 'count']].to_csv('rf_cv_wo_day.csv', index=False )
def preprocess_df(data):
data['cp_type'] =(data['cp_type'] == 'trt_cp' ).astype(int)
 data['cp_dose'] =(data['cp_dose'] == 'D2' ).astype(int)
 return data<prepare_x_and_y>
fi = pd.Series(pd.DataFrame(fis ).mean().values, index=feats )
x_train = preprocess_df(train_df.drop(columns="sig_id"))
x_test =preprocess_df(test_df.drop(columns="sig_id"))
y_train = train_target_df.drop(columns="sig_id")
 N_FEATURES = x_train.shape[1]<choose_model_class>
feats = [c for c in feats if c not in ['holiday']]
def create_model() :
model = tf.keras.Sequential([
tf.keras.layers.Input(N_FEATURES),
tf.keras.layers.GaussianNoise(NOISE_STD_VECTOR),
tf.keras.layers.BatchNormalization() ,
tfa.layers.WeightNormalization(tf.keras.layers.Dense(3072, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.4),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(3072, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.4),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(N_TARGETS, activation="sigmoid"))
])
 model.compile(optimizer=tfa.optimizers.Lookahead(tf.optimizers.Adam(lr=LR), sync_period=10), loss='binary_crossentropy', metrics=["accuracy"])
 return model<train_model>
score, preds, fis = cv(df, test, 20, feats, 'rentals' )
def build_train(resume_models = None, repeat_number = 0, folds = 5, skip_folds = 0):
models = []
oof_preds = y_train.copy()
kfold = KFold(folds, shuffle = True)
 for fold,(train_ind, val_ind)in enumerate(kfold.split(x_train)) :
print('-'*50)
 print(f'Training fold {fold + 1}')
 cb_lr_schedule = tf.keras.callbacks.ReduceLROnPlateau(monitor = 'val_loss', factor = 0.4, patience = 3, verbose = 0, min_delta = 0.0001, mode = 'auto')
 checkpoint_path = f'repeat:{repeat_number}_Fold:{fold}.hdf5'
cb_checkpt = tf.keras.callbacks.ModelCheckpoint(checkpoint_path, monitor = 'val_loss', verbose = 0, save_best_only = True, save_weights_only = True, mode = 'min')
 model = create_model()
model.fit(x_train.values[train_ind],
y_train.values[train_ind],
validation_data=(x_train.values[val_ind], y_train.values[val_ind]),
callbacks = [cb_lr_schedule, cb_checkpt],
epochs=EPOCHS, batch_size=BATCH_SIZE, verbose=0 
)
 model.load_weights(checkpoint_path)
 oof_preds.loc[val_ind, :] = model.predict(x_train.values[val_ind])
 models.append(model)
 return models, oof_preds<define_search_space>
test['count'] = np.exp(preds )
<define_variables><EOS> 
test[['datetime', 'count']].to_csv('rf_cv_wo_holiday.csv', index=False )