{"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":"2022-07-14T11:20:57.255787Z","iopub.execute_input":"2022-07-14T11:20:57.256259Z","iopub.status.idle":"2022-07-14T11:20:57.265387Z","shell.execute_reply.started":"2022-07-14T11:20:57.256220Z","shell.execute_reply":"2022-07-14T11:20:57.263901Z"},"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":"2022-07-14T11:20:57.268989Z","iopub.execute_input":"2022-07-14T11:20:57.269348Z","iopub.status.idle":"2022-07-14T11:20:57.960705Z","shell.execute_reply.started":"2022-07-14T11:20:57.269317Z","shell.execute_reply":"2022-07-14T11:20:57.959683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reading Data (EDA and Visualizaiton)\ntrain_df = pd.read_csv(\"../input/tabular-playground-series-may-2022/train.csv\")\ntest_df = pd.read_csv(\"../input/tabular-playground-series-may-2022/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:20:57.962716Z","iopub.execute_input":"2022-07-14T11:20:57.963479Z","iopub.status.idle":"2022-07-14T11:21:14.060097Z","shell.execute_reply.started":"2022-07-14T11:20:57.963436Z","shell.execute_reply":"2022-07-14T11:21:14.059063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:21:14.061491Z","iopub.execute_input":"2022-07-14T11:21:14.061971Z","iopub.status.idle":"2022-07-14T11:21:14.098766Z","shell.execute_reply.started":"2022-07-14T11:21:14.061934Z","shell.execute_reply":"2022-07-14T11:21:14.097702Z"},"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":"2022-07-14T11:21:14.101197Z","iopub.execute_input":"2022-07-14T11:21:14.101535Z","iopub.status.idle":"2022-07-14T11:21:14.202976Z","shell.execute_reply.started":"2022-07-14T11:21:14.101503Z","shell.execute_reply":"2022-07-14T11:21:14.201763Z"},"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":"2022-07-14T11:21:14.204013Z","iopub.execute_input":"2022-07-14T11:21:14.204342Z","iopub.status.idle":"2022-07-14T11:21:14.210608Z","shell.execute_reply.started":"2022-07-14T11:21:14.204314Z","shell.execute_reply":"2022-07-14T11:21:14.209522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preprocessor.fit_transform(X_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:21:14.212184Z","iopub.execute_input":"2022-07-14T11:21:14.212485Z","iopub.status.idle":"2022-07-14T11:21:20.796741Z","shell.execute_reply.started":"2022-07-14T11:21:14.212459Z","shell.execute_reply":"2022-07-14T11:21:20.795269Z"},"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":"2022-07-14T11:21:20.798420Z","iopub.execute_input":"2022-07-14T11:21:20.798779Z","iopub.status.idle":"2022-07-14T11:21:26.588476Z","shell.execute_reply.started":"2022-07-14T11:21:20.798748Z","shell.execute_reply":"2022-07-14T11:21:26.587207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.options.display.max_columns=60","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:21:26.590178Z","iopub.execute_input":"2022-07-14T11:21:26.590944Z","iopub.status.idle":"2022-07-14T11:21:26.595071Z","shell.execute_reply.started":"2022-07-14T11:21:26.590911Z","shell.execute_reply":"2022-07-14T11:21:26.593973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_transformed.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:21:26.596536Z","iopub.execute_input":"2022-07-14T11:21:26.596836Z","iopub.status.idle":"2022-07-14T11:21:26.654451Z","shell.execute_reply.started":"2022-07-14T11:21:26.596810Z","shell.execute_reply":"2022-07-14T11:21:26.653480Z"},"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":"2022-07-14T11:21:26.657755Z","iopub.execute_input":"2022-07-14T11:21:26.658209Z","iopub.status.idle":"2022-07-14T11:21:26.664333Z","shell.execute_reply.started":"2022-07-14T11:21:26.658174Z","shell.execute_reply":"2022-07-14T11:21:26.663158Z"},"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":"2022-07-14T11:21:26.665713Z","iopub.execute_input":"2022-07-14T11:21:26.666141Z","iopub.status.idle":"2022-07-14T11:22:31.494818Z","shell.execute_reply.started":"2022-07-14T11:21:26.666108Z","shell.execute_reply":"2022-07-14T11:22:31.493333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"MAE scores:\\n\", scores)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:22:31.496598Z","iopub.execute_input":"2022-07-14T11:22:31.497432Z","iopub.status.idle":"2022-07-14T11:22:31.506045Z","shell.execute_reply.started":"2022-07-14T11:22:31.497382Z","shell.execute_reply":"2022-07-14T11:22:31.504483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(scores.mean())\n","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:22:31.507998Z","iopub.execute_input":"2022-07-14T11:22:31.508653Z","iopub.status.idle":"2022-07-14T11:22:31.522910Z","shell.execute_reply.started":"2022-07-14T11:22:31.508606Z","shell.execute_reply":"2022-07-14T11:22:31.521300Z"},"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)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:22:31.524899Z","iopub.execute_input":"2022-07-14T11:22:31.525588Z","iopub.status.idle":"2022-07-14T11:22:51.672741Z","shell.execute_reply.started":"2022-07-14T11:22:31.525530Z","shell.execute_reply":"2022-07-14T11:22:51.671336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = mean_absolute_error(y_train, preds)\nprint(score)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:22:51.674622Z","iopub.execute_input":"2022-07-14T11:22:51.675133Z","iopub.status.idle":"2022-07-14T11:22:51.692923Z","shell.execute_reply.started":"2022-07-14T11:22:51.675086Z","shell.execute_reply":"2022-07-14T11:22:51.691167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test=model.predict(test_df)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:22:51.695213Z","iopub.execute_input":"2022-07-14T11:22:51.696206Z","iopub.status.idle":"2022-07-14T11:22:56.078687Z","shell.execute_reply.started":"2022-07-14T11:22:51.696139Z","shell.execute_reply":"2022-07-14T11:22:56.077237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:22:56.080809Z","iopub.execute_input":"2022-07-14T11:22:56.081743Z","iopub.status.idle":"2022-07-14T11:22:56.090105Z","shell.execute_reply.started":"2022-07-14T11:22:56.081687Z","shell.execute_reply":"2022-07-14T11:22:56.088818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_prob=model.predict_proba(test_df)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:22:56.092131Z","iopub.execute_input":"2022-07-14T11:22:56.092978Z","iopub.status.idle":"2022-07-14T11:23:00.465397Z","shell.execute_reply.started":"2022-07-14T11:22:56.092926Z","shell.execute_reply":"2022-07-14T11:23:00.463986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_prob.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:23:00.467429Z","iopub.execute_input":"2022-07-14T11:23:00.468291Z","iopub.status.idle":"2022-07-14T11:23:00.476247Z","shell.execute_reply.started":"2022-07-14T11:23:00.468237Z","shell.execute_reply":"2022-07-14T11:23:00.475062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_prob","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:23:00.478180Z","iopub.execute_input":"2022-07-14T11:23:00.479038Z","iopub.status.idle":"2022-07-14T11:23:00.492536Z","shell.execute_reply.started":"2022-07-14T11:23:00.478982Z","shell.execute_reply":"2022-07-14T11:23:00.491013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub=y_test_prob[:,1]","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:23:00.495038Z","iopub.execute_input":"2022-07-14T11:23:00.496437Z","iopub.status.idle":"2022-07-14T11:23:00.502872Z","shell.execute_reply.started":"2022-07-14T11:23:00.496375Z","shell.execute_reply":"2022-07-14T11:23:00.501451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.score(test_df,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:23:00.507235Z","iopub.execute_input":"2022-07-14T11:23:00.508614Z","iopub.status.idle":"2022-07-14T11:23:04.984713Z","shell.execute_reply.started":"2022-07-14T11:23:00.508547Z","shell.execute_reply":"2022-07-14T11:23:04.983624Z"},"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":"2022-07-14T11:23:04.986158Z","iopub.execute_input":"2022-07-14T11:23:04.986580Z","iopub.status.idle":"2022-07-14T11:23:05.086360Z","shell.execute_reply.started":"2022-07-14T11:23:04.986535Z","shell.execute_reply":"2022-07-14T11:23:05.085101Z"},"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":"2022-07-14T11:23:05.088019Z","iopub.execute_input":"2022-07-14T11:23:05.088870Z","iopub.status.idle":"2022-07-14T11:23:06.709891Z","shell.execute_reply.started":"2022-07-14T11:23:05.088822Z","shell.execute_reply":"2022-07-14T11:23:06.708222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(submission)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:23:06.711869Z","iopub.execute_input":"2022-07-14T11:23:06.712506Z","iopub.status.idle":"2022-07-14T11:23:06.721004Z","shell.execute_reply.started":"2022-07-14T11:23:06.712468Z","shell.execute_reply":"2022-07-14T11:23:06.719705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.id.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T11:23:06.722537Z","iopub.execute_input":"2022-07-14T11:23:06.723495Z","iopub.status.idle":"2022-07-14T11:23:06.734482Z","shell.execute_reply.started":"2022-07-14T11:23:06.723447Z","shell.execute_reply":"2022-07-14T11:23:06.733583Z"},"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":"2022-07-14T11:23:06.735724Z","iopub.execute_input":"2022-07-14T11:23:06.736358Z","iopub.status.idle":"2022-07-14T11:23:06.748092Z","shell.execute_reply.started":"2022-07-14T11:23:06.736322Z","shell.execute_reply":"2022-07-14T11:23:06.746844Z"},"trusted":true},"execution_count":null,"outputs":[]}]}