{"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-11T20:29:59.789506Z","iopub.execute_input":"2022-08-11T20:29:59.790000Z","iopub.status.idle":"2022-08-11T20:29:59.799081Z","shell.execute_reply.started":"2022-08-11T20:29:59.789958Z","shell.execute_reply":"2022-08-11T20:29:59.798082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/titanic/train.csv\")\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:30:03.787503Z","iopub.execute_input":"2022-08-11T20:30:03.787894Z","iopub.status.idle":"2022-08-11T20:30:03.811702Z","shell.execute_reply.started":"2022-08-11T20:30:03.787862Z","shell.execute_reply":"2022-08-11T20:30:03.810510Z"},"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-11T20:30:07.504726Z","iopub.execute_input":"2022-08-11T20:30:07.505155Z","iopub.status.idle":"2022-08-11T20:30:07.519151Z","shell.execute_reply.started":"2022-08-11T20:30:07.505120Z","shell.execute_reply":"2022-08-11T20:30:07.518197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:30:10.121270Z","iopub.execute_input":"2022-08-11T20:30:10.121790Z","iopub.status.idle":"2022-08-11T20:30:10.133317Z","shell.execute_reply.started":"2022-08-11T20:30:10.121742Z","shell.execute_reply":"2022-08-11T20:30:10.131818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe().transpose()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:30:13.178696Z","iopub.execute_input":"2022-08-11T20:30:13.179094Z","iopub.status.idle":"2022-08-11T20:30:13.218514Z","shell.execute_reply.started":"2022-08-11T20:30:13.179063Z","shell.execute_reply":"2022-08-11T20:30:13.217258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mediaIdade = train['Age'].mean()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:30:16.851524Z","iopub.execute_input":"2022-08-11T20:30:16.851951Z","iopub.status.idle":"2022-08-11T20:30:16.857936Z","shell.execute_reply.started":"2022-08-11T20:30:16.851918Z","shell.execute_reply":"2022-08-11T20:30:16.856943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Substituindo os dados nulos do campo Age com a média das idades\ntrain.update(train['Age'].fillna(mediaIdade))","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:30:19.456620Z","iopub.execute_input":"2022-08-11T20:30:19.457033Z","iopub.status.idle":"2022-08-11T20:30:19.466780Z","shell.execute_reply.started":"2022-08-11T20:30:19.457000Z","shell.execute_reply":"2022-08-11T20:30:19.465846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Age'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:30:21.424522Z","iopub.execute_input":"2022-08-11T20:30:21.424907Z","iopub.status.idle":"2022-08-11T20:30:21.432087Z","shell.execute_reply.started":"2022-08-11T20:30:21.424875Z","shell.execute_reply":"2022-08-11T20:30:21.431341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Substituindo as cabines não informadas pelo valor numérico -1\ntrain.update(train['Cabin'].fillna(-1))","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:30:29.448838Z","iopub.execute_input":"2022-08-11T20:30:29.449709Z","iopub.status.idle":"2022-08-11T20:30:29.459587Z","shell.execute_reply.started":"2022-08-11T20:30:29.449666Z","shell.execute_reply":"2022-08-11T20:30:29.458238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Cabin'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:30:31.433354Z","iopub.execute_input":"2022-08-11T20:30:31.433724Z","iopub.status.idle":"2022-08-11T20:30:31.441495Z","shell.execute_reply.started":"2022-08-11T20:30:31.433695Z","shell.execute_reply":"2022-08-11T20:30:31.440551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ASeparando por Faixa Etária \n#for i in train['Age']:\n#  try:\n#    a = int(i)\n#    if (a >= 18) & (a < 24):\n#      train.Age = train.Age.replace(i, \"1\")\n#    elif (a >= 24) & (a < 45):\n#        train.Age = train.Age.replace(i, \"2\")\n#    else:\n#        train.Age = train.Age.replace(i, \"3\")\n#  except:\n#    pass","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:12:31.584229Z","iopub.execute_input":"2022-08-11T19:12:31.584740Z","iopub.status.idle":"2022-08-11T19:12:31.590285Z","shell.execute_reply.started":"2022-08-11T19:12:31.584704Z","shell.execute_reply":"2022-08-11T19:12:31.589100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Quantidade de pessoas por Faixa Etária\n#train.Age.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:12:35.180864Z","iopub.execute_input":"2022-08-11T19:12:35.181579Z","iopub.status.idle":"2022-08-11T19:12:35.186207Z","shell.execute_reply.started":"2022-08-11T19:12:35.181538Z","shell.execute_reply":"2022-08-11T19:12:35.184590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Substitui Sexo por valor numérico\ntrain['Sex'] = train['Sex'].map({'female': 1,'male': 2})","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:30:35.465635Z","iopub.execute_input":"2022-08-11T20:30:35.466430Z","iopub.status.idle":"2022-08-11T20:30:35.472912Z","shell.execute_reply.started":"2022-08-11T20:30:35.466387Z","shell.execute_reply":"2022-08-11T20:30:35.472002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:30:38.431917Z","iopub.execute_input":"2022-08-11T20:30:38.432322Z","iopub.status.idle":"2022-08-11T20:30:38.450415Z","shell.execute_reply.started":"2022-08-11T20:30:38.432286Z","shell.execute_reply":"2022-08-11T20:30:38.449166Z"},"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":"#escolher as colunas que serão usadas no treinamento\ncolunas = ['PassengerId','Age','Sex','Pclass','SibSp','Parch','Fare']\nX = train[colunas]\ny = train.Survived","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:41:19.725441Z","iopub.execute_input":"2022-08-11T20:41:19.725861Z","iopub.status.idle":"2022-08-11T20:41:19.732723Z","shell.execute_reply.started":"2022-08-11T20:41:19.725825Z","shell.execute_reply":"2022-08-11T20:41:19.731288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:41:21.609400Z","iopub.execute_input":"2022-08-11T20:41:21.609833Z","iopub.status.idle":"2022-08-11T20:41:21.624018Z","shell.execute_reply.started":"2022-08-11T20:41:21.609797Z","shell.execute_reply":"2022-08-11T20:41:21.622940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:41:24.445617Z","iopub.execute_input":"2022-08-11T20:41:24.445995Z","iopub.status.idle":"2022-08-11T20:41:24.454406Z","shell.execute_reply.started":"2022-08-11T20:41:24.445965Z","shell.execute_reply":"2022-08-11T20:41:24.453268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:41:27.451791Z","iopub.execute_input":"2022-08-11T20:41:27.452212Z","iopub.status.idle":"2022-08-11T20:41:27.457353Z","shell.execute_reply.started":"2022-08-11T20:41:27.452157Z","shell.execute_reply":"2022-08-11T20:41:27.456135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelo = RandomForestClassifier(n_estimators=100, n_jobs=-1, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:41:30.199602Z","iopub.execute_input":"2022-08-11T20:41:30.200016Z","iopub.status.idle":"2022-08-11T20:41:30.205677Z","shell.execute_reply.started":"2022-08-11T20:41:30.199975Z","shell.execute_reply":"2022-08-11T20:41:30.204775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelo.fit(X,y)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:41:32.241448Z","iopub.execute_input":"2022-08-11T20:41:32.241928Z","iopub.status.idle":"2022-08-11T20:41:32.600616Z","shell.execute_reply.started":"2022-08-11T20:41:32.241894Z","shell.execute_reply":"2022-08-11T20:41:32.599428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dados_teste = pd.read_csv(\"/kaggle/input/titanic/test.csv\")\ndados_teste.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:41:34.624540Z","iopub.execute_input":"2022-08-11T20:41:34.624958Z","iopub.status.idle":"2022-08-11T20:41:34.645581Z","shell.execute_reply.started":"2022-08-11T20:41:34.624919Z","shell.execute_reply":"2022-08-11T20:41:34.644639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = dados_teste[colunas]\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:41:37.874366Z","iopub.execute_input":"2022-08-11T20:41:37.874789Z","iopub.status.idle":"2022-08-11T20:41:37.891327Z","shell.execute_reply.started":"2022-08-11T20:41:37.874753Z","shell.execute_reply":"2022-08-11T20:41:37.889997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Substitui Sexo por valor numérico\ntest['Sex'] = test['Sex'].map({'female': 1,'male': 2})\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:41:41.464622Z","iopub.execute_input":"2022-08-11T20:41:41.465031Z","iopub.status.idle":"2022-08-11T20:41:41.480879Z","shell.execute_reply.started":"2022-08-11T20:41:41.464998Z","shell.execute_reply":"2022-08-11T20:41:41.479809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:41:48.083627Z","iopub.execute_input":"2022-08-11T20:41:48.084018Z","iopub.status.idle":"2022-08-11T20:41:48.094799Z","shell.execute_reply.started":"2022-08-11T20:41:48.083987Z","shell.execute_reply":"2022-08-11T20:41:48.093505Z"},"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-11T20:41:50.092531Z","iopub.execute_input":"2022-08-11T20:41:50.092937Z","iopub.status.idle":"2022-08-11T20:41:50.101043Z","shell.execute_reply.started":"2022-08-11T20:41:50.092906Z","shell.execute_reply":"2022-08-11T20:41:50.099800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:41:52.020988Z","iopub.execute_input":"2022-08-11T20:41:52.021556Z","iopub.status.idle":"2022-08-11T20:41:52.033857Z","shell.execute_reply.started":"2022-08-11T20:41:52.021507Z","shell.execute_reply":"2022-08-11T20:41:52.032598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:41:53.895361Z","iopub.execute_input":"2022-08-11T20:41:53.895959Z","iopub.status.idle":"2022-08-11T20:41:53.919592Z","shell.execute_reply.started":"2022-08-11T20:41:53.895900Z","shell.execute_reply":"2022-08-11T20:41:53.918024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_X = test[colunas]\n","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:41:56.225384Z","iopub.execute_input":"2022-08-11T20:41:56.226432Z","iopub.status.idle":"2022-08-11T20:41:56.232899Z","shell.execute_reply.started":"2022-08-11T20:41:56.226389Z","shell.execute_reply":"2022-08-11T20:41:56.231329Z"},"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-11T20:33:46.129064Z","iopub.execute_input":"2022-08-11T20:33:46.129489Z","iopub.status.idle":"2022-08-11T20:33:46.239762Z","shell.execute_reply.started":"2022-08-11T20:33:46.129453Z","shell.execute_reply":"2022-08-11T20:33:46.238845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicoes","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:42:00.396547Z","iopub.execute_input":"2022-08-11T20:42:00.396972Z","iopub.status.idle":"2022-08-11T20:42:00.404602Z","shell.execute_reply.started":"2022-08-11T20:42:00.396935Z","shell.execute_reply":"2022-08-11T20:42:00.403732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:42:02.932598Z","iopub.execute_input":"2022-08-11T20:42:02.933760Z","iopub.status.idle":"2022-08-11T20:42:02.948308Z","shell.execute_reply.started":"2022-08-11T20:42:02.933703Z","shell.execute_reply":"2022-08-11T20:42:02.946900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.Series(predicoes, index=test['PassengerId'])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:42:15.857644Z","iopub.execute_input":"2022-08-11T20:42:15.858048Z","iopub.status.idle":"2022-08-11T20:42:15.869350Z","shell.execute_reply.started":"2022-08-11T20:42:15.858017Z","shell.execute_reply":"2022-08-11T20:42:15.868049Z"},"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-11T20:44:24.739000Z","iopub.execute_input":"2022-08-11T20:44:24.739436Z","iopub.status.idle":"2022-08-11T20:44:24.753241Z","shell.execute_reply.started":"2022-08-11T20:44:24.739397Z","shell.execute_reply":"2022-08-11T20:44:24.752290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!head -n10 submission.csv","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:44:19.558539Z","iopub.execute_input":"2022-08-11T20:44:19.559138Z","iopub.status.idle":"2022-08-11T20:44:20.717541Z","shell.execute_reply.started":"2022-08-11T20:44:19.559102Z","shell.execute_reply":"2022-08-11T20:44:20.715689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Fazer a previsão do conjunto de testes e enviar para o placar","metadata":{}}]}