Kaggle competition: Predict Future Sales
NB_pipe = Pipeline([('NaiveBayes pipline', full_pipline_NB), 
('cat_bayes', cat_bayes)])
 cross_val_score(NB_pipe,X,y,cv=10,scoring='accuracy' ).mean()<compute_train_metric>
items = pd.read_csv('.. /input/items.csv')
 shops = pd.read_csv('.. /input/shops.csv')
 cats = pd.read_csv('.. /input/item_categories.csv')
 train = pd.read_csv('.. /input/sales_train.csv')
 test = pd.read_csv('.. /input/test.csv' ).set_index('ID' )
DT_pipe = Pipeline([('DecisionTree pipline', full_pipline_DT), 
('Decision Tree', DT_clf)])
 cross_val_score(DT_pipe,X,y,cv=10,scoring='accuracy' ).mean()<compute_train_metric>
train = train[train.item_price<100000]
train = train[train.item_cnt_day<1001]
SVM_pipe = Pipeline([('SVM pipline', full_pipline_SVM), 
('SVM', svm_clf)])
 cross_val_score(SVM_pipe,X,y,cv=10,scoring='accuracy' ).mean()<compute_train_metric>
median = train[(train.shop_id==32)&(train.item_id==2973)&(train.date_block_num==4)&(train.item_price>0)].item_price.median()
train.loc[train.item_price<0, 'item_price'] = median
voting_clf_4 = VotingClassifier(
 estimators = [('Logistic Regresion',LR_pipe),('SVM',SVM_pipe),('Naive Bayes',NB_pipe),('Decision Tree',DT_pipe)],
voting = 'hard' 
)
 cross_val_score(voting_clf_4,X,y,cv=10,scoring='accuracy' ).mean()<compute_train_metric>
train.loc[train.shop_id == 0, 'shop_id'] = 57
test.loc[test.shop_id == 0, 'shop_id'] = 57
train.loc[train.shop_id == 1, 'shop_id'] = 58
test.loc[test.shop_id == 1, 'shop_id'] = 58
train.loc[train.shop_id == 10, 'shop_id'] = 11
test.loc[test.shop_id == 10, 'shop_id'] = 11
voting_clf_SVM_DT = VotingClassifier(
 estimators = [('SVM',SVM_pipe),('Decision Tree',DT_pipe)],
voting = 'hard' 
)
 cross_val_score(voting_clf_SVM_DT,X,y,cv=10,scoring='accuracy' ).mean()<compute_train_metric>
shops.loc[shops.shop_name == 'Сергиев Посад ТЦ "7Я"', 'shop_name'] = 'СергиевПосад ТЦ "7Я"'
shops['city'] = shops['shop_name'].str.split(' ' ).map(lambda x: x[0])
 shops.loc[shops.city == '!Якутск', 'city'] = 'Якутск'
shops['city_code'] = LabelEncoder().fit_transform(shops['city'])
 shops = shops[['shop_id','city_code']]
cats['split'] = cats['item_category_name'].str.split('-')
 cats['type'] = cats['split'].map(lambda x: x[0].strip())
 cats['type_code'] = LabelEncoder().fit_transform(cats['type'])
 cats['subtype'] = cats['split'].map(lambda x: x[1].strip() if len(x)> 1 else x[0].strip())
 cats['subtype_code'] = LabelEncoder().fit_transform(cats['subtype'])
 cats = cats[['item_category_id','type_code', 'subtype_code']]
items.drop(['item_name'], axis=1, inplace=True )
voting_clf_NB_DT = VotingClassifier(
 estimators = [('Naive Bayes',NB_pipe),('Decision Tree',DT_pipe)],
voting = 'hard' 
)
 cross_val_score(voting_clf_NB_DT,X,y,cv=10,scoring='accuracy' ).mean()<drop_column>
len(list(set(test.item_id)- set(test.item_id ).intersection(set(train.item_id)))) , len(list(set(test.item_id))), len(test )
X_test = test_data.drop(columns=['Survived'] )<train_model>
ts = time.time()
matrix = []
cols = ['date_block_num','shop_id','item_id']
for i in range(34):
sales = train[train.date_block_num==i]
matrix.append(np.array(list(product([i], sales.shop_id.unique() , sales.item_id.unique())) , dtype='int16'))
matrix = pd.DataFrame(np.vstack(matrix), columns=cols)
 matrix['date_block_num'] = matrix['date_block_num'].astype(np.int8)
 matrix['shop_id'] = matrix['shop_id'].astype(np.int8)
 matrix['item_id'] = matrix['item_id'].astype(np.int16)
 matrix.sort_values(cols,inplace=True)
 time.time() - ts
