text_plain_output_1.png
import cudf

train = cudf.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')
train['customer_id'] = train['customer_id'].str[-16:].str.hex_to_int().astype('int64')
train['article_id'] = train.article_id.astype('int32')
train.t_dat = cudf.to_datetime(train.t_dat)
train = train[['t_dat', 'customer_id', 'article_id']]
train.to_parquet('train.pqt', index=False)

tmp = train.groupby('customer_id').t_dat.max().reset_index()
tmp.columns = ['customer_id', 'max_dat']
train = train.merge(tmp, on=['customer_id'], how='left')
train['diff_dat'] = (train.max_dat - train.t_dat).dt.days
train = train.loc[train['diff_dat'] <= 6]

tmp = train.groupby(['customer_id', 'article_id'])['t_dat'].agg('count').reset_index()
tmp.columns = ['customer_id', 'article_id', 'ct']
train = train.merge(tmp, on=['customer_id', 'article_id'], how='left')
train = train.sort_values(['ct', 't_dat'], ascending=False)
train = train.drop_duplicates(['customer_id', 'article_id'])
train = train.sort_values(['ct', 't_dat'], ascending=False)

import pandas as pd, numpy as np
train = train.to_pandas()
pairs = np.load('../input/hmitempairs/pairs_cudf.npy', allow_pickle=True).item()
train['article_id2'] = train.article_id.map(pairs)

train2 = train[['customer_id', 'article_id2']].copy()
train2 = train2.loc[train2.article_id2.notnull()]
train2 = train2.drop_duplicates(['customer_id', 'article_id2'])
train2 = train2.rename({'article_id2': 'article_id'}, axis=1)

train = train[['customer_id', 'article_id']]
train = pd.concat([train, train2], axis=0, ignore_index=True)
train.article_id = train.article_id.astype('int32')
train = train.drop_duplicates(['customer_id', 'article_id'])

train.article_id = ' 0' + train.article_id.astype('str')
preds = cudf.DataFrame(train.groupby('customer_id').article_id.sum().reset_index())
preds.columns = ['customer_id', 'prediction']

train = cudf.read_parquet('train.pqt')
train.t_dat = cudf.to_datetime(train.t_dat)
train = train.loc[train.t_dat >= cudf.to_datetime('2020-09-16')]
top12 = ' 0' + ' 0'.join(train.article_id.value_counts().to_pandas().index.astype('str')[:12])
print("Last week's top 12 popular items:")
print(top12)