{"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":"import warnings\nwarnings.filterwarnings('ignore')\n\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom sklearn.preprocessing import OneHotEncoder, StandardScaler\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.decomposition import TruncatedSVD\nfrom sklearn.metrics import accuracy_score, confusion_matrix, ConfusionMatrixDisplay\n\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.ensemble import RandomForestClassifier","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:51:50.507139Z","iopub.execute_input":"2022-08-11T07:51:50.507502Z","iopub.status.idle":"2022-08-11T07:51:50.514820Z","shell.execute_reply.started":"2022-08-11T07:51:50.507474Z","shell.execute_reply":"2022-08-11T07:51:50.513661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/tabular-playground-series-aug-2022/train.csv\")\ntest_df = pd.read_csv(\"../input/tabular-playground-series-aug-2022/test.csv\")\n\ntest_df = test_df.drop(['attribute_1'], axis = 1)\n\ntest_df_copy = test_df.copy()\n\nprint('train set: ',train_df.shape)\nprint('test set: ',test_df.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:51:50.517414Z","iopub.execute_input":"2022-08-11T07:51:50.517858Z","iopub.status.idle":"2022-08-11T07:51:50.689086Z","shell.execute_reply.started":"2022-08-11T07:51:50.517816Z","shell.execute_reply":"2022-08-11T07:51:50.687983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:51:50.690579Z","iopub.execute_input":"2022-08-11T07:51:50.690898Z","iopub.status.idle":"2022-08-11T07:51:50.723605Z","shell.execute_reply.started":"2022-08-11T07:51:50.690870Z","shell.execute_reply":"2022-08-11T07:51:50.722546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:51:50.724882Z","iopub.execute_input":"2022-08-11T07:51:50.725232Z","iopub.status.idle":"2022-08-11T07:51:50.754079Z","shell.execute_reply.started":"2022-08-11T07:51:50.725203Z","shell.execute_reply":"2022-08-11T07:51:50.753037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:51:50.757336Z","iopub.execute_input":"2022-08-11T07:51:50.758148Z","iopub.status.idle":"2022-08-11T07:51:50.766291Z","shell.execute_reply.started":"2022-08-11T07:51:50.758102Z","shell.execute_reply":"2022-08-11T07:51:50.765034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:51:50.767830Z","iopub.execute_input":"2022-08-11T07:51:50.768167Z","iopub.status.idle":"2022-08-11T07:51:50.796316Z","shell.execute_reply.started":"2022-08-11T07:51:50.768130Z","shell.execute_reply":"2022-08-11T07:51:50.795220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:51:50.797894Z","iopub.execute_input":"2022-08-11T07:51:50.798299Z","iopub.status.idle":"2022-08-11T07:51:50.812878Z","shell.execute_reply.started":"2022-08-11T07:51:50.798267Z","shell.execute_reply":"2022-08-11T07:51:50.811648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['failure'].value_counts()\ntrain_df['failure'].value_counts().plot(kind='bar')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:51:50.813980Z","iopub.execute_input":"2022-08-11T07:51:50.814656Z","iopub.status.idle":"2022-08-11T07:51:51.035931Z","shell.execute_reply.started":"2022-08-11T07:51:50.814620Z","shell.execute_reply":"2022-08-11T07:51:51.034771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = train_df.drop(['id', 'product_code', 'failure','attribute_1'], axis=1)\ny_train = train_df['failure']\n\ntest_df = test_df.drop(['id', 'product_code'], axis=1)\n\nX_train, X_test, Y_train, Y_test = train_test_split(x_train, y_train, stratify=y_train, test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:51:51.037257Z","iopub.execute_input":"2022-08-11T07:51:51.037626Z","iopub.status.idle":"2022-08-11T07:51:51.062523Z","shell.execute_reply.started":"2022-08-11T07:51:51.037594Z","shell.execute_reply":"2022-08-11T07:51:51.061720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numric_columns=x_train.select_dtypes(exclude='object').columns\nprint(numric_columns)\nprint('*'*100)\ncategorical_columns = x_train.select_dtypes(include='object').columns\nprint(categorical_columns)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:51:51.063892Z","iopub.execute_input":"2022-08-11T07:51:51.064480Z","iopub.status.idle":"2022-08-11T07:51:51.074666Z","shell.execute_reply.started":"2022-08-11T07:51:51.064449Z","shell.execute_reply":"2022-08-11T07:51:51.073672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numeric_features = Pipeline(steps=[('handleMissing',SimpleImputer(strategy='mean')),('scaling', StandardScaler(with_mean=True))])\nprint(numeric_features)\n\ncategorical_features=Pipeline(steps=[('handlingmissing',SimpleImputer(strategy='most_frequent')),('encoding',OneHotEncoder()),('scaling',StandardScaler(with_mean=False))])\nprint(categorical_features)\n\nprocessing = ColumnTransformer([('numeic',numeric_features,numric_columns),('cat',categorical_features,categorical_columns)])\nprint(processing)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:51:51.075987Z","iopub.execute_input":"2022-08-11T07:51:51.077522Z","iopub.status.idle":"2022-08-11T07:51:51.108196Z","shell.execute_reply.started":"2022-08-11T07:51:51.077479Z","shell.execute_reply":"2022-08-11T07:51:51.107004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1=Pipeline(steps = [('processing',processing),(\"pca\",TruncatedSVD(n_components=20,random_state=0)),('modeling',LogisticRegression())])\nmodel1.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:51:51.109619Z","iopub.execute_input":"2022-08-11T07:51:51.110464Z","iopub.status.idle":"2022-08-11T07:51:51.759797Z","shell.execute_reply.started":"2022-08-11T07:51:51.110427Z","shell.execute_reply":"2022-08-11T07:51:51.758677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted1 = model1.predict(X_test)\naccuracy_score(predicted1, Y_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:51:51.764562Z","iopub.execute_input":"2022-08-11T07:51:51.765805Z","iopub.status.idle":"2022-08-11T07:51:51.798603Z","shell.execute_reply.started":"2022-08-11T07:51:51.765762Z","shell.execute_reply":"2022-08-11T07:51:51.797425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model1.predict_proba(test_df)[:,1]\npred = model1.predict(X_test)\n\ncm = confusion_matrix(Y_test,pred, labels = model1.classes_)\ndisp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels = model1.classes_)\ndisp.plot()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:51:51.800486Z","iopub.execute_input":"2022-08-11T07:51:51.801216Z","iopub.status.idle":"2022-08-11T07:51:52.102076Z","shell.execute_reply.started":"2022-08-11T07:51:51.801173Z","shell.execute_reply":"2022-08-11T07:51:52.100906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2=Pipeline(steps = [('processing',processing),(\"pca\",TruncatedSVD(n_components=20,random_state=0)),('modeling',RandomForestClassifier())])\nmodel2.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:51:52.103410Z","iopub.execute_input":"2022-08-11T07:51:52.104212Z","iopub.status.idle":"2022-08-11T07:52:09.390362Z","shell.execute_reply.started":"2022-08-11T07:51:52.104178Z","shell.execute_reply":"2022-08-11T07:52:09.389049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted2 = model2.predict(X_test)\naccuracy_score(predicted2, Y_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:52:09.391644Z","iopub.execute_input":"2022-08-11T07:52:09.392072Z","iopub.status.idle":"2022-08-11T07:52:09.656302Z","shell.execute_reply.started":"2022-08-11T07:52:09.392029Z","shell.execute_reply":"2022-08-11T07:52:09.655209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model2.predict_proba(test_df)[:,1]\npred = model2.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:52:09.657801Z","iopub.execute_input":"2022-08-11T07:52:09.658235Z","iopub.status.idle":"2022-08-11T07:52:10.446647Z","shell.execute_reply.started":"2022-08-11T07:52:09.658191Z","shell.execute_reply":"2022-08-11T07:52:10.445347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(Y_test,pred, labels = model2.classes_)\ndisp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels = model2.classes_)\ndisp.plot()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:52:10.448147Z","iopub.execute_input":"2022-08-11T07:52:10.448459Z","iopub.status.idle":"2022-08-11T07:52:10.627048Z","shell.execute_reply.started":"2022-08-11T07:52:10.448432Z","shell.execute_reply":"2022-08-11T07:52:10.625919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('../input/tabular-playground-series-aug-2022/sample_submission.csv')\nsubmission['failure'] = prediction\n\nsubmission.to_csv('submission.csv', index = False)\n\nsubmission.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T07:52:27.688808Z","iopub.execute_input":"2022-08-11T07:52:27.689228Z","iopub.status.idle":"2022-08-11T07:52:27.745806Z","shell.execute_reply.started":"2022-08-11T07:52:27.689194Z","shell.execute_reply":"2022-08-11T07:52:27.744684Z"},"trusted":true},"execution_count":null,"outputs":[]}]}