{"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 in \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 \"../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# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Data import"},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/facebook-recruiting-iv-human-or-bot/train.csv.zip\")\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = pd.read_csv(\"/kaggle/input/facebook-recruiting-iv-human-or-bot/test.csv.zip\")\ntest.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bids = pd.read_csv(\"/kaggle/input/facebook-recruiting-iv-human-or-bot/bids.csv.zip\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bids.head(50)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bids.sort_values(by='bidder_id' ,ascending = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bids[\"bidder_id\"].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alldata = pd.concat([train,test])\nalldata.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alldata[\"payment_account\"] = alldata[\"payment_account\"].apply(lambda x:x[:5])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alldata.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alldata[\"payment_account\"].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alldata[\"address\"].sort_values().head(30)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alldata[\"address\"] = alldata[\"address\"].apply(lambda x:x[:5])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alldata.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alldata[\"address\"].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x = bids.groupby(\"bidder_id\")[\"bid_id\"].count().reset_index()  # bidder_id를 기준으로 bid_id가 몇번 나왔는지 카운트\nx","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alldata = alldata.merge(x,on = \"bidder_id\",how = \"left\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alldata","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alldata[\"bid_id\"] = alldata[\"bid_id\"].fillna(0)\nalldata","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alldata.isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alldata3 = alldata[alldata[\"bid_id\"]<20000]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\n\nplt.figure(figsize = (20,12))\nsns.boxplot(alldata3[\"outcome\"],alldata3[\"bid_id\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (20,12))\nsns.boxplot(alldata3[\"outcome\"],alldata3[\"device\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"n_unique = bids.groupby(\"bidder_id\")[\"auction\",\"device\",\"country\"].nunique().reset_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alldata = alldata.merge(n_unique,on = \"bidder_id\",how = \"left\")\nalldata.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()\nalldata[\"payment_account\"] = le.fit_transform(alldata[\"payment_account\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alldata.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alldata[\"address\"] = le.fit_transform(alldata[\"address\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alldata.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from datetime import datetime\nbids[\"datetime\"] = bids[\"time\"].apply(lambda x: datetime.fromtimestamp(x/7000000))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bids[\"year\"] = bids[\"datetime\"].dt.year\nbids[\"month\"] = bids[\"datetime\"].dt.month\nbids[\"day\"] = bids[\"datetime\"].dt.day","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"year_mean = bids.groupby(\"bidder_id\")[\"year\"].mean().reset_index()\nmonth_mean = bids.groupby(\"bidder_id\")[\"month\"].mean().reset_index()\nday = bids.groupby(\"bidder_id\")[\"day\"].mean().reset_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alldata = alldata.merge(year_mean, on = \"bidder_id\", how = \"left\")\nalldata = alldata.merge(month_mean, on = \"bidder_id\", how = \"left\")\nalldata = alldata.merge(day, on = \"bidder_id\", how = \"left\")\nalldata.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"time_diff = bids.groupby(\"bidder_id\")[\"time\"].apply(lambda x : x.max() - x.min()).reset_index()\nalldata = alldata.merge(time_diff, on = \"bidder_id\",how = \"left\")\nalldata.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alldata[\"time\"] = alldata[\"time\"]/alldata[\"bid_id\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alldata2 = alldata.drop([\"bidder_id\",\"outcome\"],axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alldata2.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alldata2 = alldata2.fillna(-1)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train2 = alldata2[:len(train)]\ntrain2.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test2 = alldata2[len(train):]\ntest2.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nrf = RandomForestClassifier(n_estimators = 170, max_depth = 7)      # n_estimators : 트리의 갯수 지정\nrf.fit(train2,train[\"outcome\"])                       # train2 데이터에서 count열을 기준으로 모델 학습  \nresult = rf.predict_proba(test2)          ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result = result[:,1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train[\"outcome\"].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub = pd.read_csv(\"/kaggle/input/facebook-recruiting-iv-human-or-bot/sampleSubmission.csv\")\nsub.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub[\"prediction\"] = result\nsub.to_csv(\"sub.csv\",index = False)","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}