Kaggle competition: Digit Recognizer
id_price = monthly_sales[['ID', 'avg_price']]
x_test_df = test_ids.merge(items, on='item_id')
 x_test_df = x_test_df.merge(id_price, how='left', on='ID')
 x_test_df.insert(loc=2, column='month', value=11)
 x_test_df.insert(loc=3, column='year', value=2015)
 x_test_df.insert(loc=4, column='date_block_num', value=34)
 x_test_df = x_test_df.drop_duplicates()
x_test_df['avg_price'].fillna(x_test_df['avg_price'].mean() , inplace=True)
 monthly_sales_subset = monthly_sales[['ID', 'date_block_num', 'shop_id', 'item_category_id', 'avg_price', 'month', 'year', 'item_cnt_month']]
x_all_df = pd.concat(( x_test_df, monthly_sales_subset))
for lag in range(1, 13):
x_all_df = calculate_item_cnt_lagged(x_all_df, lag)
 x_test_df = x_all_df[x_all_df['date_block_num'] == 34]
x_test_df = x_test_df[['ID', 'shop_id', 'item_category_id', 'avg_price', 'month', 'year',
'item_cnt_lag1', 'item_cnt_lag2', 'item_cnt_lag3', 'item_cnt_lag4',
'item_cnt_lag5', 'item_cnt_lag6', 'item_cnt_lag7', 'item_cnt_lag8',
'item_cnt_lag9', 'item_cnt_lag10', 'item_cnt_lag11', 'item_cnt_lag12']]
x_test_df.head()<import_modules>
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split, KFold
import tensorflow as tf
from tensorflow.keras import models, layers, optimizers
from tensorflow.keras.utils import to_categorical
from scipy.sparse import hstack, vstack
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import OneHotEncoder, normalize<prepare_x_and_y>
train_df = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test_df = pd.read_csv("/kaggle/input/digit-recognizer/test.csv")
 sub_df = pd.read_csv("/kaggle/input/digit-recognizer/sample_submission.csv")
 train_df.shape, test_df.shape
x_train_price = np.array(monthly_sales['avg_price'] ).reshape(-1, 1)
 x_test_price = np.array(x_test_df['avg_price'] ).reshape(-1, 1)
 x_train_price = normalize(x_train_price)
 x_test_price = normalize(x_test_price)
 x_train_lags = np.array(monthly_sales[['item_cnt_lag1', 'item_cnt_lag2', 'item_cnt_lag3', 'item_cnt_lag4',
'item_cnt_lag5', 'item_cnt_lag6', 'item_cnt_lag7', 'item_cnt_lag8',
'item_cnt_lag9', 'item_cnt_lag10', 'item_cnt_lag11', 'item_cnt_lag12']])
 x_test_lags = np.array(x_test_df[['item_cnt_lag1', 'item_cnt_lag2', 'item_cnt_lag3', 'item_cnt_lag4',
'item_cnt_lag5', 'item_cnt_lag6', 'item_cnt_lag7', 'item_cnt_lag8',
'item_cnt_lag9', 'item_cnt_lag10', 'item_cnt_lag11', 'item_cnt_lag12']])
 x_train_categorical = np.array(monthly_sales[['shop_id', 'item_category_id', 'month', 'year']])
 x_test_categorical = np.array(x_test_df[['shop_id', 'item_category_id', 'month', 'year']])
 x_all_categorical = np.concatenate(( x_train_categorical, x_test_categorical))
y_train = np.array(monthly_sales['item_cnt_month'])
 encoder = OneHotEncoder()
encoder.fit(x_all_categorical)
 x_train_categorical = encoder.transform(x_train_categorical)
 x_test_categorical = encoder.transform(x_test_categorical)
 x_train = hstack([x_train_categorical, x_train_price, x_train_lags])
 x_test = hstack([x_test_categorical, x_test_price, x_test_lags])
 y_train = np.clip(y_train, 0, 20)
 print(x_train.shape)
 print(x_test.shape )<compute_train_metric>
train_X, test_X = train_test_split(train_df, test_size=0.2, random_state=1)
 train_y, test_y = train_X.pop("label"), test_X.pop("label")
 train_X, test_X = train_X.values, test_X.values
train_X.shape
gradient_boost = GradientBoostingRegressor(n_estimators=500)
 gradient_boost.fit(x_train, y_train)
 train_pred = gradient_boost.predict(x_train)
 rmse = np.sqrt(mean_squared_error(y_train, train_pred))
print(f"RMSE on training set: {rmse}" )<save_to_csv>
train_X = train_X.reshape(( train_X.shape[0], 28, 28, 1))
test_X = test_X.reshape(( test_X.shape[0], 28, 28, 1))
train_X, test_X = train_X / 255.0, test_X / 255.0
train_y = to_categorical(train_y)
 test_y = to_categorical(test_y)
 train_X.shape, test_X.shape, train_y.shape, test_y.shape
