{"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-14T06:25:34.959345Z","iopub.execute_input":"2022-08-14T06:25:34.959773Z","iopub.status.idle":"2022-08-14T06:25:34.992879Z","shell.execute_reply.started":"2022-08-14T06:25:34.959673Z","shell.execute_reply":"2022-08-14T06:25:34.991892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-08-14T06:54:56.227967Z","iopub.execute_input":"2022-08-14T06:54:56.228649Z","iopub.status.idle":"2022-08-14T06:54:56.609192Z","shell.execute_reply.started":"2022-08-14T06:54:56.228616Z","shell.execute_reply":"2022-08-14T06:54:56.608454Z"},"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-14T06:28:21.629685Z","iopub.execute_input":"2022-08-14T06:28:21.630155Z","iopub.status.idle":"2022-08-14T06:28:21.760665Z","shell.execute_reply.started":"2022-08-14T06:28:21.630117Z","shell.execute_reply":"2022-08-14T06:28:21.760017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-14T06:29:45.227143Z","iopub.execute_input":"2022-08-14T06:29:45.227489Z","iopub.status.idle":"2022-08-14T06:29:45.256944Z","shell.execute_reply.started":"2022-08-14T06:29:45.227463Z","shell.execute_reply":"2022-08-14T06:29:45.256286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-14T06:41:56.010096Z","iopub.execute_input":"2022-08-14T06:41:56.010443Z","iopub.status.idle":"2022-08-14T06:41:56.097856Z","shell.execute_reply.started":"2022-08-14T06:41:56.010414Z","shell.execute_reply":"2022-08-14T06:41:56.096843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target = 'failure'","metadata":{"execution":{"iopub.status.busy":"2022-08-14T06:43:18.746568Z","iopub.execute_input":"2022-08-14T06:43:18.746936Z","iopub.status.idle":"2022-08-14T06:43:18.750971Z","shell.execute_reply.started":"2022-08-14T06:43:18.746905Z","shell.execute_reply":"2022-08-14T06:43:18.750212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train[target].value_counts() / len(train))","metadata":{"execution":{"iopub.status.busy":"2022-08-14T06:43:56.602199Z","iopub.execute_input":"2022-08-14T06:43:56.603158Z","iopub.status.idle":"2022-08-14T06:43:56.609921Z","shell.execute_reply.started":"2022-08-14T06:43:56.603126Z","shell.execute_reply":"2022-08-14T06:43:56.609041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-14T06:49:39.963020Z","iopub.execute_input":"2022-08-14T06:49:39.963862Z","iopub.status.idle":"2022-08-14T06:49:39.970084Z","shell.execute_reply.started":"2022-08-14T06:49:39.963823Z","shell.execute_reply":"2022-08-14T06:49:39.969117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_0 = train[train[target]==0]\ntrain_1 = train[train[target]==1]\nprint(train_0.shape,train_1.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T06:49:27.164214Z","iopub.execute_input":"2022-08-14T06:49:27.164661Z","iopub.status.idle":"2022-08-14T06:49:27.178123Z","shell.execute_reply.started":"2022-08-14T06:49:27.164625Z","shell.execute_reply":"2022-08-14T06:49:27.177040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(sns.countplot(x=train_0['attribute_0'], data=train_0))\n","metadata":{"execution":{"iopub.status.busy":"2022-08-14T06:59:56.467285Z","iopub.execute_input":"2022-08-14T06:59:56.467632Z","iopub.status.idle":"2022-08-14T06:59:56.601624Z","shell.execute_reply.started":"2022-08-14T06:59:56.467603Z","shell.execute_reply":"2022-08-14T06:59:56.600908Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x=train_1['attribute_0'], data=train_1)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T06:59:05.668402Z","iopub.execute_input":"2022-08-14T06:59:05.668782Z","iopub.status.idle":"2022-08-14T06:59:05.787778Z","shell.execute_reply.started":"2022-08-14T06:59:05.668754Z","shell.execute_reply":"2022-08-14T06:59:05.786906Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(sns.countplot(x=train_0['attribute_1'], data=train_0))","metadata":{"execution":{"iopub.status.busy":"2022-08-14T07:00:01.486311Z","iopub.execute_input":"2022-08-14T07:00:01.486644Z","iopub.status.idle":"2022-08-14T07:00:01.622543Z","shell.execute_reply.started":"2022-08-14T07:00:01.486618Z","shell.execute_reply":"2022-08-14T07:00:01.621806Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(sns.countplot(x=train_1['attribute_1'], data=train_1))","metadata":{"execution":{"iopub.status.busy":"2022-08-14T07:00:11.188849Z","iopub.execute_input":"2022-08-14T07:00:11.189833Z","iopub.status.idle":"2022-08-14T07:00:11.696599Z","shell.execute_reply.started":"2022-08-14T07:00:11.189798Z","shell.execute_reply":"2022-08-14T07:00:11.695659Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature Selection","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-14T07:12:36.439959Z","iopub.execute_input":"2022-08-14T07:12:36.440276Z","iopub.status.idle":"2022-08-14T07:12:36.446934Z","shell.execute_reply.started":"2022-08-14T07:12:36.440250Z","shell.execute_reply":"2022-08-14T07:12:36.446072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numeric_col = ['loading','measurement_17','measurement_0', 'measurement_1','measurement_2','measurement_16']","metadata":{"execution":{"iopub.status.busy":"2022-08-14T07:16:34.205345Z","iopub.execute_input":"2022-08-14T07:16:34.205708Z","iopub.status.idle":"2022-08-14T07:16:34.210370Z","shell.execute_reply.started":"2022-08-14T07:16:34.205666Z","shell.execute_reply":"2022-08-14T07:16:34.209434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[numeric_col].isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-14T07:17:54.851891Z","iopub.execute_input":"2022-08-14T07:17:54.852218Z","iopub.status.idle":"2022-08-14T07:17:54.860927Z","shell.execute_reply.started":"2022-08-14T07:17:54.852192Z","shell.execute_reply":"2022-08-14T07:17:54.859940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sampled_train = train[numeric_col]\nsampled_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-14T07:22:57.291798Z","iopub.execute_input":"2022-08-14T07:22:57.292567Z","iopub.status.idle":"2022-08-14T07:22:57.299909Z","shell.execute_reply.started":"2022-08-14T07:22:57.292534Z","shell.execute_reply":"2022-08-14T07:22:57.298760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.impute import SimpleImputer\nimputer = SimpleImputer(missing_values=np.nan, strategy='median')\n                    \n# Fitting the data, function learns the stats\nimputer = imputer.fit(sampled_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T07:23:44.857520Z","iopub.execute_input":"2022-08-14T07:23:44.858254Z","iopub.status.idle":"2022-08-14T07:23:45.092699Z","shell.execute_reply.started":"2022-08-14T07:23:44.858222Z","shell.execute_reply":"2022-08-14T07:23:45.091741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = imputer.fit_transform(sampled_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T07:25:19.123452Z","iopub.execute_input":"2022-08-14T07:25:19.123819Z","iopub.status.idle":"2022-08-14T07:25:19.149470Z","shell.execute_reply.started":"2022-08-14T07:25:19.123788Z","shell.execute_reply":"2022-08-14T07:25:19.148743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testX = test[numeric_col]\ntestX.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-14T07:29:07.784987Z","iopub.execute_input":"2022-08-14T07:29:07.786285Z","iopub.status.idle":"2022-08-14T07:29:07.796006Z","shell.execute_reply.started":"2022-08-14T07:29:07.786232Z","shell.execute_reply":"2022-08-14T07:29:07.795071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testX = imputer.fit_transform(testX)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T07:29:18.035616Z","iopub.execute_input":"2022-08-14T07:29:18.035996Z","iopub.status.idle":"2022-08-14T07:29:18.057653Z","shell.execute_reply.started":"2022-08-14T07:29:18.035966Z","shell.execute_reply":"2022-08-14T07:29:18.056968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train[[target]]\ny","metadata":{"execution":{"iopub.status.busy":"2022-08-14T07:42:35.168651Z","iopub.execute_input":"2022-08-14T07:42:35.169024Z","iopub.status.idle":"2022-08-14T07:42:35.179471Z","shell.execute_reply.started":"2022-08-14T07:42:35.168997Z","shell.execute_reply":"2022-08-14T07:42:35.178739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X1 = pd.DataFrame(X)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T07:45:20.038010Z","iopub.execute_input":"2022-08-14T07:45:20.038351Z","iopub.status.idle":"2022-08-14T07:45:20.043603Z","shell.execute_reply.started":"2022-08-14T07:45:20.038325Z","shell.execute_reply":"2022-08-14T07:45:20.042622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testX1 = pd.DataFrame(testX)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T07:46:14.438916Z","iopub.execute_input":"2022-08-14T07:46:14.439279Z","iopub.status.idle":"2022-08-14T07:46:14.444076Z","shell.execute_reply.started":"2022-08-14T07:46:14.439248Z","shell.execute_reply":"2022-08-14T07:46:14.443138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\n\nmodel = RandomForestClassifier(n_estimators=200, max_depth=25, random_state=3)\nmodel.fit(X1, y)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T07:45:37.151323Z","iopub.execute_input":"2022-08-14T07:45:37.151631Z","iopub.status.idle":"2022-08-14T07:45:49.030861Z","shell.execute_reply.started":"2022-08-14T07:45:37.151605Z","shell.execute_reply":"2022-08-14T07:45:49.030051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict_proba(testX1)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T07:46:20.506874Z","iopub.execute_input":"2022-08-14T07:46:20.507225Z","iopub.status.idle":"2022-08-14T07:46:21.277802Z","shell.execute_reply.started":"2022-08-14T07:46:20.507197Z","shell.execute_reply":"2022-08-14T07:46:21.276699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"execution":{"iopub.status.busy":"2022-08-14T07:46:23.529231Z","iopub.execute_input":"2022-08-14T07:46:23.529907Z","iopub.status.idle":"2022-08-14T07:46:23.536195Z","shell.execute_reply.started":"2022-08-14T07:46:23.529870Z","shell.execute_reply":"2022-08-14T07:46:23.535217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = pd.DataFrame(predictions)\npred","metadata":{"execution":{"iopub.status.busy":"2022-08-14T07:48:09.736234Z","iopub.execute_input":"2022-08-14T07:48:09.736585Z","iopub.status.idle":"2022-08-14T07:48:09.748178Z","shell.execute_reply.started":"2022-08-14T07:48:09.736558Z","shell.execute_reply":"2022-08-14T07:48:09.747414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred[1]","metadata":{"execution":{"iopub.status.busy":"2022-08-14T07:48:17.859375Z","iopub.execute_input":"2022-08-14T07:48:17.859713Z","iopub.status.idle":"2022-08-14T07:48:17.867056Z","shell.execute_reply.started":"2022-08-14T07:48:17.859667Z","shell.execute_reply":"2022-08-14T07:48:17.866175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred[1].round(1)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T07:49:26.358245Z","iopub.execute_input":"2022-08-14T07:49:26.358597Z","iopub.status.idle":"2022-08-14T07:49:26.367712Z","shell.execute_reply.started":"2022-08-14T07:49:26.358569Z","shell.execute_reply":"2022-08-14T07:49:26.367020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = pd.DataFrame(pred[1].round(1))\npreds\n","metadata":{"execution":{"iopub.status.busy":"2022-08-14T07:50:13.455656Z","iopub.execute_input":"2022-08-14T07:50:13.456028Z","iopub.status.idle":"2022-08-14T07:50:13.467494Z","shell.execute_reply.started":"2022-08-14T07:50:13.456001Z","shell.execute_reply":"2022-08-14T07:50:13.466551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'id': test.id, 'failure': preds[1]})\noutput.to_csv('submission.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2022-08-14T07:53:09.847060Z","iopub.execute_input":"2022-08-14T07:53:09.847442Z","iopub.status.idle":"2022-08-14T07:53:09.879787Z","shell.execute_reply.started":"2022-08-14T07:53:09.847397Z","shell.execute_reply":"2022-08-14T07:53:09.878744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}