Kaggle competition: Predict Future Sales
<SOS> metric: rmse Kaggle data source: predict-future-sales<drop_column>
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.ensemble import RandomForestRegressor
train_df = train_df.drop(['Name', 'Title'], axis=1)
 test_df = test_df.drop(['Name', 'Title'], axis=1)
 combine = [train_df, test_df]<data_type_conversions>
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))
for dataset in combine:
dataset['Sex'] = dataset['Sex'].\
map({'female': 1, 'male': 0} ).astype(int)
 train_df.head()<define_variables>
data = pd.read_pickle('.. /input/eda-preprocessing-feature-engineering/all_data.pkl')
 data = data[data.date_block_num > 5]
test = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/test.csv' ).set_index('ID')
 dropcols = [
"item_cnt_month_lag_12",
"item_cnt_month_lag_12_adv",
"date_item_target_enc_lag_12",
"date_shop_target_enc_lag_12",
"date_city_target_enc_lag_1",
"date_city_target_enc_lag_2",
"date_city_target_enc_lag_3",
"date_type_target_enc_lag_1",
"date_subtype_target_enc_lag_1",
"new_item_cat_avg_lag_1",
"new_item_cat_avg_lag_2",
"new_item_cat_avg_lag_3",
"new_item_shop_cat_avg_lag_1",
"new_item_shop_cat_avg_lag_2",
"new_item_shop_cat_avg_lag_3",
]
X_train = data[data.date_block_num < 33].drop(['item_cnt_month']+dropcols, axis=1)
 Y_train = data[data.date_block_num < 33]['item_cnt_month']
X_valid = data[data.date_block_num == 33].drop(['item_cnt_month']+dropcols, axis=1)
 Y_valid = data[data.date_block_num == 33]['item_cnt_month']
X_test = data[data.date_block_num == 34].drop(['item_cnt_month']+dropcols, axis=1)
 del data
gc.collect()
guess_ages = np.zeros(( 2,3),dtype=int)
 guess_ages<define_variables>
start_time = time.time()
rf = RandomForestRegressor(n_estimators=60,
min_samples_leaf=15,
n_jobs=-1,
random_state=0)
 rf.fit(X_train, Y_train)
 print(f"Training took {time.time() - start_time} s")
 start_time = time.time()
Y_train_pred = rf.predict(X_train ).clip(0, 20)
 print(f"Predicting on train set took {time.time() - start_time} s")
 start_time = time.time()
Y_valid_pred = rf.predict(X_valid ).clip(0, 20)
 print(f"Predicting on valid set took {time.time() - start_time} s")
 print(f"TRAIN RMSE: {round(np.sqrt(mean_squared_error(Y_train, Y_train_pred)) , 5)}, VALID RMSE: {round(np.sqrt(mean_squared_error(Y_valid, Y_valid_pred)) , 5)}")
 dump(rf, 'rf_model.joblib' )
for idx,dataset in enumerate(combine):
if idx==0:
print('Working on Training Data set
')
 else:
print('-'*35)
 print('Working on Test Data set
')
 print('Guess values of age based on sex and pclass of the passenger...')
 for i in range(0, 2):
for j in range(0,3):
guess_df = dataset[(dataset['Sex'] == i)\
&(dataset['Pclass'] == j+1)]['Age'].dropna()
age_guess = guess_df.median()
guess_ages[i,j] = int(age_guess)
 print('Guess_Age table:
',guess_ages)
 print('
Assigning age values to NAN age values in the dataset...')
 for i in range(0, 2):
for j in range(0, 3):
dataset.loc[(dataset.Age.isnull())&(dataset.Sex == i)\
&(dataset.Pclass == j+1),'Age'] = guess_ages[i,j]
dataset['Age'] = dataset['Age'].astype(int)
 print()
print('Done!
')
 train_df.head()<feature_engineering>
start_time = time.time()
Y_test = rf.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('rf_y_test.csv', index=False)
 train_preds = pd.DataFrame({
"ID": X_train.index,
"item_cnt_month": Y_train_pred
})
 train_preds.to_csv('rf_y_train.csv', index=False)
 valid_preds = pd.DataFrame({
"ID": X_valid.index,
"item_cnt_month": Y_valid_pred
})
 valid_preds.to_csv('rf_y_valid.csv', index=False)