Kaggle competition: Rossmann Store Sales
def decode_image(filename, label=None, image_size=(img_size, img_size)) :
bits = tf.io.read_file(filename)
 image = tf.image.decode_jpeg(bits, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.image.resize(image, image_size)
 if label is None:
return image
else:
return image, label
def data_augment(image, label=None, seed=2020):
image = tf.image.random_flip_left_right(image, seed=seed)
 image = tf.image.random_flip_up_down(image, seed=seed)
 if tf.random.uniform([1])>0.4:
image = tf.image.adjust_brightness(image, 0.2)
 if label is None:
return image
else:
return image, label<categorify>
import statsmodels.api as sm
from statsmodels.sandbox.regression.predstd import wls_prediction_std
import math
import sklearn.preprocessing as skpe
import sklearn.model_selection as ms
import sklearn.metrics as sklm
import sklearn.ensemble as sken
import sklearn.linear_model as lm
import seaborn as sns
import matplotlib.pyplot as plt
train_set =(tf.data.Dataset 
.from_tensor_slices(( train_tpu_paths, train_labels)) 
.map(decode_image, num_parallel_calls=AUTO)
.map(data_augment, num_parallel_calls=AUTO)
.repeat() 
.shuffle(512)
.batch(BATCH_SIZE)
.prefetch(AUTO))
test_dataset =(
 tf.data.Dataset 
.from_tensor_slices(test_tpu_paths)
.map(decode_image, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
 )<choose_model_class>
path=".. /input/rossmann-store-sales/train.csv"
train=pd.read_csv(path)
 print(train.shape)
 train.head()
def get_model_eff() :
base_model = efn.EfficientNetB7(weights='noisy-student', include_top=False, pooling='avg', input_shape=(None, None, 3))
x = base_model.output
predictions = tf.keras.layers.Dense(n_classes, activation="softmax" )(x)
 return tf.keras.models.Model(inputs=base_model.input, outputs=predictions )<find_best_params>
path1=".. /input/rossmann-store-sales/test.csv"
test=pd.read_csv(path1)
 print(test.shape)
 test.head()
with strategy.scope() :
model1 = get_model_eff()
model1.compile(optimizer='adam', loss='categorical_crossentropy',metrics=['accuracy'] )<train_model>
path2=".. /input/rossmann-store-sales/store.csv"
store_df=pd.read_csv(path2)
 print(store_df.shape)
 store_df.head()
hist1 = model1.fit(train_set,
steps_per_epoch=train_labels.shape[0]//BATCH_SIZE,
epochs = EPOCHS,
callbacks=[lr_callback],
)<predict_on_test>
train['Year'] = train['Date'].apply(lambda x: int(str(x)[:4]))
train['Month'] = train['Date'].apply(lambda x: int(str(x)[5:7]))
test['Year'] = test['Date'].apply(lambda x: int(str(x)[:4]))
test['Month'] = test['Date'].apply(lambda x: int(str(x)[5:7]))
train['Date'] = train['Date'].apply(lambda x:(str(x)[:7]))
test['Date'] = test['Date'].apply(lambda x:(str(x)[:7]))
avg_sales = train.groupby('Date')["Sales"].mean()
pct_change_sales = train.groupby('Date')["Sales"].sum().pct_change()
fig,(axis1,axis2)= plt.subplots(2,1,sharex=True,figsize=(15,8))
ax1 = avg_sales.plot(legend=True,ax=axis1,marker='o',title="Average Sales")
 ax1.set_xticks(range(len(avg_sales)))
 ax1.set_xticklabels(avg_sales.index.tolist() , rotation=90)
 ax2 = pct_change_sales.plot(legend=True,ax=axis2,marker='o',rot=90,colormap="summer",title="Sales Percent Change" )
%%time
preds1 = model1.predict(test_dataset )<set_options>
store_df['PromoInterval'].value_counts()
with strategy.scope() :
gc.collect()<choose_model_class>
train_store = pd.merge(right=store_df,left=train,how='outer',on='Store')
 train_store.head()
def get_model_inc() :
base_model = InceptionResNetV2(weights='imagenet', include_top=False, pooling='avg', input_shape=(img_size, img_size, 3))
x = base_model.output
predictions = tf.keras.layers.Dense(n_classes, activation="softmax" )(x)
 return tf.keras.models.Model(inputs=base_model.input, outputs=predictions)
 with strategy.scope() :
model2 = get_model_inc()
model2.compile(optimizer='adam', loss='categorical_crossentropy',metrics=['accuracy'])
 model2.compile(
 optimizer = 'adam',
loss = 'categorical_crossentropy',
metrics=['accuracy'] 
)
<train_model>
train_store.isnull().sum()
hist2 = model2.fit(train_set,
steps_per_epoch=train_labels.shape[0]//BATCH_SIZE,
epochs = EPOCHS,
callbacks=[lr_callback],
)<predict_on_test>
med_sales_per_customer = train_store['Sales_per_customer'].astype('float' ).median(axis=0)
 train_store['Sales_per_customer'].replace(np.nan,med_sales_per_customer,inplace=True)
 med_comp_dist = train_store['CompetitionDistance'].astype('float' ).median(axis=0)
 train_store['CompetitionDistance'].replace(np.nan,math.floor(med_comp_dist),inplace=True)
 med_comp_month = train_store['CompetitionOpenSinceMonth'].astype('float' ).median(axis=0)
 train_store['CompetitionOpenSinceMonth'].replace(np.nan,math.floor(med_comp_month),inplace=True)
 med_comp_year = train_store['CompetitionOpenSinceYear'].astype('float' ).median(axis=0)
 train_store['CompetitionOpenSinceYear'].replace(np.nan,math.floor(med_comp_year),inplace=True)
 med_promo2_week = train_store['Promo2SinceWeek'].astype('float' ).median(axis=0)
 train_store['Promo2SinceWeek'].replace(np.nan,math.floor(med_promo2_week),inplace=True)
 med_promo2_year = train_store['Promo2SinceYear'].astype('float' ).median(axis=0)
 train_store['Promo2SinceYear'].replace(np.nan,math.floor(med_promo2_year),inplace=True)
 train_store.drop(['PromoInterval'],axis=1,inplace=True )
%%time
preds2 = model2.predict(test_dataset )<prepare_output>
train.drop(['Customers','Sales_per_customer'],axis=1,inplace=True)
 train['Open'] = train['Open'].astype(float )
preds =(preds1+preds2)/2<prepare_output>
train.drop(['Year','Month','SchoolHoliday','Date'],axis=1,inplace=True)
 test.drop(['Year','Month','SchoolHoliday','Date'],axis=1,inplace=True )
sub_df.iloc[:, 1:]=preds<save_to_csv>
train_dummies = pd.get_dummies(train['DayOfWeek'], prefix='Day')
 train_dummies.drop(['Day_7'], axis=1, inplace=True)
 test_dummies = pd.get_dummies(test['DayOfWeek'],prefix='Day')
 test_dummies.drop(['Day_7'], axis=1, inplace=True)
 train = train.join(train_dummies)
 test = test.join(test_dummies )
sub_df.to_csv("submission.csv", index=False )<load_from_csv>
test["Open"][test["Open"] != test["Open"]] =(test["DayOfWeek"] != 7 ).astype(float )
su=pd.read_csv("submission.csv" )<install_modules>
train.drop(['DayOfWeek'], axis=1,inplace=True)
 test.drop(['DayOfWeek'], axis=1,inplace=True)
 train= train[train_store["Open"] != 0]
!pip install -q efficientnet_pytorch<define_variables>
closed_ids = test["Id"][test["Open"] == 0].values
base_path = Path('/kaggle/input/plant-pathology-2020-fgvc7/' )<categorify>
test = test[test["Open"] != 0]
<feature_engineering><EOS> 
train_dic = dict(list(train.groupby('Store')))
 test_dic = dict(list(test.groupby('Store')))
 submission = pd.Series()
scores = []
for i in test_dic:
store = train_dic[i]
train_x = store.drop(["Sales","Store"],axis=1)
 train_y = store["Sales"]
test_x = test_dic[i].copy()
store_ids = test_x["Id"]
test_x.drop(["Id","Store"], axis=1,inplace=True)
 lr = lm.LinearRegression(normalize=True)
 lr.fit(train_x, train_y)
 pred_y = lr.predict(test_x)
 scores.append(lr.score(train_x, train_y))
submission = submission.append(pd.Series(pred_y, index=store_ids))
submission = submission.append(pd.Series(0, index=closed_ids))
submission = pd.DataFrame({ "Id": submission.index, "Sales": submission.values})
 submission.to_csv('rossmann.csv', index=False )