{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport math \nimport time\nimport dask.dataframe as dd\nfrom dask import delayed\nfrom dask.distributed  import Client\nimport ray\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-04-18T19:25:58.497565Z","iopub.execute_input":"2022-04-18T19:25:58.498436Z","iopub.status.idle":"2022-04-18T19:26:00.15698Z","shell.execute_reply.started":"2022-04-18T19:25:58.498302Z","shell.execute_reply":"2022-04-18T19:26:00.15602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles=pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/articles.csv')\ncustomers=pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/customers.csv')\ntransactions=pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-04-18T19:28:43.048846Z","iopub.execute_input":"2022-04-18T19:28:43.049417Z","iopub.status.idle":"2022-04-18T19:29:33.910118Z","shell.execute_reply.started":"2022-04-18T19:28:43.049372Z","shell.execute_reply":"2022-04-18T19:29:33.909027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import hashlib\ncustomers['customer_id']=customers['customer_id'].apply(lambda s:int(hashlib.sha1(s.encode(\"utf-8\")).hexdigest(), 16)%10**10)","metadata":{"execution":{"iopub.status.busy":"2022-04-18T19:27:16.315112Z","iopub.execute_input":"2022-04-18T19:27:16.315429Z","iopub.status.idle":"2022-04-18T19:27:19.889337Z","shell.execute_reply.started":"2022-04-18T19:27:16.315389Z","shell.execute_reply":"2022-04-18T19:27:19.888481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions['customer_id']=transactions['customer_id'].apply(lambda s:int(hashlib.sha1(s.encode(\"utf-8\")).hexdigest(), 16)%10**10)","metadata":{"execution":{"iopub.status.busy":"2022-04-18T19:27:19.891494Z","iopub.execute_input":"2022-04-18T19:27:19.891766Z","iopub.status.idle":"2022-04-18T19:28:40.855782Z","shell.execute_reply.started":"2022-04-18T19:27:19.891734Z","shell.execute_reply":"2022-04-18T19:28:40.854834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def reduce_mem_usage(df):\n    \"\"\" iterate through all the columns of a dataframe and modify the data type\n        to reduce memory usage.        \n    \"\"\"\n    start_mem = df.memory_usage().sum() / 1024**2\n    print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))\n    \n    for col in df.columns:\n        col_type = df[col].dtype\n        \n        if col_type != object:\n            c_min = df[col].min()\n            c_max = df[col].max()\n            if str(col_type)[:3] == 'int':\n                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n                    df[col] = df[col].astype(np.int8)\n                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n                    df[col] = df[col].astype(np.int16)\n                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n                    df[col] = df[col].astype(np.int32)\n                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n                    df[col] = df[col].astype(np.int64)  \n            else:\n                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n                    df[col] = df[col].astype(np.float16)\n                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n                    df[col] = df[col].astype(np.float32)\n                else:\n                    df[col] = df[col].astype(np.float64)\n        else:\n            df[col] = df[col].astype('category')\n\n    end_mem = df.memory_usage().sum() / 1024**2\n    print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))\n    print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))\n    \n    return df\n","metadata":{"execution":{"iopub.status.busy":"2022-04-18T19:28:40.857175Z","iopub.execute_input":"2022-04-18T19:28:40.857454Z","iopub.status.idle":"2022-04-18T19:28:40.873962Z","shell.execute_reply.started":"2022-04-18T19:28:40.857408Z","shell.execute_reply":"2022-04-18T19:28:40.873142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(customers.info())\nprint(articles.info())\nprint(transactions.info())","metadata":{"execution":{"iopub.status.busy":"2022-04-18T19:28:40.875042Z","iopub.execute_input":"2022-04-18T19:28:40.875285Z","iopub.status.idle":"2022-04-18T19:28:41.57244Z","shell.execute_reply.started":"2022-04-18T19:28:40.875256Z","shell.execute_reply":"2022-04-18T19:28:41.571274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers['fashion_news_frequency'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T19:28:41.573607Z","iopub.execute_input":"2022-04-18T19:28:41.573864Z","iopub.status.idle":"2022-04-18T19:28:41.730232Z","shell.execute_reply.started":"2022-04-18T19:28:41.573833Z","shell.execute_reply":"2022-04-18T19:28:41.729215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers['club_member_status'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T19:28:41.731285Z","iopub.execute_input":"2022-04-18T19:28:41.731555Z","iopub.status.idle":"2022-04-18T19:28:41.88623Z","shell.execute_reply.started":"2022-04-18T19:28:41.731523Z","shell.execute_reply":"2022-04-18T19:28:41.885621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers['club_member_status'].fillna(0.0,inplace=True)\ncustomers['club_member_status'].replace('ACTIVE','3.0',inplace=True)\ncustomers['club_member_status'].replace('PRE-CREATE','2.0',inplace=True)\ncustomers['club_member_status'].replace('LEFT CLUB','1.0',inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-18T19:42:40.87573Z","iopub.execute_input":"2022-04-18T19:42:40.875999Z","iopub.status.idle":"2022-04-18T19:42:41.234084Z","shell.execute_reply.started":"2022-04-18T19:42:40.875972Z","shell.execute_reply":"2022-04-18T19:42:41.233037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers['club_member_status']=pd.to_numeric(customers['club_member_status'],errors='coerce')","metadata":{"execution":{"iopub.status.busy":"2022-04-18T19:43:01.954526Z","iopub.execute_input":"2022-04-18T19:43:01.95507Z","iopub.status.idle":"2022-04-18T19:43:02.737028Z","shell.execute_reply.started":"2022-04-18T19:43:01.955034Z","shell.execute_reply":"2022-04-18T19:43:02.736247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers['club_member_status']=customers['club_member_status']/customers['club_member_status'].max()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T19:43:03.751271Z","iopub.execute_input":"2022-04-18T19:43:03.751573Z","iopub.status.idle":"2022-04-18T19:43:03.77363Z","shell.execute_reply.started":"2022-04-18T19:43:03.751536Z","shell.execute_reply":"2022-04-18T19:43:03.766505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.head(20)","metadata":{"execution":{"iopub.status.busy":"2022-04-18T19:43:05.696781Z","iopub.execute_input":"2022-04-18T19:43:05.697189Z","iopub.status.idle":"2022-04-18T19:43:05.720171Z","shell.execute_reply.started":"2022-04-18T19:43:05.697159Z","shell.execute_reply":"2022-04-18T19:43:05.719367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers['fashion_news_frequency']=customers['fashion_news_frequency'].fillna(0.0)\ncustomers['fashion_news_frequency'].replace('NONE','0.0',inplace=True)\ncustomers['fashion_news_frequency'].replace('None','0.0',inplace=True)\ncustomers['fashion_news_frequency'].replace('Monthly','1.0',inplace=True)\ncustomers['fashion_news_frequency'].replace('Regularly','2.0',inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-18T19:38:19.383641Z","iopub.execute_input":"2022-04-18T19:38:19.384181Z","iopub.status.idle":"2022-04-18T19:38:19.405788Z","shell.execute_reply.started":"2022-04-18T19:38:19.384144Z","shell.execute_reply":"2022-04-18T19:38:19.405072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers['fashion_news_frequency']=pd.to_numeric(customers['fashion_news_frequency'],errors='coerce')\ncustomers['fashion_news_frequency']=customers['fashion_news_frequency']/customers['fashion_news_frequency'].max()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T19:38:30.095861Z","iopub.execute_input":"2022-04-18T19:38:30.096436Z","iopub.status.idle":"2022-04-18T19:38:30.12097Z","shell.execute_reply.started":"2022-04-18T19:38:30.096402Z","shell.execute_reply":"2022-04-18T19:38:30.120322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers[['FN','Active']]=customers[['FN','Active']].fillna(0.0)\ncustomers['age']=customers['age'].fillna(customers['age'].median())","metadata":{"execution":{"iopub.status.busy":"2022-04-18T19:38:51.489362Z","iopub.execute_input":"2022-04-18T19:38:51.489685Z","iopub.status.idle":"2022-04-18T19:38:51.587156Z","shell.execute_reply.started":"2022-04-18T19:38:51.48964Z","shell.execute_reply":"2022-04-18T19:38:51.586226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers['age']=customers['age']/customers['age'].max()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T19:39:20.796212Z","iopub.execute_input":"2022-04-18T19:39:20.796633Z","iopub.status.idle":"2022-04-18T19:39:20.809939Z","shell.execute_reply.started":"2022-04-18T19:39:20.796593Z","shell.execute_reply":"2022-04-18T19:39:20.808962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers=reduce_mem_usage(customers)\narticles=reduce_mem_usage(articles)\ntransactions=reduce_mem_usage(transactions)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:33:09.87798Z","iopub.execute_input":"2022-04-12T19:33:09.878982Z","iopub.status.idle":"2022-04-12T19:33:17.401636Z","shell.execute_reply.started":"2022-04-12T19:33:09.878913Z","shell.execute_reply":"2022-04-12T19:33:17.400959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_features(dataframe, features_name, id_col_name):\n    \"\"\"\n    Generate features that will be ready for feeding into lightfm\n\n    Parameters\n    ----------\n    dataframe: Dataframe\n        Pandas Dataframe which contains features\n    features_name : List\n        List of feature columns name avaiable in dataframe\n    id_col_name: String\n        Column name which contains id of the question or\n        answer that the features will map to.\n        There are two possible values for this variable.\n        1. questions_id_num\n        2. professionals_id_num\n\n    Returns\n    -------\n    Pandas Series\n        A pandas series containing process features\n        that are ready for feed into lightfm.\n        The format of each value\n        will be (user_id, ['feature_1', 'feature_2', 'feature_3'])\n        Ex. -> (1, ['military', 'army', '5'])\n    \"\"\"\n\n    features = dataframe[features_name].apply(\n        lambda x: ','.join(x.map(str)), axis=1)\n    features = features.str.split(',')\n    features = list(zip(dataframe[id_col_name], features))\n    return features\n\ndef generate_feature_list(dataframe, features_name):\n    \"\"\"\n    Generate features list for mapping \n\n    Parameters\n    ----------\n    dataframe: Dataframe\n        Pandas Dataframe for Users or Q&A. \n    features_name : List\n        List of feature columns name avaiable in dataframe. \n        \n    Returns\n    -------\n    List of all features for mapping \n    \"\"\"\n    features = dataframe[features_name].apply(\n        lambda x: ','.join(x.map(str)), axis=1)\n    features = features.str.split(',')\n    features = features.apply(pd.Series).stack().reset_index(drop=True)\n    return features\n\n","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:33:17.403383Z","iopub.execute_input":"2022-04-12T19:33:17.403626Z","iopub.status.idle":"2022-04-12T19:33:17.41342Z","shell.execute_reply.started":"2022-04-12T19:33:17.403599Z","shell.execute_reply":"2022-04-12T19:33:17.412163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles['features']=create_features(articles,['product_code','product_type_no','graphical_appearance_no','colour_group_code','perceived_colour_value_id','perceived_colour_master_id','department_no','index_code','index_group_no','section_no','garment_group_no'],'article_id')\ncustomers['features']=create_features(customers,['fashion_news_frequency','club_member_status','age'],'customer_id')","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:33:17.415214Z","iopub.execute_input":"2022-04-12T19:33:17.415657Z","iopub.status.idle":"2022-04-12T19:35:58.425814Z","shell.execute_reply.started":"2022-04-12T19:33:17.415623Z","shell.execute_reply":"2022-04-12T19:35:58.425007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from lightfm.data import Dataset\ndataset=Dataset()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:35:58.428653Z","iopub.execute_input":"2022-04-12T19:35:58.429212Z","iopub.status.idle":"2022-04-12T19:35:59.201533Z","shell.execute_reply.started":"2022-04-12T19:35:58.429164Z","shell.execute_reply":"2022-04-12T19:35:59.200348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles_feature_list=generate_feature_list(articles,['product_code','product_type_no','graphical_appearance_no','colour_group_code','perceived_colour_value_id','perceived_colour_master_id','department_no','index_code','index_group_no','section_no','garment_group_no'])\ncustomers_feature_list=generate_feature_list(customers,['fashion_news_frequency','club_member_status','age'])","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:35:59.202878Z","iopub.execute_input":"2022-04-12T19:35:59.203487Z","iopub.status.idle":"2022-04-12T19:44:00.696525Z","shell.execute_reply.started":"2022-04-12T19:35:59.203447Z","shell.execute_reply":"2022-04-12T19:44:00.695073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.fit(set(customers['customer_id']),set(articles['article_id']),item_features=articles_feature_list,user_features=customers_feature_list)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:44:00.698236Z","iopub.execute_input":"2022-04-12T19:44:00.698493Z","iopub.status.idle":"2022-04-12T19:44:05.468108Z","shell.execute_reply.started":"2022-04-12T19:44:00.698463Z","shell.execute_reply":"2022-04-12T19:44:05.466723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_customers,n_articles=dataset.interactions_shape()\n#print(\"Number of customers: {}, Number of articles: {}\".format(n_customers,n_articles))","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:44:05.469714Z","iopub.execute_input":"2022-04-12T19:44:05.469976Z","iopub.status.idle":"2022-04-12T19:44:05.475381Z","shell.execute_reply.started":"2022-04-12T19:44:05.469946Z","shell.execute_reply":"2022-04-12T19:44:05.474278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions['t_dat']=pd.to_datetime(transactions['t_dat'],yearfirst=True).astype('int64')//10**9","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:44:05.477729Z","iopub.execute_input":"2022-04-12T19:44:05.478031Z","iopub.status.idle":"2022-04-12T19:44:08.078286Z","shell.execute_reply.started":"2022-04-12T19:44:05.477998Z","shell.execute_reply":"2022-04-12T19:44:08.077275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions['t_dat']=(transactions['t_dat']-transactions['t_dat'].min())/transactions['t_dat'].max()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:44:08.079689Z","iopub.execute_input":"2022-04-12T19:44:08.07996Z","iopub.status.idle":"2022-04-12T19:44:08.635612Z","shell.execute_reply.started":"2022-04-12T19:44:08.079929Z","shell.execute_reply":"2022-04-12T19:44:08.634475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"h=transactions.groupby(['customer_id','article_id']).agg(pricefactor=pd.NamedAgg('price','mean'),timesfactor=pd.NamedAgg('t_dat','count'),timefactor=pd.NamedAgg('t_dat','max')).reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T20:30:18.941959Z","iopub.execute_input":"2022-04-12T20:30:18.942252Z","iopub.status.idle":"2022-04-12T20:31:05.784175Z","shell.execute_reply.started":"2022-04-12T20:30:18.942219Z","shell.execute_reply":"2022-04-12T20:31:05.783159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"h.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T20:31:05.785924Z","iopub.execute_input":"2022-04-12T20:31:05.786248Z","iopub.status.idle":"2022-04-12T20:31:05.800561Z","shell.execute_reply.started":"2022-04-12T20:31:05.786196Z","shell.execute_reply":"2022-04-12T20:31:05.799698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rmoutliers(x):\n    if x>5:\n        return 5\n    else:\n        return x\n        ","metadata":{"execution":{"iopub.status.busy":"2022-04-12T20:31:05.803071Z","iopub.execute_input":"2022-04-12T20:31:05.803608Z","iopub.status.idle":"2022-04-12T20:31:05.817374Z","shell.execute_reply.started":"2022-04-12T20:31:05.803541Z","shell.execute_reply":"2022-04-12T20:31:05.816203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"h['timesfactor']=h['timesfactor'].apply(rmoutliers)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T20:31:05.820703Z","iopub.execute_input":"2022-04-12T20:31:05.821061Z","iopub.status.idle":"2022-04-12T20:31:27.455276Z","shell.execute_reply.started":"2022-04-12T20:31:05.821022Z","shell.execute_reply":"2022-04-12T20:31:27.454151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"h['pricefactor'].hist()\n","metadata":{"execution":{"iopub.status.busy":"2022-04-12T20:31:27.456606Z","iopub.execute_input":"2022-04-12T20:31:27.456877Z","iopub.status.idle":"2022-04-12T20:31:31.223379Z","shell.execute_reply.started":"2022-04-12T20:31:27.456846Z","shell.execute_reply":"2022-04-12T20:31:31.222283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"h['timefactor']=h['timefactor']*10","metadata":{"execution":{"iopub.status.busy":"2022-04-12T20:31:31.224821Z","iopub.execute_input":"2022-04-12T20:31:31.225055Z","iopub.status.idle":"2022-04-12T20:31:31.528796Z","shell.execute_reply.started":"2022-04-12T20:31:31.225025Z","shell.execute_reply":"2022-04-12T20:31:31.527794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-04-12T20:32:06.986536Z","iopub.execute_input":"2022-04-12T20:32:06.987241Z","iopub.status.idle":"2022-04-12T20:32:07.037983Z","shell.execute_reply.started":"2022-04-12T20:32:06.987196Z","shell.execute_reply":"2022-04-12T20:32:07.037305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"h['weight']=h['pricefactor']*h['timesfactor']*h['timefactor']\ndel h['pricefactor']\ndel h['timesfactor']\ndel h['timefactor']","metadata":{"execution":{"iopub.status.busy":"2022-04-12T20:31:31.530226Z","iopub.execute_input":"2022-04-12T20:31:31.530461Z","iopub.status.idle":"2022-04-12T20:31:32.038925Z","shell.execute_reply.started":"2022-04-12T20:31:31.530432Z","shell.execute_reply":"2022-04-12T20:31:32.038101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"h['interaction']=list(zip(h.customer_id,h.article_id,h.weight))","metadata":{"execution":{"iopub.status.busy":"2022-04-12T20:29:42.93924Z","iopub.execute_input":"2022-04-12T20:29:42.939999Z","iopub.status.idle":"2022-04-12T20:30:18.939954Z","shell.execute_reply.started":"2022-04-12T20:29:42.939958Z","shell.execute_reply":"2022-04-12T20:30:18.938877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customer_features = dataset.build_user_features(customers['features'])\narticle_features = dataset.build_item_features(articles['features'])","metadata":{"execution":{"iopub.status.busy":"2022-04-09T18:48:22.381736Z","iopub.execute_input":"2022-04-09T18:48:22.382765Z","iopub.status.idle":"2022-04-09T18:48:38.388342Z","shell.execute_reply.started":"2022-04-09T18:48:22.382649Z","shell.execute_reply":"2022-04-09T18:48:38.387461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(interactions, weights) = dataset.build_interactions(h['interaction'])","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-09T18:48:38.389827Z","iopub.execute_input":"2022-04-09T18:48:38.390204Z","iopub.status.idle":"2022-04-09T18:50:09.593843Z","shell.execute_reply.started":"2022-04-09T18:48:38.39012Z","shell.execute_reply":"2022-04-09T18:50:09.592714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"user_mapping,f,item_mapping,r=dataset.mapping()","metadata":{"execution":{"iopub.status.busy":"2022-04-09T18:50:09.595266Z","iopub.execute_input":"2022-04-09T18:50:09.595524Z","iopub.status.idle":"2022-04-09T18:50:09.600178Z","shell.execute_reply.started":"2022-04-09T18:50:09.595489Z","shell.execute_reply":"2022-04-09T18:50:09.59941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\ndel h\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-04-09T18:50:15.49345Z","iopub.execute_input":"2022-04-09T18:50:15.493777Z","iopub.status.idle":"2022-04-09T18:50:18.697669Z","shell.execute_reply.started":"2022-04-09T18:50:15.493743Z","shell.execute_reply":"2022-04-09T18:50:18.696551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from lightfm import LightFM\nmodel = LightFM(\n    max_sampled=13,\n    loss='bpr',\n    learning_schedule='adadelta',\n    random_state=639)\n\n# Fitting to combined dataset with pure collaborative filtering result\nstart = time.time() \nmodel.fit(interactions,\n                                user_features=customer_features, \n                                item_features=article_features, \n                                sample_weight=weights, \n                                epochs=70, \n                                num_threads=4,  verbose=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T18:50:18.700031Z","iopub.execute_input":"2022-04-09T18:50:18.70063Z","iopub.status.idle":"2022-04-09T18:57:26.748901Z","shell.execute_reply.started":"2022-04-09T18:50:18.700577Z","shell.execute_reply":"2022-04-09T18:57:26.74784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hash_func=lambda s: int(hashlib.sha1(s.encode(\"utf-8\")).hexdigest(), 16)%10**10","metadata":{"execution":{"iopub.status.busy":"2022-04-09T18:50:09.663348Z","iopub.status.idle":"2022-04-09T18:50:09.663741Z","shell.execute_reply.started":"2022-04-09T18:50:09.663522Z","shell.execute_reply":"2022-04-09T18:50:09.663553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_art_ids=lambda keys,args: [keys[x] for x in args]","metadata":{"execution":{"iopub.status.busy":"2022-04-09T18:50:09.665029Z","iopub.status.idle":"2022-04-09T18:50:09.665427Z","shell.execute_reply.started":"2022-04-09T18:50:09.665223Z","shell.execute_reply":"2022-04-09T18:50:09.665249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del transactions","metadata":{"execution":{"iopub.status.busy":"2022-04-09T18:50:09.666677Z","iopub.status.idle":"2022-04-09T18:50:09.667054Z","shell.execute_reply.started":"2022-04-09T18:50:09.666849Z","shell.execute_reply":"2022-04-09T18:50:09.666879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"item_internal_ids=np.array(list(item_mapping.values()),dtype=np.int32)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T18:50:09.668278Z","iopub.status.idle":"2022-04-09T18:50:09.668648Z","shell.execute_reply.started":"2022-04-09T18:50:09.668448Z","shell.execute_reply":"2022-04-09T18:50:09.668475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"item_keys=list(item_mapping.keys())","metadata":{"execution":{"iopub.status.busy":"2022-04-09T18:50:09.670631Z","iopub.status.idle":"2022-04-09T18:50:09.67102Z","shell.execute_reply.started":"2022-04-09T18:50:09.670815Z","shell.execute_reply":"2022-04-09T18:50:09.670844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_for_customer(customer_id):\n    return ' '.join('0'+str(elm) for elm in get_art_ids(item_keys,np.argpartition(model.predict(user_mapping[hash_func(customer_id)],item_internal_ids,num_threads=4),-12)[-12:]))","metadata":{"execution":{"iopub.status.busy":"2022-04-09T18:50:09.672107Z","iopub.status.idle":"2022-04-09T18:50:09.672531Z","shell.execute_reply.started":"2022-04-09T18:50:09.672322Z","shell.execute_reply":"2022-04-09T18:50:09.672353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s_submission=pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-04-09T18:50:09.676638Z","iopub.status.idle":"2022-04-09T18:50:09.67805Z","shell.execute_reply.started":"2022-04-09T18:50:09.677845Z","shell.execute_reply":"2022-04-09T18:50:09.677868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s_submission.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T18:50:09.679349Z","iopub.status.idle":"2022-04-09T18:50:09.679744Z","shell.execute_reply.started":"2022-04-09T18:50:09.679528Z","shell.execute_reply":"2022-04-09T18:50:09.679555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s_submission['prediction']=s_submission.apply(lambda r: predict_for_customer(r['customer_id']),axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T18:50:09.68178Z","iopub.status.idle":"2022-04-09T18:50:09.682123Z","shell.execute_reply.started":"2022-04-09T18:50:09.681936Z","shell.execute_reply":"2022-04-09T18:50:09.681955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s_submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-09T18:50:09.683343Z","iopub.status.idle":"2022-04-09T18:50:09.683739Z","shell.execute_reply.started":"2022-04-09T18:50:09.683514Z","shell.execute_reply":"2022-04-09T18:50:09.683543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s_submission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T18:50:09.684971Z","iopub.status.idle":"2022-04-09T18:50:09.685383Z","shell.execute_reply.started":"2022-04-09T18:50:09.685138Z","shell.execute_reply":"2022-04-09T18:50:09.685194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}