{"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-05T09:26:04.958977Z","iopub.execute_input":"2022-08-05T09:26:04.959573Z","iopub.status.idle":"2022-08-05T09:26:04.994561Z","shell.execute_reply.started":"2022-08-05T09:26:04.959461Z","shell.execute_reply":"2022-08-05T09:26:04.993272Z"},"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')\nsumbission=pd.read_csv('/kaggle/input/tabular-playground-series-aug-2022/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:04.996999Z","iopub.execute_input":"2022-08-05T09:26:04.997493Z","iopub.status.idle":"2022-08-05T09:26:05.341136Z","shell.execute_reply.started":"2022-08-05T09:26:04.997447Z","shell.execute_reply":"2022-08-05T09:26:05.340168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_columns', None)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:05.342770Z","iopub.execute_input":"2022-08-05T09:26:05.343608Z","iopub.status.idle":"2022-08-05T09:26:05.348547Z","shell.execute_reply.started":"2022-08-05T09:26:05.343571Z","shell.execute_reply":"2022-08-05T09:26:05.347396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:05.350184Z","iopub.execute_input":"2022-08-05T09:26:05.351167Z","iopub.status.idle":"2022-08-05T09:26:05.402344Z","shell.execute_reply.started":"2022-08-05T09:26:05.351116Z","shell.execute_reply":"2022-08-05T09:26:05.400864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:05.405897Z","iopub.execute_input":"2022-08-05T09:26:05.406330Z","iopub.status.idle":"2022-08-05T09:26:05.425692Z","shell.execute_reply.started":"2022-08-05T09:26:05.406293Z","shell.execute_reply":"2022-08-05T09:26:05.424159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:05.428053Z","iopub.execute_input":"2022-08-05T09:26:05.428840Z","iopub.status.idle":"2022-08-05T09:26:06.768225Z","shell.execute_reply.started":"2022-08-05T09:26:05.428802Z","shell.execute_reply":"2022-08-05T09:26:06.766470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(train['failure'])","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:06.770212Z","iopub.execute_input":"2022-08-05T09:26:06.770623Z","iopub.status.idle":"2022-08-05T09:26:07.166398Z","shell.execute_reply.started":"2022-08-05T09:26:06.770577Z","shell.execute_reply":"2022-08-05T09:26:07.164967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_columns=['attribute_2','attribute_3']","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:07.168799Z","iopub.execute_input":"2022-08-05T09:26:07.169578Z","iopub.status.idle":"2022-08-05T09:26:07.176840Z","shell.execute_reply.started":"2022-08-05T09:26:07.169533Z","shell.execute_reply":"2022-08-05T09:26:07.175109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['attribute_2'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:07.180866Z","iopub.execute_input":"2022-08-05T09:26:07.181520Z","iopub.status.idle":"2022-08-05T09:26:07.199477Z","shell.execute_reply.started":"2022-08-05T09:26:07.181429Z","shell.execute_reply":"2022-08-05T09:26:07.197791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:07.202425Z","iopub.execute_input":"2022-08-05T09:26:07.204468Z","iopub.status.idle":"2022-08-05T09:26:07.235908Z","shell.execute_reply.started":"2022-08-05T09:26:07.204413Z","shell.execute_reply":"2022-08-05T09:26:07.234615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_columns=['attribute_0','attribute_1']","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:07.241328Z","iopub.execute_input":"2022-08-05T09:26:07.241707Z","iopub.status.idle":"2022-08-05T09:26:07.247475Z","shell.execute_reply.started":"2022-08-05T09:26:07.241676Z","shell.execute_reply":"2022-08-05T09:26:07.246248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()\nvar = ['attribute_0','attribute_1']\nfor i in var:\n    train[i] = le.fit_transform(train[i])\n    test[i] = le.fit_transform(test[i])","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:07.249066Z","iopub.execute_input":"2022-08-05T09:26:07.250189Z","iopub.status.idle":"2022-08-05T09:26:07.461989Z","shell.execute_reply.started":"2022-08-05T09:26:07.250141Z","shell.execute_reply":"2022-08-05T09:26:07.460953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['product_code'] = train['product_code'].replace({'A':0,\n                                      'B':1,\n                                      'C':2,\n                                      'D':3,\n                                      'E':4})\n\ntest['product_code'] = test['product_code'].replace({'F':5,\n                                      'I':6,\n                                      'G':7,\n                                      'H':8})","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:07.463995Z","iopub.execute_input":"2022-08-05T09:26:07.464786Z","iopub.status.idle":"2022-08-05T09:26:07.497631Z","shell.execute_reply.started":"2022-08-05T09:26:07.464729Z","shell.execute_reply":"2022-08-05T09:26:07.496403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:07.499299Z","iopub.execute_input":"2022-08-05T09:26:07.500329Z","iopub.status.idle":"2022-08-05T09:26:07.531617Z","shell.execute_reply.started":"2022-08-05T09:26:07.500284Z","shell.execute_reply":"2022-08-05T09:26:07.530539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:07.532893Z","iopub.execute_input":"2022-08-05T09:26:07.533805Z","iopub.status.idle":"2022-08-05T09:26:07.563070Z","shell.execute_reply.started":"2022-08-05T09:26:07.533771Z","shell.execute_reply":"2022-08-05T09:26:07.562283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.impute import SimpleImputer","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:07.564378Z","iopub.execute_input":"2022-08-05T09:26:07.564893Z","iopub.status.idle":"2022-08-05T09:26:07.813817Z","shell.execute_reply.started":"2022-08-05T09:26:07.564863Z","shell.execute_reply":"2022-08-05T09:26:07.812427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s = SimpleImputer(missing_values = np.nan, strategy = 'median')\nvar = ['loading','measurement_3','measurement_4','measurement_5','measurement_6','measurement_7','measurement_8','measurement_9','measurement_10',\n       'measurement_11','measurement_12','measurement_13','measurement_14','measurement_15','measurement_16','measurement_17']\ntrain[var] = s.fit_transform(train[var])\ntest[var] = s.transform(test[var])","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:07.815604Z","iopub.execute_input":"2022-08-05T09:26:07.816030Z","iopub.status.idle":"2022-08-05T09:26:07.943807Z","shell.execute_reply.started":"2022-08-05T09:26:07.815988Z","shell.execute_reply":"2022-08-05T09:26:07.942356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:07.945494Z","iopub.execute_input":"2022-08-05T09:26:07.945867Z","iopub.status.idle":"2022-08-05T09:26:07.963231Z","shell.execute_reply.started":"2022-08-05T09:26:07.945832Z","shell.execute_reply":"2022-08-05T09:26:07.961737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import GradientBoostingClassifier\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:07.964909Z","iopub.execute_input":"2022-08-05T09:26:07.965679Z","iopub.status.idle":"2022-08-05T09:26:08.084580Z","shell.execute_reply.started":"2022-08-05T09:26:07.965643Z","shell.execute_reply":"2022-08-05T09:26:08.082886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=train['failure']\ntrain.drop(['id','failure'],axis=1,inplace=True)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:08.086397Z","iopub.execute_input":"2022-08-05T09:26:08.087198Z","iopub.status.idle":"2022-08-05T09:26:08.163179Z","shell.execute_reply.started":"2022-08-05T09:26:08.087135Z","shell.execute_reply":"2022-08-05T09:26:08.162306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=train\ny=pd.DataFrame(y)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:08.164798Z","iopub.execute_input":"2022-08-05T09:26:08.165453Z","iopub.status.idle":"2022-08-05T09:26:08.170675Z","shell.execute_reply.started":"2022-08-05T09:26:08.165417Z","shell.execute_reply":"2022-08-05T09:26:08.169458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Handling imblance data set from SMOTE method","metadata":{}},{"cell_type":"code","source":"X=train.values","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:08.172884Z","iopub.execute_input":"2022-08-05T09:26:08.173605Z","iopub.status.idle":"2022-08-05T09:26:08.192618Z","shell.execute_reply.started":"2022-08-05T09:26:08.173553Z","shell.execute_reply":"2022-08-05T09:26:08.190866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y['failure'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:08.195155Z","iopub.execute_input":"2022-08-05T09:26:08.196459Z","iopub.status.idle":"2022-08-05T09:26:08.206918Z","shell.execute_reply.started":"2022-08-05T09:26:08.196404Z","shell.execute_reply":"2022-08-05T09:26:08.206028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from imblearn.over_sampling import SMOTE\nsmote= SMOTE(sampling_strategy='minority',random_state=42)\nx_sm,y_sm=smote.fit_resample(X,y)\ny_sm.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:08.208453Z","iopub.execute_input":"2022-08-05T09:26:08.209046Z","iopub.status.idle":"2022-08-05T09:26:09.512359Z","shell.execute_reply.started":"2022-08-05T09:26:08.209013Z","shell.execute_reply":"2022-08-05T09:26:09.511152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_test,y_train,y_test=train_test_split(x_sm,y_sm,random_state=42,test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:13.636322Z","iopub.execute_input":"2022-08-05T09:26:13.636726Z","iopub.status.idle":"2022-08-05T09:26:13.652845Z","shell.execute_reply.started":"2022-08-05T09:26:13.636695Z","shell.execute_reply":"2022-08-05T09:26:13.651935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape,x_test.shape,y_train.shape,y_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:15.694291Z","iopub.execute_input":"2022-08-05T09:26:15.694708Z","iopub.status.idle":"2022-08-05T09:26:15.703380Z","shell.execute_reply.started":"2022-08-05T09:26:15.694674Z","shell.execute_reply":"2022-08-05T09:26:15.702147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgr=GradientBoostingClassifier()\nxgr.fit(X=x_train,y=y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:16.457943Z","iopub.execute_input":"2022-08-05T09:26:16.458765Z","iopub.status.idle":"2022-08-05T09:26:42.453615Z","shell.execute_reply.started":"2022-08-05T09:26:16.458727Z","shell.execute_reply":"2022-08-05T09:26:42.452012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=xgr.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:42.456490Z","iopub.execute_input":"2022-08-05T09:26:42.457461Z","iopub.status.idle":"2022-08-05T09:26:42.479972Z","shell.execute_reply.started":"2022-08-05T09:26:42.457422Z","shell.execute_reply":"2022-08-05T09:26:42.478575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:42.481546Z","iopub.execute_input":"2022-08-05T09:26:42.481930Z","iopub.status.idle":"2022-08-05T09:26:42.490811Z","shell.execute_reply.started":"2022-08-05T09:26:42.481898Z","shell.execute_reply":"2022-08-05T09:26:42.489041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nprint(accuracy_score(pred,y_test))","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:42.494058Z","iopub.execute_input":"2022-08-05T09:26:42.494918Z","iopub.status.idle":"2022-08-05T09:26:42.504300Z","shell.execute_reply.started":"2022-08-05T09:26:42.494876Z","shell.execute_reply":"2022-08-05T09:26:42.503325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.drop(['id'],inplace=True,axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:42.505965Z","iopub.execute_input":"2022-08-05T09:26:42.506682Z","iopub.status.idle":"2022-08-05T09:26:42.518507Z","shell.execute_reply.started":"2022-08-05T09:26:42.506646Z","shell.execute_reply":"2022-08-05T09:26:42.517132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sumbission['failure']=xgr.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:42.520198Z","iopub.execute_input":"2022-08-05T09:26:42.520635Z","iopub.status.idle":"2022-08-05T09:26:42.569126Z","shell.execute_reply.started":"2022-08-05T09:26:42.520600Z","shell.execute_reply":"2022-08-05T09:26:42.567803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sumbission['failure'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:42.570769Z","iopub.execute_input":"2022-08-05T09:26:42.571154Z","iopub.status.idle":"2022-08-05T09:26:42.582597Z","shell.execute_reply.started":"2022-08-05T09:26:42.571084Z","shell.execute_reply":"2022-08-05T09:26:42.581521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sumbission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T09:26:42.584241Z","iopub.execute_input":"2022-08-05T09:26:42.584803Z","iopub.status.idle":"2022-08-05T09:26:42.617162Z","shell.execute_reply.started":"2022-08-05T09:26:42.584767Z","shell.execute_reply":"2022-08-05T09:26:42.616005Z"},"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":[]}]}