Kaggle competition: Predict Future Sales
<SOS> metric: rmse Kaggle data source: predict-future-sales<compute_test_metric>
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
warnings.filterwarnings('ignore')
 pd.set_option('display.max_rows', 500)
 pd.set_option('display.max_columns', 100)
print('Neural Network accuracy:',str(round(model.evaluate(X_val.values, \
Y_val.values, batch_size=50,verbose=False)[1]*100,2)) ,'%' )<choose_model_class>
start = time.time()
data = pd.read_pickle('.. /input/feature-engineering-xgboost/data.pkl')
 print('data input costs {:.2f} seconds'.format(time.time() -start))
print('data has {} rows and {} columns'.format(data.shape[0], data.shape[1]))
test = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/test.csv' ).set_index('ID')
 print('test has {} rows and {} columns'.format(test.shape[0], test.shape[1]))
base_model = RandomForestClassifier(n_jobs=-1,
random_state=42)
 parameters = {
"max_depth": [None, 1,2,3,4,5,6,7,8,9,10,11,12,13,14],
"n_estimators": [350, 400, 450, 500, 550, 600,650,700,750],
"max_features": ["auto", 1,2,3,4,5,6,7,8,9,10,11,12,13,14],
"criterion": ["gini", "entropy"],
"bootstrap": [False, True],
"class_weight": [None, "balanced"]
}
optimal_model = RandomizedSearchCV(base_model,
param_distributions=parameters,
n_iter=30,
cv=3,
verbose=2,
scoring="accuracy")
 optimal_model.fit(X_train, Y_train)
 optimal_model.best_estimator_
pred = optimal_model.predict(X_val )<save_to_csv>
data = data[[
'date_block_num',
'shop_id',
'item_id',
'item_cnt_month',
'city_code',
'item_category_id',
'type_code',
'subtype_code',
'item_cnt_month_lag_1',
'item_cnt_month_lag_2',
'item_cnt_month_lag_3',
'item_cnt_month_lag_6',
'item_cnt_month_lag_12',
'date_avg_item_cnt_lag_1',
'date_item_avg_item_cnt_lag_1',
'date_item_avg_item_cnt_lag_2',
'date_item_avg_item_cnt_lag_3',
'date_item_avg_item_cnt_lag_6',
'date_item_avg_item_cnt_lag_12',
'date_shop_avg_item_cnt_lag_1',
'date_shop_avg_item_cnt_lag_2',
'date_shop_avg_item_cnt_lag_3',
'date_shop_avg_item_cnt_lag_6',
'date_shop_avg_item_cnt_lag_12',
'date_cat_avg_item_cnt_lag_1',
'date_shop_cat_avg_item_cnt_lag_1',
'date_city_avg_item_cnt_lag_1',
'date_item_city_avg_item_cnt_lag_1',
'delta_price_lag',
'month',
'days',
'item_shop_last_sale',
'item_last_sale',
'item_shop_first_sale',
'item_first_sale',
]]
X_train = data[data.date_block_num < 33].drop(['item_cnt_month'], 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'], 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'], axis=1)
 del data
gc.collect() ;
Y_submission = optimal_model.predict(X_submission)
 submission = pd.DataFrame({
"PassengerId": test_df["PassengerId"],
"Survived": Y_submission
})
 submission.to_csv('titanic.csv', index=False )<load_from_csv>
start = time.time()
print('cpu training begins...')
 model = XGBRegressor(
 max_depth=8,
n_estimators=1000,
min_child_weight=300,
colsample_bytree=0.8,
subsample=0.8,
eta=0.3,
seed=42 
)
 model.fit(
 X_train,
Y_train,
eval_metric='rmse',
eval_set=[(X_train, Y_train),(X_valid, Y_valid)],
verbose=True,
early_stopping_rounds = 10)
 print('CPU Xgb costs {:.2f} seconds'.format(time.time() -start))
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_data = pd.read_csv("/kaggle/input/titanic/test.csv" )<count_missing_values>
start = time.time()
print('gpu training begins...')
 model = XGBRegressor(
 max_depth=8,
n_estimators=1000,
min_child_weight=300,
colsample_bytree=0.8,
subsample=0.8,
eta=0.3,
seed=42,
tree_method='gpu_hist' 
)
 model.fit(
 X_train,
Y_train,
eval_metric='rmse',
eval_set=[(X_train, Y_train),(X_valid, Y_valid)],
verbose=True,
early_stopping_rounds = 10)
 print('GPU Xgb costs {:.2f} seconds'.format(time.time() -start))
Col_with_missing = [col for col in train_data.columns if train_data[col].isnull().any() ]
print(Col_with_missing )<count_missing_values>
start = time.time()
print('cpu training begins...')
 model = CatBoostRegressor(eval_metric='RMSE',
iterations=1000,
max_ctr_complexity=4,
random_seed=42,
od_type='Iter',
od_wait=100,
verbose=50,
depth=4,
metric_period = 50 
)
 Y_train = np.array(Y_train ).astype('float32')
 Y_valid = np.array(Y_valid ).astype('float32')
 model.fit(X_train,
Y_train,
eval_set=[(X_train, Y_train),(X_valid, Y_valid)],
verbose=True,
use_best_model=True)
 print('CPU Cat costs {:.2f} seconds'.format(time.time() -start))
Col_with_missing = [col for col in test_data.columns if test_data[col].isnull().any() ]
print(Col_with_missing )<define_variables>
print('learning rate:',model.learning_rate_)
 Y_pred = model.predict(X_valid ).clip(0, 20)
 Y_test = model.predict(X_test ).clip(0, 20)
 submission = pd.DataFrame({ 'ID': test.index, 'item_cnt_month': Y_test})
 submission.to_csv('submission.csv', index=False )
s =(train_data.dtypes == 'object')
 object_cols = list(s[s].index)
 print("Categorical variables:")
 print(object_cols )<prepare_x_and_y>
start = time.time()
print('gpu training begins...')
 model = CatBoostRegressor(eval_metric='RMSE',
iterations=1000,
max_ctr_complexity=4,
random_seed=42,
od_type='Iter',
od_wait=100,
verbose=50,
depth=8,
metric_period = 50,
task_type='GPU' 
)
 Y_train = np.array(Y_train ).astype('float32')
 Y_valid = np.array(Y_valid ).astype('float32')
 model.fit(X_train,
Y_train,
eval_set=[(X_valid, Y_valid)],
verbose=True,
use_best_model=True)
 print('GPU Cat costs {:.2f} seconds'.format(time.time() -start))