{"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-11T01:04:42.173006Z","iopub.execute_input":"2022-08-11T01:04:42.173598Z","iopub.status.idle":"2022-08-11T01:04:42.184578Z","shell.execute_reply.started":"2022-08-11T01:04:42.173557Z","shell.execute_reply":"2022-08-11T01:04:42.183551Z"},"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-11T01:04:42.258819Z","iopub.execute_input":"2022-08-11T01:04:42.259646Z","iopub.status.idle":"2022-08-11T01:04:42.271509Z","shell.execute_reply.started":"2022-08-11T01:04:42.259606Z","shell.execute_reply":"2022-08-11T01:04:42.269792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T01:04:42.347216Z","iopub.execute_input":"2022-08-11T01:04:42.348243Z","iopub.status.idle":"2022-08-11T01:04:42.359781Z","shell.execute_reply.started":"2022-08-11T01:04:42.348193Z","shell.execute_reply":"2022-08-11T01:04:42.358704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-08-11T01:04:42.444638Z","iopub.execute_input":"2022-08-11T01:04:42.445350Z","iopub.status.idle":"2022-08-11T01:04:42.454660Z","shell.execute_reply.started":"2022-08-11T01:04:42.445305Z","shell.execute_reply":"2022-08-11T01:04:42.453420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colunas = ['Pclass','SibSp','Parch','Fare']\nX = train[colunas]\ny = train.Survived","metadata":{"execution":{"iopub.status.busy":"2022-08-11T01:04:42.535207Z","iopub.execute_input":"2022-08-11T01:04:42.536326Z","iopub.status.idle":"2022-08-11T01:04:42.544098Z","shell.execute_reply.started":"2022-08-11T01:04:42.536272Z","shell.execute_reply":"2022-08-11T01:04:42.542747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-11T01:04:42.622160Z","iopub.execute_input":"2022-08-11T01:04:42.623233Z","iopub.status.idle":"2022-08-11T01:04:42.631264Z","shell.execute_reply.started":"2022-08-11T01:04:42.623190Z","shell.execute_reply":"2022-08-11T01:04:42.629939Z"},"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-11T01:04:42.720138Z","iopub.execute_input":"2022-08-11T01:04:42.721087Z","iopub.status.idle":"2022-08-11T01:04:42.732874Z","shell.execute_reply.started":"2022-08-11T01:04:42.721047Z","shell.execute_reply":"2022-08-11T01:04:42.731659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicoes = modelo.predict(val_X)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T01:04:42.810014Z","iopub.execute_input":"2022-08-11T01:04:42.810691Z","iopub.status.idle":"2022-08-11T01:04:42.817612Z","shell.execute_reply.started":"2022-08-11T01:04:42.810653Z","shell.execute_reply":"2022-08-11T01:04:42.816552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_y","metadata":{"execution":{"iopub.status.busy":"2022-08-11T01:04:42.898882Z","iopub.execute_input":"2022-08-11T01:04:42.899495Z","iopub.status.idle":"2022-08-11T01:04:42.907818Z","shell.execute_reply.started":"2022-08-11T01:04:42.899459Z","shell.execute_reply":"2022-08-11T01:04:42.906317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicoes","metadata":{"execution":{"iopub.status.busy":"2022-08-11T01:04:42.994906Z","iopub.execute_input":"2022-08-11T01:04:42.995535Z","iopub.status.idle":"2022-08-11T01:04:43.003251Z","shell.execute_reply.started":"2022-08-11T01:04:42.995501Z","shell.execute_reply":"2022-08-11T01:04:43.001948Z"},"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-11T01:04:43.090747Z","iopub.execute_input":"2022-08-11T01:04:43.092674Z","iopub.status.idle":"2022-08-11T01:04:43.099966Z","shell.execute_reply.started":"2022-08-11T01:04:43.092626Z","shell.execute_reply":"2022-08-11T01:04:43.098800Z"},"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-11T01:04:43.183792Z","iopub.execute_input":"2022-08-11T01:04:43.185205Z","iopub.status.idle":"2022-08-11T01:04:43.197525Z","shell.execute_reply.started":"2022-08-11T01:04:43.185158Z","shell.execute_reply":"2022-08-11T01:04:43.195886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dados_teste = pd.read_csv(\"/kaggle/input/titanic/test.csv\")\ndados_teste","metadata":{"execution":{"iopub.status.busy":"2022-08-11T01:04:43.278061Z","iopub.execute_input":"2022-08-11T01:04:43.279087Z","iopub.status.idle":"2022-08-11T01:04:43.312519Z","shell.execute_reply.started":"2022-08-11T01:04:43.279043Z","shell.execute_reply":"2022-08-11T01:04:43.311556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = dados_teste[colunas]\ntest","metadata":{"execution":{"iopub.status.busy":"2022-08-11T01:04:43.374494Z","iopub.execute_input":"2022-08-11T01:04:43.375637Z","iopub.status.idle":"2022-08-11T01:04:43.392004Z","shell.execute_reply.started":"2022-08-11T01:04:43.375594Z","shell.execute_reply":"2022-08-11T01:04:43.390791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T01:04:43.464817Z","iopub.execute_input":"2022-08-11T01:04:43.465238Z","iopub.status.idle":"2022-08-11T01:04:43.475265Z","shell.execute_reply.started":"2022-08-11T01:04:43.465203Z","shell.execute_reply":"2022-08-11T01:04:43.474331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"media = test.Fare.median()\ntest.fillna(media,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T01:04:43.566634Z","iopub.execute_input":"2022-08-11T01:04:43.567424Z","iopub.status.idle":"2022-08-11T01:04:43.576689Z","shell.execute_reply.started":"2022-08-11T01:04:43.567333Z","shell.execute_reply":"2022-08-11T01:04:43.574816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T01:04:43.664456Z","iopub.execute_input":"2022-08-11T01:04:43.664882Z","iopub.status.idle":"2022-08-11T01:04:43.674959Z","shell.execute_reply.started":"2022-08-11T01:04:43.664846Z","shell.execute_reply":"2022-08-11T01:04:43.673675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicoes = modelo.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T01:04:43.753780Z","iopub.execute_input":"2022-08-11T01:04:43.754228Z","iopub.status.idle":"2022-08-11T01:04:43.763845Z","shell.execute_reply.started":"2022-08-11T01:04:43.754186Z","shell.execute_reply":"2022-08-11T01:04:43.762523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'PassengerId': dados_teste.PassengerId,\n                       'Survived': predicoes})\noutput.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T01:04:43.846648Z","iopub.execute_input":"2022-08-11T01:04:43.847457Z","iopub.status.idle":"2022-08-11T01:04:43.856072Z","shell.execute_reply.started":"2022-08-11T01:04:43.847418Z","shell.execute_reply":"2022-08-11T01:04:43.854695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Pré-processamento","metadata":{}},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T01:04:43.926073Z","iopub.execute_input":"2022-08-11T01:04:43.926874Z","iopub.status.idle":"2022-08-11T01:04:43.944800Z","shell.execute_reply.started":"2022-08-11T01:04:43.926834Z","shell.execute_reply":"2022-08-11T01:04:43.943862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#mulheres que sobreviveram\nfem = train.loc[train.Sex == \"female\"][\"Survived\"]\nrate_fem = sum(fem)/len(fem)\n\nprint(\"% de mulheres que sobreviveram:\", rate_fem)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T01:04:43.985967Z","iopub.execute_input":"2022-08-11T01:04:43.986595Z","iopub.status.idle":"2022-08-11T01:04:43.994075Z","shell.execute_reply.started":"2022-08-11T01:04:43.986561Z","shell.execute_reply":"2022-08-11T01:04:43.993017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train[\"Survived\"])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T01:04:44.040129Z","iopub.execute_input":"2022-08-11T01:04:44.040568Z","iopub.status.idle":"2022-08-11T01:04:44.047689Z","shell.execute_reply.started":"2022-08-11T01:04:44.040533Z","shell.execute_reply":"2022-08-11T01:04:44.046576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Uma estimativa\nfrom 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-11T01:04:44.090995Z","iopub.execute_input":"2022-08-11T01:04:44.092417Z","iopub.status.idle":"2022-08-11T01:04:44.108882Z","shell.execute_reply.started":"2022-08-11T01:04:44.092349Z","shell.execute_reply":"2022-08-11T01:04:44.107624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X, y)\npredictions = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T01:04:44.122724Z","iopub.execute_input":"2022-08-11T01:04:44.123463Z","iopub.status.idle":"2022-08-11T01:04:44.339094Z","shell.execute_reply.started":"2022-08-11T01:04:44.123407Z","shell.execute_reply":"2022-08-11T01:04:44.337891Z"},"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-11T01:04:44.340896Z","iopub.execute_input":"2022-08-11T01:04:44.341307Z","iopub.status.idle":"2022-08-11T01:04:44.349894Z","shell.execute_reply.started":"2022-08-11T01:04:44.341270Z","shell.execute_reply":"2022-08-11T01:04:44.348785Z"},"trusted":true},"execution_count":null,"outputs":[]}]}