Kaggle competition: Google Analytics Customer Revenue Prediction
<SOS> metric: RMSE Kaggle data source: google-analytics-customer-revenue-prediction<import_modules>
%matplotlib inline
plt.style.use('ggplot')
Conv2D,
MaxPooling2D,
AveragePooling2D,
Dropout,
Flatten,
Input 
)
<compute_test_metric>
def load_df(csv_path='.. /input/train.csv', JSON_COLUMNS = ['device', 'geoNetwork', 'totals', 'trafficSource'], load_all=False):
if not load_all:
df = pd.read_csv(csv_path,
converters={column: json.loads for column in JSON_COLUMNS},
dtype={'fullVisitorId': 'str'}, nrows=250000)
 else:
df = pd.read_csv(csv_path,
converters={column: json.loads for column in JSON_COLUMNS},
dtype={'fullVisitorId': 'str'})
 for column in JSON_COLUMNS:
column_as_df = json_normalize(df[column])
 column_as_df.columns = [f"{column}.{subcolumn}" for subcolumn in column_as_df.columns]
df = df.drop(column, axis=1 ).merge(column_as_df, right_index=True, left_index=True)
 return df
def metrics(pred_tag, y_true_tag):
print(classification_report(pred_tag, y_true_tag))
print('- ' * 20)
 print("PREC: ", precision_score(pred_tag, y_true_tag, average='micro'))
print("REC: ", recall_score(pred_tag, y_true_tag, average='micro'))
print("F1: ", f1_score(pred_tag, y_true_tag, average='micro'))<load_from_csv>
%%time
train = load_df(".. /input/train_v2.csv" )
train_id = pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 test_id = pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 dig_id = pd.read_csv('.. /input/Kannada-MNIST/Dig-MNIST.csv' )<define_variables>
%%time
test = load_df(".. /input/test_v2.csv", load_all=True )
x, y, z = 28, 28, 1
qtd_classes = 10
images = []
images_labels = []
valid = []
valid_labels = []
for count,(index, row)in enumerate(train_id.iterrows()):
images.append(row.values[1:].reshape(x, y, z))
images_labels.append(row.values[:1])
 for count,(index, row)in enumerate(dig_id.iterrows()):
valid.append(row.values[1:].reshape(x, y, z))
valid_labels.append(row.values[:1] )<concatenate>
train['trafficSource.adwordsClickInfo.isVideoAd'].fillna(True, inplace=True)
 test['trafficSource.adwordsClickInfo.isVideoAd'].fillna(True, inplace=True)
 train['trafficSource.isTrueDirect'].fillna(False, inplace=True)
 test['trafficSource.isTrueDirect'].fillna(False, inplace=True)
 train['date'] = pd.to_datetime(train['date'].apply(lambda x: str(x)[:4] + '-' + str(x)[4:6] + '-' + str(x)[6:]))
test['date'] = pd.to_datetime(test['date'].apply(lambda x: str(x)[:4] + '-' + str(x)[4:6] + '-' + str(x)[6:]))
TESTER = []
for count,(index, row)in enumerate(test_id.iterrows()):
TESTER.append(row.values[1:].reshape(x, y, z))<categorify>
cols_to_drop = [col for col in train.columns if train[col].nunique(dropna=False)== 1]
train.drop(cols_to_drop, axis=1, inplace=True)
 test.drop([col for col in cols_to_drop if col in test.columns], axis=1, inplace=True)
 print(f'Dropped {len(cols_to_drop)} columns.' )
SUBMIT = np.asarray(TESTER)/ 255.
 TEST = np.asarray(valid)/ 255.
 TRAIN = np.asarray(images)/ 255.
 TRAIN_labels = to_categorical(np.asarray(images_labels), qtd_classes)
 TEST_labels = to_categorical(np.asarray(valid_labels), qtd_classes )<concatenate>
for col in ['visitNumber', 'totals.hits', 'totals.pageviews', 'totals.transactionRevenue']:
train[col] = train[col].astype(float )
train = np.concatenate([TEST, TRAIN])
 labels = np.concatenate([TRAIN_labels, TEST_labels] )<split>
train.drop(['customDimensions', 'hits', 'trafficSource.referralPath', 'trafficSource.source', 'totals.totalTransactionRevenue'], axis=1, inplace=True)
 test.drop(['customDimensions', 'hits', 'trafficSource.referralPath', 'trafficSource.source', 'totals.totalTransactionRevenue'], axis=1, inplace=True )
