Kaggle competition: Predict Future Sales
<SOS> metric: rmse Kaggle data source: predict-future-sales<split>
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import gc
import pickle
import time
from joblib import dump, load
from sklearn.metrics import mean_squared_error
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
import lightgbm as lgb
import xgboost as xgb
from xgboost import XGBRegressor
random_seed = 2
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.3, random_state=random_seed )<choose_model_class>
def get_imports() :
for name, val in globals().items() :
if isinstance(val, types.ModuleType):
name = val.__name__.split(".")[0]
elif isinstance(val, type):
name = val.__module__.split(".")[0]
if name == "PIL":
name = "Pillow"
elif name == "sklearn":
name = "scikit-learn"
yield name
imports = list(set(get_imports()))
requirements = []
for m in pkg_resources.working_set:
if m.project_name in imports and m.project_name!="pip":
requirements.append(( m.project_name, m.version))
for r in requirements:
print("{}=={}".format(*r))
datagen = ImageDataGenerator(zoom_range = 0.1, width_shift_range = 0.1, height_shift_range = 0.1, rotation_range = 10 )<choose_model_class>
xgbm = load(".. /input/xgboost-model/xgb_model.joblib")
 lgbm = load(".. /input/lightgbm-model/lightgbm_model.joblib")
 rfm = load(".. /input/randomforest-model/rf_model.joblib" )
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(3, 3), activation = 'relu', input_shape =(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters = 32, kernel_size =(3, 3), activation = 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 32, kernel_size =(5, 5), activation = 'relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(strides =(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3, 3), activation = 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size =(3, 3), activation = 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size =(5, 5), activation = 'relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(strides =(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(512, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(1024, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation = 'softmax'))<choose_model_class>
xgb_train = pd.read_csv('.. /input/xgboost-model/xgb_y_train.csv' ).set_index('ID')
 xgb_valid = pd.read_csv('.. /input/xgboost-model/xgb_y_valid.csv' ).set_index('ID')
 xgb_test = pd.read_csv('.. /input/xgboost-model/xgb_submission.csv' ).set_index('ID')
 lgb_train = pd.read_csv('.. /input/lightgbm-model/gbm_y_train.csv' ).set_index('ID')
 lgb_valid = pd.read_csv('.. /input/lightgbm-model/gbm_y_valid.csv' ).set_index('ID')
 lgb_test = pd.read_csv('.. /input/lightgbm-model/gbm_y_test.csv' ).set_index('ID')
 rf_train = pd.read_csv('.. /input/randomforest-model/rf_y_train.csv' ).set_index('ID')
 rf_valid = pd.read_csv('.. /input/randomforest-model/rf_y_valid.csv' ).set_index('ID')
 rf_test = pd.read_csv('.. /input/randomforest-model/rf_y_test.csv' ).set_index('ID')
 test = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/test.csv' ).set_index('ID')
model.compile(optimizer='adam',metrics=['accuracy'],loss='categorical_crossentropy' )<choose_model_class>
X_train = np.hstack([xgb_train.values, lgb_train.values, rf_train.values])
 X_valid = np.hstack([xgb_valid.values, lgb_valid.values, rf_valid.values])
 X_test = np.hstack([xgb_test.values, lgb_test.values, rf_test.values] )
reduction_lr = ReduceLROnPlateau(monitor='val_accuracy',patience=2, verbose=1, factor=0.2, min_lr=0.00001 )<train_model>
data = pd.read_pickle('.. /input/eda-preprocessing-feature-engineering/all_data.pkl')
 data = data[data.date_block_num > 5]
Y_train = data[data.date_block_num < 33]['item_cnt_month']
Y_valid = data[data.date_block_num == 33]['item_cnt_month']
del data
gc.collect()
hist = model.fit_generator(datagen.flow(X_train,Y_train,batch_size=32),epochs=20,validation_data =(X_val,Y_val),callbacks=[reduction_lr] )<compute_test_metric>
start_time = time.time()
lr = LinearRegression(n_jobs=-1, normalize=True)
 lr.fit(X_valid, Y_valid)
 print(f"Training took {time.time() - start_time} s")
 start_time = time.time()
Y_valid_pred = lr.predict(X_valid ).clip(0, 20)
 print(f"Predicting on valid set took {time.time() - start_time} s")
 print(f"VALID RMSE: {round(np.sqrt(mean_squared_error(Y_valid, Y_valid_pred)) , 5)}")
 dump(lr, 'stacking_model.joblib' )
final_loss, final_acc = model.evaluate(X_val, Y_val, verbose=0)
 print("Final loss: {0:.4f}, final accuracy: {1:.4f}".format(final_loss, final_acc))<predict_on_test>
start_time = time.time()
Y_test = lr.predict(X_test ).clip(0, 20)
 print(f"Predicting test set took {time.time() - start_time} s")
 submission = pd.DataFrame({
"ID": test.index,
"item_cnt_month": Y_test
})
 submission.to_csv('submission.csv', index=False)