voting_clf_NB_DT.fit(X,y )<prepare_output>
train['revenue'] = train['item_price'] * train['item_cnt_day']
predictions = voting_clf_NB_DT.predict(X_test)
 predictions = pd.to_numeric(predictions, downcast='integer')
 output = pd.DataFrame({'PassengerId': list(X_test.index), 'Survived': predictions} )<save_to_csv>
ts = time.time()
group = train.groupby(['date_block_num','shop_id','item_id'] ).agg({'item_cnt_day': ['sum']})
 group.columns = ['item_cnt_month']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=cols, how='left')
 matrix['item_cnt_month'] =(matrix['item_cnt_month'] 
.fillna(0)
.clip(0,20)
.astype(np.float16))
time.time() - ts
output.to_csv('Submission_NB_DT.csv', index=False )<load_from_csv>
test['date_block_num'] = 34
test['date_block_num'] = test['date_block_num'].astype(np.int8)
 test['shop_id'] = test['shop_id'].astype(np.int8)
 test['item_id'] = test['item_id'].astype(np.int16 )
train=pd.read_csv(".. /input/titanic/train.csv")
 test=pd.read_csv(".. /input/titanic/test.csv" )<count_missing_values>
ts = time.time()
matrix = pd.concat([matrix, test], ignore_index=True, sort=False, keys=cols)
 matrix.fillna(0, inplace=True)
 time.time() - ts
train.isnull().sum()<count_missing_values>
ts = time.time()
matrix = pd.merge(matrix, shops, on=['shop_id'], how='left')
 matrix = pd.merge(matrix, items, on=['item_id'], how='left')
 matrix = pd.merge(matrix, cats, on=['item_category_id'], how='left')
 matrix['city_code'] = matrix['city_code'].astype(np.int8)
 matrix['item_category_id'] = matrix['item_category_id'].astype(np.int8)
 matrix['type_code'] = matrix['type_code'].astype(np.int8)
 matrix['subtype_code'] = matrix['subtype_code'].astype(np.int8)
 time.time() - ts
test.isnull().sum()<drop_column>
def lag_feature(df, lags, col):
tmp = df[['date_block_num','shop_id','item_id',col]]
for i in lags:
shifted = tmp.copy()
shifted.columns = ['date_block_num','shop_id','item_id', col+'_lag_'+str(i)]
shifted['date_block_num'] += i
df = pd.merge(df, shifted, on=['date_block_num','shop_id','item_id'], how='left')
 return df
train.drop(columns=['PassengerId','Name','Ticket','Cabin'],inplace=True)
 test.drop(columns=['PassengerId','Name','Ticket','Cabin'],inplace=True)
