{"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-12T04:09:09.194597Z","iopub.execute_input":"2022-08-12T04:09:09.194985Z","iopub.status.idle":"2022-08-12T04:09:09.205808Z","shell.execute_reply.started":"2022-08-12T04:09:09.194953Z","shell.execute_reply":"2022-08-12T04:09:09.204357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/titanic/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-12T04:09:09.211912Z","iopub.execute_input":"2022-08-12T04:09:09.212297Z","iopub.status.idle":"2022-08-12T04:09:09.225318Z","shell.execute_reply.started":"2022-08-12T04:09:09.212264Z","shell.execute_reply":"2022-08-12T04:09:09.223898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#quantidade de nulos em cada coluna\ntrain.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T04:09:09.227518Z","iopub.execute_input":"2022-08-12T04:09:09.228479Z","iopub.status.idle":"2022-08-12T04:09:09.238870Z","shell.execute_reply.started":"2022-08-12T04:09:09.228444Z","shell.execute_reply":"2022-08-12T04:09:09.237831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-08-12T04:09:09.240918Z","iopub.execute_input":"2022-08-12T04:09:09.241510Z","iopub.status.idle":"2022-08-12T04:09:09.250497Z","shell.execute_reply.started":"2022-08-12T04:09:09.241478Z","shell.execute_reply":"2022-08-12T04:09:09.249372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#escolher as colunas que serão usadas no treinamento\ncolunas = ['Pclass','SibSp','Parch','Fare']\nX = train[colunas]\ny = train.Survived","metadata":{"execution":{"iopub.status.busy":"2022-08-12T04:09:09.252097Z","iopub.execute_input":"2022-08-12T04:09:09.252832Z","iopub.status.idle":"2022-08-12T04:09:09.259479Z","shell.execute_reply.started":"2022-08-12T04:09:09.252794Z","shell.execute_reply":"2022-08-12T04:09:09.258135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"há a necessidade de um pré-processamento nos outros campos","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_X, val_X, train_y, val_y = train_test_split(X,y,random_state = 0)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T04:09:09.261890Z","iopub.execute_input":"2022-08-12T04:09:09.262429Z","iopub.status.idle":"2022-08-12T04:09:09.276629Z","shell.execute_reply.started":"2022-08-12T04:09:09.262381Z","shell.execute_reply":"2022-08-12T04:09:09.275386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier\nmodelo = DecisionTreeClassifier(random_state=1)\nmodelo.fit(train_X,train_y)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T04:09:09.278330Z","iopub.execute_input":"2022-08-12T04:09:09.278912Z","iopub.status.idle":"2022-08-12T04:09:09.296175Z","shell.execute_reply.started":"2022-08-12T04:09:09.278880Z","shell.execute_reply":"2022-08-12T04:09:09.294273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#testando nos dados de teste\npredicoes = modelo.predict(val_X)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T04:09:09.298216Z","iopub.execute_input":"2022-08-12T04:09:09.299045Z","iopub.status.idle":"2022-08-12T04:09:09.308911Z","shell.execute_reply.started":"2022-08-12T04:09:09.298996Z","shell.execute_reply":"2022-08-12T04:09:09.307642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_y","metadata":{"execution":{"iopub.status.busy":"2022-08-12T04:09:09.310751Z","iopub.execute_input":"2022-08-12T04:09:09.311463Z","iopub.status.idle":"2022-08-12T04:09:09.321413Z","shell.execute_reply.started":"2022-08-12T04:09:09.311413Z","shell.execute_reply":"2022-08-12T04:09:09.320364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicoes","metadata":{"execution":{"iopub.status.busy":"2022-08-12T04:09:09.322937Z","iopub.execute_input":"2022-08-12T04:09:09.323479Z","iopub.status.idle":"2022-08-12T04:09:09.336279Z","shell.execute_reply.started":"2022-08-12T04:09:09.323444Z","shell.execute_reply":"2022-08-12T04:09:09.334653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sklearn.metrics as metrics\nmetrics.accuracy_score(val_y, predicoes)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T04:09:09.340182Z","iopub.execute_input":"2022-08-12T04:09:09.340674Z","iopub.status.idle":"2022-08-12T04:09:09.349873Z","shell.execute_reply.started":"2022-08-12T04:09:09.340637Z","shell.execute_reply":"2022-08-12T04:09:09.348408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report\nprint(classification_report(val_y,predicoes))","metadata":{"execution":{"iopub.status.busy":"2022-08-12T04:09:09.351960Z","iopub.execute_input":"2022-08-12T04:09:09.353516Z","iopub.status.idle":"2022-08-12T04:09:09.367492Z","shell.execute_reply.started":"2022-08-12T04:09:09.353457Z","shell.execute_reply":"2022-08-12T04:09:09.365757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Fazer a previsão do conjunto de testes e enviar para o placar","metadata":{}},{"cell_type":"code","source":"dados_teste = pd.read_csv(\"/kaggle/input/titanic/test.csv\")\ndados_teste","metadata":{"execution":{"iopub.status.busy":"2022-08-12T04:09:09.369693Z","iopub.execute_input":"2022-08-12T04:09:09.370547Z","iopub.status.idle":"2022-08-12T04:09:09.401193Z","shell.execute_reply.started":"2022-08-12T04:09:09.370497Z","shell.execute_reply":"2022-08-12T04:09:09.399834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = dados_teste[colunas]\ntest","metadata":{"execution":{"iopub.status.busy":"2022-08-12T04:09:09.403240Z","iopub.execute_input":"2022-08-12T04:09:09.404015Z","iopub.status.idle":"2022-08-12T04:09:09.421355Z","shell.execute_reply.started":"2022-08-12T04:09:09.403968Z","shell.execute_reply":"2022-08-12T04:09:09.420149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T04:09:09.423417Z","iopub.execute_input":"2022-08-12T04:09:09.424097Z","iopub.status.idle":"2022-08-12T04:09:09.434759Z","shell.execute_reply.started":"2022-08-12T04:09:09.424048Z","shell.execute_reply":"2022-08-12T04:09:09.433320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#preencher os valores nulos com a mediana\nmedia = test.Fare.median()\ntest.fillna(media,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T04:09:09.436141Z","iopub.execute_input":"2022-08-12T04:09:09.437302Z","iopub.status.idle":"2022-08-12T04:09:09.447933Z","shell.execute_reply.started":"2022-08-12T04:09:09.437267Z","shell.execute_reply":"2022-08-12T04:09:09.446834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T04:09:09.449962Z","iopub.execute_input":"2022-08-12T04:09:09.450811Z","iopub.status.idle":"2022-08-12T04:09:09.463866Z","shell.execute_reply.started":"2022-08-12T04:09:09.450776Z","shell.execute_reply":"2022-08-12T04:09:09.462870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicoes = modelo.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T04:09:09.465165Z","iopub.execute_input":"2022-08-12T04:09:09.465837Z","iopub.status.idle":"2022-08-12T04:09:09.473728Z","shell.execute_reply.started":"2022-08-12T04:09:09.465795Z","shell.execute_reply":"2022-08-12T04:09:09.472468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#produzir o arquivo de resposta\noutput = pd.DataFrame({'PassengerId': dados_teste.PassengerId,\n                       'Survived': predicoes})\noutput.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T04:09:09.475407Z","iopub.execute_input":"2022-08-12T04:09:09.475786Z","iopub.status.idle":"2022-08-12T04:09:09.490292Z","shell.execute_reply.started":"2022-08-12T04:09:09.475754Z","shell.execute_reply":"2022-08-12T04:09:09.489138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idade=train.loc[train.Age>=30][\"Survived\"]\nrate_idade=sum(idade)/len(idade)\nprint(\"%sobreviventes acima de 30 anos\", rate_idade)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T04:09:09.491833Z","iopub.execute_input":"2022-08-12T04:09:09.492767Z","iopub.status.idle":"2022-08-12T04:09:09.504082Z","shell.execute_reply.started":"2022-08-12T04:09:09.492716Z","shell.execute_reply":"2022-08-12T04:09:09.502114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\n\ny = train[\"Survived\"]\n\nfeatures = [\"Pclass\", \"Sex\", \"SibSp\", \"Parch\"]\nX = pd.get_dummies(train[features])\nX_test = pd.get_dummies(dados_teste[features])\n\nmodel = RandomForestClassifier(n_estimators=100, max_depth=6, random_state=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T04:09:09.505755Z","iopub.execute_input":"2022-08-12T04:09:09.506375Z","iopub.status.idle":"2022-08-12T04:09:09.527056Z","shell.execute_reply.started":"2022-08-12T04:09:09.506328Z","shell.execute_reply":"2022-08-12T04:09:09.526050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X, y)\npredictions = model.predict(X_test)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-12T04:09:09.528738Z","iopub.execute_input":"2022-08-12T04:09:09.529331Z","iopub.status.idle":"2022-08-12T04:09:09.720322Z","shell.execute_reply.started":"2022-08-12T04:09:09.529296Z","shell.execute_reply":"2022-08-12T04:09:09.718980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'PassengerId': dados_teste.PassengerId, 'Survived': predictions})\noutput.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T04:09:09.721914Z","iopub.execute_input":"2022-08-12T04:09:09.722304Z","iopub.status.idle":"2022-08-12T04:09:09.730995Z","shell.execute_reply.started":"2022-08-12T04:09:09.722270Z","shell.execute_reply":"2022-08-12T04:09:09.729933Z"},"trusted":true},"execution_count":null,"outputs":[]}]}