Kaggle competition: Rossmann Store Sales
<SOS> metric: RootMeanSquarePercentageError Kaggle data source: rossmann-store-sales<define_variables>
sns.set_style('whitegrid')
 %matplotlib inline
df_train = pd.read_csv(".. /input/train.csv")
 df_store = pd.read_csv(".. /input/store.csv")
 df_test = pd.read_csv(".. /input/test.csv")
 df_train['Year'] = df_train['Date'].apply(lambda x: int(x[:4]))
df_train['Month'] = df_train['Date'].apply(lambda x: int(x[5:7]))
df_train.head()
Train_input_images = np.zeros(( num_examples_train, n_h, n_w, n_c))
Test_input_images = np.zeros(( num_examples_test, n_h, n_w, n_c))<categorify>
df_train['StateHoliday'] = df_train['StateHoliday'].replace(0, '0')
 df_train["HolidayBin"] = df_train['StateHoliday'].map({"0": 0, "a": 1, "b": 1, "c": 1})
 df_train.StateHoliday.unique()
for example in range(num_examples_train):
Train_input_images[example,:28,:28,0] = train_file.iloc[example, 1:].values.reshape(28,28)
 Train_input_images[example,:28,:28,1] = train_file.iloc[example, 1:].values.reshape(28,28)
 Train_input_images[example,:28,:28,2] = train_file.iloc[example, 1:].values.reshape(28,28)
 for example in range(num_examples_test):
Test_input_images[example,:28,:28,0] = test_file.iloc[example, :].values.reshape(28,28)
 Test_input_images[example,:28,:28,1] = test_file.iloc[example, :].values.reshape(28,28)
 Test_input_images[example,:28,:28,2] = test_file.iloc[example, :].values.reshape(28,28 )<train_model>
average_customers = df_train.groupby('Month')["Customers"].mean()
average_sales = df_train.groupby('Month')['Sales'].mean()
total_sales_customers = df_train.groupby('Store')['Sales', 'Customers'].sum()
total_sales_customers.head()
for example in range(num_examples_train):
Train_input_images[example] = cv2.resize(Train_input_images[example],(n_h, n_w))
for example in range(num_examples_test):
Test_input_images[example] = cv2.resize(Test_input_images[example],(n_h, n_w))<define_variables>
df_total_sales_customers = pd.DataFrame({'Sales': total_sales_customers['Sales'],
'Customers': total_sales_customers['Customers']},
index = total_sales_customers.index)
 df_total_sales_customers = df_total_sales_customers.reset_index()
df_total_sales_customers.head()
Train_labels = np.array(train_file.iloc[:, 0] )<init_hyperparams>
avg_sales_customers = df_train.groupby('Store')['Sales', 'Customers'].mean()
avg_sales_customers.head()
train_datagen = ImageDataGenerator(
 rotation_range=27,
width_shift_range=0.3,
height_shift_range=0.2,
shear_range=0.3,
zoom_range=0.2,
horizontal_flip=False)
 validation_datagen = ImageDataGenerator()<choose_model_class>
df_avg_sales_customers = pd.DataFrame({'Sales': avg_sales_customers['Sales'],
'Customers': avg_sales_customers['Customers']},
index = avg_sales_customers.index)
 df_avg_sales_customers = df_avg_sales_customers.reset_index()
df_stores_avg = df_avg_sales_customers.join(df_store.set_index('Store'), on='Store')
 df_stores_avg.head()
pretrained_model = keras.applications.resnet50.ResNet50(input_shape=(n_h, n_w, n_c),
include_top=False, weights='imagenet')
 model = keras.Sequential([
pretrained_model,
keras.layers.Flatten() ,
keras.layers.Dense(units=60, activation='relu'),
keras.layers.Dense(units=10, activation='softmax')
 ] )<choose_model_class>
df_stores_new = df_total_sales_customers.join(df_store.set_index('Store'), on='Store')
 df_stores_new.head()
