{"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)\nimport matplotlib.pyplot as plt \nimport seaborn as sns \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","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/cat-in-the-dat-ii/train.csv', index_col=\"id\")\ndata_test = pd.read_csv('/kaggle/input/cat-in-the-dat-ii/test.csv', index_col=\"id\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.shape, data_test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data_test.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#is it cheating or not?\nfull_data = pd.concat([data, data_test], sort=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"full_data.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"full_data.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for col in full_data.columns.drop(\"target\"):\n    full_data[col].fillna(full_data[col].mode()[0], inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"full_data.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"full_data[\"target\"].groupby(full_data[\"ord_2\"]).mean().sort_values()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"full_data[\"bin_0\"].unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_ord_convert_dict(ord_col, target_col):\n    sorted_target_mean = target_col.groupby(ord_col).mean().sort_values()\n    map_dict = {sorted_target_mean.index[x]: x for x in range(len(ord_col.unique()))}\n    return map_dict","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for column_num in range(5):\n    column_name = \"bin_\" + str(column_num)\n    map_dict = get_ord_convert_dict(full_data[column_name], full_data[\"target\"]) \n    print(map_dict)\n    full_data[column_name+\"_converted\"] = full_data[column_name].map(map_dict)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(full_data[\"bin_3\"])\nprint(full_data[\"bin_3_converted\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"full_data[\"nom_0\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"full_data[\"nom_3\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for column_num in range(10):\n    column_name = \"nom_\" + str(column_num)\n    all_cats = full_data[column_name].unique()\n    if len(all_cats) < 10: \n        for cat in all_cats:\n            new_col_name = \"Is\" + \"\".join(cat.split(\" \"))\n            full_data[new_col_name] = full_data[column_name] == cat \n            print(full_data[new_col_name])\n    else:\n        map_dict = get_ord_convert_dict(full_data[column_name], full_data[\"target\"]) \n        full_data[column_name+\"_converted\"] = full_data[column_name].map(map_dict)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"full_data.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for column_num in range(6):\n    column_name = \"ord_\" + str(column_num) \n    map_dict = get_ord_convert_dict(full_data[column_name], full_data[\"target\"]) \n    print(map_dict)\n    full_data[column_name+\"_converted\"] = full_data[column_name].map(map_dict)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"full_data[\"target\"].groupby(full_data[\"ord_4\"]).mean().sort_values()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"full_data[\"day\"].unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"full_data[\"day\"].unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weekdays = [\"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\", \"Sunday\"]\nfor daynum, weekday in enumerate(weekdays, start=1): \n    full_data[\"Is\"+weekday] = full_data[\"day\"] == daynum","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"full_data.loc[:, \"day\":]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"full_data[\"month\"].unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"full_data[\"month\"].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"full_data.groupby(\"month\")[\"target\"].mean().sort_values()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#I will choose later 1hot or label but for now I will do both\n\n#1hot\nmonths = [\"January\", \"February\", \"March\", \"April\", \"May\", \"June\",\n          \"July\", \"August\", \"September\", \"October\", \"November\", \"December\"]\nfor monthnum, month in enumerate(months, start=1):\n    full_data[\"Is\"+month] = full_data[\"month\"] == monthnum\n\n#label \nmap_dict = get_ord_convert_dict(full_data[\"month\"], full_data[\"target\"]) \nprint(map_dict)\nfull_data[\"month_converted\"] = full_data[\"month\"].map(map_dict) + 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"full_data.loc[:, \"month\":]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"full_data.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"to_drop = [full_data.columns[:23]]\nprint(to_drop)\nfull_data.drop(*to_drop, axis=1, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"full_data.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train = full_data[:600000].copy(deep=True)\ny_train = X_train[\"target\"]; X_train.drop(\"target\", axis=1, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train1hot = X_train.drop(\"month_converted\", axis=1) \nX_train1hot","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_trainlabel = X_train.drop([\"Is\"+x for x in months], axis=1) \nX_trainlabel","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler \n\nX_train_scaled = StandardScaler().fit_transform(X_train)\nX_train1hot_scaled = StandardScaler().fit_transform(X_train1hot)\nX_trainlabel_scaled = StandardScaler().fit_transform(X_trainlabel)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import cross_val_score\n\nlinear1hot = LogisticRegression()\nlinear1hot.fit(X_train1hot_scaled, y_train)\nprint(linear1hot.score(X_train1hot_scaled, y_train))\nprint(cross_val_score(linear1hot, X_train1hot_scaled, y_train, scoring=\"roc_auc\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"linearlabel = LogisticRegression()\nlinearlabel.fit(X_trainlabel_scaled, y_train)\nprint(linearlabel.score(X_trainlabel_scaled, y_train))\nprint(cross_val_score(linearlabel, X_trainlabel_scaled, y_train, scoring=\"roc_auc\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#looks like label and 1hot are absolutely identical in this case, then lets delete the one that uses more memory\nprint(X_train1hot_scaled.size)\nprint(X_trainlabel_scaled.size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true},"cell_type":"code","source":"del X_train1hot, X_train1hot_scaled, linear1hot ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_test = full_data[600000:].drop([\"target\"] + [\"Is\"+x for x in months], axis=1)\nX_test_scaled = StandardScaler().fit_transform(X_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = linearlabel.predict(X_test_scaled)\npredictions","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result = pd.DataFrame({\n    \"id\": X_test.index,\n    \"target\": predictions\n})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result.to_csv('cat_submission.csv', index=False)","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}