x_train, x_test, y_train, y_test = train_test_split(train, labels, test_size = 0.3 )<define_variables>
for col in train.columns:
if train[col].isnull().sum() > 0:
rate = train[col].isnull().sum() * 100 / train.shape[0]
print(f'Column {col} has {rate:.4f}% missing values.')
 if train[col].dtype == 'object':
if(train[col] == 'not available in demo dataset' ).sum() > 0:
rate =(train[col] == 'not available in demo dataset' ).sum() * 100 / train.shape[0]
print(f'Column {col} has {rate:.4f}% values not available in dataset.' )
NUM_CLASSES = 10
EPOCHS = 3
BATCH_SIZE = 64<choose_model_class>
counts = train.loc[train['totals.transactionRevenue'] > 0, 'fullVisitorId'].value_counts()
print('There are {0} paying users({1} total)in train data.'.format(len(counts), train['fullVisitorId'].nunique()))
print('{0} users({1:.4f}% of paying)have 1 paid transaction.'.format(np.sum(counts == 1), 100 * np.sum(counts == 1)/ len(counts)))
 print('{0} users({1:.4f}% of paying)have 2 paid transaction.'.format(np.sum(counts == 2), 100 * np.sum(counts == 2)/ len(counts)))
 print('')
 print('Count of non-zero transactions per user:')
 counts.head(10 )
def model_cnn(hp):
inputShape = Input(shape=(x, y, z))
tmp_model = inputShape
num_layers = hp.Int('num_layers', 2, 8, default=6)
 lr = hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])
 for idx in range(num_layers):
idx = str(idx)
 filters = hp.Int('filters_' + idx, 32, 256, step=32, default=64)
 tmp_model = Conv2D(filters=filters, kernel_size = 3, padding="same", activation='relu' )(tmp_model)
 if tmp_model.shape[1] >= 8:
pool_type = hp.Choice('pool_' + idx, values=['max', 'avg'])
 if pool_type == 'max':
tmp_model = MaxPooling2D(2 )(tmp_model)
 elif pool_type == 'avg':
tmp_model = AveragePooling2D(2 )(tmp_model)
 tmp_model = Dropout(0.25 )(tmp_model)
 tmp_model = Flatten()(tmp_model)
 tmp_model = Dense(units=hp.Int('units',
min_value=32,
max_value=512,
step=32), activation='relu' )(tmp_model)
 tmp_model = Dropout(0.5 )(tmp_model)
 model = keras.Model(inputShape, Dense(10, activation='softmax' )(tmp_model))
model.compile(optimizer=Adam(lr),
loss='categorical_crossentropy',
metrics=['accuracy'])
 return model<predict_on_test>
def show_count_sum(df, col):
return df.groupby(col ).agg({'totals.transactionRevenue': ['count', 'mean']} ).sort_values(( 'totals.transactionRevenue', 'count'), ascending=False ).head()
show_count_sum(train_, 'geoNetwork.subContinent' )
loss, accu = best_model.evaluate(x_test, y_test, verbose = 0)
 print("%s: %.2f%%" %('Accuracy...', accu))
print("%s: %.2f" %('Loss...', loss))
print('-'*35)
 preds = np.argmax(best_model.predict(x_test), axis = 1)
 y_true = np.argmax(y_test,axis = 1)
 metrics(preds, y_true )<prepare_output>
show_count_sum(train_.loc[train_['geoNetwork.subContinent'] == 'Northern America'], 'geoNetwork.metro' )
results = best_model.predict(SUBMIT)
 results = np.argmax(results,axis = 1)
 data_out = pd.DataFrame({'id': range(len(SUBMIT)) , 'label': results} )<save_to_csv>
show_count_sum(train_.loc[train_['geoNetwork.subContinent'] == 'Northern America'], 'geoNetwork.networkDomain' )
data_out.to_csv('submission.csv', index = None )<import_modules>
show_count_sum(train_.loc[train_['geoNetwork.subContinent'] == 'Northern America'], 'geoNetwork.region' )
import numpy as np
import pandas as pd
import datetime
from sklearn.model_selection import train_test_split
from keras.utils.np_utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Flatten, Dropout, Conv2D, MaxPooling2D, Lambda, LeakyReLU
from keras.layers.normalization import BatchNormalization
from keras.optimizers import Adam
from keras.callbacks import LearningRateScheduler<load_from_csv>
show_count_sum(train_, 'trafficSource.medium' )
train = pd.read_csv("/kaggle/input/Kannada-MNIST/train.csv")
 Dig = pd.read_csv("/kaggle/input/Kannada-MNIST/Dig-MNIST.csv")
 train_data = train.append(Dig)
 y_train = np.array(train_data["label"])
 y_train = to_categorical(y_train)
 x_train = train_data.drop("label",axis=1 ).values
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
 x_train.shape, y_train.shape<split>
