{"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-08-13T09:13:31.960067Z","iopub.execute_input":"2022-08-13T09:13:31.960559Z","iopub.status.idle":"2022-08-13T09:13:31.972764Z","shell.execute_reply.started":"2022-08-13T09:13:31.960523Z","shell.execute_reply":"2022-08-13T09:13:31.970858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv(\"/kaggle/input/tabular-playground-series-aug-2022/train.csv\")\ntest=pd.read_csv(\"/kaggle/input/tabular-playground-series-aug-2022/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:13:34.652798Z","iopub.execute_input":"2022-08-13T09:13:34.653486Z","iopub.status.idle":"2022-08-13T09:13:34.944176Z","shell.execute_reply.started":"2022-08-13T09:13:34.653433Z","shell.execute_reply":"2022-08-13T09:13:34.942994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:13:51.035942Z","iopub.execute_input":"2022-08-13T09:13:51.036388Z","iopub.status.idle":"2022-08-13T09:13:51.044294Z","shell.execute_reply.started":"2022-08-13T09:13:51.036352Z","shell.execute_reply":"2022-08-13T09:13:51.042775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.product_code=train.product_code.map({'A':0, 'B':1, 'C':2, 'D':3, 'E':4})\ntest.product_code=test.product_code.map({'A':0, 'B':1, 'C':2, 'D':3, 'E':4})\ntrain.attribute_1=train.attribute_1.map({'material_5':0, 'material_6':1, 'material_8':2})\ntest.attribute_1=test.attribute_1.map({'material_5':0, 'material_6':1, 'material_8':2})\ntrain.attribute_0=train.attribute_0.map({'material_5':0, 'material_7':1})\ntest.attribute_0=test.attribute_0.map({'material_5':0, 'material_7':1})\n\n","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:13:56.423803Z","iopub.execute_input":"2022-08-13T09:13:56.426486Z","iopub.status.idle":"2022-08-13T09:13:56.469479Z","shell.execute_reply.started":"2022-08-13T09:13:56.426445Z","shell.execute_reply":"2022-08-13T09:13:56.468340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features=test.columns\ntrain=train[features]","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:13:58.234641Z","iopub.execute_input":"2022-08-13T09:13:58.235173Z","iopub.status.idle":"2022-08-13T09:13:58.248086Z","shell.execute_reply.started":"2022-08-13T09:13:58.235135Z","shell.execute_reply":"2022-08-13T09:13:58.246540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['psuedo']=0\ntest['psuedo']=1","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:13:59.477199Z","iopub.execute_input":"2022-08-13T09:13:59.477805Z","iopub.status.idle":"2022-08-13T09:13:59.485784Z","shell.execute_reply.started":"2022-08-13T09:13:59.477763Z","shell.execute_reply":"2022-08-13T09:13:59.484054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combined=pd.concat([train,test])","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:14:01.460825Z","iopub.execute_input":"2022-08-13T09:14:01.461261Z","iopub.status.idle":"2022-08-13T09:14:01.483511Z","shell.execute_reply.started":"2022-08-13T09:14:01.461226Z","shell.execute_reply":"2022-08-13T09:14:01.482371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#combined=combined.fillna(0)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:13:24.164569Z","iopub.execute_input":"2022-08-13T09:13:24.165251Z","iopub.status.idle":"2022-08-13T09:13:24.170362Z","shell.execute_reply.started":"2022-08-13T09:13:24.165214Z","shell.execute_reply":"2022-08-13T09:13:24.168593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=combined.to_numpy()\ny=combined['psuedo'].to_numpy()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:14:03.437153Z","iopub.execute_input":"2022-08-13T09:14:03.437582Z","iopub.status.idle":"2022-08-13T09:14:03.466120Z","shell.execute_reply.started":"2022-08-13T09:14:03.437548Z","shell.execute_reply":"2022-08-13T09:14:03.464558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x.shape,y.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:14:05.532912Z","iopub.execute_input":"2022-08-13T09:14:05.533353Z","iopub.status.idle":"2022-08-13T09:14:05.545373Z","shell.execute_reply.started":"2022-08-13T09:14:05.533317Z","shell.execute_reply":"2022-08-13T09:14:05.543878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.ensemble import RandomForestClassifier\nimport xgboost as xgb\nclf = xgb.XGBClassifier( seed = 10)\ncv = StratifiedKFold(n_splits=3)\nscore=[]\nfor tr_ind, te_ind in cv.split(x, y):\n    clf.fit(x[tr_ind],y[tr_ind])\n    y_pred=clf.predict(x[te_ind])\n    print(y_pred)\n    sc=roc_auc_score(y[te_ind],y_pred)\n    print(sc)\n    score.append(sc)\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:14:07.221577Z","iopub.execute_input":"2022-08-13T09:14:07.222033Z","iopub.status.idle":"2022-08-13T09:14:13.911188Z","shell.execute_reply.started":"2022-08-13T09:14:07.221997Z","shell.execute_reply":"2022-08-13T09:14:13.909663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(sum(score)/len(score))","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:14:13.913900Z","iopub.execute_input":"2022-08-13T09:14:13.914283Z","iopub.status.idle":"2022-08-13T09:14:13.921922Z","shell.execute_reply.started":"2022-08-13T09:14:13.914249Z","shell.execute_reply":"2022-08-13T09:14:13.920175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import cross_val_predict\nimport xgboost as xgb\nmodel = xgb.XGBClassifier(seed = 10)\ncv_preds = cross_val_predict(model, x, y, cv=5, n_jobs=-1, method='predict')","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:14:13.924454Z","iopub.execute_input":"2022-08-13T09:14:13.925540Z","iopub.status.idle":"2022-08-13T09:14:29.726201Z","shell.execute_reply.started":"2022-08-13T09:14:13.925472Z","shell.execute_reply":"2022-08-13T09:14:29.724261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combined['pb']=cv_preds","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:14:29.730551Z","iopub.execute_input":"2022-08-13T09:14:29.731450Z","iopub.status.idle":"2022-08-13T09:14:29.739802Z","shell.execute_reply.started":"2022-08-13T09:14:29.731400Z","shell.execute_reply":"2022-08-13T09:14:29.737955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=combined[combined.psuedo==0]","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:14:29.742842Z","iopub.execute_input":"2022-08-13T09:14:29.744064Z","iopub.status.idle":"2022-08-13T09:14:29.781589Z","shell.execute_reply.started":"2022-08-13T09:14:29.744010Z","shell.execute_reply":"2022-08-13T09:14:29.779946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation=train[train.pb==1]","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:14:29.783598Z","iopub.execute_input":"2022-08-13T09:14:29.784629Z","iopub.status.idle":"2022-08-13T09:14:29.795891Z","shell.execute_reply.started":"2022-08-13T09:14:29.784577Z","shell.execute_reply":"2022-08-13T09:14:29.794376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation ","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:14:29.799724Z","iopub.execute_input":"2022-08-13T09:14:29.800316Z","iopub.status.idle":"2022-08-13T09:14:29.857563Z","shell.execute_reply.started":"2022-08-13T09:14:29.800279Z","shell.execute_reply":"2022-08-13T09:14:29.856055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv(\"/kaggle/input/tabular-playground-series-aug-2022/train.csv\")\ntest=pd.read_csv(\"/kaggle/input/tabular-playground-series-aug-2022/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:14:29.860468Z","iopub.execute_input":"2022-08-13T09:14:29.861162Z","iopub.status.idle":"2022-08-13T09:14:30.036673Z","shell.execute_reply.started":"2022-08-13T09:14:29.861107Z","shell.execute_reply":"2022-08-13T09:14:30.035661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.product_code=train.product_code.map({'A':0, 'B':1, 'C':2, 'D':3, 'E':4})\ntest.product_code=test.product_code.map({'A':0, 'B':1, 'C':2, 'D':3, 'E':4})\ntrain.attribute_1=train.attribute_1.map({'material_5':0, 'material_6':1, 'material_8':2})\ntest.attribute_1=test.attribute_1.map({'material_5':0, 'material_6':1, 'material_8':2})\ntrain.attribute_0=train.attribute_0.map({'material_5':0, 'material_7':1})\ntest.attribute_0=test.attribute_0.map({'material_5':0, 'material_7':1})","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:14:30.038436Z","iopub.execute_input":"2022-08-13T09:14:30.039159Z","iopub.status.idle":"2022-08-13T09:14:30.068809Z","shell.execute_reply.started":"2022-08-13T09:14:30.039078Z","shell.execute_reply":"2022-08-13T09:14:30.066436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_set=train[train.id.isin(list(validation.id)) ]","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:14:33.682498Z","iopub.execute_input":"2022-08-13T09:14:33.682955Z","iopub.status.idle":"2022-08-13T09:14:33.695601Z","shell.execute_reply.started":"2022-08-13T09:14:33.682918Z","shell.execute_reply":"2022-08-13T09:14:33.694557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=train[~train.id.isin(list(validation.id))]","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:14:35.338276Z","iopub.execute_input":"2022-08-13T09:14:35.338706Z","iopub.status.idle":"2022-08-13T09:14:35.350782Z","shell.execute_reply.started":"2022-08-13T09:14:35.338673Z","shell.execute_reply":"2022-08-13T09:14:35.349507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:14:37.706992Z","iopub.execute_input":"2022-08-13T09:14:37.707993Z","iopub.status.idle":"2022-08-13T09:14:37.749278Z","shell.execute_reply.started":"2022-08-13T09:14:37.707947Z","shell.execute_reply":"2022-08-13T09:14:37.747803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_set","metadata":{"execution":{"iopub.status.busy":"2022-08-13T04:52:27.858709Z","iopub.execute_input":"2022-08-13T04:52:27.859114Z","iopub.status.idle":"2022-08-13T04:52:27.894482Z","shell.execute_reply.started":"2022-08-13T04:52:27.859080Z","shell.execute_reply":"2022-08-13T04:52:27.893483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features=test.columns\nx_train,y_train=train[features].to_numpy(),train['failure'].to_numpy()\nx_val,y_val=val_set[features].to_numpy(),val_set['failure'].to_numpy()\nx_test=test[features].to_numpy()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:14:42.110949Z","iopub.execute_input":"2022-08-13T09:14:42.111435Z","iopub.status.idle":"2022-08-13T09:14:42.130720Z","shell.execute_reply.started":"2022-08-13T09:14:42.111399Z","shell.execute_reply":"2022-08-13T09:14:42.129189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape,y_train.shape,x_val.shape,y_val.shape,x_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:14:45.083018Z","iopub.execute_input":"2022-08-13T09:14:45.083796Z","iopub.status.idle":"2022-08-13T09:14:45.092362Z","shell.execute_reply.started":"2022-08-13T09:14:45.083759Z","shell.execute_reply":"2022-08-13T09:14:45.091215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.utils.class_weight import compute_sample_weight\nsample_weights = compute_sample_weight(\n    class_weight='balanced',\n    y=train['failure'] #provide your own target name\n)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:14:46.930958Z","iopub.execute_input":"2022-08-13T09:14:46.931393Z","iopub.status.idle":"2022-08-13T09:14:46.941415Z","shell.execute_reply.started":"2022-08-13T09:14:46.931358Z","shell.execute_reply":"2022-08-13T09:14:46.940069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import xgboost\nmodel = xgboost.XGBClassifier()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:16:16.702655Z","iopub.execute_input":"2022-08-13T09:16:16.703192Z","iopub.status.idle":"2022-08-13T09:16:16.710248Z","shell.execute_reply.started":"2022-08-13T09:16:16.703151Z","shell.execute_reply":"2022-08-13T09:16:16.708855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x_train,y_train,sample_weight=sample_weights)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:16:20.783032Z","iopub.execute_input":"2022-08-13T09:16:20.783530Z","iopub.status.idle":"2022-08-13T09:16:27.390710Z","shell.execute_reply.started":"2022-08-13T09:16:20.783493Z","shell.execute_reply":"2022-08-13T09:16:27.389200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"roc_auc_score(y_val,model.predict(x_val))","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:16:29.824212Z","iopub.execute_input":"2022-08-13T09:16:29.824707Z","iopub.status.idle":"2022-08-13T09:16:29.845977Z","shell.execute_reply.started":"2022-08-13T09:16:29.824669Z","shell.execute_reply":"2022-08-13T09:16:29.844572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_tp=model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:16:32.284947Z","iopub.execute_input":"2022-08-13T09:16:32.285397Z","iopub.status.idle":"2022-08-13T09:16:32.327433Z","shell.execute_reply.started":"2022-08-13T09:16:32.285361Z","shell.execute_reply":"2022-08-13T09:16:32.326169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sam=pd.DataFrame()\nsam['id']=test.id\nsam['failure']=y_tp","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:16:41.017405Z","iopub.execute_input":"2022-08-13T09:16:41.017830Z","iopub.status.idle":"2022-08-13T09:16:41.030191Z","shell.execute_reply.started":"2022-08-13T09:16:41.017795Z","shell.execute_reply":"2022-08-13T09:16:41.028955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sam.to_csv(\"sample.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:16:44.998878Z","iopub.execute_input":"2022-08-13T09:16:45.000426Z","iopub.status.idle":"2022-08-13T09:16:45.034903Z","shell.execute_reply.started":"2022-08-13T09:16:45.000371Z","shell.execute_reply":"2022-08-13T09:16:45.033807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Since roc_auc area under curve is close to 1 means it is very easy to seperatate train set and test set meaning the train and test are very easy to differntiate hence the train and test set are from different distribution ","metadata":{}},{"cell_type":"code","source":"sam.to_csv(\"./sam.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T03:23:18.536978Z","iopub.execute_input":"2022-08-13T03:23:18.537494Z","iopub.status.idle":"2022-08-13T03:23:18.584456Z","shell.execute_reply.started":"2022-08-13T03:23:18.537455Z","shell.execute_reply":"2022-08-13T03:23:18.583375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.to_csv(\"train_p.csv\")\nval_set.to_csv(\"val.csv\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}}]}