{"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":"markdown","source":"Problem statement\n\nThe August 2022 edition of the Tabular Playground Series in an opportunity to help the fictional company Keep It Dry improve its main product Super Soaker. The product is used in factories to absorb spills and leaks.\n\nThe company has just completed a large testing study for different product prototypes. Can you use this data to build a model that predicts product failures?\n","metadata":{}},{"cell_type":"markdown","source":"Import libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport os","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:09:53.044576Z","iopub.execute_input":"2022-08-01T12:09:53.045519Z","iopub.status.idle":"2022-08-01T12:09:54.160308Z","shell.execute_reply.started":"2022-08-01T12:09:53.045398Z","shell.execute_reply":"2022-08-01T12:09:54.159158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Load files","metadata":{}},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:09:54.164890Z","iopub.execute_input":"2022-08-01T12:09:54.166503Z","iopub.status.idle":"2022-08-01T12:09:54.174367Z","shell.execute_reply.started":"2022-08-01T12:09:54.166456Z","shell.execute_reply":"2022-08-01T12:09:54.173068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Read files","metadata":{}},{"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\")\nsubmission = pd.read_csv(\"/kaggle/input/tabular-playground-series-aug-2022/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:09:54.177187Z","iopub.execute_input":"2022-08-01T12:09:54.177978Z","iopub.status.idle":"2022-08-01T12:09:54.556128Z","shell.execute_reply.started":"2022-08-01T12:09:54.177934Z","shell.execute_reply":"2022-08-01T12:09:54.555269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Train dataset","metadata":{}},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:09:54.558613Z","iopub.execute_input":"2022-08-01T12:09:54.559144Z","iopub.status.idle":"2022-08-01T12:09:54.634502Z","shell.execute_reply.started":"2022-08-01T12:09:54.559111Z","shell.execute_reply":"2022-08-01T12:09:54.633582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:09:54.637478Z","iopub.execute_input":"2022-08-01T12:09:54.637965Z","iopub.status.idle":"2022-08-01T12:09:54.691453Z","shell.execute_reply.started":"2022-08-01T12:09:54.637917Z","shell.execute_reply":"2022-08-01T12:09:54.690295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:09:54.696777Z","iopub.execute_input":"2022-08-01T12:09:54.699851Z","iopub.status.idle":"2022-08-01T12:09:54.731285Z","shell.execute_reply.started":"2022-08-01T12:09:54.699797Z","shell.execute_reply":"2022-08-01T12:09:54.730093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.dropna()\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:09:54.734173Z","iopub.execute_input":"2022-08-01T12:09:54.734570Z","iopub.status.idle":"2022-08-01T12:09:54.808360Z","shell.execute_reply.started":"2022-08-01T12:09:54.734526Z","shell.execute_reply":"2022-08-01T12:09:54.807196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(train['failure'])","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:09:54.809998Z","iopub.execute_input":"2022-08-01T12:09:54.810710Z","iopub.status.idle":"2022-08-01T12:09:55.216452Z","shell.execute_reply.started":"2022-08-01T12:09:54.810666Z","shell.execute_reply":"2022-08-01T12:09:55.215063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 7))\nax = plt.subplot()\nax.scatter(train[train['failure'] == 1]['measurement_3'], train[train['failure'] == 1]['measurement_4'], c='green', s=train[train['failure'] == 1]['measurement_5'])\nax.scatter(train[train['failure'] == 0]['measurement_3'], train[train['failure'] == 0]['measurement_4'], c='red', s=train[train['failure'] == 0]['measurement_5']);\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:09:55.220191Z","iopub.execute_input":"2022-08-01T12:09:55.221025Z","iopub.status.idle":"2022-08-01T12:09:55.777937Z","shell.execute_reply.started":"2022-08-01T12:09:55.220966Z","shell.execute_reply":"2022-08-01T12:09:55.776751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target = train['failure']","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:09:55.779319Z","iopub.execute_input":"2022-08-01T12:09:55.779737Z","iopub.status.idle":"2022-08-01T12:09:55.786284Z","shell.execute_reply.started":"2022-08-01T12:09:55.779694Z","shell.execute_reply":"2022-08-01T12:09:55.785264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Test and submission","metadata":{}},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:09:55.792065Z","iopub.execute_input":"2022-08-01T12:09:55.793287Z","iopub.status.idle":"2022-08-01T12:09:55.849070Z","shell.execute_reply.started":"2022-08-01T12:09:55.793241Z","shell.execute_reply":"2022-08-01T12:09:55.847654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:09:55.850828Z","iopub.execute_input":"2022-08-01T12:09:55.851247Z","iopub.status.idle":"2022-08-01T12:09:55.871498Z","shell.execute_reply.started":"2022-08-01T12:09:55.851180Z","shell.execute_reply":"2022-08-01T12:09:55.870449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Combine train and test","metadata":{}},{"cell_type":"code","source":"combi = train.drop(['failure'], axis=1).append(test)\ncombi","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:09:55.873053Z","iopub.execute_input":"2022-08-01T12:09:55.873421Z","iopub.status.idle":"2022-08-01T12:09:55.952521Z","shell.execute_reply.started":"2022-08-01T12:09:55.873388Z","shell.execute_reply":"2022-08-01T12:09:55.951271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Drop id","metadata":{}},{"cell_type":"code","source":"combi = combi.drop(['id'], axis=1)\ncombi","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:09:55.954405Z","iopub.execute_input":"2022-08-01T12:09:55.955110Z","iopub.status.idle":"2022-08-01T12:09:56.061917Z","shell.execute_reply.started":"2022-08-01T12:09:55.955065Z","shell.execute_reply":"2022-08-01T12:09:56.060650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Check for null values","metadata":{}},{"cell_type":"code","source":"combi.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:09:56.063747Z","iopub.execute_input":"2022-08-01T12:09:56.064544Z","iopub.status.idle":"2022-08-01T12:09:56.091702Z","shell.execute_reply.started":"2022-08-01T12:09:56.064485Z","shell.execute_reply":"2022-08-01T12:09:56.089633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Impute null values","metadata":{}},{"cell_type":"code","source":"from sklearn.experimental import enable_iterative_imputer\nfrom sklearn.impute import IterativeImputer\n\nimp = IterativeImputer(random_state=42)\n\ndate = pd.Timestamp('2200-01-01')\n\nfor col in combi:\n    if combi[col].dtype==\"object\":\n        combi[col].fillna(\"not listed\", inplace=True)\n    if combi[col].dtype==\"int\":\n        #X[col].fillna(X[col].mode()[0], inplace=True)\n        combi[col].fillna(combi[col].mean(), inplace=True)\n        combi[col] = round(combi[col])\n    if combi[col].dtype=='float':\n       combi[col] = imp.fit_transform(combi[col].values.reshape(-1,1))\n    if combi[col].dtype==\"datetime64[ns]\":\n        combi[col].fillna(date, inplace=True)\ncombi\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:09:56.093951Z","iopub.execute_input":"2022-08-01T12:09:56.094398Z","iopub.status.idle":"2022-08-01T12:09:56.566757Z","shell.execute_reply.started":"2022-08-01T12:09:56.094351Z","shell.execute_reply":"2022-08-01T12:09:56.565440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combi.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:09:56.568342Z","iopub.execute_input":"2022-08-01T12:09:56.569404Z","iopub.status.idle":"2022-08-01T12:09:56.595639Z","shell.execute_reply.started":"2022-08-01T12:09:56.569355Z","shell.execute_reply":"2022-08-01T12:09:56.593809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Encode object columns","metadata":{}},{"cell_type":"code","source":"from sklearn import preprocessing\nfrom sklearn.preprocessing import OrdinalEncoder\n\nenc = OrdinalEncoder()\n\nfor col in combi:\n    if combi[col].dtype==\"object\":\n        combi[col] = enc.fit_transform(combi[col].values.reshape(-1,1))\ncombi","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:09:56.597387Z","iopub.execute_input":"2022-08-01T12:09:56.597876Z","iopub.status.idle":"2022-08-01T12:09:56.714695Z","shell.execute_reply.started":"2022-08-01T12:09:56.597833Z","shell.execute_reply":"2022-08-01T12:09:56.713354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Heatmap","metadata":{}},{"cell_type":"code","source":"corr = combi.corr()\nf, ax = plt.subplots(figsize=(12, 9))\nsns.heatmap(corr, vmax=.8, square=True);","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:09:56.716978Z","iopub.execute_input":"2022-08-01T12:09:56.717638Z","iopub.status.idle":"2022-08-01T12:09:57.577122Z","shell.execute_reply.started":"2022-08-01T12:09:56.717560Z","shell.execute_reply":"2022-08-01T12:09:57.575907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Remove columns that have a high correlation","metadata":{}},{"cell_type":"code","source":"columns = np.full((corr.shape[0],), True, dtype=bool)\nfor i in range(corr.shape[0]):\n    for j in range(i+1, corr.shape[0]):\n        if corr.iloc[i,j] >= 0.9:\n            if columns[j]:\n                columns[j] = False\nselected_columns = combi.columns[columns]\ncombi = combi[selected_columns]\ncombi","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:09:57.578833Z","iopub.execute_input":"2022-08-01T12:09:57.579448Z","iopub.status.idle":"2022-08-01T12:09:57.673401Z","shell.execute_reply.started":"2022-08-01T12:09:57.579403Z","shell.execute_reply":"2022-08-01T12:09:57.672070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Normalise","metadata":{}},{"cell_type":"code","source":"combi = (combi - combi.min()) / (combi.max() - combi.min())\ncombi","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:09:57.677246Z","iopub.execute_input":"2022-08-01T12:09:57.677831Z","iopub.status.idle":"2022-08-01T12:09:57.751598Z","shell.execute_reply.started":"2022-08-01T12:09:57.677794Z","shell.execute_reply":"2022-08-01T12:09:57.750440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Define X and y","metadata":{}},{"cell_type":"code","source":"y = target\nX = combi[: len(train)]\nX_test = combi[len(train) :]","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:09:57.753090Z","iopub.execute_input":"2022-08-01T12:09:57.754104Z","iopub.status.idle":"2022-08-01T12:09:57.760385Z","shell.execute_reply.started":"2022-08-01T12:09:57.754057Z","shell.execute_reply":"2022-08-01T12:09:57.759333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Split dataset into training and validating sets","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.1, random_state=42)\nX_train.shape, X_val.shape, y_train.shape,y_val.shape, X_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:09:57.761741Z","iopub.execute_input":"2022-08-01T12:09:57.762721Z","iopub.status.idle":"2022-08-01T12:09:57.780247Z","shell.execute_reply.started":"2022-08-01T12:09:57.762688Z","shell.execute_reply":"2022-08-01T12:09:57.778706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Select model","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\n\nmodel = RandomForestClassifier(max_depth=2, random_state=42, class_weight='balanced', n_estimators=5000).fit(X_train, y_train)\nprint(model.score(X_train, y_train))","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:09:57.781504Z","iopub.execute_input":"2022-08-01T12:09:57.782645Z","iopub.status.idle":"2022-08-01T12:10:43.252626Z","shell.execute_reply.started":"2022-08-01T12:09:57.782598Z","shell.execute_reply":"2022-08-01T12:10:43.251399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(X_val)\nprint(model.score(X_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:10:43.254268Z","iopub.execute_input":"2022-08-01T12:10:43.254966Z","iopub.status.idle":"2022-08-01T12:10:45.334180Z","shell.execute_reply.started":"2022-08-01T12:10:43.254913Z","shell.execute_reply":"2022-08-01T12:10:45.332876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\n\nprint(confusion_matrix(y_val, y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:10:45.336038Z","iopub.execute_input":"2022-08-01T12:10:45.336476Z","iopub.status.idle":"2022-08-01T12:10:45.345607Z","shell.execute_reply.started":"2022-08-01T12:10:45.336434Z","shell.execute_reply":"2022-08-01T12:10:45.344587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Predict on test set","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(X_test)\npredictions","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:10:45.347115Z","iopub.execute_input":"2022-08-01T12:10:45.347840Z","iopub.status.idle":"2022-08-01T12:10:51.700153Z","shell.execute_reply.started":"2022-08-01T12:10:45.347806Z","shell.execute_reply":"2022-08-01T12:10:51.699355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Prepare and submit","metadata":{}},{"cell_type":"code","source":"submission['failure'] = predictions\nsubmission.to_csv('submission.csv', index=False)\nmy_submission = pd.read_csv(\"submission.csv\")\nmy_submission","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:10:51.701395Z","iopub.execute_input":"2022-08-01T12:10:51.701883Z","iopub.status.idle":"2022-08-01T12:10:51.761359Z","shell.execute_reply.started":"2022-08-01T12:10:51.701851Z","shell.execute_reply":"2022-08-01T12:10:51.760422Z"},"trusted":true},"execution_count":null,"outputs":[]}]}