{"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","execution":{"iopub.status.busy":"2021-11-15T07:08:21.206210Z","iopub.execute_input":"2021-11-15T07:08:21.206539Z","iopub.status.idle":"2021-11-15T07:08:21.219640Z","shell.execute_reply.started":"2021-11-15T07:08:21.206500Z","shell.execute_reply":"2021-11-15T07:08:21.218552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/facebook-recruiting-iv-human-or-bot/train.csv.zip')\nbids_df = pd.read_csv('/kaggle/input/facebook-recruiting-iv-human-or-bot/bids.csv.zip')","metadata":{"execution":{"iopub.status.busy":"2021-11-15T07:08:21.221338Z","iopub.execute_input":"2021-11-15T07:08:21.221636Z","iopub.status.idle":"2021-11-15T07:08:41.578552Z","shell.execute_reply.started":"2021-11-15T07:08:21.221600Z","shell.execute_reply":"2021-11-15T07:08:41.577627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-15T07:08:41.580237Z","iopub.execute_input":"2021-11-15T07:08:41.580499Z","iopub.status.idle":"2021-11-15T07:08:41.593365Z","shell.execute_reply.started":"2021-11-15T07:08:41.580466Z","shell.execute_reply":"2021-11-15T07:08:41.592591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bids_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-15T07:08:41.594365Z","iopub.execute_input":"2021-11-15T07:08:41.595105Z","iopub.status.idle":"2021-11-15T07:08:41.620956Z","shell.execute_reply.started":"2021-11-15T07:08:41.595064Z","shell.execute_reply":"2021-11-15T07:08:41.619743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['outcome'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-11-15T07:08:41.623311Z","iopub.execute_input":"2021-11-15T07:08:41.623542Z","iopub.status.idle":"2021-11-15T07:08:41.632012Z","shell.execute_reply.started":"2021-11-15T07:08:41.623516Z","shell.execute_reply":"2021-11-15T07:08:41.631128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2021-11-15T07:08:41.633609Z","iopub.execute_input":"2021-11-15T07:08:41.634136Z","iopub.status.idle":"2021-11-15T07:08:41.652666Z","shell.execute_reply.started":"2021-11-15T07:08:41.634085Z","shell.execute_reply":"2021-11-15T07:08:41.651788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bids_df.info()","metadata":{"execution":{"iopub.status.busy":"2021-11-15T07:08:41.654211Z","iopub.execute_input":"2021-11-15T07:08:41.654728Z","iopub.status.idle":"2021-11-15T07:08:41.672817Z","shell.execute_reply.started":"2021-11-15T07:08:41.654682Z","shell.execute_reply":"2021-11-15T07:08:41.671910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Merge two tables.","metadata":{}},{"cell_type":"code","source":"train_df.set_index('bidder_id')\nbids_df.set_index('bidder_id')\ndf = pd.merge(train_df, bids_df)\ndf.info()","metadata":{"execution":{"iopub.status.busy":"2021-11-15T07:08:41.674590Z","iopub.execute_input":"2021-11-15T07:08:41.674926Z","iopub.status.idle":"2021-11-15T07:08:49.979604Z","shell.execute_reply.started":"2021-11-15T07:08:41.674881Z","shell.execute_reply":"2021-11-15T07:08:49.978711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop('bid_id', axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-11-15T07:08:49.980909Z","iopub.execute_input":"2021-11-15T07:08:49.981208Z","iopub.status.idle":"2021-11-15T07:08:51.718567Z","shell.execute_reply.started":"2021-11-15T07:08:49.981173Z","shell.execute_reply":"2021-11-15T07:08:51.717825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Top 3 sources of bot","metadata":{}},{"cell_type":"code","source":"mask = df['outcome'] == 1.0\ndf_bot = df[mask]","metadata":{"execution":{"iopub.status.busy":"2021-11-15T07:08:51.719541Z","iopub.execute_input":"2021-11-15T07:08:51.720307Z","iopub.status.idle":"2021-11-15T07:08:51.820558Z","shell.execute_reply.started":"2021-11-15T07:08:51.720262Z","shell.execute_reply":"2021-11-15T07:08:51.819760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\n\nsns.countplot(x=\"country\", data=df_bot, \n             order=df.country.value_counts().iloc[:3].index)","metadata":{"execution":{"iopub.status.busy":"2021-11-15T07:08:51.824454Z","iopub.execute_input":"2021-11-15T07:08:51.824801Z","iopub.status.idle":"2021-11-15T07:08:52.837570Z","shell.execute_reply.started":"2021-11-15T07:08:51.824764Z","shell.execute_reply":"2021-11-15T07:08:52.836867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x=\"ip\", data=df_bot, \n             order=df.ip.value_counts().iloc[:3].index)","metadata":{"execution":{"iopub.status.busy":"2021-11-15T07:08:52.839226Z","iopub.execute_input":"2021-11-15T07:08:52.839683Z","iopub.status.idle":"2021-11-15T07:08:56.682087Z","shell.execute_reply.started":"2021-11-15T07:08:52.839640Z","shell.execute_reply":"2021-11-15T07:08:56.681095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x=\"device\", data=df_bot, \n             order=df.device.value_counts().iloc[:3].index)","metadata":{"execution":{"iopub.status.busy":"2021-11-15T07:08:56.683352Z","iopub.execute_input":"2021-11-15T07:08:56.683609Z","iopub.status.idle":"2021-11-15T07:08:57.767817Z","shell.execute_reply.started":"2021-11-15T07:08:56.683575Z","shell.execute_reply":"2021-11-15T07:08:57.767158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x=\"url\", data=df_bot, \n             order=df.url.value_counts().iloc[:3].index)","metadata":{"execution":{"iopub.status.busy":"2021-11-15T07:08:57.769157Z","iopub.execute_input":"2021-11-15T07:08:57.769677Z","iopub.status.idle":"2021-11-15T07:08:59.871199Z","shell.execute_reply.started":"2021-11-15T07:08:57.769630Z","shell.execute_reply":"2021-11-15T07:08:59.869989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x=\"merchandise\", data=df_bot, \n             order=df.merchandise.value_counts().iloc[:3].index)","metadata":{"execution":{"iopub.status.busy":"2021-11-15T07:08:59.872472Z","iopub.execute_input":"2021-11-15T07:08:59.872800Z","iopub.status.idle":"2021-11-15T07:09:00.865970Z","shell.execute_reply.started":"2021-11-15T07:08:59.872764Z","shell.execute_reply":"2021-11-15T07:09:00.865167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Encode labels","metadata":{}},{"cell_type":"code","source":"target = 'outcome'\ncategorical_features = []\nnumeric_features = []\nfeatures = df.columns.values.tolist()\nfor col in features:\n    if df[col].dtype != 'object': \n        numeric_features.append(col)\n    else:\n        categorical_features.append(col)","metadata":{"execution":{"iopub.status.busy":"2021-11-15T07:09:00.867047Z","iopub.execute_input":"2021-11-15T07:09:00.867263Z","iopub.status.idle":"2021-11-15T07:09:00.873298Z","shell.execute_reply.started":"2021-11-15T07:09:00.867235Z","shell.execute_reply":"2021-11-15T07:09:00.872593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n# Encoding categorical features\nfor col in categorical_features:\n    le = LabelEncoder()\n    le.fit(list(df[col].astype(str).values))\n    df[col] = le.transform(list(df[col].astype(str).values))","metadata":{"execution":{"iopub.status.busy":"2021-11-15T07:09:00.874535Z","iopub.execute_input":"2021-11-15T07:09:00.874861Z","iopub.status.idle":"2021-11-15T07:09:58.952181Z","shell.execute_reply.started":"2021-11-15T07:09:00.874831Z","shell.execute_reply":"2021-11-15T07:09:58.951102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Build Model","metadata":{}},{"cell_type":"code","source":"y = df['outcome']\nX = df.drop('outcome', axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-11-15T07:09:58.953580Z","iopub.execute_input":"2021-11-15T07:09:58.953923Z","iopub.status.idle":"2021-11-15T07:09:59.214405Z","shell.execute_reply.started":"2021-11-15T07:09:58.953877Z","shell.execute_reply":"2021-11-15T07:09:59.213125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2021-11-15T07:09:59.216182Z","iopub.execute_input":"2021-11-15T07:09:59.216834Z","iopub.status.idle":"2021-11-15T07:09:59.987109Z","shell.execute_reply.started":"2021-11-15T07:09:59.216778Z","shell.execute_reply":"2021-11-15T07:09:59.986023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nforest = RandomForestClassifier(n_estimators=5, random_state=2)\nforest.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2021-11-15T07:09:59.988091Z","iopub.execute_input":"2021-11-15T07:09:59.988323Z","iopub.status.idle":"2021-11-15T07:10:30.618383Z","shell.execute_reply.started":"2021-11-15T07:09:59.988294Z","shell.execute_reply":"2021-11-15T07:10:30.617481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Accuracy on training set: {:.3f}\".format(forest.score(X_train, y_train))) \nprint(\"Accuracy on test set: {:.3f}\".format(forest.score(X_test, y_test)))","metadata":{"execution":{"iopub.status.busy":"2021-11-15T07:10:30.619651Z","iopub.execute_input":"2021-11-15T07:10:30.620432Z","iopub.status.idle":"2021-11-15T07:10:32.968475Z","shell.execute_reply.started":"2021-11-15T07:10:30.620387Z","shell.execute_reply":"2021-11-15T07:10:32.967720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = X.columns.values.tolist()\nimportances = forest.feature_importances_\nindices = np.argsort(importances)\n\nimport matplotlib.pyplot as plt\n\nplt.title('Feature Importances')\nplt.barh(range(len(indices)), importances[indices], color='b', align='center')\nplt.yticks(range(len(indices)), [features[i] for i in indices])\nplt.xlabel('Relative Importance')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-15T07:10:32.970227Z","iopub.execute_input":"2021-11-15T07:10:32.971137Z","iopub.status.idle":"2021-11-15T07:10:33.181467Z","shell.execute_reply.started":"2021-11-15T07:10:32.971088Z","shell.execute_reply":"2021-11-15T07:10:33.180133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=forest.predict(X_test)\nfrom sklearn.metrics import confusion_matrix\n\ncm=confusion_matrix(y_test,y_pred)\n\nconf_matrix=pd.DataFrame(data=cm,columns=['Predicted:0','Predicted:1'],index=['Actual:0','Actual:1'])\nplt.figure(figsize = (8,5))\nsns.heatmap(conf_matrix, annot=True,fmt='d',cmap=\"YlGnBu\")\n","metadata":{"execution":{"iopub.status.busy":"2021-11-15T07:10:33.183843Z","iopub.execute_input":"2021-11-15T07:10:33.184463Z","iopub.status.idle":"2021-11-15T07:10:34.752514Z","shell.execute_reply.started":"2021-11-15T07:10:33.184408Z","shell.execute_reply":"2021-11-15T07:10:34.750490Z"},"trusted":true},"execution_count":null,"outputs":[]}]}