test_pred = gradient_boost.predict(x_test)
 test_pred = x_test_df.assign(item_cnt_month=test_pred)
 test_pred = test_pred[['ID', 'item_cnt_month']]
test_pred = test_pred.sort_values(by='ID')
 test_pred.to_csv('submission.csv', index=False )<set_options>
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32,(5, 5), activation="relu",
kernel_initializer="he_uniform",
kernel_regularizer=tf.keras.regularizers.l2(0.001),
padding="same",
input_shape=(28, 28, 1)) ,
tf.keras.layers.Conv2D(32,(5, 5), activation="relu",
kernel_initializer="he_uniform",
kernel_regularizer=tf.keras.regularizers.l2(0.001),
padding="same"),
tf.keras.layers.MaxPooling2D(( 2, 2)) ,
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Conv2D(64,(3, 3), activation="relu",
kernel_initializer="he_uniform",
kernel_regularizer=tf.keras.regularizers.l2(0.001),
padding="same"),
tf.keras.layers.Conv2D(64,(3, 3), activation="relu",
kernel_initializer="he_uniform",
kernel_regularizer=tf.keras.regularizers.l2(0.001),
padding="same"),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.MaxPooling2D(( 2, 2)) ,
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(256, activation="relu",
kernel_initializer="uniform"),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(10, activation="softmax")
 ])
 model.compile(optimizer=tf.keras.optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-02, decay=0.0),
loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
metrics=["accuracy"])
 history = model.fit(train_X, train_y, epochs=17, validation_split=0.2, verbose=1)
 score = model.evaluate(test_X, test_y, verbose=0)
 print(f"Test_loss: {score[0]} / Test accuracy: {score[1]}" )
sns.set(style="darkgrid")
 rcParams['figure.figsize'] = 12, 4<load_from_csv>
n_folds = 10
acc_fold = []
loss_fold = []
inputs = np.concatenate(( train_X, test_X), axis=0)
 targets = np.concatenate(( train_y, test_y), axis=0)
items = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/items.csv')
 shops = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/shops.csv')
 cats = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/item_categories.csv')
 train = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/sales_train.csv')
 test = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/test.csv' )<feature_engineering>
def evaluate_model(inputs, targets, n_folds=n_folds):
kfold = KFold(n_splits=n_folds, shuffle=True, random_state=2)
 fold_no = 1
for train_ix, test_ix in kfold.split(inputs, targets):
print(f"Train for fold {fold_no}...")
 history = model.fit(inputs[train_ix], targets[train_ix], epochs=7, verbose=1)
 scores = model.evaluate(inputs[test_ix], targets[test_ix], verbose=0)
 print(f"Score for fold {fold_no}: {model.metrics_names[0]} of {scores[0]}; {model.metrics_names[1]} of {scores[1]*100}%")
 acc_fold.append(scores[1] * 100)
 loss_fold.append(scores[0])
 fold_no = fold_no + 1
evaluate_model(inputs, targets)
 print("score per fold" )
train =(
 train
[ 
(train['item_price'] > 0)& 
(train['item_price'] < 300000)& 
(train['item_cnt_day'] < 1000)
 ] 
.reset_index(drop = True)
)
 train.loc[train['item_cnt_day'] < 0, 'item_cnt_day'] = 0<feature_engineering>
test_df = test_df.values
test_df = test_df.reshape(( test_df.shape[0], 28, 28, 1))
test_df = test_df / 255.0
for i in [(0, 57),(1, 58),(10, 11)]:
train.loc[train['shop_id'] == i[0], 'shop_id'] = i[1]
test.loc[test['shop_id'] == i[0], 'shop_id'] = i[1]<feature_engineering>
pred = model.predict(test_df)
 pred = [np.argmax(y, axis=None, out=None)for y in pred]
shops.loc[shops['shop_name'] == 'Сергиев Посад ТЦ "7Я"', 'shop_name'] = 'СергиевПосад ТЦ "7Я"'
shops['city'] = shops.shop_name.str.split(' ' ).map(lambda x: x[0])
 shops['category'] = shops.shop_name.str.split(' ' ).map(lambda x: x[1])
 shops.loc[shops['city'] == '!Якутск', 'city'] = 'Якутск'<feature_engineering>
sub_df["Label"] = pred
sub_df.head()
categories = []
for categ in shops['category'].unique() :
if len(shops[shops['category'] == categ])> 4:
categories.append(categ)
 shops['category'] = shops['category'].apply(lambda x: x if x in categories else 'other' )<categorify>
sub_df.to_csv("my_submision1.csv", index=False )