del grouped, counts, train_
X_train, X_valid, Y_train, Y_valid = train_test_split(x_train, y_train, test_size = 0.10, random_state=0)
 train_datagen = ImageDataGenerator(rescale = 1./255.,
rotation_range = 10,
width_shift_range = 0.25,
height_shift_range = 0.25,
shear_range = 0.1,
zoom_range = 0.25,
horizontal_flip = False)
 valid_datagen = ImageDataGenerator(rescale=1./255 )<choose_model_class>
train['month'] = train['date'].dt.month
train['day'] = train['date'].dt.day
train['weekday'] = train['date'].dt.weekday
train['weekofyear'] = train['date'].dt.weekofyear
train['month_unique_user_count'] = train.groupby('month')['fullVisitorId'].transform('nunique')
 train['day_unique_user_count'] = train.groupby('day')['fullVisitorId'].transform('nunique')
 train['weekday_unique_user_count'] = train.groupby('weekday')['fullVisitorId'].transform('nunique')
 train['weekofyear_unique_user_count'] = train.groupby('weekofyear')['fullVisitorId'].transform('nunique')
 test['month'] = test['date'].dt.month
test['day'] = test['date'].dt.day
test['weekday'] = test['date'].dt.weekday
test['weekofyear'] = test['date'].dt.weekofyear
test['month_unique_user_count'] = test.groupby('month')['fullVisitorId'].transform('nunique')
 test['day_unique_user_count'] = test.groupby('day')['fullVisitorId'].transform('nunique')
 test['weekday_unique_user_count'] = test.groupby('weekday')['fullVisitorId'].transform('nunique')
 test['weekofyear_unique_user_count'] = test.groupby('weekofyear')['fullVisitorId'].transform('nunique' )
