{"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":"2023-01-09T15:13:30.110817Z","iopub.execute_input":"2023-01-09T15:13:30.112022Z","iopub.status.idle":"2023-01-09T15:13:30.126917Z","shell.execute_reply.started":"2023-01-09T15:13:30.111968Z","shell.execute_reply":"2023-01-09T15:13:30.125779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/emsi-5iir-ds/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/emsi-5iir-ds/test.csv\")\nsubmission = pd.read_csv(\"/kaggle/input/emsi-5iir-ds/sample_submission.csv\")\n#The shape of a DataFrame is a tuple of array dimensions that tells\n#the number of rows and columns of a given DataFrame.\nprint(\"Train Set:\", train.shape, \"Test Set:\", test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-01-09T15:13:30.128565Z","iopub.execute_input":"2023-01-09T15:13:30.128908Z","iopub.status.idle":"2023-01-09T15:13:38.061528Z","shell.execute_reply.started":"2023-01-09T15:13:30.128878Z","shell.execute_reply":"2023-01-09T15:13:38.060402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(train.head())\ndisplay(test.head())\ndisplay(submission.head())","metadata":{"execution":{"iopub.status.busy":"2023-01-09T15:13:38.062980Z","iopub.execute_input":"2023-01-09T15:13:38.063749Z","iopub.status.idle":"2023-01-09T15:13:38.116896Z","shell.execute_reply.started":"2023-01-09T15:13:38.063716Z","shell.execute_reply":"2023-01-09T15:13:38.115950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print(train.dtypes)\ndisplay(train.corr())\ntrain.select_dtypes(['float64']).head()\ntrain.select_dtypes(['int64']).head()","metadata":{"execution":{"iopub.status.busy":"2023-01-09T15:13:38.118671Z","iopub.execute_input":"2023-01-09T15:13:38.119284Z","iopub.status.idle":"2023-01-09T15:13:40.861700Z","shell.execute_reply.started":"2023-01-09T15:13:38.119248Z","shell.execute_reply":"2023-01-09T15:13:40.860571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#The info() method prints information about the DataFrame.\ntrain.info()","metadata":{"execution":{"iopub.status.busy":"2023-01-09T15:13:40.862946Z","iopub.execute_input":"2023-01-09T15:13:40.863464Z","iopub.status.idle":"2023-01-09T15:13:40.969813Z","shell.execute_reply.started":"2023-01-09T15:13:40.863431Z","shell.execute_reply":"2023-01-09T15:13:40.968715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()\n#The target is binary. With 51 % zeros and 49 % ones, it is almost balanced\n(train.target.value_counts() / len(train)).round(2)","metadata":{"execution":{"iopub.status.busy":"2023-01-09T15:13:40.970994Z","iopub.execute_input":"2023-01-09T15:13:40.971393Z","iopub.status.idle":"2023-01-09T15:13:41.065628Z","shell.execute_reply.started":"2023-01-09T15:13:40.971363Z","shell.execute_reply":"2023-01-09T15:13:41.064278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#describe is for display statistics for each column max value, min value...\ntrain.describe()","metadata":{"execution":{"iopub.status.busy":"2023-01-09T15:13:41.068541Z","iopub.execute_input":"2023-01-09T15:13:41.069245Z","iopub.status.idle":"2023-01-09T15:13:42.217871Z","shell.execute_reply.started":"2023-01-09T15:13:41.069212Z","shell.execute_reply":"2023-01-09T15:13:42.216825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sb","metadata":{"execution":{"iopub.status.busy":"2023-01-09T15:13:42.219073Z","iopub.execute_input":"2023-01-09T15:13:42.219774Z","iopub.status.idle":"2023-01-09T15:13:42.225959Z","shell.execute_reply.started":"2023-01-09T15:13:42.219728Z","shell.execute_reply":"2023-01-09T15:13:42.224520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfloat_features = [f for f in train.columns if train[f].dtype == 'float64']\nint_features = [f for f in train.columns if train[f].dtype == 'int64' and f != 'id']\nobject_features = list(train.select_dtypes(include='object').columns)","metadata":{"execution":{"iopub.status.busy":"2023-01-09T15:13:42.230655Z","iopub.execute_input":"2023-01-09T15:13:42.231010Z","iopub.status.idle":"2023-01-09T15:13:42.258922Z","shell.execute_reply.started":"2023-01-09T15:13:42.230979Z","shell.execute_reply":"2023-01-09T15:13:42.257972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Correlation matrix of the float features\n#there is no strong correlation between float features and target column\nplt.figure(figsize=(12, 12))\nsb.heatmap(train[float_features + ['target']].corr(), center=0, annot=True, fmt='.2f')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-09T15:13:42.260115Z","iopub.execute_input":"2023-01-09T15:13:42.260695Z","iopub.status.idle":"2023-01-09T15:13:44.463220Z","shell.execute_reply.started":"2023-01-09T15:13:42.260661Z","shell.execute_reply":"2023-01-09T15:13:44.462094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib.ticker import MaxNLocator\nfigure = plt.figure(figsize=(16, 16))","metadata":{"execution":{"iopub.status.busy":"2023-01-09T15:13:44.464787Z","iopub.execute_input":"2023-01-09T15:13:44.465749Z","iopub.status.idle":"2023-01-09T15:13:44.474888Z","shell.execute_reply.started":"2023-01-09T15:13:44.465708Z","shell.execute_reply":"2023-01-09T15:13:44.473677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 12))\nsb.heatmap(train[int_features + ['target']].corr(), center=0, annot=True, fmt='.2f')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-09T15:13:44.476307Z","iopub.execute_input":"2023-01-09T15:13:44.477142Z","iopub.status.idle":"2023-01-09T15:13:46.491657Z","shell.execute_reply.started":"2023-01-09T15:13:44.477102Z","shell.execute_reply":"2023-01-09T15:13:46.490536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#string column always has length 10\ntrain.f_27.str.len().min(), train.f_27.str.len().max(), test.f_27.str.len().min(), test.f_27.str.len().max()","metadata":{"execution":{"iopub.status.busy":"2023-01-09T15:13:46.492914Z","iopub.execute_input":"2023-01-09T15:13:46.493300Z","iopub.status.idle":"2023-01-09T15:13:47.603514Z","shell.execute_reply.started":"2023-01-09T15:13:46.493267Z","shell.execute_reply":"2023-01-09T15:13:47.602535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.f_27.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-01-09T15:13:47.604753Z","iopub.execute_input":"2023-01-09T15:13:47.605059Z","iopub.status.idle":"2023-01-09T15:13:48.470954Z","shell.execute_reply.started":"2023-01-09T15:13:47.605023Z","shell.execute_reply":"2023-01-09T15:13:48.470110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.concat([train, test]).f_27.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-01-09T15:13:48.474266Z","iopub.execute_input":"2023-01-09T15:13:48.475374Z","iopub.status.idle":"2023-01-09T15:13:50.308044Z","shell.execute_reply.started":"2023-01-09T15:13:48.475327Z","shell.execute_reply":"2023-01-09T15:13:50.306883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#binary positions position0 & position2 & position5\nfor i in range(10):\n    print(f'Position {i}')\n    tg = train.groupby(train.f_27.str.get(i))\n    temp = pd.DataFrame({'size': tg.size(), 'probability': tg.target.mean().round(2)})\n    print(temp)\n    print()","metadata":{"execution":{"iopub.status.busy":"2023-01-09T15:13:50.309891Z","iopub.execute_input":"2023-01-09T15:13:50.310326Z","iopub.status.idle":"2023-01-09T15:13:56.130807Z","shell.execute_reply.started":"2023-01-09T15:13:50.310288Z","shell.execute_reply":"2023-01-09T15:13:56.129488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#we can see that the target probability depends on the unique character count\nunique_characters = train.f_27.apply(lambda s: len(set(s))).rename('unique_characters')\ntg = train.groupby(unique_characters)\ntemp = pd.DataFrame({'size': tg.size(), 'probability': tg.target.mean().round(2)})\nprint(temp)","metadata":{"execution":{"iopub.status.busy":"2023-01-09T15:13:56.132048Z","iopub.execute_input":"2023-01-09T15:13:56.132409Z","iopub.status.idle":"2023-01-09T15:13:57.016136Z","shell.execute_reply.started":"2023-01-09T15:13:56.132329Z","shell.execute_reply":"2023-01-09T15:13:57.015013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 12))\ntrain['f_27'].value_counts()[:50].plot(kind='bar') #shows the top 50 common values\nplt.title('f_27 Top 50 Most Common Values', {'size': '32'}) #Adds title","metadata":{"execution":{"iopub.status.busy":"2023-01-09T15:13:57.018577Z","iopub.execute_input":"2023-01-09T15:13:57.019001Z","iopub.status.idle":"2023-01-09T15:13:58.923593Z","shell.execute_reply.started":"2023-01-09T15:13:57.018959Z","shell.execute_reply":"2023-01-09T15:13:58.922680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/emsi-5iir-ds/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/emsi-5iir-ds/test.csv\")\n\nfloat_features = [f for f in train.columns if train[f].dtype == 'float64']\nint_features = [f for f in train.columns if train[f].dtype == 'int64' and f != 'id']\n\ntrain = train.drop(\"id\",axis = 1)\ntarget = train.pop(\"target\")\ndisplay(train.head(), target.head())\n\n# f_27 label encoding\nfor i in range(10):\n    train[\"f_27_{}\".format(i+1)] = train[\"f_27\"].astype(str).apply(lambda x: x[i:i+1])\n    test[\"f_27_{}\".format(i+1)] = test[\"f_27\"].astype(str).apply(lambda x: x[i:i+1])\ndisplay(train.head(),test.head())\n\n# f_27 label encoding\nfrom sklearn.preprocessing import LabelEncoder\n\nfor i in range(10):\n    le = LabelEncoder()\n    train[\"f_27_{}\".format(i+1)] = le.fit_transform(train[\"f_27_{}\".format(i+1)])\n    \n    for label in np.unique(test[\"f_27_{}\".format(i+1)]):\n        if label not in le.classes_: # unseen label \n            le.classes_ = np.append(le.classes_, label)\n            \n    test[\"f_27_{}\".format(i+1)] = le.transform(test[\"f_27_{}\".format(i+1)])\n    \ndisplay(train.head(),test.head())","metadata":{"execution":{"iopub.status.busy":"2023-01-09T15:13:58.924987Z","iopub.execute_input":"2023-01-09T15:13:58.925312Z","iopub.status.idle":"2023-01-09T15:14:20.078392Z","shell.execute_reply.started":"2023-01-09T15:13:58.925284Z","shell.execute_reply":"2023-01-09T15:14:20.077332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.drop(\"id\",axis = 1)\ndisplay(test.head())","metadata":{"execution":{"iopub.status.busy":"2023-01-09T15:14:20.079796Z","iopub.execute_input":"2023-01-09T15:14:20.080120Z","iopub.status.idle":"2023-01-09T15:14:20.281501Z","shell.execute_reply.started":"2023-01-09T15:14:20.080090Z","shell.execute_reply":"2023-01-09T15:14:20.280411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.drop(\"f_27\",axis = 1)\ntest = test.drop(\"f_27\",axis = 1)","metadata":{"execution":{"iopub.status.busy":"2023-01-09T15:14:20.282833Z","iopub.execute_input":"2023-01-09T15:14:20.283169Z","iopub.status.idle":"2023-01-09T15:14:20.692775Z","shell.execute_reply.started":"2023-01-09T15:14:20.283139Z","shell.execute_reply":"2023-01-09T15:14:20.691534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from catboost  import CatBoostClassifier\n\nmodel = CatBoostClassifier(n_estimators = 500, learning_rate = 0.05, random_state=43)\nmodel.fit(train, target)\n\ncat_pred = model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2023-01-09T15:14:20.701709Z","iopub.execute_input":"2023-01-09T15:14:20.702137Z","iopub.status.idle":"2023-01-09T15:15:52.880975Z","shell.execute_reply.started":"2023-01-09T15:14:20.702097Z","shell.execute_reply":"2023-01-09T15:15:52.879773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission[\"target\"] = cat_pred\nsubmission","metadata":{"execution":{"iopub.status.busy":"2023-01-09T15:15:52.885407Z","iopub.execute_input":"2023-01-09T15:15:52.885757Z","iopub.status.idle":"2023-01-09T15:15:52.898294Z","shell.execute_reply.started":"2023-01-09T15:15:52.885726Z","shell.execute_reply":"2023-01-09T15:15:52.897089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submissionF.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2023-01-09T15:15:52.899818Z","iopub.execute_input":"2023-01-09T15:15:52.900169Z","iopub.status.idle":"2023-01-09T15:15:53.624165Z","shell.execute_reply.started":"2023-01-09T15:15:52.900138Z","shell.execute_reply":"2023-01-09T15:15:53.623176Z"},"trusted":true},"execution_count":null,"outputs":[]}]}