{"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 numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport tqdm\nimport gc\nimport sys\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2022-04-25T18:56:41.871140Z","iopub.execute_input":"2022-04-25T18:56:41.871811Z","iopub.status.idle":"2022-04-25T18:56:42.913639Z","shell.execute_reply.started":"2022-04-25T18:56:41.871772Z","shell.execute_reply":"2022-04-25T18:56:42.912492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Preparation","metadata":{}},{"cell_type":"markdown","source":"As dataset is quite large , we will be solving the problem by breaking down dataset into Chunks and Merging the results afterwards.","metadata":{}},{"cell_type":"code","source":"date = pd.read_csv('../input/bosch-production-line-performance/train_date.csv.zip', nrows=10000)\nnumeric = pd.read_csv('../input/bosch-production-line-performance/train_numeric.csv.zip', nrows=10000)\ncategory = pd.read_csv('../input/bosch-production-line-performance/train_categorical.csv.zip', nrows=10000)","metadata":{"execution":{"iopub.status.busy":"2022-04-25T18:56:42.916634Z","iopub.execute_input":"2022-04-25T18:56:42.917115Z","iopub.status.idle":"2022-04-25T18:56:50.169901Z","shell.execute_reply.started":"2022-04-25T18:56:42.917063Z","shell.execute_reply":"2022-04-25T18:56:50.168948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"date","metadata":{"execution":{"iopub.status.busy":"2022-04-25T18:56:50.171267Z","iopub.execute_input":"2022-04-25T18:56:50.171799Z","iopub.status.idle":"2022-04-25T18:56:50.222592Z","shell.execute_reply.started":"2022-04-25T18:56:50.171766Z","shell.execute_reply":"2022-04-25T18:56:50.221268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The data represents measurements of parts as they move through Bosch's production lines. Each part has a unique Id. The goal is to predict which parts will fail quality control (represented by a 'Response' = 1).\n\nThe dataset contains an extremely large number of anonymized features. Features are named according to a convention that tells you the production line, the station on the line, and a feature number. E.g. L3_S36_F3939 is a feature measured on line 3, station 36, and is feature number 3939.\n\nOn account of the large size of the dataset, we have separated the files by the type of feature they contain: numerical, categorical, and finally, a file with date features. The date features provide a timestamp for when each measurement was taken. Each date column ends in a number that corresponds to the previous feature number. E.g. the value of L0_S0_D1 is the time at which L0_S0_F0 was taken.","metadata":{}},{"cell_type":"code","source":"numeric","metadata":{"execution":{"iopub.status.busy":"2022-04-25T18:56:50.224486Z","iopub.execute_input":"2022-04-25T18:56:50.224950Z","iopub.status.idle":"2022-04-25T18:56:50.268032Z","shell.execute_reply.started":"2022-04-25T18:56:50.224910Z","shell.execute_reply":"2022-04-25T18:56:50.266934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"category","metadata":{"execution":{"iopub.status.busy":"2022-04-25T18:56:50.272052Z","iopub.execute_input":"2022-04-25T18:56:50.272440Z","iopub.status.idle":"2022-04-25T18:56:50.312817Z","shell.execute_reply.started":"2022-04-25T18:56:50.272383Z","shell.execute_reply":"2022-04-25T18:56:50.311559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# FEATURE ENGINEERING","metadata":{}},{"cell_type":"markdown","source":"### The list of numeric features is selected based on the other XGBOOST classifier check the numericclassifier notebook","metadata":{}},{"cell_type":"code","source":"num_feats = ['Id',\n       'L3_S30_F3514', 'L0_S9_F200', 'L3_S29_F3430', 'L0_S11_F314',\n       'L0_S0_F18', 'L3_S35_F3896', 'L0_S12_F350', 'L3_S36_F3918',\n       'L0_S0_F20', 'L3_S30_F3684', 'L1_S24_F1632', 'L0_S2_F48',\n       'L3_S29_F3345', 'L0_S18_F449', 'L0_S21_F497', 'L3_S29_F3433',\n       'L3_S30_F3764', 'L0_S1_F24', 'L3_S30_F3554', 'L0_S11_F322',\n       'L3_S30_F3564', 'L3_S29_F3327', 'L0_S2_F36', 'L0_S9_F180',\n       'L3_S33_F3855', 'L0_S0_F4', 'L0_S21_F477', 'L0_S5_F114',\n       'L0_S6_F122', 'L1_S24_F1122', 'L0_S9_F165', 'L0_S18_F439',\n       'L1_S24_F1490', 'L0_S6_F132', 'L3_S29_F3379', 'L3_S29_F3336',\n       'L0_S3_F80', 'L3_S30_F3749', 'L1_S24_F1763', 'L0_S10_F219',\n 'Response']","metadata":{"execution":{"iopub.status.busy":"2022-04-25T18:56:50.316947Z","iopub.execute_input":"2022-04-25T18:56:50.317288Z","iopub.status.idle":"2022-04-25T18:56:50.325877Z","shell.execute_reply.started":"2022-04-25T18:56:50.317258Z","shell.execute_reply":"2022-04-25T18:56:50.324489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"length = date.drop('Id', axis=1).count()\ndate_cols = length.reset_index().sort_values(by=0, ascending=False)\nstations = sorted(date_cols['index'].str.split('_',expand=True)[1].unique().tolist())\ndate_cols['station'] = date_cols['index'].str.split('_',expand=True)[1]\ndate_cols = date_cols.drop_duplicates('station', keep='first')['index'].tolist()","metadata":{"execution":{"iopub.status.busy":"2022-04-25T18:56:50.327392Z","iopub.execute_input":"2022-04-25T18:56:50.327864Z","iopub.status.idle":"2022-04-25T18:56:50.449763Z","shell.execute_reply.started":"2022-04-25T18:56:50.327836Z","shell.execute_reply":"2022-04-25T18:56:50.448658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = None\nfor chunk in pd.read_csv('../input/bosch-production-line-performance/train_date.csv.zip',usecols=['Id'] + date_cols,chunksize=50000,low_memory=False):\n\n    chunk.columns = ['Id'] + stations\n    chunk['start_station'] = -1\n    chunk['end_station'] = -1\n    \n    for s in stations:\n        chunk[s] = 1 * (chunk[s] >= 0)\n        id_not_null = chunk[chunk[s] == 1].Id\n        chunk.loc[(chunk['start_station']== -1) & (chunk.Id.isin(id_not_null)),'start_station'] = int(s[1:])\n        chunk.loc[chunk.Id.isin(id_not_null),'end_station'] = int(s[1:])   \n    data = pd.concat([data, chunk])","metadata":{"execution":{"iopub.status.busy":"2022-04-25T18:56:50.451557Z","iopub.execute_input":"2022-04-25T18:56:50.451902Z","iopub.status.idle":"2022-04-25T18:58:23.167617Z","shell.execute_reply.started":"2022-04-25T18:56:50.451872Z","shell.execute_reply":"2022-04-25T18:58:23.166663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for chunk in pd.read_csv('../input/bosch-production-line-performance/test_date.csv.zip',usecols=['Id'] + date_cols,chunksize=50000,low_memory=False):\n    \n    chunk.columns = ['Id'] + stations\n    chunk['start_station'] = -1\n    chunk['end_station'] = -1\n    for s in stations:\n        chunk[s] = 1 * (chunk[s] >= 0)\n        id_not_null = chunk[chunk[s] == 1].Id\n        chunk.loc[(chunk['start_station']== -1) & (chunk.Id.isin(id_not_null)),'start_station'] = int(s[1:])\n        chunk.loc[chunk.Id.isin(id_not_null),'end_station'] = int(s[1:])   \n    data = pd.concat([data, chunk])\ndel chunk\ngc.collect()   ","metadata":{"execution":{"iopub.status.busy":"2022-04-25T18:58:23.168904Z","iopub.execute_input":"2022-04-25T18:58:23.169243Z","iopub.status.idle":"2022-04-25T19:00:16.298052Z","shell.execute_reply.started":"2022-04-25T18:58:23.169201Z","shell.execute_reply":"2022-04-25T19:00:16.294615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data[['Id','start_station','end_station']]\nusefuldatefeatures = ['Id']+date_cols","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:00:16.299810Z","iopub.execute_input":"2022-04-25T19:00:16.300263Z","iopub.status.idle":"2022-04-25T19:00:16.342845Z","shell.execute_reply.started":"2022-04-25T19:00:16.300215Z","shell.execute_reply":"2022-04-25T19:00:16.341790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"minmaxfeatures = None\nfor chunk in pd.read_csv('../input/bosch-production-line-performance/train_date.csv.zip',usecols=usefuldatefeatures,chunksize=50000,low_memory=False):\n    features = chunk.columns.values.tolist()\n    features.remove('Id')\n    df_mindate_chunk = chunk[['Id']].copy()\n    df_mindate_chunk['mindate'] = chunk[features].min(axis=1).values\n    df_mindate_chunk['maxdate'] = chunk[features].max(axis=1).values\n    df_mindate_chunk['min_time_station'] =  chunk[features].idxmin(axis = 1).apply(lambda s: int(s.split('_')[1][1:]) if s is not np.nan else -1)\n    df_mindate_chunk['max_time_station'] =  chunk[features].idxmax(axis = 1).apply(lambda s: int(s.split('_')[1][1:]) if s is not np.nan else -1)\n    minmaxfeatures = pd.concat([minmaxfeatures, df_mindate_chunk])\n\ndel chunk\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:00:16.344731Z","iopub.execute_input":"2022-04-25T19:00:16.345177Z","iopub.status.idle":"2022-04-25T19:01:31.691837Z","shell.execute_reply.started":"2022-04-25T19:00:16.345139Z","shell.execute_reply":"2022-04-25T19:01:31.690660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for chunk in pd.read_csv('../input/bosch-production-line-performance/test_date.csv.zip',usecols=usefuldatefeatures,chunksize=50000,low_memory=False):\n    features = chunk.columns.values.tolist()\n    features.remove('Id')\n    df_mindate_chunk = chunk[['Id']].copy()\n    df_mindate_chunk['mindate'] = chunk[features].min(axis=1).values\n    df_mindate_chunk['maxdate'] = chunk[features].max(axis=1).values\n    df_mindate_chunk['min_time_station'] =  chunk[features].idxmin(axis = 1).apply(lambda s: int(s.split('_')[1][1:]) if s is not np.nan else -1)\n    df_mindate_chunk['max_time_station'] =  chunk[features].idxmax(axis = 1).apply(lambda s: int(s.split('_')[1][1:]) if s is not np.nan else -1)\n    minmaxfeatures = pd.concat([minmaxfeatures, df_mindate_chunk])\n\ndel chunk\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:01:31.693585Z","iopub.execute_input":"2022-04-25T19:01:31.694005Z","iopub.status.idle":"2022-04-25T19:02:46.912693Z","shell.execute_reply.started":"2022-04-25T19:01:31.693962Z","shell.execute_reply":"2022-04-25T19:02:46.911375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"minmaxfeatures.sort_values(by=['mindate', 'Id'], inplace=True)\nminmaxfeatures['min_Id_rev'] = -minmaxfeatures.Id.diff().shift(-1)\nminmaxfeatures['min_Id'] = minmaxfeatures.Id.diff()","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:02:46.914542Z","iopub.execute_input":"2022-04-25T19:02:46.914867Z","iopub.status.idle":"2022-04-25T19:02:49.635930Z","shell.execute_reply.started":"2022-04-25T19:02:46.914836Z","shell.execute_reply":"2022-04-25T19:02:49.634468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols = [['Id']+date_cols,num_feats]","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:02:49.637578Z","iopub.execute_input":"2022-04-25T19:02:49.637914Z","iopub.status.idle":"2022-04-25T19:02:49.643188Z","shell.execute_reply.started":"2022-04-25T19:02:49.637871Z","shell.execute_reply":"2022-04-25T19:02:49.641794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindata = None\ntestdata = None","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:02:49.645175Z","iopub.execute_input":"2022-04-25T19:02:49.645706Z","iopub.status.idle":"2022-04-25T19:02:49.656304Z","shell.execute_reply.started":"2022-04-25T19:02:49.645661Z","shell.execute_reply":"2022-04-25T19:02:49.655467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainfiles = ['train_date.csv.zip','train_numeric.csv.zip']\ntestfiles = ['test_date.csv.zip','test_numeric.csv.zip']","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:02:49.657764Z","iopub.execute_input":"2022-04-25T19:02:49.658296Z","iopub.status.idle":"2022-04-25T19:02:49.669318Z","shell.execute_reply.started":"2022-04-25T19:02:49.658259Z","shell.execute_reply":"2022-04-25T19:02:49.668364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i,f in enumerate(trainfiles):\n    \n    subset = None\n    \n    for chunk in pd.read_csv('../input/bosch-production-line-performance/' + f,usecols=cols[i],chunksize=100000,low_memory=False):\n        subset = pd.concat([subset, chunk])\n    \n    if traindata is None:\n        traindata = subset.copy()\n    else:\n        traindata = pd.merge(traindata, subset.copy(), on=\"Id\")\n        \ndel subset,chunk\ngc.collect()\ndel cols[1][-1]","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:02:49.670791Z","iopub.execute_input":"2022-04-25T19:02:49.671103Z","iopub.status.idle":"2022-04-25T19:05:00.166651Z","shell.execute_reply.started":"2022-04-25T19:02:49.671071Z","shell.execute_reply":"2022-04-25T19:05:00.165316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, f in enumerate(testfiles):\n    subset = None\n    \n    for chunk in pd.read_csv('../input/bosch-production-line-performance/' + f,usecols=cols[i],chunksize=100000,low_memory=False):\n        subset = pd.concat([subset, chunk])\n        \n    if testdata is None:\n        testdata = subset.copy()\n    else:\n        testdata = pd.merge(testdata, subset.copy(), on=\"Id\")\n    \ndel subset,chunk\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:05:00.168573Z","iopub.execute_input":"2022-04-25T19:05:00.169068Z","iopub.status.idle":"2022-04-25T19:07:10.863177Z","shell.execute_reply.started":"2022-04-25T19:05:00.169020Z","shell.execute_reply":"2022-04-25T19:07:10.861781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindata = traindata.merge(minmaxfeatures, on='Id')\ntraindata = traindata.merge(data, on='Id')\ntestdata = testdata.merge(minmaxfeatures, on='Id')\ntestdata = testdata.merge(data, on='Id')","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:07:10.864913Z","iopub.execute_input":"2022-04-25T19:07:10.865220Z","iopub.status.idle":"2022-04-25T19:07:18.063858Z","shell.execute_reply.started":"2022-04-25T19:07:10.865191Z","shell.execute_reply":"2022-04-25T19:07:18.062655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del minmaxfeatures,data\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:07:18.065259Z","iopub.execute_input":"2022-04-25T19:07:18.065585Z","iopub.status.idle":"2022-04-25T19:07:18.158837Z","shell.execute_reply.started":"2022-04-25T19:07:18.065556Z","shell.execute_reply":"2022-04-25T19:07:18.158052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindata","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:07:18.160132Z","iopub.execute_input":"2022-04-25T19:07:18.160661Z","iopub.status.idle":"2022-04-25T19:07:18.720328Z","shell.execute_reply.started":"2022-04-25T19:07:18.160614Z","shell.execute_reply":"2022-04-25T19:07:18.719489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testdata","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:07:18.721972Z","iopub.execute_input":"2022-04-25T19:07:18.722279Z","iopub.status.idle":"2022-04-25T19:07:19.265770Z","shell.execute_reply.started":"2022-04-25T19:07:18.722248Z","shell.execute_reply":"2022-04-25T19:07:19.264873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindata.fillna(value=0,inplace=True)\ntestdata.fillna(value=0,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:07:19.267310Z","iopub.execute_input":"2022-04-25T19:07:19.267684Z","iopub.status.idle":"2022-04-25T19:07:22.141777Z","shell.execute_reply.started":"2022-04-25T19:07:19.267652Z","shell.execute_reply":"2022-04-25T19:07:22.140727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mcc(tp, tn, fp, fn):\n    num = tp * tn - fp * fn\n    den = (tp + fp) * (tp + fn) * (tn + fp) * (tn + fn)\n    if den == 0:\n        return 0\n    else:\n        return num / np.sqrt(den)","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:07:22.143263Z","iopub.execute_input":"2022-04-25T19:07:22.143605Z","iopub.status.idle":"2022-04-25T19:07:22.151198Z","shell.execute_reply.started":"2022-04-25T19:07:22.143570Z","shell.execute_reply":"2022-04-25T19:07:22.149874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def eval_mcc(y_true, y_prob):\n    idx = np.argsort(y_prob)\n    y_true_sort = y_true[idx]\n    n = y_true.shape[0]\n    nump = 1.0 * np.sum(y_true) \n    numn = n - nump \n    tp,fp = nump,numn\n    tn,fn = 0.0,0.0\n    best_mcc = 0.0\n    best_id = -1\n    mccs = np.zeros(n)\n    for i in range(n):\n        if y_true_sort[i] == 1:\n            tp -= 1.0\n            fn += 1.0\n        else:\n            fp -= 1.0\n            tn += 1.0\n        new_mcc = mcc(tp, tn, fp, fn)\n        mccs[i] = new_mcc\n        if new_mcc >= best_mcc:\n            best_mcc = new_mcc\n            best_id = i\n    return best_mcc","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:07:22.153284Z","iopub.execute_input":"2022-04-25T19:07:22.153626Z","iopub.status.idle":"2022-04-25T19:07:22.167894Z","shell.execute_reply.started":"2022-04-25T19:07:22.153594Z","shell.execute_reply":"2022-04-25T19:07:22.166446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mcc_eval(y_prob, dtrain):\n    y_true = dtrain.get_label()\n    best_mcc = eval_mcc(y_true, y_prob)\n    return 'MCC', best_mcc","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:07:22.169508Z","iopub.execute_input":"2022-04-25T19:07:22.169824Z","iopub.status.idle":"2022-04-25T19:07:22.181561Z","shell.execute_reply.started":"2022-04-25T19:07:22.169795Z","shell.execute_reply":"2022-04-25T19:07:22.180479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.set_printoptions(suppress=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:07:22.185543Z","iopub.execute_input":"2022-04-25T19:07:22.186611Z","iopub.status.idle":"2022-04-25T19:07:22.197653Z","shell.execute_reply.started":"2022-04-25T19:07:22.186554Z","shell.execute_reply":"2022-04-25T19:07:22.196734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:07:22.200000Z","iopub.execute_input":"2022-04-25T19:07:22.200603Z","iopub.status.idle":"2022-04-25T19:07:22.209168Z","shell.execute_reply.started":"2022-04-25T19:07:22.200554Z","shell.execute_reply":"2022-04-25T19:07:22.208062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total = traindata[traindata['Response']==0].sample(frac=1).head(400000)\ntotal = pd.concat([total,traindata[traindata['Response']==1]]).sample(frac=1)","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:07:22.210328Z","iopub.execute_input":"2022-04-25T19:07:22.210765Z","iopub.status.idle":"2022-04-25T19:07:26.611940Z","shell.execute_reply.started":"2022-04-25T19:07:22.210734Z","shell.execute_reply":"2022-04-25T19:07:26.611034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:07:26.613306Z","iopub.execute_input":"2022-04-25T19:07:26.613943Z","iopub.status.idle":"2022-04-25T19:07:26.745994Z","shell.execute_reply.started":"2022-04-25T19:07:26.613898Z","shell.execute_reply":"2022-04-25T19:07:26.744777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X,y = total.drop(['Response','Id'],axis=1),total['Response']","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:07:26.747622Z","iopub.execute_input":"2022-04-25T19:07:26.748040Z","iopub.status.idle":"2022-04-25T19:07:26.866644Z","shell.execute_reply.started":"2022-04-25T19:07:26.747988Z","shell.execute_reply":"2022-04-25T19:07:26.865027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42,stratify=y)","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:07:26.874001Z","iopub.execute_input":"2022-04-25T19:07:26.874483Z","iopub.status.idle":"2022-04-25T19:07:27.622452Z","shell.execute_reply.started":"2022-04-25T19:07:26.874448Z","shell.execute_reply":"2022-04-25T19:07:27.621253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# MODELLING","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nfrom sklearn.ensemble import AdaBoostClassifier\nfrom sklearn.ensemble import BaggingClassifier\nfrom sklearn.linear_model import ElasticNet\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.tree import DecisionTreeClassifier","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:07:27.624614Z","iopub.execute_input":"2022-04-25T19:07:27.625059Z","iopub.status.idle":"2022-04-25T19:07:27.833448Z","shell.execute_reply.started":"2022-04-25T19:07:27.625013Z","shell.execute_reply":"2022-04-25T19:07:27.832185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" # models selected for testing \ndef get_models():\n\tmodels = list()\n\tmodels.append(AdaBoostClassifier(n_estimators=500,random_state=11))\n\tmodels.append(BaggingClassifier(n_estimators=500,n_jobs=-1,verbose=1,random_state=11))\n\tmodels.append(RandomForestClassifier(n_estimators=500,n_jobs=-1,verbose=1,random_state=11))\n\treturn models \n\n# loading the models \nmodels = get_models()\n\n#fit the models according to training dataset  \n#create loaded_models folder in google collab \ndef fit_base_models(X_train , y_train , models):\n\tfor model in models:\n\t\tmodel.fit(X_train, y_train) \n\n\nfit_base_models(X_train , y_train , models) \nfrom sklearn.metrics import matthews_corrcoef\n\n# storing the predicted values by each model in given array and printing the rmse ( error)\nypredicted = []\ndef evaluate_models(X_test, y_test, models):\n    for model in models:\n        yhat = model.predict(X_test) \n        print(model)\n        ypredicted.append(yhat)\n        mcc = matthews_corrcoef(y_test, yhat)\n        print('%s: MCC %.3f' % (model.__class__.__name__, mcc))\n\n#predicted values on our testdataset testX \nevaluate_models(X_test , y_test , models)","metadata":{"execution":{"iopub.status.busy":"2022-04-25T19:10:08.456312Z","iopub.execute_input":"2022-04-25T19:10:08.456891Z","iopub.status.idle":"2022-04-25T21:06:44.278728Z","shell.execute_reply.started":"2022-04-25T19:10:08.456843Z","shell.execute_reply":"2022-04-25T21:06:44.276681Z"},"trusted":true},"execution_count":null,"outputs":[]}]}