{"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":"markdown","source":"# **DATA DOWNLOAD**","metadata":{"_uuid":"34b2abb5-9a38-4f2f-bd49-af2e2f164f86","_cell_guid":"43f2d06d-cab6-4e77-862d-1f0b763901ef","trusted":true}},{"cell_type":"code","source":"import pandas as pd\nimport os\n\nfname_tran ='../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv'\nfname_cus ='../input/h-and-m-personalized-fashion-recommendations/customers.csv'\nfname_article ='../input/h-and-m-personalized-fashion-recommendations/articles.csv'","metadata":{"execution":{"iopub.status.busy":"2022-02-18T01:59:50.202642Z","iopub.execute_input":"2022-02-18T01:59:50.203071Z","iopub.status.idle":"2022-02-18T01:59:50.242723Z","shell.execute_reply.started":"2022-02-18T01:59:50.202939Z","shell.execute_reply":"2022-02-18T01:59:50.242045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv(fname_tran, sep=',')\ndata","metadata":{"_uuid":"57010869-e732-4675-a70d-fc08a26e5a78","_cell_guid":"24114668-4a92-4e8e-9b1b-3085ee2e72a2","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T01:59:50.245034Z","iopub.execute_input":"2022-02-18T01:59:50.245545Z","iopub.status.idle":"2022-02-18T02:00:47.941494Z","shell.execute_reply.started":"2022-02-18T01:59:50.245507Z","shell.execute_reply":"2022-02-18T02:00:47.940758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **PREPROCESS DATA**","metadata":{"_uuid":"9f8b859e-e8c8-45cf-b375-d3820bb43785","_cell_guid":"6a8a4dc5-ad40-42d8-adff-938d8e93214c","trusted":true}},{"cell_type":"code","source":"using_cols = ['customer_id', 'article_id', 'price']\ndata = data[using_cols]\ndata.head(10)","metadata":{"_uuid":"fca9ec6b-98a4-45a5-af42-729ae25fbf42","_cell_guid":"9bed9aba-9695-420f-ba4c-89b195218aa5","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:00:47.942852Z","iopub.execute_input":"2022-02-18T02:00:47.943523Z","iopub.status.idle":"2022-02-18T02:00:48.337407Z","shell.execute_reply.started":"2022-02-18T02:00:47.943482Z","shell.execute_reply":"2022-02-18T02:00:48.336619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# same customer buy same items -> increase count value \ndata.loc[:,'count'] = 1","metadata":{"_uuid":"10132724-12d7-49fc-917d-7067d4312445","_cell_guid":"6f48dbb6-ae1a-44be-86b1-e2a515518cdf","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:00:48.339693Z","iopub.execute_input":"2022-02-18T02:00:48.340149Z","iopub.status.idle":"2022-02-18T02:00:48.409072Z","shell.execute_reply.started":"2022-02-18T02:00:48.34011Z","shell.execute_reply":"2022-02-18T02:00:48.408108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.groupby(['customer_id','article_id'], as_index=False).sum()\n#because price is subordinated to article","metadata":{"_uuid":"b15aa5ca-5379-4cd9-b5c3-836635828fea","_cell_guid":"9d1f3c8a-f4cf-44d9-8898-667cae509645","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:00:48.410792Z","iopub.execute_input":"2022-02-18T02:00:48.411096Z","iopub.status.idle":"2022-02-18T02:01:21.40282Z","shell.execute_reply.started":"2022-02-18T02:00:48.411056Z","shell.execute_reply":"2022-02-18T02:01:21.402003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data","metadata":{"_uuid":"c8360909-b147-43bb-b6cb-1b149c788e43","_cell_guid":"9567d9b0-7062-4eef-99f9-85215b9cca97","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:01:21.404293Z","iopub.execute_input":"2022-02-18T02:01:21.404555Z","iopub.status.idle":"2022-02-18T02:01:21.423833Z","shell.execute_reply.started":"2022-02-18T02:01:21.40452Z","shell.execute_reply":"2022-02-18T02:01:21.422942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"user_unique = data['customer_id'].unique()\narticle_unique = data['article_id'].unique()","metadata":{"_uuid":"a5a5fd35-161f-46c1-a206-226076740444","_cell_guid":"2b7caff6-6ae1-4a97-b437-1e8261c40db5","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:01:21.425216Z","iopub.execute_input":"2022-02-18T02:01:21.426036Z","iopub.status.idle":"2022-02-18T02:01:26.232576Z","shell.execute_reply.started":"2022-02-18T02:01:21.425997Z","shell.execute_reply":"2022-02-18T02:01:26.231777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"user_unique","metadata":{"_uuid":"5ab4f55d-c5ff-4df4-bf94-ad7be218980c","_cell_guid":"71f59825-ae69-41a0-8bdb-568f01bae873","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:01:26.23396Z","iopub.execute_input":"2022-02-18T02:01:26.234209Z","iopub.status.idle":"2022-02-18T02:01:26.240557Z","shell.execute_reply.started":"2022-02-18T02:01:26.234175Z","shell.execute_reply":"2022-02-18T02:01:26.239816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"user_to_idx = {v:k for k,v in enumerate(user_unique)}\narticle_to_idx = {v:k for k,v in enumerate(article_unique)}","metadata":{"_uuid":"7e57532d-5edb-4b2f-88df-be1f1dad5fb0","_cell_guid":"5e93a745-0370-4755-ae3e-3da38b7b2a29","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:01:26.242123Z","iopub.execute_input":"2022-02-18T02:01:26.24262Z","iopub.status.idle":"2022-02-18T02:01:26.848771Z","shell.execute_reply.started":"2022-02-18T02:01:26.242582Z","shell.execute_reply":"2022-02-18T02:01:26.847978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_user_data = data['customer_id'].map(user_to_idx.get).dropna()\n\nif len(temp_user_data) == len(data):  \n    print('no-null')\n    data['customer_id'] = temp_user_data   \nelse:\n    print('detect null')\n\ntemp_artist_data = data['article_id'].map(article_to_idx.get).dropna()\nif len(temp_artist_data) == len(data):\n    print('no-null')\n    data['article_id'] = temp_artist_data\nelse:\n    print('detect null')\n\ndata","metadata":{"_uuid":"0e8c7ff3-5188-47da-8e86-356aa6684702","_cell_guid":"49e4466a-526f-4713-82d0-271aa2341201","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:01:26.852067Z","iopub.execute_input":"2022-02-18T02:01:26.852339Z","iopub.status.idle":"2022-02-18T02:02:00.148551Z","shell.execute_reply.started":"2022-02-18T02:01:26.8523Z","shell.execute_reply":"2022-02-18T02:02:00.147803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ALS","metadata":{}},{"cell_type":"code","source":"from scipy.sparse import csr_matrix\n\nnum_user = data['customer_id'].nunique()\nnum_artist = data['article_id'].nunique()\n\ncsr_data = csr_matrix((data['count'], (data.customer_id, data.article_id)), shape= (num_user, num_artist))\ncsr_data","metadata":{"_uuid":"2b9946d0-e77f-4d6a-bf11-fc898d587767","_cell_guid":"564d30cf-3938-4b8a-841a-05398fe8810d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:02:00.150023Z","iopub.execute_input":"2022-02-18T02:02:00.150411Z","iopub.status.idle":"2022-02-18T02:02:02.070646Z","shell.execute_reply.started":"2022-02-18T02:02:00.150368Z","shell.execute_reply":"2022-02-18T02:02:02.069882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from implicit.als import AlternatingLeastSquares\nimport os\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2022-02-18T02:02:02.072405Z","iopub.execute_input":"2022-02-18T02:02:02.07287Z","iopub.status.idle":"2022-02-18T02:02:02.403093Z","shell.execute_reply.started":"2022-02-18T02:02:02.072829Z","shell.execute_reply":"2022-02-18T02:02:02.402351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.environ['OPENBLAS_NUM_THREADS']='1'\nos.environ['KMP_DUPLICATE_LIB_OK']='True'\nos.environ['MKL_NUM_THREADS']='1'\n#article has 12 feature, customer estimatley has 6 and transaction data has 5\nals_model = AlternatingLeastSquares(factors=360, regularization=0.01, use_gpu=True, iterations=5, dtype=np.float32, calculate_training_loss=True)","metadata":{"_uuid":"36d4e0b2-5b84-42c5-a3a7-a8a389c9a7d8","_cell_guid":"0a85e591-23de-46c5-a722-b9beaf9932d9","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:02:02.404379Z","iopub.execute_input":"2022-02-18T02:02:02.404641Z","iopub.status.idle":"2022-02-18T02:02:02.41053Z","shell.execute_reply.started":"2022-02-18T02:02:02.404607Z","shell.execute_reply":"2022-02-18T02:02:02.409639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"csr_data_transpose = csr_data.T\ncsr_data_transpose","metadata":{"_uuid":"97f08485-6fde-4105-9748-ecfeb06b5fac","_cell_guid":"2f95a170-b2e3-4abd-afde-abe7ac482291","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:02:02.411998Z","iopub.execute_input":"2022-02-18T02:02:02.412448Z","iopub.status.idle":"2022-02-18T02:02:02.423017Z","shell.execute_reply.started":"2022-02-18T02:02:02.412409Z","shell.execute_reply":"2022-02-18T02:02:02.422334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = als_model.fit(csr_data_transpose)","metadata":{"_uuid":"25330bee-2a00-420c-bf79-672e4e33a0be","_cell_guid":"1d2e4831-0dc3-41db-9d0f-dee2c7d12f9b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:02:02.424254Z","iopub.execute_input":"2022-02-18T02:02:02.424708Z","iopub.status.idle":"2022-02-18T02:02:35.098354Z","shell.execute_reply.started":"2022-02-18T02:02:02.424669Z","shell.execute_reply":"2022-02-18T02:02:35.097495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a_user, a_article = user_to_idx['000058a12d5b43e67d225668fa1f8d618c13dc232df0cad8ffe7ad4a1091e318'], article_to_idx[176209023]\na_user_vector, a_article_vector = als_model.user_factors[a_user], als_model.item_factors[a_article]","metadata":{"_uuid":"89e714c6-a060-43b4-bc86-802c2d6c1d86","_cell_guid":"b19cdf0c-e15e-45ab-a65f-5e0c8e34a37d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:02:35.099656Z","iopub.execute_input":"2022-02-18T02:02:35.100036Z","iopub.status.idle":"2022-02-18T02:02:35.105692Z","shell.execute_reply.started":"2022-02-18T02:02:35.099996Z","shell.execute_reply":"2022-02-18T02:02:35.104833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_article = article_to_idx[176209023]\ntest_article_vector = als_model.item_factors[test_article]\nnp.dot(a_user, test_article_vector)","metadata":{"_uuid":"a938fb51-d229-4c1c-9a50-96deebccec7f","_cell_guid":"3c42052b-5b3e-4180-af10-2b3884afce31","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:02:35.107361Z","iopub.execute_input":"2022-02-18T02:02:35.107631Z","iopub.status.idle":"2022-02-18T02:02:35.126376Z","shell.execute_reply.started":"2022-02-18T02:02:35.107594Z","shell.execute_reply":"2022-02-18T02:02:35.125612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a_test_article = 176209023\nmovie_id = article_to_idx[a_test_article]\nsimilar_article = als_model.similar_items(movie_id, N=15)\nsimilar_article","metadata":{"_uuid":"e41cdc01-565b-463f-a9e8-75179f96c9ed","_cell_guid":"cdd9b5c0-13a2-4367-bb25-218379a4bc41","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:02:35.127503Z","iopub.execute_input":"2022-02-18T02:02:35.127856Z","iopub.status.idle":"2022-02-18T02:02:35.231175Z","shell.execute_reply.started":"2022-02-18T02:02:35.127784Z","shell.execute_reply":"2022-02-18T02:02:35.230356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idx_to_article = {v:k for k,v in article_to_idx.items()}\nj = [idx_to_article[i[0]] for i in similar_article]\nprint(j)","metadata":{"_uuid":"803a224e-7af6-48f6-b60f-27edb2f0d812","_cell_guid":"b3d82aee-e389-4cc1-871f-1e8136dfa830","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:02:35.232755Z","iopub.execute_input":"2022-02-18T02:02:35.233304Z","iopub.status.idle":"2022-02-18T02:02:35.258724Z","shell.execute_reply.started":"2022-02-18T02:02:35.233265Z","shell.execute_reply":"2022-02-18T02:02:35.258026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import Image\nnum_sim = 4\nImage(f'../input/h-and-m-personalized-fashion-recommendations/images/0{str(j[num_sim])[:2]}/0{int(j[num_sim])}.jpg' , width = 200)","metadata":{"_uuid":"1bfc4f16-1f2f-47e5-9484-333a37ca19be","_cell_guid":"3fd9b3bb-d08e-486d-bbfc-2b4971f9139e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:02:35.260218Z","iopub.execute_input":"2022-02-18T02:02:35.260698Z","iopub.status.idle":"2022-02-18T02:02:35.293739Z","shell.execute_reply.started":"2022-02-18T02:02:35.260661Z","shell.execute_reply":"2022-02-18T02:02:35.292982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"user = user_to_idx['00000dbacae5abe5e23885899a1fa44253a17956c6d1c3d25f88aa139fdfc657']\n# recommend에서는 user*item CSR Matrix를 받습니다.\narticle_recommended = als_model.recommend(user, csr_data, N=12, filter_already_liked_items=True)\narticle_recommended","metadata":{"_uuid":"b19ff3e5-6e17-4ec1-9e7c-f80da1f58045","_cell_guid":"27d9fa4b-bc68-4d7c-919d-33f2e77ebcf9","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:02:35.294786Z","iopub.execute_input":"2022-02-18T02:02:35.295064Z","iopub.status.idle":"2022-02-18T02:02:35.34254Z","shell.execute_reply.started":"2022-02-18T02:02:35.295029Z","shell.execute_reply":"2022-02-18T02:02:35.341779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"k = [str(idx_to_article[i[0]]) for i in article_recommended]\nprint(' '.join(k))","metadata":{"_uuid":"a00fc248-a93f-4350-94c3-70ab3b7b3149","_cell_guid":"2a11ce42-4a0e-4957-a061-f53b2779dd5c","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:02:35.344083Z","iopub.execute_input":"2022-02-18T02:02:35.344631Z","iopub.status.idle":"2022-02-18T02:02:35.350749Z","shell.execute_reply.started":"2022-02-18T02:02:35.344589Z","shell.execute_reply":"2022-02-18T02:02:35.349976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import Image\nnum = 1\nImage(f'../input/h-and-m-personalized-fashion-recommendations/images/0{str(k[num])[:2]}/0{int(k[num])}.jpg' , width = 200)","metadata":{"_uuid":"eb44952b-96c6-4dad-b5e3-1d5a518d7b3c","_cell_guid":"ac4c10f5-46f9-4a63-a0f2-d300e53d524d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:02:35.352392Z","iopub.execute_input":"2022-02-18T02:02:35.352981Z","iopub.status.idle":"2022-02-18T02:02:35.377748Z","shell.execute_reply.started":"2022-02-18T02:02:35.352933Z","shell.execute_reply":"2022-02-18T02:02:35.37698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **SUBMISSION**","metadata":{"_uuid":"a838615d-ca28-4579-8ebc-b2ca8b5ddfa9","_cell_guid":"706cae9c-ea33-41be-a2c3-e24149c14555","trusted":true}},{"cell_type":"code","source":"data_R = pd.read_csv(fname_tran, sep=',')","metadata":{"execution":{"iopub.status.busy":"2022-02-18T02:02:35.37921Z","iopub.execute_input":"2022-02-18T02:02:35.379738Z","iopub.status.idle":"2022-02-18T02:03:29.094696Z","shell.execute_reply.started":"2022-02-18T02:02:35.379697Z","shell.execute_reply":"2022-02-18T02:03:29.090831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/sample_submission.csv')","metadata":{"_uuid":"6f4521c1-c823-47e0-b33e-5a932139b380","_cell_guid":"cf203c29-ec23-4183-b820-1911c05159b6","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:03:29.100046Z","iopub.execute_input":"2022-02-18T02:03:29.100397Z","iopub.status.idle":"2022-02-18T02:03:33.215737Z","shell.execute_reply.started":"2022-02-18T02:03:29.100358Z","shell.execute_reply":"2022-02-18T02:03:33.214955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_R['customer_id']","metadata":{"_uuid":"d291d913-80fd-4745-862e-b70a8f4ca48e","_cell_guid":"46104e6c-8a4e-4aec-8efd-d17feeedaa69","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:03:33.216905Z","iopub.execute_input":"2022-02-18T02:03:33.21718Z","iopub.status.idle":"2022-02-18T02:03:33.237122Z","shell.execute_reply.started":"2022-02-18T02:03:33.217146Z","shell.execute_reply":"2022-02-18T02:03:33.236325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"users_sub = submission.drop_duplicates(['customer_id'], keep = 'first', ignore_index = True)","metadata":{"_uuid":"fa41b684-c375-4210-85e8-eb6f104a6d51","_cell_guid":"158299f7-ab1a-4007-954d-ea4a6323fa54","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:03:33.23835Z","iopub.execute_input":"2022-02-18T02:03:33.239129Z","iopub.status.idle":"2022-02-18T02:03:33.784694Z","shell.execute_reply.started":"2022-02-18T02:03:33.239086Z","shell.execute_reply":"2022-02-18T02:03:33.783816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"users_sub","metadata":{"_uuid":"ba80232c-1b2b-48c5-b43d-81cd976da9ee","_cell_guid":"88a1b5f0-a46a-47b2-9b81-3fc0da408c34","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:03:33.78946Z","iopub.execute_input":"2022-02-18T02:03:33.789713Z","iopub.status.idle":"2022-02-18T02:03:33.802704Z","shell.execute_reply.started":"2022-02-18T02:03:33.789681Z","shell.execute_reply":"2022-02-18T02:03:33.801908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if users_sub['customer_id'].head(1).to_numpy()[0] in user_to_idx:\n    print(\"contain\")","metadata":{"_uuid":"ed3976b0-7314-46cd-8bb9-2fff059c3c06","_cell_guid":"d74275ac-0b6e-4375-9ea8-a57d3bc8b90a","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:03:33.804483Z","iopub.execute_input":"2022-02-18T02:03:33.804865Z","iopub.status.idle":"2022-02-18T02:03:33.811456Z","shell.execute_reply.started":"2022-02-18T02:03:33.804826Z","shell.execute_reply":"2022-02-18T02:03:33.810467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"users_a = users_sub['customer_id']","metadata":{"_uuid":"4c88aef0-702e-43e8-8add-d38844f363e7","_cell_guid":"f7defb6e-98e5-4eeb-912d-f5a99339e101","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:03:33.812709Z","iopub.execute_input":"2022-02-18T02:03:33.813511Z","iopub.status.idle":"2022-02-18T02:03:33.818073Z","shell.execute_reply.started":"2022-02-18T02:03:33.813473Z","shell.execute_reply":"2022-02-18T02:03:33.817102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nimport tensorflow as tf\n\ndef run():\n    result_list = []\n    num = 0\n    time_count = 0\n\n    for user in users_a:\n        start = time.time()\n\n        if user not in user_to_idx:\n            user_id = 0\n        else:\n            user_id = user_to_idx[user]\n\n        article_recommended = als_model.recommend(user_id, csr_data, N=12, filter_already_liked_items=False)\n        \n        lists = ['0'+str(idx_to_article[i[0]]) for i in article_recommended]\n        recommand_list = ' '.join(lists)\n        \n        result_list.append(recommand_list)\n        end = time.time()\n        time_count = time_count + (end - start)\n\n        if num%1000 == 0 :\n            print ('.' , end = ' ')\n        if num%10000 == 0:\n            time_left = ((len(users_a)-num) / 10000) * time_count / 60\n            print(f\"{time_count:.5f} sec / TIME_LEFT(min): \",time_left)\n            time_count = 0\n        num = num +1\n    return result_list","metadata":{"_uuid":"24a6936c-31aa-401c-a9e4-ea22c031bf74","_cell_guid":"79246972-e516-4411-acf4-9b45b310fb8a","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:03:33.819398Z","iopub.execute_input":"2022-02-18T02:03:33.819901Z","iopub.status.idle":"2022-02-18T02:03:37.966009Z","shell.execute_reply.started":"2022-02-18T02:03:33.819861Z","shell.execute_reply":"2022-02-18T02:03:37.965208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_lists = run()","metadata":{"_uuid":"4c1d3ee5-51e8-4b32-ad95-175225c669f6","_cell_guid":"4c4defca-8a81-4f1d-979a-ed39dc4e81f2","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:03:37.967315Z","iopub.execute_input":"2022-02-18T02:03:37.967576Z","iopub.status.idle":"2022-02-18T02:06:51.663941Z","shell.execute_reply.started":"2022-02-18T02:03:37.967541Z","shell.execute_reply":"2022-02-18T02:06:51.662784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_list = [result_lists[i] if i<len(result_lists) else '0110065001 0110065001 0699080001 0838055001 0351484027 0351484033 0458543009 0715303001 0759871001 0566140001 0351484026 0399223029' for i in range(len(submission))]","metadata":{"_uuid":"93e7b0b2-4af9-41b0-931b-2fc5b6fe26e5","_cell_guid":"1790c5b4-a242-4632-99ad-f37e9bc7424d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:06:51.665149Z","iopub.status.idle":"2022-02-18T02:06:51.666086Z","shell.execute_reply.started":"2022-02-18T02:06:51.665819Z","shell.execute_reply":"2022-02-18T02:06:51.665847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['customer_id'] = users_sub['customer_id']\nsubmission['prediction'] = final_list\nsubmission","metadata":{"_uuid":"a8c9cbc2-7cc2-41ff-ac61-b299231b77b4","_cell_guid":"094658fb-2c93-4697-b77c-ea24c664fe6f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:06:51.667337Z","iopub.status.idle":"2022-02-18T02:06:51.667886Z","shell.execute_reply.started":"2022-02-18T02:06:51.667624Z","shell.execute_reply":"2022-02-18T02:06:51.667649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('prediction.csv', index=False)","metadata":{"_uuid":"b3485e2f-c969-47d6-a2b8-0beb00b5a0d6","_cell_guid":"3ea3f36f-0885-450f-acee-4f4347d74b92","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-18T02:06:51.669407Z","iopub.status.idle":"2022-02-18T02:06:51.670299Z","shell.execute_reply.started":"2022-02-18T02:06:51.670042Z","shell.execute_reply":"2022-02-18T02:06:51.670069Z"},"trusted":true},"execution_count":null,"outputs":[]}]}