{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train = pd.read_csv(\n    '/kaggle/input/riiid-test-answer-prediction/train.csv',\n    usecols=[\n        'timestamp', \n        'user_id', \n        'content_id', \n        'content_type_id',\n        'task_container_id'\n    ],\n       dtype={\n           'timestamp': 'int64',\n           'user_id': 'int32',\n           'content_id': 'int16',\n       }\n)\ntrain =train.sort_values(by=['user_id', 'timestamp'], ascending=True)\nquestions = pd.read_csv('/kaggle/input/riiid-test-answer-prediction/questions.csv')\nlectures = pd.read_csv('/kaggle/input/riiid-test-answer-prediction/lectures.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#userごと、時系列ごとにソートしてます\ntrain","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#必要な行だけnumpy配列で取り出し\ntype_list = train[\"content_type_id\"].values\ncontent_list = train[\"content_id\"].values\nuser_list = train[\"user_id\"].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#userが変わったタイミングでフラグを立てる配列を作成\nuser_change_flg = []\npre_user = 0\nfor i in user_list:\n    user = i\n    if user == pre_user:\n        user_change_flg.append(0)\n    elif user != pre_user:\n        user_change_flg.append(1)\n    pre_user = user","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"user_change_flg","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\"\"\"\n-ざっくり流れ-\ntrainの上から順にcontent_type_idを確認する\nlectureならlecturedには0をappendして、lectureリストにcontent_idをappend\nquestionならlecturedにlectureリストをappend\nuserがかわるごとにlectureリストを空に更新\n\nlectured が　[[],[],[],0,[223],[223],0,[223,454],[],[],0,[451]....]　みたいになる想定\n\"\"\"\n\nlecture_list = []\nlectured = []\n\n#content_type_idを順に走査\nfor i, content_type in enumerate(type_list):\n    #userが変わってたらlectureリスト更新\n    if user_change_flg[i] == 1:\n        lecture_list = []\n    #questionならlecturedにlectureリストappend\n    if content_type == 0:\n        lectured.append(lecture_list)\n    #lectureならlectureリストにcontent_idをappendして、lecturedに0をappend\n    elif content_type == 1:\n        lecture_list.append(content_list[i])\n        lectured.append(0)\n    #進捗確認用\n    if i % 1000000 == 0:\n        print(i)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#0をはさまずにいきなりcontent_idが増えてる 逆に0の後でもcontent_id増えてないし\n#原因がわからん...\nlectured","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#lectured配列をtrainのlectured列に挿入\ntrain[\"lectured\"] = lectured\ntrain","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\"\"\"\nlecture_list = [[]*i for i in range(train[\"user_id_rank\"].max()+1)]\n#trainデータを上から順に確認して\nfor i in range(len(train)):\n    #questionの行だった場合で\n    if train[\"content_type_id\"][i]==0:\n        #回答userがlectureをuserが受けたことがあった場合は\n        if lecture_list[train[\"user_id_rank\"][i]] is not None:\n            #受けたlectureのtag集をlectured列に挿入してください\n            train[\"lectured\"][i] = lecture_list[train[\"user_id_rank\"][i]]\n    #lectureの行だった場合は\n    elif train[\"content_type_id\"][i]==0:\n        #意訳：受けたlectureのtagをlectureリストにappendしてください\n        #直訳：該当行のcontent_idと同じ番号のlecrture_idをもつlecturesのtagをlectureリストのuser_id_rank番目にappendしてください\n        lecturelist[train[\"user_id_rank\"]].append(lectures.loc[lectures[\"lecture_id\"]==train[\"content_id\"][i], \"tag\"])\n    #進捗確認用\n    if i % 100==0:\n        print(i)\n\"\"\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ans.to_csv(\"./train2.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}