{"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-06T09:05:58.606248Z","iopub.execute_input":"2022-08-06T09:05:58.607065Z","iopub.status.idle":"2022-08-06T09:05:58.616861Z","shell.execute_reply.started":"2022-08-06T09:05:58.607015Z","shell.execute_reply":"2022-08-06T09:05:58.615579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom sklearn.preprocessing import LabelEncoder, MinMaxScaler\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.ensemble import RandomForestClassifier\nfrom catboost import CatBoostClassifier\nfrom sklearn.ensemble import GradientBoostingClassifier, ExtraTreesClassifier\nfrom sklearn.discriminant_analysis import LinearDiscriminantAnalysis\nfrom sklearn.model_selection import StratifiedKFold, cross_val_score\nfrom sklearn.metrics import roc_auc_score\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T09:05:58.700016Z","iopub.execute_input":"2022-08-06T09:05:58.700962Z","iopub.status.idle":"2022-08-06T09:05:58.707490Z","shell.execute_reply.started":"2022-08-06T09:05:58.700917Z","shell.execute_reply":"2022-08-06T09:05:58.706495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '/kaggle/input/tabular-playground-series-aug-2022/train.csv'\ntest_path = '/kaggle/input/tabular-playground-series-aug-2022/test.csv'\nsubmission_path = '/kaggle/input/tabular-playground-series-aug-2022/sample_submission.csv'","metadata":{"execution":{"iopub.status.busy":"2022-08-06T09:05:58.780753Z","iopub.execute_input":"2022-08-06T09:05:58.781412Z","iopub.status.idle":"2022-08-06T09:05:58.786281Z","shell.execute_reply.started":"2022-08-06T09:05:58.781379Z","shell.execute_reply":"2022-08-06T09:05:58.785214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(train_path)\ntest_data = pd.read_csv(test_path)\nsample_sub = pd.read_csv(submission_path)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T09:05:58.870797Z","iopub.execute_input":"2022-08-06T09:05:58.871564Z","iopub.status.idle":"2022-08-06T09:05:59.034356Z","shell.execute_reply.started":"2022-08-06T09:05:58.871518Z","shell.execute_reply":"2022-08-06T09:05:59.033263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['failure'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T09:05:59.036651Z","iopub.execute_input":"2022-08-06T09:05:59.037351Z","iopub.status.idle":"2022-08-06T09:05:59.046474Z","shell.execute_reply.started":"2022-08-06T09:05:59.037303Z","shell.execute_reply":"2022-08-06T09:05:59.045754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Train data missing value is = {} %'.format(100 * train_data.isna().sum().sum() / (len(train_data)*24)))\nprint('Test data missing value is  = {} %'.format(100 * test_data.isna().sum().sum() / (len(test_data)*24)))","metadata":{"execution":{"iopub.status.busy":"2022-08-06T09:05:59.047565Z","iopub.execute_input":"2022-08-06T09:05:59.048475Z","iopub.status.idle":"2022-08-06T09:05:59.069497Z","shell.execute_reply.started":"2022-08-06T09:05:59.048445Z","shell.execute_reply":"2022-08-06T09:05:59.068727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.fillna(train_data.mean(), inplace=True)\ntest_data.fillna(train_data.mean(), inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T09:05:59.081149Z","iopub.execute_input":"2022-08-06T09:05:59.082045Z","iopub.status.idle":"2022-08-06T09:05:59.708968Z","shell.execute_reply.started":"2022-08-06T09:05:59.081999Z","shell.execute_reply":"2022-08-06T09:05:59.707983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data['failure'] = -1\ndata = pd.concat([train_data, test_data]).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T09:05:59.710570Z","iopub.execute_input":"2022-08-06T09:05:59.710968Z","iopub.status.idle":"2022-08-06T09:05:59.740586Z","shell.execute_reply.started":"2022-08-06T09:05:59.710938Z","shell.execute_reply":"2022-08-06T09:05:59.739572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_columns = ['product_code', 'attribute_0', 'attribute_1']\nnum_columns = [col for col in train_data.columns if col not in ['failure', 'id'] + cat_columns]\nfeature_columns = cat_columns + num_columns\ntarget = 'failure'","metadata":{"execution":{"iopub.status.busy":"2022-08-06T09:05:59.741877Z","iopub.execute_input":"2022-08-06T09:05:59.742169Z","iopub.status.idle":"2022-08-06T09:05:59.747425Z","shell.execute_reply.started":"2022-08-06T09:05:59.742144Z","shell.execute_reply":"2022-08-06T09:05:59.746374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in cat_columns:\n    le = LabelEncoder()\n    le.fit(data[col])\n    train_data[col] = le.transform(train_data[col])\n    test_data[col] = le.transform(test_data[col])","metadata":{"execution":{"iopub.status.busy":"2022-08-06T09:05:59.750673Z","iopub.execute_input":"2022-08-06T09:05:59.751187Z","iopub.status.idle":"2022-08-06T09:05:59.794622Z","shell.execute_reply.started":"2022-08-06T09:05:59.751124Z","shell.execute_reply":"2022-08-06T09:05:59.793734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T09:05:59.795651Z","iopub.execute_input":"2022-08-06T09:05:59.795947Z","iopub.status.idle":"2022-08-06T09:05:59.821682Z","shell.execute_reply.started":"2022-08-06T09:05:59.795920Z","shell.execute_reply":"2022-08-06T09:05:59.820559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train_data[feature_columns]\nlabel = train_data[target]\ntest = test_data[feature_columns]","metadata":{"execution":{"iopub.status.busy":"2022-08-06T09:05:59.823140Z","iopub.execute_input":"2022-08-06T09:05:59.823867Z","iopub.status.idle":"2022-08-06T09:05:59.834993Z","shell.execute_reply.started":"2022-08-06T09:05:59.823825Z","shell.execute_reply":"2022-08-06T09:05:59.834217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def model_train(model, model_name, kfold=5):\n    oof_preds = np.zeros((train_data.shape[0]))\n    test_preds = np.zeros(test_data.shape[0])\n    skf = StratifiedKFold(n_splits=kfold)\n\n    for k, (train_index, test_index) in enumerate(skf.split(train, label)):\n        x_train, x_test = train.iloc[train_index, :], train.iloc[test_index, :]\n        y_train, y_test = label.iloc[train_index], label.iloc[test_index]\n\n        model.fit(x_train,y_train)\n\n        y_pred = model.predict_proba(x_test)[:,1]\n        oof_preds[test_index] = y_pred.ravel()\n        auc = roc_auc_score(y_test,y_pred)\n        print(\"Model = %s, KFold = %d, val_auc = %.4f\" % (model_name, k, auc))\n        test_fold_preds = model.predict_proba(test)[:, 1]\n        test_preds += test_fold_preds.ravel()\n    print(\"Overall Model = %s, AUC = %.4f\" % (model_name, roc_auc_score(label, oof_preds)))\n    return test_preds","metadata":{"execution":{"iopub.status.busy":"2022-08-06T09:05:59.836254Z","iopub.execute_input":"2022-08-06T09:05:59.836898Z","iopub.status.idle":"2022-08-06T09:05:59.845393Z","shell.execute_reply.started":"2022-08-06T09:05:59.836864Z","shell.execute_reply":"2022-08-06T09:05:59.844556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr = LogisticRegression(max_iter = 500, C=0.0001, penalty='l2', solver='newton-cg')\nlr_test_preds = model_train(lr, \"LogisticRegression\", 5) / 5","metadata":{"execution":{"iopub.status.busy":"2022-08-06T09:05:59.846516Z","iopub.execute_input":"2022-08-06T09:05:59.847074Z","iopub.status.idle":"2022-08-06T09:06:03.889337Z","shell.execute_reply.started":"2022-08-06T09:05:59.847043Z","shell.execute_reply":"2022-08-06T09:06:03.888237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rfc = RandomForestClassifier(n_estimators=50)\nrfc_test_preds = model_train(rfc, \"RandomForestClassifier\", 5) / 5","metadata":{"execution":{"iopub.status.busy":"2022-08-06T09:06:03.890667Z","iopub.execute_input":"2022-08-06T09:06:03.891370Z","iopub.status.idle":"2022-08-06T09:06:30.322162Z","shell.execute_reply.started":"2022-08-06T09:06:03.891330Z","shell.execute_reply":"2022-08-06T09:06:30.321031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cbc = CatBoostClassifier(verbose = 0)\ncbc_test_preds = model_train(cbc, \"CatBoostClassifier\", 5) / 5","metadata":{"execution":{"iopub.status.busy":"2022-08-06T09:06:30.324821Z","iopub.execute_input":"2022-08-06T09:06:30.325388Z","iopub.status.idle":"2022-08-06T09:07:10.407820Z","shell.execute_reply.started":"2022-08-06T09:06:30.325347Z","shell.execute_reply":"2022-08-06T09:07:10.406498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gbc = GradientBoostingClassifier()\ngbc_test_preds = model_train(gbc, \"GradientBoostingClassifier\", 5) / 5","metadata":{"execution":{"iopub.status.busy":"2022-08-06T09:07:10.409182Z","iopub.execute_input":"2022-08-06T09:07:10.409488Z","iopub.status.idle":"2022-08-06T09:08:06.423692Z","shell.execute_reply.started":"2022-08-06T09:07:10.409462Z","shell.execute_reply":"2022-08-06T09:08:06.422669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lda = LinearDiscriminantAnalysis()\nlda_test_preds = model_train(lda, \"LinearDiscriminantAnalysis\", 5) / 5","metadata":{"execution":{"iopub.status.busy":"2022-08-06T09:08:06.425127Z","iopub.execute_input":"2022-08-06T09:08:06.425445Z","iopub.status.idle":"2022-08-06T09:08:07.395175Z","shell.execute_reply.started":"2022-08-06T09:08:06.425417Z","shell.execute_reply":"2022-08-06T09:08:07.393774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_preds = lr_test_preds\ntest_preds = lr_test_preds * 0.9 + lda_test_preds * 0.1","metadata":{"execution":{"iopub.status.busy":"2022-08-06T09:09:03.644255Z","iopub.execute_input":"2022-08-06T09:09:03.644659Z","iopub.status.idle":"2022-08-06T09:09:03.649923Z","shell.execute_reply.started":"2022-08-06T09:09:03.644609Z","shell.execute_reply":"2022-08-06T09:09:03.648865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_preds = pd.Series(test_preds, name='failure')\nsample_sub['failure'] = test_preds\nsubmission = sample_sub.copy()\nsubmission.to_csv('submission.csv',index= False)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T09:09:32.332877Z","iopub.execute_input":"2022-08-06T09:09:32.333283Z","iopub.status.idle":"2022-08-06T09:09:32.387279Z","shell.execute_reply.started":"2022-08-06T09:09:32.333250Z","shell.execute_reply":"2022-08-06T09:09:32.386349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T09:09:40.008087Z","iopub.execute_input":"2022-08-06T09:09:40.008713Z","iopub.status.idle":"2022-08-06T09:09:40.018089Z","shell.execute_reply.started":"2022-08-06T09:09:40.008680Z","shell.execute_reply":"2022-08-06T09:09:40.017070Z"},"trusted":true},"execution_count":null,"outputs":[]}]}