{"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-07T20:43:33.037314Z","iopub.execute_input":"2023-01-07T20:43:33.038104Z","iopub.status.idle":"2023-01-07T20:43:33.073086Z","shell.execute_reply.started":"2023-01-07T20:43:33.037972Z","shell.execute_reply":"2023-01-07T20:43:33.072209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.feature_extraction.text import CountVectorizer\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.pipeline import Pipeline,make_pipeline\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import KFold\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn import svm\nfrom sklearn.metrics import mean_absolute_error","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:43:56.996369Z","iopub.execute_input":"2023-01-07T20:43:56.996836Z","iopub.status.idle":"2023-01-07T20:43:57.004653Z","shell.execute_reply.started":"2023-01-07T20:43:56.996801Z","shell.execute_reply":"2023-01-07T20:43:57.003304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reading Data (EDA and Visualizaiton)\ntrain_df = pd.read_csv(\"/kaggle/input/emsi-5iir-ds/train.csv\")\ntest_df = pd.read_csv(\"/kaggle/input/emsi-5iir-ds/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:44:17.726859Z","iopub.execute_input":"2023-01-07T20:44:17.727321Z","iopub.status.idle":"2023-01-07T20:44:36.765127Z","shell.execute_reply.started":"2023-01-07T20:44:17.727281Z","shell.execute_reply":"2023-01-07T20:44:36.763356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Size of the train_df :\",train_df.shape[0])\nprint(\"Size of the test_df: \",test_df.shape[0])","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:44:36.779337Z","iopub.execute_input":"2023-01-07T20:44:36.779800Z","iopub.status.idle":"2023-01-07T20:44:36.800242Z","shell.execute_reply.started":"2023-01-07T20:44:36.779763Z","shell.execute_reply":"2023-01-07T20:44:36.798713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:44:44.605705Z","iopub.execute_input":"2023-01-07T20:44:44.606143Z","iopub.status.idle":"2023-01-07T20:44:44.654251Z","shell.execute_reply.started":"2023-01-07T20:44:44.606109Z","shell.execute_reply":"2023-01-07T20:44:44.652932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## check null values\ntrain_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:45:00.988071Z","iopub.execute_input":"2023-01-07T20:45:00.988581Z","iopub.status.idle":"2023-01-07T20:45:01.100996Z","shell.execute_reply.started":"2023-01-07T20:45:00.988514Z","shell.execute_reply":"2023-01-07T20:45:01.099662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Total columns are \",train_df.shape[1])\nnum_cols = train_df.select_dtypes([\"int\",\"float\"])\nprint(\"Number of numerical columns are : \",num_cols.shape[1])\ncat_cols = train_df.select_dtypes([\"object\"])\nprint(\"Number of categorical columns are : \",cat_cols.shape[1])","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:45:11.475483Z","iopub.execute_input":"2023-01-07T20:45:11.476973Z","iopub.status.idle":"2023-01-07T20:45:11.576001Z","shell.execute_reply.started":"2023-01-07T20:45:11.476913Z","shell.execute_reply":"2023-01-07T20:45:11.574127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n\ntarget = train_df.target\nsns.countplot(x=target.value_counts())\nplt.show() ## data seems balanced","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:45:22.076351Z","iopub.execute_input":"2023-01-07T20:45:22.076791Z","iopub.status.idle":"2023-01-07T20:45:22.544978Z","shell.execute_reply.started":"2023-01-07T20:45:22.076758Z","shell.execute_reply":"2023-01-07T20:45:22.543604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## creating a model with out modification how do it perform ?\n## we can get idea of what to do since it is binary classification problem we could use logistic regression ","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:45:32.765490Z","iopub.execute_input":"2023-01-07T20:45:32.766309Z","iopub.status.idle":"2023-01-07T20:45:32.772078Z","shell.execute_reply.started":"2023-01-07T20:45:32.766256Z","shell.execute_reply":"2023-01-07T20:45:32.770641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=train_df.drop([\"id\",\"target\"],axis=1)\ny_train=train_df[\"target\"]\nX_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:45:47.427864Z","iopub.execute_input":"2023-01-07T20:45:47.428273Z","iopub.status.idle":"2023-01-07T20:45:47.805551Z","shell.execute_reply.started":"2023-01-07T20:45:47.428241Z","shell.execute_reply":"2023-01-07T20:45:47.804039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preprocessor = ColumnTransformer(transformers=[\n(\"scaler\",StandardScaler(),[col for col in X_train.columns if col != \"f_27\"]),\n    (\"countvectorizer\",CountVectorizer(analyzer=\"char\"),\"f_27\")\n])","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:45:58.861072Z","iopub.execute_input":"2023-01-07T20:45:58.861616Z","iopub.status.idle":"2023-01-07T20:45:58.869192Z","shell.execute_reply.started":"2023-01-07T20:45:58.861570Z","shell.execute_reply":"2023-01-07T20:45:58.867663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preprocessor.fit_transform(X_train)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:46:03.505407Z","iopub.execute_input":"2023-01-07T20:46:03.505952Z","iopub.status.idle":"2023-01-07T20:46:11.197749Z","shell.execute_reply.started":"2023-01-07T20:46:03.505890Z","shell.execute_reply":"2023-01-07T20:46:11.195629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_transformed=pd.DataFrame(preprocessor.transform(train_df),columns=preprocessor.get_feature_names_out())\nX_train_transformed.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:46:17.247387Z","iopub.execute_input":"2023-01-07T20:46:17.247821Z","iopub.status.idle":"2023-01-07T20:46:24.258312Z","shell.execute_reply.started":"2023-01-07T20:46:17.247785Z","shell.execute_reply":"2023-01-07T20:46:24.256847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.options.display.max_columns=60","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:46:35.915370Z","iopub.execute_input":"2023-01-07T20:46:35.915804Z","iopub.status.idle":"2023-01-07T20:46:35.922313Z","shell.execute_reply.started":"2023-01-07T20:46:35.915768Z","shell.execute_reply":"2023-01-07T20:46:35.920730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_transformed.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:46:38.345252Z","iopub.execute_input":"2023-01-07T20:46:38.346354Z","iopub.status.idle":"2023-01-07T20:46:38.417163Z","shell.execute_reply.started":"2023-01-07T20:46:38.346306Z","shell.execute_reply":"2023-01-07T20:46:38.415882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Pipeline([(\"preprocessor\",ColumnTransformer([\n(\"scaler\",StandardScaler(),[col for col in X_train.columns if col != \"f_27\"]),\n    (\"countvectorizer\",CountVectorizer(analyzer=\"char\"),\"f_27\")\n])),\n                 (\"estimator\",LogisticRegression()) \n                 ])","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:46:54.566264Z","iopub.execute_input":"2023-01-07T20:46:54.566742Z","iopub.status.idle":"2023-01-07T20:46:54.574563Z","shell.execute_reply.started":"2023-01-07T20:46:54.566700Z","shell.execute_reply":"2023-01-07T20:46:54.573003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## DO ALL OF THIS INSIDE A CROSSVALIDATION LOOP\n### This part does not runclf = svm.SVC(kernel='linear', C=1).fit(X_train_transformed, Y_train)\nscores = -1 * cross_val_score(model,X_train, y_train,\n                              cv=5,\n                              scoring='neg_mean_absolute_error')","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:48:14.418882Z","iopub.execute_input":"2023-01-07T20:48:14.419600Z","iopub.status.idle":"2023-01-07T20:49:28.638780Z","shell.execute_reply.started":"2023-01-07T20:48:14.419558Z","shell.execute_reply":"2023-01-07T20:49:28.636857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"MAE scores:\\n\", scores)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:53:09.558945Z","iopub.execute_input":"2023-01-07T20:53:09.559814Z","iopub.status.idle":"2023-01-07T20:53:09.566786Z","shell.execute_reply.started":"2023-01-07T20:53:09.559769Z","shell.execute_reply":"2023-01-07T20:53:09.565461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(scores.mean())","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:53:49.997463Z","iopub.execute_input":"2023-01-07T20:53:49.998491Z","iopub.status.idle":"2023-01-07T20:53:50.005751Z","shell.execute_reply.started":"2023-01-07T20:53:49.998434Z","shell.execute_reply":"2023-01-07T20:53:50.004634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### FIT THE DATA ON TRAIN SPLIT AND EVALUATIE ON TEST SPLIT BASED ON COMPETETION METRICS\nmodel.fit(X_train,y_train)\npreds= model.predict(X_train)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:53:58.786729Z","iopub.execute_input":"2023-01-07T20:53:58.787133Z","iopub.status.idle":"2023-01-07T20:54:23.724184Z","shell.execute_reply.started":"2023-01-07T20:53:58.787103Z","shell.execute_reply":"2023-01-07T20:54:23.722845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = mean_absolute_error(y_train, preds)\nprint(score)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:55:12.673529Z","iopub.execute_input":"2023-01-07T20:55:12.674413Z","iopub.status.idle":"2023-01-07T20:55:12.686325Z","shell.execute_reply.started":"2023-01-07T20:55:12.674362Z","shell.execute_reply":"2023-01-07T20:55:12.684957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test=model.predict(test_df)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:55:23.568029Z","iopub.execute_input":"2023-01-07T20:55:23.568459Z","iopub.status.idle":"2023-01-07T20:55:29.162255Z","shell.execute_reply.started":"2023-01-07T20:55:23.568418Z","shell.execute_reply":"2023-01-07T20:55:29.159424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:55:31.445500Z","iopub.execute_input":"2023-01-07T20:55:31.445958Z","iopub.status.idle":"2023-01-07T20:55:31.452789Z","shell.execute_reply.started":"2023-01-07T20:55:31.445919Z","shell.execute_reply":"2023-01-07T20:55:31.451784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_prob=model.predict_proba(test_df)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:56:36.643844Z","iopub.execute_input":"2023-01-07T20:56:36.644299Z","iopub.status.idle":"2023-01-07T20:56:42.313401Z","shell.execute_reply.started":"2023-01-07T20:56:36.644259Z","shell.execute_reply":"2023-01-07T20:56:42.311669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_prob.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:56:50.615602Z","iopub.execute_input":"2023-01-07T20:56:50.616033Z","iopub.status.idle":"2023-01-07T20:56:50.624754Z","shell.execute_reply.started":"2023-01-07T20:56:50.616000Z","shell.execute_reply":"2023-01-07T20:56:50.623130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_prob=model.predict_proba(test_df)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:56:59.987620Z","iopub.execute_input":"2023-01-07T20:56:59.988126Z","iopub.status.idle":"2023-01-07T20:57:05.466410Z","shell.execute_reply.started":"2023-01-07T20:56:59.988082Z","shell.execute_reply":"2023-01-07T20:57:05.464662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_prob","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:57:30.696825Z","iopub.execute_input":"2023-01-07T20:57:30.697225Z","iopub.status.idle":"2023-01-07T20:57:30.705871Z","shell.execute_reply.started":"2023-01-07T20:57:30.697194Z","shell.execute_reply":"2023-01-07T20:57:30.704437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub=y_test_prob[:,1]","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:57:44.685241Z","iopub.execute_input":"2023-01-07T20:57:44.685822Z","iopub.status.idle":"2023-01-07T20:57:44.691213Z","shell.execute_reply.started":"2023-01-07T20:57:44.685774Z","shell.execute_reply":"2023-01-07T20:57:44.690032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.score(test_df,y_test)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:57:50.988140Z","iopub.execute_input":"2023-01-07T20:57:50.988632Z","iopub.status.idle":"2023-01-07T20:57:56.509313Z","shell.execute_reply.started":"2023-01-07T20:57:50.988579Z","shell.execute_reply":"2023-01-07T20:57:56.508067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"id\",\"target\")\nout = pd.DataFrame(test_df[\"id\"],sub)\nprint(test_df[\"id\"],sub)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:58:01.815472Z","iopub.execute_input":"2023-01-07T20:58:01.815915Z","iopub.status.idle":"2023-01-07T20:58:01.916087Z","shell.execute_reply.started":"2023-01-07T20:58:01.815880Z","shell.execute_reply":"2023-01-07T20:58:01.914584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'id': test_df.id,\n                           'target': sub})\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:58:19.106603Z","iopub.execute_input":"2023-01-07T20:58:19.107055Z","iopub.status.idle":"2023-01-07T20:58:20.716282Z","shell.execute_reply.started":"2023-01-07T20:58:19.107008Z","shell.execute_reply":"2023-01-07T20:58:20.714994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(submission)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:58:24.015509Z","iopub.execute_input":"2023-01-07T20:58:24.015932Z","iopub.status.idle":"2023-01-07T20:58:24.024830Z","shell.execute_reply.started":"2023-01-07T20:58:24.015899Z","shell.execute_reply":"2023-01-07T20:58:24.023879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.id.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:58:33.854020Z","iopub.execute_input":"2023-01-07T20:58:33.854888Z","iopub.status.idle":"2023-01-07T20:58:33.861960Z","shell.execute_reply.started":"2023-01-07T20:58:33.854842Z","shell.execute_reply":"2023-01-07T20:58:33.861081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Prediciton Probability is the submission\n### Use K-Fold\n### ","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:58:45.088228Z","iopub.execute_input":"2023-01-07T20:58:45.088669Z","iopub.status.idle":"2023-01-07T20:58:45.094207Z","shell.execute_reply.started":"2023-01-07T20:58:45.088633Z","shell.execute_reply":"2023-01-07T20:58:45.092843Z"},"trusted":true},"execution_count":null,"outputs":[]}]}