{"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":"import os\nimport matplotlib.pyplot as plt\nimport seaborn as sb\nfrom sklearn.preprocessing import StandardScaler\nimport scipy.stats as stats\nfrom sklearn.model_selection import learning_curve\nfrom sklearn.metrics import make_scorer, roc_auc_score\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.svm import SVC\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.model_selection import GridSearchCV\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport seaborn as sns\nimport io \nimport seaborn as sb\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split \nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.preprocessing import MinMaxScaler\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.ensemble import VotingRegressor\nfrom sklearn.svm import SVR\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.model_selection import cross_val_score\n%matplotlib inline\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-02-07T06:12:37.235013Z","iopub.execute_input":"2023-02-07T06:12:37.235484Z","iopub.status.idle":"2023-02-07T06:12:38.555869Z","shell.execute_reply.started":"2023-02-07T06:12:37.235444Z","shell.execute_reply":"2023-02-07T06:12:38.554956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pathlib import Path\npath = Path('../input/creditriskscoringwithlogreg2022')\ndata_devsample = pd.read_csv(path / 'data_devsample.csv')\ndata_to_score = pd.read_csv(path / 'data_to_score.csv')\nprint(\"data_to_score_shape: \"+str(data_to_score.shape)+\" data_devsample_shape: \"+str(data_devsample.shape))\n","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:12:38.560567Z","iopub.execute_input":"2023-02-07T06:12:38.563157Z","iopub.status.idle":"2023-02-07T06:12:44.978125Z","shell.execute_reply.started":"2023-02-07T06:12:38.563120Z","shell.execute_reply":"2023-02-07T06:12:44.977062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in data_devsample.columns:\n    num_nan_vals = len(data_devsample[data_devsample[str(col)].isnull()])\n    num_valed_vals = 80000-num_nan_vals\n    if ((num_nan_vals/num_valed_vals)*100)>=40:\n        data_devsample.drop(str(col),inplace = True ,axis=1)\n        \ndata_devsample.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:12:44.979595Z","iopub.execute_input":"2023-02-07T06:12:44.980092Z","iopub.status.idle":"2023-02-07T06:12:52.256764Z","shell.execute_reply.started":"2023-02-07T06:12:44.980047Z","shell.execute_reply":"2023-02-07T06:12:52.255799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_devsample.head(3)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:12:52.260188Z","iopub.execute_input":"2023-02-07T06:12:52.260674Z","iopub.status.idle":"2023-02-07T06:12:52.290160Z","shell.execute_reply.started":"2023-02-07T06:12:52.260636Z","shell.execute_reply":"2023-02-07T06:12:52.288958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* **Check for Multicollinearity if  any variables is highly correlated with each other** ","metadata":{}},{"cell_type":"code","source":"num_col = data_devsample._get_numeric_data().columns\nto_drop = {}\nfor col in num_col:\n    cor = data_devsample.corr().abs()[data_devsample.corr().abs()[str(col)]>0.70]\n    for corcol in cor.index:\n        if corcol != col  :\n            to_drop[col]=corcol\nto_drop","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:12:52.291621Z","iopub.execute_input":"2023-02-07T06:12:52.292750Z","iopub.status.idle":"2023-02-07T06:14:37.422011Z","shell.execute_reply.started":"2023-02-07T06:12:52.292715Z","shell.execute_reply":"2023-02-07T06:14:37.419837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* **Removing the highly corelated variables**","metadata":{}},{"cell_type":"code","source":"droplist= ['AMT_GOODS_PRICE','FLAG_EMP_PHONE','SK_ID_CURR','REGION_RATING_CLIENT_W_CITY','LIVE_REGION_NOT_WORK_REGION','LIVE_CITY_NOT_WORK_CITY','OBS_60_CNT_SOCIAL_CIRCLE','DEF_60_CNT_SOCIAL_CIRCLE','DAY','AMT_CREDIT']\ndata_devsample.drop(droplist,inplace=True, axis=1)\ndata_devsample.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:37.423477Z","iopub.execute_input":"2023-02-07T06:14:37.424121Z","iopub.status.idle":"2023-02-07T06:14:37.448872Z","shell.execute_reply.started":"2023-02-07T06:14:37.424084Z","shell.execute_reply":"2023-02-07T06:14:37.447774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* **Check for NULL values**","metadata":{}},{"cell_type":"code","source":"num_nulls =data_devsample.isnull().sum().sort_values(ascending = False)\nnullscols = num_nulls[num_nulls >1000].index.tolist()\nnullscols","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:37.450601Z","iopub.execute_input":"2023-02-07T06:14:37.451028Z","iopub.status.idle":"2023-02-07T06:14:37.498718Z","shell.execute_reply.started":"2023-02-07T06:14:37.450988Z","shell.execute_reply":"2023-02-07T06:14:37.497742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_devsample.loc[: ,nullscols].head(5)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:37.500150Z","iopub.execute_input":"2023-02-07T06:14:37.500785Z","iopub.status.idle":"2023-02-07T06:14:37.518925Z","shell.execute_reply.started":"2023-02-07T06:14:37.500747Z","shell.execute_reply":"2023-02-07T06:14:37.517795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_devsample.drop(columns = ['DAYS_BIRTH',\"TIME\"], inplace = True, axis = 1)\ndata_devsample.boxplot(column=['EXT_SOURCE_3'])","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:37.520600Z","iopub.execute_input":"2023-02-07T06:14:37.521026Z","iopub.status.idle":"2023-02-07T06:14:37.814776Z","shell.execute_reply.started":"2023-02-07T06:14:37.520988Z","shell.execute_reply":"2023-02-07T06:14:37.813799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"median_value = data_devsample.EXT_SOURCE_3.median()\ndata_devsample.dropna(subset=['AMT_REQ_CREDIT_BUREAU_DAY','AMT_REQ_CREDIT_BUREAU_YEAR', \n                              'AMT_REQ_CREDIT_BUREAU_QRT', 'AMT_REQ_CREDIT_BUREAU_HOUR',\n                              'AMT_REQ_CREDIT_BUREAU_WEEK', 'AMT_REQ_CREDIT_BUREAU_MON'],inplace = True)\ndata_devsample.fillna(value=median_value, inplace=True)\n","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:37.819175Z","iopub.execute_input":"2023-02-07T06:14:37.820037Z","iopub.status.idle":"2023-02-07T06:14:37.880094Z","shell.execute_reply.started":"2023-02-07T06:14:37.819982Z","shell.execute_reply":"2023-02-07T06:14:37.879158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_devsample.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:37.881655Z","iopub.execute_input":"2023-02-07T06:14:37.882027Z","iopub.status.idle":"2023-02-07T06:14:37.890339Z","shell.execute_reply.started":"2023-02-07T06:14:37.881993Z","shell.execute_reply":"2023-02-07T06:14:37.889190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* **Handling the catagorical variables**","metadata":{}},{"cell_type":"code","source":"catcol = set(data_devsample.columns) - set(num_col)\ndata_devsample[catcol].head(5)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:37.892136Z","iopub.execute_input":"2023-02-07T06:14:37.892476Z","iopub.status.idle":"2023-02-07T06:14:37.923538Z","shell.execute_reply.started":"2023-02-07T06:14:37.892442Z","shell.execute_reply":"2023-02-07T06:14:37.922516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_devsample[catcol].describe()","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:37.925102Z","iopub.execute_input":"2023-02-07T06:14:37.925551Z","iopub.status.idle":"2023-02-07T06:14:38.021948Z","shell.execute_reply.started":"2023-02-07T06:14:37.925513Z","shell.execute_reply":"2023-02-07T06:14:38.020867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_devsample[\"ORGANIZATION_TYPE\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:38.023293Z","iopub.execute_input":"2023-02-07T06:14:38.023716Z","iopub.status.idle":"2023-02-07T06:14:38.037232Z","shell.execute_reply.started":"2023-02-07T06:14:38.023680Z","shell.execute_reply":"2023-02-07T06:14:38.036242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* **Aberrantly there are some mistakes in data entry the is variable names that should be entered under another name that will help reduce the number of features after the encoding**","metadata":{}},{"cell_type":"markdown","source":"\n**Business Entity Type 1&2&3 >> Business**\n\n**Transport: type 1&2&3&4  >> Industry: type 1**\n\n**Mobile & Telecom >> Industry: type 3**\n\n**Construction >> Industry: type 5**\n\n**Services & Hotel & Cleaning & Restaurant >> Industry: type 6**\n\n**Advertising >> Industry: type 7**\n\n**Agriculture >> Industry: type 8**\n\n**School & Kindergarten & University  >>Industry: type 10**\n\n**Trade: type 1&2&3&4&5&6 & Bank & Insurance & Realtor >> Industry: type 11**\n\n**Medicine & Emergency >> Industry: type 12**\n\n**Religion & Culture &  Security & Electricity & Housing & Postal >> Other**\n\n**Legal Services & Security Ministries & Police & Military >> Government**","metadata":{}},{"cell_type":"code","source":"NEW_ORGANIZATION_TYPE = {\n           \"Government\":[\"Legal Services\",\"Security Ministries\",\"Police\",\"Military\",\"Postal\"],\n           \"Other\":[\"Religion\",\"Electricity\",\"Security\",\"Culture\",\"Housing\"],\n           \"Industry: type 12\":[\"Emergency\",\"Medicine\"],\n           \"Industry: type 11\":[\"Realtor\",\"Insurance\",\"Bank\",\"Military\",\"Trade: type 1\",\n                                \"Trade: type 2\",\"Trade: type 3\",\"Trade: type 4\",\"Trade: type 5\",\"Trade: type 6\"],\n           \"Industry: type 10\":[\"School\",\"Kindergarten\",\"University\"],\n           \"Industry: type 8\":[\"Agriculture\"],\n           \"Industry: type 7\":[\"Advertising\"],\n           \"Industry: type 6\":[\"Services\",\"Hotel\",\"Cleaning\",\"Restaurant\"],\n           \"Industry: type 5\":[\"Construction\"],\n           \"Industry: type 3\":[\"Telecom\",\"Mobile\"],\n           \"Industry: type 1\":[\"Transport: type 1\",\"Transport: type 2\",\"Transport: type 3\"],\n           \"Business\":[\"Business Entity Type 1\",\"Business Entity Type 2\",\"Business Entity Type 3\"]\n          }","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:38.038677Z","iopub.execute_input":"2023-02-07T06:14:38.039136Z","iopub.status.idle":"2023-02-07T06:14:38.048381Z","shell.execute_reply.started":"2023-02-07T06:14:38.039101Z","shell.execute_reply":"2023-02-07T06:14:38.047421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for key, values in NEW_ORGANIZATION_TYPE.items():\n    data_devsample[\"ORGANIZATION_TYPE\"].replace(dict.fromkeys(values,key),inplace = True) \ndata_devsample[\"ORGANIZATION_TYPE\"].value_counts()      \n        ","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:38.050314Z","iopub.execute_input":"2023-02-07T06:14:38.050596Z","iopub.status.idle":"2023-02-07T06:14:38.161696Z","shell.execute_reply.started":"2023-02-07T06:14:38.050570Z","shell.execute_reply":"2023-02-07T06:14:38.160841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_devsample[catcol].describe()","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:38.162818Z","iopub.execute_input":"2023-02-07T06:14:38.163725Z","iopub.status.idle":"2023-02-07T06:14:38.255915Z","shell.execute_reply.started":"2023-02-07T06:14:38.163690Z","shell.execute_reply":"2023-02-07T06:14:38.254950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_devsample[\"NAME_FAMILY_STATUS\"].value_counts()      ","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:38.257206Z","iopub.execute_input":"2023-02-07T06:14:38.257546Z","iopub.status.idle":"2023-02-07T06:14:38.269343Z","shell.execute_reply.started":"2023-02-07T06:14:38.257507Z","shell.execute_reply":"2023-02-07T06:14:38.268321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_devsample[\"NAME_FAMILY_STATUS\"].replace(\"Civil marriage\",\"Married\",inplace = True)\ndata_devsample[\"NAME_FAMILY_STATUS\"].replace(\"Widow\",\"Single / not married\",inplace = True)\ndata_devsample[\"NAME_FAMILY_STATUS\"].replace(\"Separated\",\"Single / not married\",inplace = True)\ndata_devsample[\"NAME_FAMILY_STATUS\"].value_counts()      ","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:38.270901Z","iopub.execute_input":"2023-02-07T06:14:38.271296Z","iopub.status.idle":"2023-02-07T06:14:38.292741Z","shell.execute_reply.started":"2023-02-07T06:14:38.271251Z","shell.execute_reply":"2023-02-07T06:14:38.291930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_devsample[\"WEEKDAY_APPR_PROCESS_START\"].value_counts()      ","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:38.294023Z","iopub.execute_input":"2023-02-07T06:14:38.294347Z","iopub.status.idle":"2023-02-07T06:14:38.311972Z","shell.execute_reply.started":"2023-02-07T06:14:38.294312Z","shell.execute_reply":"2023-02-07T06:14:38.311137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_devsample.drop(\"WEEKDAY_APPR_PROCESS_START\",axis = 1 , inplace = True)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:38.314089Z","iopub.execute_input":"2023-02-07T06:14:38.314734Z","iopub.status.idle":"2023-02-07T06:14:38.334438Z","shell.execute_reply.started":"2023-02-07T06:14:38.314693Z","shell.execute_reply":"2023-02-07T06:14:38.333366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_devsample[\"FLAG_OWN_REALTY\"].value_counts()      ","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:38.336414Z","iopub.execute_input":"2023-02-07T06:14:38.337265Z","iopub.status.idle":"2023-02-07T06:14:38.351869Z","shell.execute_reply.started":"2023-02-07T06:14:38.337224Z","shell.execute_reply":"2023-02-07T06:14:38.350839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_devsample[\"FLAG_OWN_CAR\"].value_counts()      ","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:38.353388Z","iopub.execute_input":"2023-02-07T06:14:38.354088Z","iopub.status.idle":"2023-02-07T06:14:38.369637Z","shell.execute_reply.started":"2023-02-07T06:14:38.354054Z","shell.execute_reply":"2023-02-07T06:14:38.368708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_devsample[\"CODE_GENDER\"].value_counts()      ","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:38.371506Z","iopub.execute_input":"2023-02-07T06:14:38.371747Z","iopub.status.idle":"2023-02-07T06:14:38.384997Z","shell.execute_reply.started":"2023-02-07T06:14:38.371724Z","shell.execute_reply":"2023-02-07T06:14:38.383929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_devsample[\"NAME_INCOME_TYPE\"].value_counts()      ","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:38.386619Z","iopub.execute_input":"2023-02-07T06:14:38.386981Z","iopub.status.idle":"2023-02-07T06:14:38.398152Z","shell.execute_reply.started":"2023-02-07T06:14:38.386946Z","shell.execute_reply":"2023-02-07T06:14:38.397089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_devsample[\"ORGANIZATION_TYPE\"].value_counts()      ","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:38.399517Z","iopub.execute_input":"2023-02-07T06:14:38.400098Z","iopub.status.idle":"2023-02-07T06:14:38.422312Z","shell.execute_reply.started":"2023-02-07T06:14:38.400064Z","shell.execute_reply":"2023-02-07T06:14:38.421520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_devsample[\"NAME_EDUCATION_TYPE\"].value_counts()      ","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:38.425379Z","iopub.execute_input":"2023-02-07T06:14:38.425666Z","iopub.status.idle":"2023-02-07T06:14:38.439965Z","shell.execute_reply.started":"2023-02-07T06:14:38.425611Z","shell.execute_reply":"2023-02-07T06:14:38.439082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_devsample[\"NAME_CONTRACT_TYPE\"].value_counts()/len(data_devsample)*100  ","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:38.446127Z","iopub.execute_input":"2023-02-07T06:14:38.446387Z","iopub.status.idle":"2023-02-07T06:14:38.461173Z","shell.execute_reply.started":"2023-02-07T06:14:38.446352Z","shell.execute_reply":"2023-02-07T06:14:38.460163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_devsample[\"NAME_TYPE_SUITE\"].value_counts()/len(data_devsample)*100     ","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:38.462622Z","iopub.execute_input":"2023-02-07T06:14:38.463057Z","iopub.status.idle":"2023-02-07T06:14:38.478940Z","shell.execute_reply.started":"2023-02-07T06:14:38.463024Z","shell.execute_reply":"2023-02-07T06:14:38.477951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_devsample[\"NAME_TYPE_SUITE\"].replace(\"Spouse, partner\",\"Accompanied\",inplace = True)\ndata_devsample[\"NAME_TYPE_SUITE\"].replace(\"Children\",\"Accompanied\",inplace = True)\ndata_devsample[\"NAME_TYPE_SUITE\"].replace(\"Other_A\",\"Accompanied\",inplace = True)\ndata_devsample[\"NAME_TYPE_SUITE\"].replace(\"Other_B\",\"Accompanied\",inplace = True)\ndata_devsample[\"NAME_TYPE_SUITE\"].replace(\"Family\",\"Accompanied\",inplace = True)\ndata_devsample[\"NAME_TYPE_SUITE\"].replace(\"Group of people\",\"Accompanied\",inplace = True)\ndata_devsample[\"NAME_TYPE_SUITE\"].replace(\"Spouse, partner\",\"Accompanied\",inplace = True)\ndata_devsample = data_devsample.loc[data_devsample[\"NAME_TYPE_SUITE\"] != 0.5352762504724826]\ndata_devsample[\"NAME_TYPE_SUITE\"].value_counts() ","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:38.482419Z","iopub.execute_input":"2023-02-07T06:14:38.482684Z","iopub.status.idle":"2023-02-07T06:14:38.536253Z","shell.execute_reply.started":"2023-02-07T06:14:38.482659Z","shell.execute_reply":"2023-02-07T06:14:38.535241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_devsample[\"NAME_HOUSING_TYPE\"].value_counts()/len(data_devsample)*100  ","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:38.537732Z","iopub.execute_input":"2023-02-07T06:14:38.538104Z","iopub.status.idle":"2023-02-07T06:14:38.550172Z","shell.execute_reply.started":"2023-02-07T06:14:38.538070Z","shell.execute_reply":"2023-02-07T06:14:38.548965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_devsample.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:38.551920Z","iopub.execute_input":"2023-02-07T06:14:38.552383Z","iopub.status.idle":"2023-02-07T06:14:38.561179Z","shell.execute_reply.started":"2023-02-07T06:14:38.552338Z","shell.execute_reply":"2023-02-07T06:14:38.559747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"catcol = set(data_devsample.columns) - set(num_col)\ndata_devsample[catcol].describe()","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:38.562854Z","iopub.execute_input":"2023-02-07T06:14:38.563354Z","iopub.status.idle":"2023-02-07T06:14:38.653127Z","shell.execute_reply.started":"2023-02-07T06:14:38.563192Z","shell.execute_reply":"2023-02-07T06:14:38.652135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = data_devsample['TARGET']\nx = data_devsample.drop([\"TARGET\"], axis = 1) \nxtrain,xtest,ytrain,ytest = train_test_split(x,y,test_size=0.20, random_state=0)\nprint(\"ytrain\" ,ytrain.shape ,\"\\n\", \"xtest\",xtest.shape)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:38.654411Z","iopub.execute_input":"2023-02-07T06:14:38.654829Z","iopub.status.idle":"2023-02-07T06:14:38.714538Z","shell.execute_reply.started":"2023-02-07T06:14:38.654796Z","shell.execute_reply":"2023-02-07T06:14:38.713469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_enc = pd.get_dummies(x, columns = set(catcol))\nxtest_enc =pd.get_dummies(xtest, columns = set(catcol))\nxtrain_enc =pd.get_dummies(xtrain, columns = set(catcol))","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:38.715720Z","iopub.execute_input":"2023-02-07T06:14:38.716085Z","iopub.status.idle":"2023-02-07T06:14:38.918559Z","shell.execute_reply.started":"2023-02-07T06:14:38.716051Z","shell.execute_reply":"2023-02-07T06:14:38.917537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def std_sclr(df):\n    scaler = StandardScaler()\n    return( pd.DataFrame(data= scaler.fit_transform(df),columns = df.columns ))\n    ","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:38.920103Z","iopub.execute_input":"2023-02-07T06:14:38.920455Z","iopub.status.idle":"2023-02-07T06:14:38.925879Z","shell.execute_reply.started":"2023-02-07T06:14:38.920418Z","shell.execute_reply":"2023-02-07T06:14:38.924878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtrain_scaled  = std_sclr(xtrain_enc)\nxtest_scaled  = std_sclr(xtest_enc)\nx_scaled  = std_sclr(x_enc)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:38.927721Z","iopub.execute_input":"2023-02-07T06:14:38.928447Z","iopub.status.idle":"2023-02-07T06:14:39.204244Z","shell.execute_reply.started":"2023-02-07T06:14:38.928409Z","shell.execute_reply":"2023-02-07T06:14:39.203218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Modeling**","metadata":{"execution":{"iopub.status.busy":"2023-02-20T06:29:09.366659Z","iopub.execute_input":"2023-02-20T06:29:09.367201Z","iopub.status.idle":"2023-02-20T06:29:09.395712Z","shell.execute_reply.started":"2023-02-20T06:29:09.367106Z","shell.execute_reply":"2023-02-20T06:29:09.394834Z"}}},{"cell_type":"code","source":"SVC_par = {\"kernel\" : [\"rbf\" , \"poly\"],\n           \"C\" : [1,2,3]\n            }\nforest_par = {\"n_estimators\" : [100,200,300],\n                  \"max_depth\" : [20,35,50],\n                  \"min_samples_split\" : [5,10,15],\n                  \"max_features\" : [\"sqrt\"]\n                 }\nlogreg_par = {\n    'C' : [0.1,0.2,0.5]\n}","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:39.206047Z","iopub.execute_input":"2023-02-07T06:14:39.206457Z","iopub.status.idle":"2023-02-07T06:14:39.214187Z","shell.execute_reply.started":"2023-02-07T06:14:39.206418Z","shell.execute_reply":"2023-02-07T06:14:39.213019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"forest = RandomForestClassifier()\nlog_reg = LogisticRegression()\nsvc = SVC()","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:39.215733Z","iopub.execute_input":"2023-02-07T06:14:39.216696Z","iopub.status.idle":"2023-02-07T06:14:39.227928Z","shell.execute_reply.started":"2023-02-07T06:14:39.216659Z","shell.execute_reply":"2023-02-07T06:14:39.227074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"forest_grid = GridSearchCV(forest,forest_par,\n             cv = 5,return_train_score = True)\nSVR_grid =  GridSearchCV(svc,SVC_par,\n             cv = 5 ,return_train_score = True)\nlog_grid = GridSearchCV(log_reg,logreg_par,\n             cv = 5 ,return_train_score = True)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:39.231234Z","iopub.execute_input":"2023-02-07T06:14:39.231549Z","iopub.status.idle":"2023-02-07T06:14:39.240300Z","shell.execute_reply.started":"2023-02-07T06:14:39.231525Z","shell.execute_reply":"2023-02-07T06:14:39.239328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"regressors = [forest_grid,SVR_grid,log_grid]\nmean_rmse = {}\nfor reg in regressors:\n    reg.fit(xtrain_scaled,ytrain)\n    scores = cross_val_score(reg.best_estimator_,xtrain_scaled,ytrain,cv = 5, scoring = 'f1')\n    mean_rmse[str(reg)] = scores\n","metadata":{"execution":{"iopub.status.busy":"2023-02-07T06:14:39.241660Z","iopub.execute_input":"2023-02-07T06:14:39.241982Z","iopub.status.idle":"2023-02-07T11:46:59.204948Z","shell.execute_reply.started":"2023-02-07T06:14:39.241954Z","shell.execute_reply":"2023-02-07T11:46:59.203585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s = str(regressors[0])\nmean_rmse[s] = mean_rmse[s].mean()","metadata":{"execution":{"iopub.status.busy":"2023-02-07T11:46:59.279201Z","iopub.execute_input":"2023-02-07T11:46:59.279836Z","iopub.status.idle":"2023-02-07T11:46:59.289038Z","shell.execute_reply.started":"2023-02-07T11:46:59.279796Z","shell.execute_reply":"2023-02-07T11:46:59.287559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s = str(regressors[1])\nmean_rmse[s] = mean_rmse[s].mean()","metadata":{"execution":{"iopub.status.busy":"2023-02-07T11:46:59.291102Z","iopub.execute_input":"2023-02-07T11:46:59.292135Z","iopub.status.idle":"2023-02-07T11:46:59.302811Z","shell.execute_reply.started":"2023-02-07T11:46:59.292088Z","shell.execute_reply":"2023-02-07T11:46:59.301701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s = str(regressors[2])\nmean_rmse[s] = mean_rmse[s].mean()","metadata":{"execution":{"iopub.status.busy":"2023-02-07T11:46:59.304760Z","iopub.execute_input":"2023-02-07T11:46:59.305691Z","iopub.status.idle":"2023-02-07T11:46:59.317251Z","shell.execute_reply.started":"2023-02-07T11:46:59.305651Z","shell.execute_reply":"2023-02-07T11:46:59.316002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in regressors:\n    print('f1-score for '+str(i.estimator)+' is '+str(mean_rmse[str(i)]))","metadata":{"execution":{"iopub.status.busy":"2023-02-07T12:54:48.705631Z","iopub.execute_input":"2023-02-07T12:54:48.706053Z","iopub.status.idle":"2023-02-07T12:54:48.716436Z","shell.execute_reply.started":"2023-02-07T12:54:48.706017Z","shell.execute_reply":"2023-02-07T12:54:48.715400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ypred = forest_grid.best_estimator_.predict(xtest_scaled)\nmean_squared_error(ytest, ypred)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T13:12:58.142846Z","iopub.execute_input":"2023-02-07T13:12:58.143858Z","iopub.status.idle":"2023-02-07T13:12:59.119113Z","shell.execute_reply.started":"2023-02-07T13:12:58.143823Z","shell.execute_reply":"2023-02-07T13:12:59.117976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ypred = SVR_grid.best_estimator_.predict(xtest_scaled)\nmean_squared_error(ytest, ypred)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T13:12:59.121588Z","iopub.execute_input":"2023-02-07T13:12:59.122004Z","iopub.status.idle":"2023-02-07T13:13:29.505520Z","shell.execute_reply.started":"2023-02-07T13:12:59.121966Z","shell.execute_reply":"2023-02-07T13:13:29.504026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ypred = log_grid.best_estimator_.predict(xtest_scaled)\nmean_squared_error(ytest, ypred)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T13:13:29.510972Z","iopub.execute_input":"2023-02-07T13:13:29.514152Z","iopub.status.idle":"2023-02-07T13:13:29.546617Z","shell.execute_reply.started":"2023-02-07T13:13:29.514115Z","shell.execute_reply":"2023-02-07T13:13:29.539732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtest_scaled['NAME_INCOME_TYPE_Businessman'] = xtrain_scaled['CODE_GENDER_XNA']","metadata":{"execution":{"iopub.status.busy":"2023-02-07T13:07:56.393686Z","iopub.execute_input":"2023-02-07T13:07:56.394100Z","iopub.status.idle":"2023-02-07T13:07:56.404320Z","shell.execute_reply.started":"2023-02-07T13:07:56.394066Z","shell.execute_reply":"2023-02-07T13:07:56.403103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtest_scaled['NAME_INCOME_TYPE_Businessman'].rename('CODE_GENDER_XNA')","metadata":{"execution":{"iopub.status.busy":"2023-02-07T13:08:18.016378Z","iopub.execute_input":"2023-02-07T13:08:18.017430Z","iopub.status.idle":"2023-02-07T13:08:18.028427Z","shell.execute_reply.started":"2023-02-07T13:08:18.017367Z","shell.execute_reply":"2023-02-07T13:08:18.027397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtest_scaled.rename(columns={\"NAME_INCOME_TYPE_Businessman\": \"CODE_GENDER_XNA\"}, inplace  = True)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T13:10:14.586910Z","iopub.execute_input":"2023-02-07T13:10:14.587595Z","iopub.status.idle":"2023-02-07T13:10:14.594398Z","shell.execute_reply.started":"2023-02-07T13:10:14.587557Z","shell.execute_reply":"2023-02-07T13:10:14.593168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtest_scaled['CODE_GENDER_XNA']","metadata":{"execution":{"iopub.status.busy":"2023-02-07T13:10:17.609221Z","iopub.execute_input":"2023-02-07T13:10:17.610254Z","iopub.status.idle":"2023-02-07T13:10:17.621179Z","shell.execute_reply.started":"2023-02-07T13:10:17.610206Z","shell.execute_reply":"2023-02-07T13:10:17.620063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}