<drop_column>
ts = time.time()
group = matrix.groupby(['date_block_num'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num'], how='left')
 matrix['date_avg_item_cnt'] = matrix['date_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_avg_item_cnt')
 matrix.drop(['date_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
train.drop(columns=['SibSp','Parch'],inplace=True)
 test.drop(columns=['SibSp','Parch'],inplace=True)
<groupby>
ts = time.time()
group = matrix.groupby(['date_block_num', 'item_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_item_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','item_id'], how='left')
 matrix['date_item_avg_item_cnt'] = matrix['date_item_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1,2,3,6,12], 'date_item_avg_item_cnt')
 matrix.drop(['date_item_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
a=train.groupby('Pclass' ).median() ['Age'].iloc[0]
b=train.groupby('Pclass' ).median() ['Age'].iloc[1]
c=train.groupby('Pclass' ).median() ['Age'].iloc[2]
def fillAge_train(x):
age=x[0]
pclass=x[1]
if pd.isnull(age):
if pclass==1:
return a
elif pclass==2:
return b
else:
return c
else:
return age
a_test=test.groupby('Pclass' ).median() ['Age'].iloc[0]
b_test=test.groupby('Pclass' ).median() ['Age'].iloc[1]
c_test=test.groupby('Pclass' ).median() ['Age'].iloc[2]
def fillAge_test(x):
age=x[0]
pclass=x[1]
if pd.isnull(age):
if pclass==1:
return a_test
elif pclass==2:
return b_test
else:
return c_test
else:
return age
train['Age']=train[['Age','Pclass']].apply(fillAge_train,axis=1)
 test['Age']=test[['Age','Pclass']].apply(fillAge_test,axis=1)
<categorify>
ts = time.time()
group = matrix.groupby(['date_block_num', 'shop_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_shop_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','shop_id'], how='left')
 matrix['date_shop_avg_item_cnt'] = matrix['date_shop_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1,2,3,6,12], 'date_shop_avg_item_cnt')
 matrix.drop(['date_shop_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
a=test.groupby('Pclass' ).median() ['Fare'].iloc[0]
b=test.groupby('Pclass' ).median() ['Fare'].iloc[1]
c=test.groupby('Pclass' ).median() ['Fare'].iloc[2]
def fillFare(x):
fare=x[0]
pclass=x[1]
if pd.isnull(fare):
if pclass==1:
return a
elif pclass==2:
return b
else:
return c
else:
return fare
test['Fare']=test[['Fare','Pclass']].apply(fillFare,axis=1 )<data_type_conversions>
ts = time.time()
group = matrix.groupby(['date_block_num', 'item_category_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_cat_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','item_category_id'], how='left')
 matrix['date_cat_avg_item_cnt'] = matrix['date_cat_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_cat_avg_item_cnt')
 matrix.drop(['date_cat_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
train['Embarked'].fillna(train['Embarked'].mode() [0], inplace=True )<categorify>
ts = time.time()
group = matrix.groupby(['date_block_num', 'shop_id', 'item_category_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = ['date_shop_cat_avg_item_cnt']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'shop_id', 'item_category_id'], how='left')
 matrix['date_shop_cat_avg_item_cnt'] = matrix['date_shop_cat_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_shop_cat_avg_item_cnt')
 matrix.drop(['date_shop_cat_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
train=pd.get_dummies(train,columns=['Sex','Embarked','TravelAlone'],drop_first=True)
 test=pd.get_dummies(test,columns=['Sex','Embarked','TravelAlone'],drop_first=True)
<feature_engineering>
ts = time.time()
group = matrix.groupby(['date_block_num', 'shop_id', 'type_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = ['date_shop_type_avg_item_cnt']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'shop_id', 'type_code'], how='left')
 matrix['date_shop_type_avg_item_cnt'] = matrix['date_shop_type_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_shop_type_avg_item_cnt')
 matrix.drop(['date_shop_type_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
train['AgeCateg'] = pd.cut(train['Age'], 5)
 train[['AgeCateg', 'Survived']].groupby(['AgeCateg'], as_index=False ).mean().sort_values(by='AgeCateg', ascending=True)
 train['FareCateg'] = pd.qcut(train['Fare'], 4)
 train[['FareCateg', 'Survived']].groupby(['FareCateg'], as_index=False ).mean().sort_values(by='FareCateg', ascending=True)
 def encodeAgeFare(train):
train.loc[train['Age'] <= 16, 'Age'] = 0
train.loc[(train['Age'] > 16)&(train['Age'] <= 32), 'Age'] = 1
train.loc[(train['Age'] > 32)&(train['Age'] <= 48), 'Age'] = 2
train.loc[(train['Age'] > 48)&(train['Age'] <= 64), 'Age'] = 3
train.loc[(train['Age'] > 48)&(train['Age'] <= 80), 'Age'] = 4
train.loc[train['Fare'] <= 7.91, 'Fare'] = 0
train.loc[(train['Fare'] > 7.91)&(train['Fare'] <= 14.454), 'Fare'] = 1
train.loc[(train['Fare'] > 14.454)&(train['Fare'] <= 31.0), 'Fare'] = 2
train.loc[(train['Fare'] > 31.0)&(train['Fare'] <= 512.329), 'Fare'] = 3
encodeAgeFare(train)
 encodeAgeFare(test)
 train.drop(columns=['AgeCateg','FareCateg'],inplace=True)
<prepare_x_and_y>
ts = time.time()
group = matrix.groupby(['date_block_num', 'shop_id', 'subtype_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = ['date_shop_subtype_avg_item_cnt']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'shop_id', 'subtype_code'], how='left')
 matrix['date_shop_subtype_avg_item_cnt'] = matrix['date_shop_subtype_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_shop_subtype_avg_item_cnt')
 matrix.drop(['date_shop_subtype_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
X = train.drop('Survived',axis=1)
 y = train['Survived']<split>
ts = time.time()
group = matrix.groupby(['date_block_num', 'city_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_city_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'city_code'], how='left')
 matrix['date_city_avg_item_cnt'] = matrix['date_city_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_city_avg_item_cnt')
 matrix.drop(['date_city_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
np.random.seed(0)
 X_train,X_test,y_train,y_test = train_test_split(X,y, test_size=0.3 )<import_modules>
ts = time.time()
group = matrix.groupby(['date_block_num', 'item_id', 'city_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_item_city_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'item_id', 'city_code'], how='left')
 matrix['date_item_city_avg_item_cnt'] = matrix['date_item_city_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_item_city_avg_item_cnt')
 matrix.drop(['date_item_city_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
from xgboost import XGBClassifier
from lightgbm import LGBMClassifier
from catboost import CatBoostClassifier
from sklearn.metrics import accuracy_score,confusion_matrix
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import RandomizedSearchCV<train_on_grid>
ts = time.time()
group = matrix.groupby(['date_block_num', 'type_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_type_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'type_code'], how='left')
 matrix['date_type_avg_item_cnt'] = matrix['date_type_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_type_avg_item_cnt')
 matrix.drop(['date_type_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
xgbclassifier=XGBClassifier()
xgbclassifier.fit(X_train,y_train)
 pred_xgb=xgbclassifier.predict(X_test)
<train_model>
ts = time.time()
group = matrix.groupby(['date_block_num', 'subtype_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_subtype_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'subtype_code'], how='left')
 matrix['date_subtype_avg_item_cnt'] = matrix['date_subtype_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_subtype_avg_item_cnt')
 matrix.drop(['date_subtype_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
randomfc = RandomForestClassifier(n_estimators=100)
 randomfc.fit(X_train,y_train)
 pred_rf=randomfc.predict(X_test)
<choose_model_class>
ts = time.time()
group = train.groupby(['item_id'] ).agg({'item_price': ['mean']})
 group.columns = ['item_avg_item_price']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['item_id'], how='left')
 matrix['item_avg_item_price'] = matrix['item_avg_item_price'].astype(np.float16)
 group = train.groupby(['date_block_num','item_id'] ).agg({'item_price': ['mean']})
 group.columns = ['date_item_avg_item_price']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','item_id'], how='left')
 matrix['date_item_avg_item_price'] = matrix['date_item_avg_item_price'].astype(np.float16)
 lags = [1,2,3,4,5,6]
matrix = lag_feature(matrix, lags, 'date_item_avg_item_price')
 for i in lags:
matrix['delta_price_lag_'+str(i)] = \ 
(matrix['date_item_avg_item_price_lag_'+str(i)] - matrix['item_avg_item_price'])/ matrix['item_avg_item_price']
def select_trend(row):
for i in lags:
if row['delta_price_lag_'+str(i)]:
return row['delta_price_lag_'+str(i)]
return 0
matrix['delta_price_lag'] = matrix.apply(select_trend, axis=1)
 matrix['delta_price_lag'] = matrix['delta_price_lag'].astype(np.float16)
 matrix['delta_price_lag'].fillna(0, inplace=True)
 fetures_to_drop = ['item_avg_item_price', 'date_item_avg_item_price']
for i in lags:
fetures_to_drop += ['date_item_avg_item_price_lag_'+str(i)]
fetures_to_drop += ['delta_price_lag_'+str(i)]
matrix.drop(fetures_to_drop, axis=1, inplace=True)
 time.time() - ts
lightgb=LGBMClassifier()
lightgb.fit(X_train,y_train)
 pred_lgb=lightgb.predict(X_test)
<train_model>
ts = time.time()
group = train.groupby(['date_block_num','shop_id'] ).agg({'revenue': ['sum']})
 group.columns = ['date_shop_revenue']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','shop_id'], how='left')
 matrix['date_shop_revenue'] = matrix['date_shop_revenue'].astype(np.float32)
 group = group.groupby(['shop_id'] ).agg({'date_shop_revenue': ['mean']})
 group.columns = ['shop_avg_revenue']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['shop_id'], how='left')
 matrix['shop_avg_revenue'] = matrix['shop_avg_revenue'].astype(np.float32)
 matrix['delta_revenue'] =(matrix['date_shop_revenue'] - matrix['shop_avg_revenue'])/ matrix['shop_avg_revenue']
matrix['delta_revenue'] = matrix['delta_revenue'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'delta_revenue')
 matrix.drop(['date_shop_revenue','shop_avg_revenue','delta_revenue'], axis=1, inplace=True)
 time.time() - ts
ada=AdaBoostClassifier(n_estimators=50,learning_rate=1)
 ada.fit(X_train,y_train)
 pred_ada=ada.predict(X_test)
<train_on_grid>
matrix['month'] = matrix['date_block_num'] % 12
cbc=CatBoostClassifier(verbose=0, n_estimators=100)
 cbc.fit(X_train,y_train)
 pred_cbc=cbc.predict(X_test)
<train_model>
days = pd.Series([31,28,31,30,31,30,31,31,30,31,30,31])
 matrix['days'] = matrix['month'].map(days ).astype(np.int8 )
log=LogisticRegression(max_iter=1000)
 log.fit(X_train,y_train)
 pred_log=log.predict(X_test)
<compute_test_metric>
ts = time.time()
cache = {}
matrix['item_shop_last_sale'] = -1
matrix['item_shop_last_sale'] = matrix['item_shop_last_sale'].astype(np.int8)
 for idx, row in matrix.iterrows() :
key = str(row.item_id)+' '+str(row.shop_id)
 if key not in cache:
if row.item_cnt_month!=0:
cache[key] = row.date_block_num
else:
last_date_block_num = cache[key]
matrix.at[idx, 'item_shop_last_sale'] = row.date_block_num - last_date_block_num
cache[key] = row.date_block_num
time.time() - ts
print('XGBoost:', round(xgbclassifier.score(X_train, y_train)* 100, 2), '%.\t\t\t RandomForest:', round(randomfc.score(X_train, y_train)* 100, 2), '%.')
 print('LightGBM:', round(lightgb.score(X_train, y_train)* 100, 2), '%.\t\t\t AdaBoost:', round(ada.score(X_train, y_train)* 100, 2), '%.')
 print('CatBoost:', round(cbc.score(X_train, y_train)* 100, 2), '%.\t\t\t LogisticRegression:', round(log.score(X_train, y_train)* 100, 2), '%.' )<compute_test_metric>
ts = time.time()
cache = {}
matrix['item_last_sale'] = -1
matrix['item_last_sale'] = matrix['item_last_sale'].astype(np.int8)
 for idx, row in matrix.iterrows() :
key = row.item_id
if key not in cache:
if row.item_cnt_month!=0:
cache[key] = row.date_block_num
else:
last_date_block_num = cache[key]
if row.date_block_num>last_date_block_num:
matrix.at[idx, 'item_last_sale'] = row.date_block_num - last_date_block_num
cache[key] = row.date_block_num
time.time() - ts
print('XGBoost:', round(accuracy_score(y_test,pred_xgb)* 100, 2), '%.\t\t\t RandomForest:', round(accuracy_score(y_test,pred_rf)* 100, 2), '%.')
 print('LightGBM:', round(accuracy_score(y_test,pred_lgb)* 100, 2), '%.\t\t\t AdaBoost:', round(accuracy_score(y_test,pred_ada)* 100, 2), '%.')
 print('CatBoost:', round(accuracy_score(y_test,pred_cbc)* 100, 2), '%.\t\t\t LogisticRegression:', round(accuracy_score(y_test,pred_log)* 100, 2), '%.' )<prepare_x_and_y>
ts = time.time()
matrix['item_shop_first_sale'] = matrix['date_block_num'] - matrix.groupby(['item_id','shop_id'])['date_block_num'].transform('min')
 matrix['item_first_sale'] = matrix['date_block_num'] - matrix.groupby('item_id')['date_block_num'].transform('min')
 time.time() - ts
X_train=train.drop('Survived',axis=1)
 X_test=test.copy()
y_train = train['Survived']
model_params = {
'n_estimators': randint(4,200),
'max_features': truncnorm(a=0, b=1, loc=0.25, scale=0.1),
'min_samples_split': uniform(0.01, 0.199)
 }
rf_model = RandomForestClassifier()
clf = RandomizedSearchCV(rf_model, model_params, n_iter=100, cv=5, random_state=1)
 model = clf.fit(X_train, y_train)
 pprint(model.best_estimator_.get_params() )<predict_on_test>
ts = time.time()
matrix = matrix[matrix.date_block_num > 11]
time.time() - ts
testData=test.copy()
predictions_rf=model.predict(testData )<save_to_csv>
ts = time.time()
def fill_na(df):
for col in df.columns:
if('_lag_' in col)&(df[col].isnull().any()):
if('item_cnt' in col):
df[col].fillna(0, inplace=True)
 return df
matrix = fill_na(matrix)
 time.time() - ts
subm=pd.read_csv('.. /input/titanic/gender_submission.csv')
 Survived=pd.Series(predictions_rf)
 subm.drop(columns=['Survived'],inplace=True)
 subm['Survived']=pd.Series(predictions_rf)
 subm.to_csv(r'RandomForestSubmission.csv', index = False)
 print("Submitted" )<import_modules>
matrix.to_pickle('data.pkl')
 del matrix
del cache
del group
del items
del shops
del cats
del train
gc.collect() ;
from fastai.vision import *
import fastai
import pandas as pd<import_modules>
data = pd.read_pickle('data.pkl' )
fastai.__version__<define_variables>
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',
]]
path = Path('.. /input/dog-breed-identification')
 path_train = path / 'train'
path_test = path / 'test/test'
path_model = Path('/tmp/model/' )<train_model>
X = data[data.date_block_num <= 33].drop(['item_cnt_month'], axis=1)
 Y = data[data.date_block_num <= 33]['item_cnt_month']
X_test = data[data.date_block_num == 34].drop(['item_cnt_month'], axis=1 )
path_train, path_test<set_options>
del data
gc.collect() ;
path.ls()<define_variables>
ts = time.time()
model = XGBRegressor(
 n_jobs=-1,
learning_rate=0.2,
max_depth=8,
n_estimators=1000,
min_child_weight=300,
colsample_bytree=0.8,
subsample=0.8,
eta=0.3,
seed=42)
 X_train, X_valid, y_train, y_valid = train_test_split(X, Y, test_size=0.33, random_state=7)
 eval_set = [(X_valid, y_valid)]
model.fit(
 X_train,
y_train,
eval_metric="rmse",
eval_set=eval_set,
verbose=True,
early_stopping_rounds = 3)
 time.time() - ts
list(Path('.. /input/dog-breed-identification' ).glob('**'))<define_variables>
Y_test = model.predict(X_test ).clip(0, 20)
 submission = pd.DataFrame({
"ID": test.index,
"item_cnt_month": Y_test
} )
bs, size = 64, 224<load_from_csv>
ts = time.time()
print('-Initialize RNN with LSTM')
 model = Sequential()
model.add(LSTM(units=64, input_shape=(1, 34)))
 model.add(Dropout(0.3))
model.add(Dense(1))
model.compile(loss='mse',
optimizer='adam',
metrics=['mean_squared_error'])
 model.summary()
data = ImageDataBunch.from_csv(path=path,
folder='train',
csv_labels='labels.csv',
ds_tfms=get_transforms() ,
suffix='.jpg',
test='test/test',
size=size,
bs=bs,
num_workers=0 ).normalize(imagenet_stats )<define_variables>
ts = time.time()
print('-Reshape x_train, x_test')
 X_train = X.values
X_train = np.reshape(X_train,(X_train.shape[0], 1, X_train.shape[1]))
X_test = np.reshape(X_test.values,(X_test.shape[0], 1, X_test.shape[1]))
X_valid = np.reshape(X_valid.values,(X_valid.shape[0], 1, X_valid.shape[1]))
eval_set =(X_valid, y_valid)
 print('-Model fit')
 history = model.fit(X_train, Y, batch_size=2048, epochs=200, validation_data=eval_set)
 time.time() - ts
8178 + 2044<choose_model_class>
print('-Model predict')
 Y_test = model.predict(X_test ).clip(0, 20)
 y_pred = np.reshape(( Y_test), Y_test.shape[0],1)
 submissionLSTM = pd.DataFrame({
"ID": test.index,
"item_cnt_month": y_pred
})
learn = cnn_learner(data,
base_arch=models.resnet50,
metrics=accuracy,
model_dir=path_model )<find_best_params>
submission['item_cnt_month'] =(submission['item_cnt_month'] + submissionLSTM['item_cnt_month'])/2
print('-Save predict')
 submission.to_csv('XGBOOST_LSTM_submission.csv', index=False)
 print('output file :','XGBOOST_LSTM_submission','saved' )