{"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":"# 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 5GB 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\n\nfrom matplotlib import pyplot as plt\nimport seaborn as sns\nimport gzip\n\ntest_file = '../input/avazu-ctr-prediction/test.gz'\nsamplesubmision_file = '../input/avazu-ctr-prediction/sampleSubmission.gz'","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-31T04:34:29.937557Z","iopub.execute_input":"2023-10-31T04:34:29.937874Z","iopub.status.idle":"2023-10-31T04:34:30.654691Z","shell.execute_reply.started":"2023-10-31T04:34:29.937846Z","shell.execute_reply":"2023-10-31T04:34:30.653868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"由於文件的Row數量太過龐大 （超過4千萬筆） ，Pandas DataFrame無法讀取這麼大的資料量，因此資料以每100萬筆為一批次分批讀取，並且從每個批次的資料中，隨機抽樣5％的資料，集成一個新的train set。","metadata":{}},{"cell_type":"code","source":"chunksize = 10 ** 6\nnum_of_chunk = 0\ntrain = pd.DataFrame()\n    \nfor chunk in pd.read_csv('../input/avazu-ctr-train/train.csv', chunksize=chunksize):\n    num_of_chunk += 1\n    train = pd.concat([train, chunk.sample(frac=.05, replace=False, random_state=123)], axis=0)\n    print('Processing Chunk No. ' + str(num_of_chunk))     \n    \ntrain.reset_index(inplace=True)\n\n# 備份train 資料長度，以便稍後df重新分割索引用途\ntrain_len = len(train)\ntrain_len","metadata":{"execution":{"iopub.status.busy":"2023-10-31T04:34:30.656549Z","iopub.execute_input":"2023-10-31T04:34:30.656820Z","iopub.status.idle":"2023-10-31T04:37:52.067671Z","shell.execute_reply.started":"2023-10-31T04:34:30.656793Z","shell.execute_reply":"2023-10-31T04:37:52.066893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"再將test資料集的資料讀出，並把train, test 合併成一個df，以便同時進行資料預處理。","metadata":{}},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-31T04:56:33.328990Z","iopub.execute_input":"2023-10-31T04:56:33.329486Z","iopub.status.idle":"2023-10-31T04:56:33.359131Z","shell.execute_reply.started":"2023-10-31T04:56:33.329439Z","shell.execute_reply":"2023-10-31T04:56:33.358358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.unique(train.id.values).shape","metadata":{"execution":{"iopub.status.busy":"2023-10-31T05:00:52.082694Z","iopub.execute_input":"2023-10-31T05:00:52.083036Z","iopub.status.idle":"2023-10-31T05:00:52.288794Z","shell.execute_reply.started":"2023-10-31T05:00:52.083007Z","shell.execute_reply":"2023-10-31T05:00:52.287716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2023-10-31T04:58:34.522721Z","iopub.execute_input":"2023-10-31T04:58:34.523050Z","iopub.status.idle":"2023-10-31T04:58:35.540532Z","shell.execute_reply.started":"2023-10-31T04:58:34.523021Z","shell.execute_reply":"2023-10-31T04:58:35.539674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.id.unique().shape","metadata":{"execution":{"iopub.status.busy":"2023-10-31T04:57:11.536331Z","iopub.execute_input":"2023-10-31T04:57:11.536671Z","iopub.status.idle":"2023-10-31T04:57:11.746396Z","shell.execute_reply.started":"2023-10-31T04:57:11.536642Z","shell.execute_reply":"2023-10-31T04:57:11.745378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.index.unique().shape","metadata":{"execution":{"iopub.status.busy":"2023-10-31T04:57:57.972580Z","iopub.execute_input":"2023-10-31T04:57:57.972923Z","iopub.status.idle":"2023-10-31T04:57:58.060523Z","shell.execute_reply.started":"2023-10-31T04:57:57.972894Z","shell.execute_reply":"2023-10-31T04:57:58.059490Z"},"trusted":true},"execution_count":null,"outputs":[]}]}