model = Sequential()
model.add(Conv2D(64,(3,3), padding='same', input_shape=(28, 28, 1)))
 model.add(BatchNormalization(momentum=0.5, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(64,(3,3), padding='same'))
model.add(BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(2, 2))
model.add(Dropout(0.2))
model.add(Conv2D(128,(3,3), padding='same'))
model.add(BatchNormalization(momentum=0.2, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(128,(3,3), padding='same'))
model.add(BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(2, 2))
model.add(Dropout(0.2))
model.add(Conv2D(256,(3,3), padding='same'))
model.add(BatchNormalization(momentum=0.2, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(128,(3,3), padding='same'))
model.add(BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(2, 2))
model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(256,activation='relu',name='dense1'))
model.add(LeakyReLU(alpha=0.1))
model.add(BatchNormalization())
 model.add(Dense(10,activation='softmax'))<train_model>
train['browser_category'] = train['device.browser'] + '_' + train['device.deviceCategory']
train['browser_operatingSystem'] = train['device.browser'] + '_' + train['device.operatingSystem']
test['browser_category'] = test['device.browser'] + '_' + test['device.deviceCategory']
test['browser_operatingSystem'] = test['device.browser'] + '_' + test['device.operatingSystem']
initial_learningrate=2e-3
def lr_decay(epoch):
return initial_learningrate * 0.99 ** epoch
model.compile(loss="categorical_crossentropy", optimizer=Adam(lr=initial_learningrate), metrics=["acc"])
 model.fit_generator(train_datagen.flow(X_train, Y_train, batch_size=2048),
steps_per_epoch=300,
epochs=10,
callbacks=[LearningRateScheduler(lr_decay,verbose=1)],
validation_data=valid_datagen.flow(X_valid,Y_valid),
validation_steps=50,
verbose=1 )<save_to_csv>
train['visitNumber'] = np.log1p(train['visitNumber'])
 test['visitNumber'] = np.log1p(test['visitNumber'])
 train['totals.hits'] = np.log1p(train['totals.hits'])
 test['totals.hits'] = np.log1p(test['totals.hits'].astype(int))
train['totals.pageviews'] = np.log1p(train['totals.pageviews'].fillna(0))
test['totals.pageviews'] = np.log1p(test['totals.pageviews'].astype(float ).fillna(0))
train['sum_pageviews_per_network_domain'] = train.groupby('geoNetwork.networkDomain')['totals.pageviews'].transform('sum')
 train['count_pageviews_per_network_domain'] = train.groupby('geoNetwork.networkDomain')['totals.pageviews'].transform('count')
 train['mean_pageviews_per_network_domain'] = train.groupby('geoNetwork.networkDomain')['totals.pageviews'].transform('mean')
 train['sum_hits_per_network_domain'] = train.groupby('geoNetwork.networkDomain')['totals.hits'].transform('sum')
 train['count_hits_per_network_domain'] = train.groupby('geoNetwork.networkDomain')['totals.hits'].transform('count')
 train['mean_hits_per_network_domain'] = train.groupby('geoNetwork.networkDomain')['totals.hits'].transform('mean')
 test['sum_pageviews_per_network_domain'] = test.groupby('geoNetwork.networkDomain')['totals.pageviews'].transform('sum')
 test['count_pageviews_per_network_domain'] = test.groupby('geoNetwork.networkDomain')['totals.pageviews'].transform('count')
 test['mean_pageviews_per_network_domain'] = test.groupby('geoNetwork.networkDomain')['totals.pageviews'].transform('mean')
 test['sum_hits_per_network_domain'] = test.groupby('geoNetwork.networkDomain')['totals.hits'].transform('sum')
 test['count_hits_per_network_domain'] = test.groupby('geoNetwork.networkDomain')['totals.hits'].transform('count')
 test['mean_hits_per_network_domain'] = test.groupby('geoNetwork.networkDomain')['totals.hits'].transform('mean')
 train['mean_hits_per_day'] = train.groupby(['day'])['totals.hits'].transform('mean')
 train['sum_hits_per_day'] = train.groupby(['day'])['totals.hits'].transform('sum')
 test['mean_hits_per_day'] = test.groupby(['day'])['totals.hits'].transform('mean')
 test['sum_hits_per_day'] = test.groupby(['day'])['totals.hits'].transform('sum' )
X_test = pd.read_csv("/kaggle/input/Kannada-MNIST/test.csv")
 X_test = X_test.drop("id", axis=1 ).values
submission = pd.read_csv("/kaggle/input/Kannada-MNIST/sample_submission.csv")
 X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)
 X_test = X_test.astype(np.float32)/ 255
predictions = model.predict_classes(X_test)
 submissions=pd.DataFrame({"id": submission["id"], "label": predictions})
 submissions.to_csv("sUbmission.csv", index=False)
 print("FINISH" )<set_options>
train['sum_pageviews_per_network_domain'] = train.groupby('geoNetwork.networkDomain')['totals.pageviews'].transform('sum')
 train['count_pageviews_per_network_domain'] = train.groupby('geoNetwork.networkDomain')['totals.pageviews'].transform('count')
 train['mean_pageviews_per_network_domain'] = train.groupby('geoNetwork.networkDomain')['totals.pageviews'].transform('mean')
 train['sum_pageviews_per_region'] = train.groupby('geoNetwork.region')['totals.pageviews'].transform('sum')
 train['count_pageviews_per_region'] = train.groupby('geoNetwork.region')['totals.pageviews'].transform('count')
 train['mean_pageviews_per_region'] = train.groupby('geoNetwork.region')['totals.pageviews'].transform('mean')
 test['sum_pageviews_per_network_domain'] = test.groupby('geoNetwork.networkDomain')['totals.pageviews'].transform('sum')
 test['count_pageviews_per_network_domain'] = test.groupby('geoNetwork.networkDomain')['totals.pageviews'].transform('count')
 test['mean_pageviews_per_network_domain'] = test.groupby('geoNetwork.networkDomain')['totals.pageviews'].transform('mean')
 test['sum_pageviews_per_region'] = test.groupby('geoNetwork.region')['totals.pageviews'].transform('sum')
 test['count_pageviews_per_region'] = test.groupby('geoNetwork.region')['totals.pageviews'].transform('count')
 test['mean_pageviews_per_region'] = test.groupby('geoNetwork.region')['totals.pageviews'].transform('mean' )
%matplotlib inline<set_options>
train['sum_hits_per_network_domain'] = train.groupby('geoNetwork.networkDomain')['totals.hits'].transform('sum')
 train['count_hits_per_network_domain'] = train.groupby('geoNetwork.networkDomain')['totals.hits'].transform('count')
 train['mean_hits_per_network_domain'] = train.groupby('geoNetwork.networkDomain')['totals.hits'].transform('mean')
 train['sum_hits_per_region'] = train.groupby('geoNetwork.region')['totals.hits'].transform('sum')
 train['count_hits_per_region'] = train.groupby('geoNetwork.region')['totals.hits'].transform('count')
 train['mean_hits_per_region'] = train.groupby('geoNetwork.region')['totals.hits'].transform('mean')
 train['sum_hits_per_country'] = train.groupby('geoNetwork.country')['totals.hits'].transform('sum')
 train['count_hits_per_country'] = train.groupby('geoNetwork.country')['totals.hits'].transform('count')
 train['mean_hits_per_country'] = train.groupby('geoNetwork.country')['totals.hits'].transform('mean')
 test['sum_hits_per_network_domain'] = test.groupby('geoNetwork.networkDomain')['totals.hits'].transform('sum')
 test['count_hits_per_network_domain'] = test.groupby('geoNetwork.networkDomain')['totals.hits'].transform('count')
 test['mean_hits_per_network_domain'] = test.groupby('geoNetwork.networkDomain')['totals.hits'].transform('mean')
 test['sum_hits_per_region'] = test.groupby('geoNetwork.region')['totals.hits'].transform('sum')
 test['count_hits_per_region'] = test.groupby('geoNetwork.region')['totals.hits'].transform('count')
 test['mean_hits_per_region'] = test.groupby('geoNetwork.region')['totals.hits'].transform('mean')
 test['sum_hits_per_country'] = test.groupby('geoNetwork.country')['totals.hits'].transform('sum')
 test['count_hits_per_country'] = test.groupby('geoNetwork.country')['totals.hits'].transform('count')
 test['mean_hits_per_country'] = test.groupby('geoNetwork.country')['totals.hits'].transform('mean' )
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.compat.v1.Session(config=config )<load_from_csv>
train['user_pageviews_sum'] = train.groupby('fullVisitorId')['totals.pageviews'].transform('sum')
 train['user_hits_sum'] = train.groupby('fullVisitorId')['totals.hits'].transform('sum')
 test['user_pageviews_sum'] = test.groupby('fullVisitorId')['totals.pageviews'].transform('sum')
 test['user_hits_sum'] = test.groupby('fullVisitorId')['totals.hits'].transform('sum')
 train['user_pageviews_count'] = train.groupby('fullVisitorId')['totals.pageviews'].transform('count')
 train['user_hits_count'] = train.groupby('fullVisitorId')['totals.hits'].transform('count')
 test['user_pageviews_count'] = test.groupby('fullVisitorId')['totals.pageviews'].transform('count')
 test['user_hits_count'] = test.groupby('fullVisitorId')['totals.hits'].transform('count')
 train['user_pageviews_sum_to_mean'] = train['user_pageviews_sum'] / train['user_pageviews_sum'].mean()
train['user_hits_sum_to_mean'] = train['user_hits_sum'] / train['user_hits_sum'].mean()
test['user_pageviews_sum_to_mean'] = test['user_pageviews_sum'] / test['user_pageviews_sum'].mean()
test['user_hits_sum_to_mean'] = test['user_hits_sum'] / test['user_hits_sum'].mean()
test = pd.read_csv("/kaggle/input/Kannada-MNIST/test.csv")
 train = pd.read_csv("/kaggle/input/Kannada-MNIST/train.csv")
 dig_mnist = pd.read_csv("/kaggle/input/Kannada-MNIST/Dig-MNIST.csv")
 sample_submission = pd.read_csv("/kaggle/input/Kannada-MNIST/sample_submission.csv" )<data_type_conversions>
train['user_pageviews_to_region'] = train['user_pageviews_sum'] / train['mean_pageviews_per_region']
train['user_hits_to_region'] = train['user_hits_sum'] / train['mean_hits_per_region']
test['user_pageviews_to_region'] = test['user_pageviews_sum'] / test['mean_pageviews_per_region']
test['user_hits_to_region'] = test['user_hits_sum'] / test['mean_hits_per_region']
X_train = train.loc[:, train.columns!='label'].values.astype('uint8')
 print("X_train.shape", X_train.shape)
 y_train = train['label'].values
X_train = X_train.reshape(( X_train.shape[0],28,28))
print("X_train.shape", X_train.shape)
 print("y_train.shape",X_train.shape )<split>
num_cols = ['visitNumber', 'totals.hits', 'totals.pageviews', 'month_unique_user_count', 'day_unique_user_count', 'mean_hits_per_day'
'sum_pageviews_per_network_domain', 'sum_hits_per_network_domain', 'count_hits_per_network_domain', 'sum_hits_per_region',
'sum_hits_per_day', 'count_pageviews_per_network_domain', 'mean_pageviews_per_network_domain', 'weekday_unique_user_count',
'sum_pageviews_per_region', 'count_pageviews_per_region', 'mean_pageviews_per_region', 'user_pageviews_count', 'user_hits_count',
'count_hits_per_region', 'mean_hits_per_region', 'user_pageviews_sum', 'user_hits_sum', 'user_pageviews_sum_to_mean',
'user_hits_sum_to_mean', 'user_pageviews_to_region', 'user_hits_to_region', 'mean_pageviews_per_network_domain',
'mean_hits_per_network_domain']
no_use = ["visitNumber", "date", "fullVisitorId", "sessionId", "visitId", "visitStartTime", 'totals.transactionRevenue', 'trafficSource.referralPath']
cat_cols = [col for col in train.columns if col not in num_cols and col not in no_use]
X_train = X_train[:,:,:,None]
X_test = X_test[:,:,:,None]<define_variables>
for col in cat_cols:
if col != 'trafficSource.campaignCode':
print(col)
 lbl = LabelEncoder()
lbl.fit(list(train[col].values.astype('str')) + list(test[col].values.astype('str')))
 train[col] = lbl.transform(list(train[col].values.astype('str')))
 test[col] = lbl.transform(list(test[col].values.astype('str')) )
batch_size = 32
num_epochs = 50 
<count_unique_values>
no_use.append('trafficSource.campaignCode')
 train = train.sort_values('date')
 X = train.drop([col for col in no_use if col in train.columns], axis=1)
 y = train['totals.transactionRevenue']
X_test = test.drop([col for col in no_use if col in test.columns], axis=1)
 tscv = TimeSeriesSplit(n_splits=5 )
num_samples = X_train.shape[0]
num_classes = np.unique(y_train ).shape[0]
img_rows, img_cols = X_train[0,:,:,0].shape
classes = np.unique(y_train )<define_variables>
params = {"objective" : "regression", "metric" : "rmse", "max_depth": 12, "min_child_samples": 20, "reg_alpha": 0.1, "reg_lambda": 0.1,
"num_leaves" : 1024, "learning_rate" : 0.01, "subsample" : 0.9, "colsample_bytree" : 0.9, "subsample_freq ": 10}
n_fold = 10
folds = KFold(n_splits=n_fold, shuffle=False, random_state=42)
 model = lgb.LGBMRegressor(**params, n_estimators = 20000, nthread = 4, n_jobs = -1 )
print("num_samples",num_samples)
 print("num_classes",num_classes)
 print("img_rows",img_rows)
 print("img_cols",img_cols)
 print("classes",classes )<categorify>
prediction = np.zeros(test.shape[0])
 for fold_n,(train_index, test_index)in enumerate(folds.split(X)) :
print('Fold:', fold_n)
 X_train, X_valid = X.iloc[train_index], X.iloc[test_index]
y_train, y_valid = y.iloc[train_index], y.iloc[test_index]
model.fit(X_train, y_train,
eval_set=[(X_train, y_train),(X_valid, y_valid)], eval_metric='rmse',
verbose=500, early_stopping_rounds=100)
 y_pred = model.predict(X_test, num_iteration=model.best_iteration_)
 prediction += y_pred
prediction /= n_fold
y_train = np_utils.to_categorical(y_train, num_classes)
 y_train.shape<data_type_conversions>
test["PredictedLogRevenue"] = prediction
sub = test.groupby("fullVisitorId" ).agg({"PredictedLogRevenue" : "sum"} ).reset_index()
sub["PredictedLogRevenue"] = np.log1p(sub["PredictedLogRevenue"])
 sub["PredictedLogRevenue"] = sub["PredictedLogRevenue"].apply(lambda x : 0.0 if x < 0 else x)
 sub["PredictedLogRevenue"] = sub["PredictedLogRevenue"].fillna(0.0)
 sub.to_csv("lgb.csv", index=False)