{"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 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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 고객의 충성도를 평가하는 대회\n# E-커머스에서 많이 나오는 데이터\n# 고객들의 특징을 잘 파악하여 모델링을 하겠다!\n# 'target'이 정답 칼럼\ntrain = pd.read_csv('/kaggle/input/elo-merchant-category-recommendation/train.csv')\ntrain","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/elo-merchant-category-recommendation/test.csv')\ntest","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"alldata = pd.concat([train,test])\nalldata","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"alldata2 = alldata.drop(columns = ['first_active_month','card_id','target'])\nalldata2","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train2 = alldata2[:len(train)]\ntrain2","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test2 = alldata2[len(train):]\ntest2","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dataframe에서 index을 이용해서 데이터를 접근하는 방법(대괄호 2개를 적자!)\ntrain2.iloc[[0,1,3]]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from lightgbm import LGBMRegressor\nlgbm = LGBMRegressor(n_estimators = 10000)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 진행 중인 대회처럼!\n# K-fold CV(모든 train set을 평가에 이용할 수 있다. 단 시간이 오래 걸린다.)\n# shuffle(= '데이터를 섞는다')\nfrom sklearn.model_selection import KFold\nkf = KFold(n_splits = 5, shuffle = True, random_state = 42)\n\ni = 0\nresult = 0\nfor train_index, valid_index in kf.split(train2):\n    x_train, x_valid = train2.iloc[train_index], train2.iloc[valid_index]\n    y_train, y_valid = train['target'].iloc[train_index], train['target'].iloc[valid_index]\n    lgbm.fit(x_train, y_train, eval_set = (x_valid, y_valid), early_stopping_rounds = 20, verbose = 20)\n    result += lgbm.predict(test2) / 5\n    i = i+1\n    print(f'K-fold: {i}=============================================================')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/elo-merchant-category-recommendation/sample_submission.csv')\nsub","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['target'] = result\nsub","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('210324.csv', index = 0)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}