{"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-14T19:54:39.264210Z","iopub.execute_input":"2022-08-14T19:54:39.264814Z","iopub.status.idle":"2022-08-14T19:54:39.274125Z","shell.execute_reply.started":"2022-08-14T19:54:39.264747Z","shell.execute_reply":"2022-08-14T19:54:39.273073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/tabular-playground-series-aug-2022/train.csv')\ndf_test = pd.read_csv('/kaggle/input/tabular-playground-series-aug-2022/test.csv')\ndf_subm= pd.read_csv(\"../input/tabular-playground-series-aug-2022/sample_submission.csv\")\n\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-14T19:54:39.334256Z","iopub.execute_input":"2022-08-14T19:54:39.334788Z","iopub.status.idle":"2022-08-14T19:54:39.581431Z","shell.execute_reply.started":"2022-08-14T19:54:39.334743Z","shell.execute_reply":"2022-08-14T19:54:39.580215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.describe().T","metadata":{"execution":{"iopub.status.busy":"2022-08-14T19:54:39.584266Z","iopub.execute_input":"2022-08-14T19:54:39.584901Z","iopub.status.idle":"2022-08-14T19:54:39.707304Z","shell.execute_reply.started":"2022-08-14T19:54:39.584813Z","shell.execute_reply":"2022-08-14T19:54:39.705747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Gráfico de dados com faltando","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nfrom matplotlib import pyplot as plt\n\ndf_train_miss = df_train.isna().sum()\ndf_test_miss = df_test.isna().sum()\n\ndf_miss = pd.concat([df_train_miss, df_test_miss], keys=['train', 'test'], axis=0).reset_index()\ndf_miss.columns = ['data', 'columns', 'missing']\n\n\nplt.figure(figsize=(10, 12))\nsns.barplot(data=df_miss,y='columns', x='missing', hue='data')","metadata":{"execution":{"iopub.status.busy":"2022-08-14T19:54:39.709123Z","iopub.execute_input":"2022-08-14T19:54:39.709632Z","iopub.status.idle":"2022-08-14T19:54:40.294173Z","shell.execute_reply.started":"2022-08-14T19:54:39.709535Z","shell.execute_reply":"2022-08-14T19:54:40.292590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Analisando colunas númericas","metadata":{}},{"cell_type":"code","source":"df_train['data'] = 'train'\ndf_test['data'] = 'test'\ndf_combine = pd.concat([df_train, df_test]).reset_index()\n\nfloat_features = [col for col in df_train.columns if 'float' in str(type(df_train[col].dtype))]\n\nplt.subplots(figsize=(25,25))\nfor i, col in enumerate(float_features):\n    plt.subplot(5,5, i + 1)\n    sns.histplot(data=df_combine, x=col, hue='data', kde=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T19:54:40.297637Z","iopub.execute_input":"2022-08-14T19:54:40.298396Z","iopub.status.idle":"2022-08-14T19:54:58.538682Z","shell.execute_reply.started":"2022-08-14T19:54:40.298346Z","shell.execute_reply":"2022-08-14T19:54:58.537294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Ajustando coluna loading","metadata":{}},{"cell_type":"code","source":"df_train['loading'] = np.log(df_train['loading'])\ndf_test['loading'] = np.log(df_test['loading'])\n\nsns.histplot(data=df_train, x='loading')","metadata":{"execution":{"iopub.status.busy":"2022-08-14T19:54:58.540015Z","iopub.execute_input":"2022-08-14T19:54:58.540379Z","iopub.status.idle":"2022-08-14T19:54:58.926469Z","shell.execute_reply.started":"2022-08-14T19:54:58.540347Z","shell.execute_reply":"2022-08-14T19:54:58.925239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Correlação","metadata":{}},{"cell_type":"code","source":"df_train.drop(columns = 'id', inplace = True)\ndf_test.drop(columns = 'id', inplace = True)\n\nplt.subplots(figsize=(15,15))\nsns.heatmap(df_train.corr(), annot=True, fmt='.2f')","metadata":{"execution":{"iopub.status.busy":"2022-08-14T19:54:58.928031Z","iopub.execute_input":"2022-08-14T19:54:58.928490Z","iopub.status.idle":"2022-08-14T19:55:01.350192Z","shell.execute_reply.started":"2022-08-14T19:54:58.928449Z","shell.execute_reply":"2022-08-14T19:55:01.348913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Ajustando os dados","metadata":{}},{"cell_type":"code","source":"from sklearn.impute import SimpleImputer\n\nimputer_num = SimpleImputer(strategy=\"mean\")\nfeat_float = df_train.select_dtypes(np.float64).columns\nimputer_num.fit(df_train[feat_float])\ndf_train[feat_float] = imputer_num.transform(df_train[feat_float])\ndf_test[feat_float] = imputer_num.transform(df_test[feat_float])","metadata":{"execution":{"iopub.status.busy":"2022-08-14T19:55:01.352246Z","iopub.execute_input":"2022-08-14T19:55:01.352830Z","iopub.status.idle":"2022-08-14T19:55:01.388523Z","shell.execute_reply.started":"2022-08-14T19:55:01.352794Z","shell.execute_reply":"2022-08-14T19:55:01.387251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\nlabel_encoder = LabelEncoder()\ndf_train_enc = df_train.copy()\ndf_test_enc = df_test.copy()\n\nfeat_object = df_train.select_dtypes(object).columns\n\nfor col in feat_object:\n        df_train_enc[col] = label_encoder.fit_transform(df_train[col])\n        df_test_enc[col] = label_encoder.fit_transform(df_test[col]) \n        \ndf_train = df_train_enc\ndf_test = df_test_enc","metadata":{"execution":{"iopub.status.busy":"2022-08-14T19:55:01.390388Z","iopub.execute_input":"2022-08-14T19:55:01.391487Z","iopub.status.idle":"2022-08-14T19:55:01.459038Z","shell.execute_reply.started":"2022-08-14T19:55:01.391446Z","shell.execute_reply":"2022-08-14T19:55:01.457739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Definindo o datasetfeat_float = df_train.select_dtypes(np.float).columns\n","metadata":{}},{"cell_type":"code","source":"X = df_train.drop(['failure'], axis=1)\ny = df_train['failure']","metadata":{"execution":{"iopub.status.busy":"2022-08-14T19:55:01.460973Z","iopub.execute_input":"2022-08-14T19:55:01.461523Z","iopub.status.idle":"2022-08-14T19:55:01.469455Z","shell.execute_reply.started":"2022-08-14T19:55:01.461489Z","shell.execute_reply":"2022-08-14T19:55:01.468307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_test , y_train , y_test = train_test_split(X,y,test_size=0.2,random_state=42)\n\nprint('X_train:', X_train.shape)\nprint('y_train:', y_train.shape)\nprint('X_test:', X_test.shape)\nprint('y_test:', y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T19:55:01.472900Z","iopub.execute_input":"2022-08-14T19:55:01.473626Z","iopub.status.idle":"2022-08-14T19:55:01.492110Z","shell.execute_reply.started":"2022-08-14T19:55:01.473590Z","shell.execute_reply":"2022-08-14T19:55:01.490239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Regressão Logistica","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\n\nmodel = LogisticRegression(penalty='l1', solver = 'liblinear', C= 0.01,class_weight = 'balanced',\n                              max_iter=200, tol=0.0001, n_jobs=-1, random_state=123)\n\nmodel.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T19:55:01.494975Z","iopub.execute_input":"2022-08-14T19:55:01.496347Z","iopub.status.idle":"2022-08-14T19:55:10.361991Z","shell.execute_reply.started":"2022-08-14T19:55:01.496293Z","shell.execute_reply":"2022-08-14T19:55:10.360265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=model.predict_proba(df_test)\ny_pred","metadata":{"execution":{"iopub.status.busy":"2022-08-14T19:55:10.363713Z","iopub.execute_input":"2022-08-14T19:55:10.364149Z","iopub.status.idle":"2022-08-14T19:55:10.386292Z","shell.execute_reply.started":"2022-08-14T19:55:10.364114Z","shell.execute_reply":"2022-08-14T19:55:10.384689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_subm['failure'] = y_pred[:,1]\ndf_subm.to_csv('submission.csv', index=False)\n\ndf_subm","metadata":{"execution":{"iopub.status.busy":"2022-08-14T19:55:10.388840Z","iopub.execute_input":"2022-08-14T19:55:10.390335Z","iopub.status.idle":"2022-08-14T19:55:10.520753Z","shell.execute_reply.started":"2022-08-14T19:55:10.390270Z","shell.execute_reply":"2022-08-14T19:55:10.519836Z"},"trusted":true},"execution_count":null,"outputs":[]}]}