Optimizer = 'RMSprop'
model.compile(optimizer=Optimizer,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'] )<split>
average_storetype = df_stores_new.groupby('StoreType')['Sales', 'Customers', 'CompetitionDistance'].mean()
fig,(axis1,axis2,axis3)= plt.subplots(1,3,figsize=(15,4))
sns.barplot(average_storetype.index, average_storetype['Sales'], ax=axis1)
 sns.barplot(average_storetype.index, average_storetype['Customers'], ax=axis2)
 sns.barplot(average_storetype.index, average_storetype['CompetitionDistance'], ax=axis3)
 average_storetype.index
train_images, dev_images, train_labels, dev_labels = train_test_split(Train_input_images,
Train_labels,
test_size=0.1, train_size=0.9,
shuffle=True,
random_state=44)
 test_images = Test_input_images<train_model>
df_train = pd.read_csv(".. /input/train.csv")
 df_store = pd.read_csv(".. /input/store.csv")
 df_test = pd.read_csv(".. /input/test.csv")
class myCallback(keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('accuracy')> 0.999999):
print("Stop training!")
 self.model.stop_training = True<train_model>
closed_store_ids = df_test["Id"][df_test["Open"] == 0].values
df_train['Year'] = df_train['Date'].apply(lambda x: int(x[:4]))
df_train['Month'] = df_train['Date'].apply(lambda x: int(x[5:7]))
df_train["HolidayBin"] = df_train.StateHoliday.map({"0": 0, "a": 1, "b": 1, "c": 1})
 del df_train['Date']
del df_train['StateHoliday']
df_test['Year'] = df_test['Date'].apply(lambda x: int(x[:4]))
df_test['Month'] = df_test['Date'].apply(lambda x: int(x[5:7]))
df_test["HolidayBin"] = df_test.StateHoliday.map({"0": 0, "a": 1, "b": 1, "c": 1})
 del df_test['Date']
del df_test['StateHoliday']
df_test.head()
EPOCHS = 5
batch_size = 212
history = model.fit_generator(train_datagen.flow(train_images,train_labels, batch_size=batch_size),
steps_per_epoch=train_images.shape[0] / batch_size,
epochs=EPOCHS,
validation_data=validation_datagen.flow(dev_images,dev_labels,
batch_size=batch_size),
validation_steps=dev_images.shape[0] / batch_size,
callbacks=[callbacks] )<save_to_csv>
df_test = df_test[df_test["Open"] != 0]
df_test[df_test['Store'] == 1].head()
a = list()
for i in df_test['Store']:
a.append(float(df_store['CompetitionDistance'][df_store['Store'] == i]))
df_test['CompetitionDistance'] = a
submission = pd.read_csv('.. /input/digit-recognizer-submission/submission.csv')
 submission.to_csv('digit_submission.csv', index=False )<set_options>
a = list()
for i in df_train['Store']:
a.append(float(df_store['CompetitionDistance'][df_store['Store'] == i]))
df_train['CompetitionDistance'] = a
df_train.head()
df_train['CompetitionDistance'] = df_train['CompetitionDistance'].fillna(df_train['CompetitionDistance'].mean() )
def set_seed(seed=0):
np.random.seed(seed)
 random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.cuda.manual_seed_all(seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
os.environ['PYTHONHASHSEED'] = str(seed)
 set_seed(0 )<set_options>
df_train['CompetitionDistance'] = np.log(df_train['CompetitionDistance'])
 df_test['CompetitionDistance'] = np.log(df_test['CompetitionDistance'] )
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 print(device )<load_from_csv>
train_stores = dict(list(df_train.groupby('Store')))
 test_stores = dict(list(df_test.groupby('Store')))
 submission = pd.Series()
for i in test_stores:
store = train_stores[i]
X_train = store.drop(["Sales", "Store", "Customers"],axis=1)
 Y_train = store["Sales"]
X_test = test_stores[i].copy()
store_ids = X_test["Id"]
X_test.drop(["Id","Store"], axis=1,inplace=True)
 X_train = X_train.fillna(X_train.mean())
 X_test = X_test.fillna(X_train.mean())
 rfr = RandomForestRegressor(n_estimators = 5, criterion = 'mse')
 rfr.fit(X_train, Y_train)
 Y_pred = rfr.predict(X_test)
 submission = submission.append(pd.Series(Y_pred, index=store_ids))
submission = submission.append(pd.Series(0, index=closed_store_ids))
submission = pd.DataFrame({ "Id": submission.index, "Sales": submission.values})
 submission.to_csv('rossmann_submission.csv', index=False)
 print(len(submission))