{"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 pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:26.768838Z","iopub.execute_input":"2022-07-11T17:23:26.769259Z","iopub.status.idle":"2022-07-11T17:23:26.774159Z","shell.execute_reply.started":"2022-07-11T17:23:26.769223Z","shell.execute_reply":"2022-07-11T17:23:26.773257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Importa os dados\ntrain = pd.read_csv(\"../input/titanic/train.csv\")\ntest = pd.read_csv(\"../input/titanic/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:26.809493Z","iopub.execute_input":"2022-07-11T17:23:26.810396Z","iopub.status.idle":"2022-07-11T17:23:26.826820Z","shell.execute_reply.started":"2022-07-11T17:23:26.810359Z","shell.execute_reply":"2022-07-11T17:23:26.826101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Analize e exploração dos dados\n","metadata":{}},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:26.856811Z","iopub.execute_input":"2022-07-11T17:23:26.857753Z","iopub.status.idle":"2022-07-11T17:23:26.879957Z","shell.execute_reply.started":"2022-07-11T17:23:26.857719Z","shell.execute_reply":"2022-07-11T17:23:26.879186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:26.902862Z","iopub.execute_input":"2022-07-11T17:23:26.904719Z","iopub.status.idle":"2022-07-11T17:23:26.927858Z","shell.execute_reply.started":"2022-07-11T17:23:26.904676Z","shell.execute_reply":"2022-07-11T17:23:26.926693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Unificando data frames\nSerá unificado os data frames de test e train para fazer o preprocessamento em conjunto. \n> Esse processo alem de ganhar tempo aplicando apenas a correção em uma das bases de dados, será tambem ultil ao encontrar correlações de informaçoes faltantes, a fim de preenchelas.\n* Futuramente os dados serão separados novamente\n","metadata":{}},{"cell_type":"code","source":"TitanicFull = train.append(test, ignore_index=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:26.941423Z","iopub.execute_input":"2022-07-11T17:23:26.941812Z","iopub.status.idle":"2022-07-11T17:23:26.952117Z","shell.execute_reply.started":"2022-07-11T17:23:26.941777Z","shell.execute_reply":"2022-07-11T17:23:26.951249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TitanicFull","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:26.984935Z","iopub.execute_input":"2022-07-11T17:23:26.986030Z","iopub.status.idle":"2022-07-11T17:23:27.006966Z","shell.execute_reply.started":"2022-07-11T17:23:26.985977Z","shell.execute_reply":"2022-07-11T17:23:27.006203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Verificando o tipo de variavel de cada coluna se possui dados faltantes","metadata":{}},{"cell_type":"code","source":"TitanicFull.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:27.023776Z","iopub.execute_input":"2022-07-11T17:23:27.024130Z","iopub.status.idle":"2022-07-11T17:23:27.039446Z","shell.execute_reply.started":"2022-07-11T17:23:27.024101Z","shell.execute_reply":"2022-07-11T17:23:27.038347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('DADOS AUSENTES: ')\nTitanicFull.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:27.058704Z","iopub.execute_input":"2022-07-11T17:23:27.059374Z","iopub.status.idle":"2022-07-11T17:23:27.070313Z","shell.execute_reply.started":"2022-07-11T17:23:27.059334Z","shell.execute_reply":"2022-07-11T17:23:27.069351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Dados duplicados: ')\nTitanicFull.duplicated().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:27.132166Z","iopub.execute_input":"2022-07-11T17:23:27.132883Z","iopub.status.idle":"2022-07-11T17:23:27.143794Z","shell.execute_reply.started":"2022-07-11T17:23:27.132833Z","shell.execute_reply":"2022-07-11T17:23:27.142749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Desenvolvendo função para apresentar os dados categoricos e numericos","metadata":{}},{"cell_type":"code","source":"def plotarDadosDeMortes (df,coluna, categorico=True):\n\n    print ('\\nQuantidade de valores unicos: ', df[coluna].nunique() )\n    print ('\\nValores unicos: ', df[coluna].nunique() )\n    print ('\\nValores unicos:\\n', df[coluna].unique() )\n    print ('\\nDados faltantes: ', df[coluna].isnull().sum() )\n    print ('\\nDados valores:\\n', df[coluna].value_counts(), )\n    \n    if categorico ==True:\n        sns.countplot(data =df, x =coluna, hue='Survived',)\n        plt.legend(labels=[\"Mortos\",\"Vivos\"], title = \"Sobreviventes\")\n       # df.groupby('Survived')[coluna].value_counts().plot(kind='bar')\n    else:\n        sns.displot(df[coluna], kde=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:27.171213Z","iopub.execute_input":"2022-07-11T17:23:27.172220Z","iopub.status.idle":"2022-07-11T17:23:27.179478Z","shell.execute_reply.started":"2022-07-11T17:23:27.172163Z","shell.execute_reply":"2022-07-11T17:23:27.178054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#quantidade de mortos no desastre\nplotarDadosDeMortes(TitanicFull,'Survived')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:27.205847Z","iopub.execute_input":"2022-07-11T17:23:27.206890Z","iopub.status.idle":"2022-07-11T17:23:27.388685Z","shell.execute_reply.started":"2022-07-11T17:23:27.206840Z","shell.execute_reply":"2022-07-11T17:23:27.387680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotarDadosDeMortes(TitanicFull,'Sex')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:27.390530Z","iopub.execute_input":"2022-07-11T17:23:27.390928Z","iopub.status.idle":"2022-07-11T17:23:27.562727Z","shell.execute_reply.started":"2022-07-11T17:23:27.390897Z","shell.execute_reply":"2022-07-11T17:23:27.561831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotarDadosDeMortes(TitanicFull,'Pclass')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:27.563796Z","iopub.execute_input":"2022-07-11T17:23:27.564310Z","iopub.status.idle":"2022-07-11T17:23:27.752362Z","shell.execute_reply.started":"2022-07-11T17:23:27.564278Z","shell.execute_reply":"2022-07-11T17:23:27.751423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotarDadosDeMortes(TitanicFull,'Age',False)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:27.754596Z","iopub.execute_input":"2022-07-11T17:23:27.754904Z","iopub.status.idle":"2022-07-11T17:23:28.066311Z","shell.execute_reply.started":"2022-07-11T17:23:27.754875Z","shell.execute_reply":"2022-07-11T17:23:28.065261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Tratamento de dados faltantes","metadata":{}},{"cell_type":"code","source":"TitanicFull.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:28.067805Z","iopub.execute_input":"2022-07-11T17:23:28.068860Z","iopub.status.idle":"2022-07-11T17:23:28.078743Z","shell.execute_reply.started":"2022-07-11T17:23:28.068817Z","shell.execute_reply":"2022-07-11T17:23:28.077509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('media de idade: ',TitanicFull[\"Age\"].mean())\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:28.080087Z","iopub.execute_input":"2022-07-11T17:23:28.080536Z","iopub.status.idle":"2022-07-11T17:23:28.090112Z","shell.execute_reply.started":"2022-07-11T17:23:28.080501Z","shell.execute_reply":"2022-07-11T17:23:28.089226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Uma das formas de preencer esses dados é ultilizando a média\n\nNo entando, nem sempre esse é o melhor metodo\n\n* Um metodo mais inteligente de se resolver esse problema é verificar qual é outro dado que possui a maior correlação com a idade. Assim tentar ajustar os valores de idade de acordo com outra informação","metadata":{}},{"cell_type":"code","source":"TitanicFull.corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:28.091384Z","iopub.execute_input":"2022-07-11T17:23:28.091920Z","iopub.status.idle":"2022-07-11T17:23:28.112147Z","shell.execute_reply.started":"2022-07-11T17:23:28.091875Z","shell.execute_reply":"2022-07-11T17:23:28.110964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"A Pclasse é o dado que possui maior relação com a idade, assim:","metadata":{}},{"cell_type":"code","source":"TitanicFull[TitanicFull['Pclass']==1]['Age'].mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:28.113730Z","iopub.execute_input":"2022-07-11T17:23:28.114948Z","iopub.status.idle":"2022-07-11T17:23:28.123768Z","shell.execute_reply.started":"2022-07-11T17:23:28.114871Z","shell.execute_reply":"2022-07-11T17:23:28.122925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in TitanicFull['Pclass'].unique():\n    print(f\"A média de idade da classe {i} é de {TitanicFull[TitanicFull['Pclass']==i]['Age'].mean()}\")","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:28.125096Z","iopub.execute_input":"2022-07-11T17:23:28.125918Z","iopub.status.idle":"2022-07-11T17:23:28.135720Z","shell.execute_reply.started":"2022-07-11T17:23:28.125880Z","shell.execute_reply":"2022-07-11T17:23:28.134654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Preechendo as idedades de acordo com os valores medios de cada classe","metadata":{}},{"cell_type":"code","source":"for i in TitanicFull.index:\n     if pd.isnull(TitanicFull[\"Age\"][i]):\n            if TitanicFull['Pclass'][i] == 1:\n                  TitanicFull[\"Age\"][i] = round( TitanicFull[TitanicFull['Pclass']==1]['Age'].mean() )\n            \n            if TitanicFull['Pclass'][i] == 2:\n                  TitanicFull[\"Age\"][i] = round( TitanicFull[TitanicFull['Pclass']==2]['Age'].mean() )\n            \n            if TitanicFull['Pclass'][i] == 3:\n                  TitanicFull[\"Age\"][i] = round( TitanicFull[TitanicFull['Pclass']==3]['Age'].mean() )\n     else: continue","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:28.139360Z","iopub.execute_input":"2022-07-11T17:23:28.140136Z","iopub.status.idle":"2022-07-11T17:23:28.404983Z","shell.execute_reply.started":"2022-07-11T17:23:28.140105Z","shell.execute_reply":"2022-07-11T17:23:28.404120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TitanicFull.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:28.406369Z","iopub.execute_input":"2022-07-11T17:23:28.406656Z","iopub.status.idle":"2022-07-11T17:23:28.415405Z","shell.execute_reply.started":"2022-07-11T17:23:28.406630Z","shell.execute_reply":"2022-07-11T17:23:28.414569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotarDadosDeMortes(TitanicFull,'Embarked')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:28.416867Z","iopub.execute_input":"2022-07-11T17:23:28.417865Z","iopub.status.idle":"2022-07-11T17:23:28.611923Z","shell.execute_reply.started":"2022-07-11T17:23:28.417832Z","shell.execute_reply":"2022-07-11T17:23:28.610854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Será preenchido os dois dados faltantes com a media de idades das classes","metadata":{}},{"cell_type":"markdown","source":"Dessa forma é possivel ver que:\n* a maioria das pessoas da classe 'S' estava entre 2 e 3\n* a maioria das pessoas da classe 'C' estava entre 1 e 2\n* a maioria das pessoas da classe 'Q' estava entre 2 e 3","metadata":{}},{"cell_type":"code","source":"TitanicFull[TitanicFull['Embarked']=='S']['Pclass'].mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:28.613732Z","iopub.execute_input":"2022-07-11T17:23:28.614162Z","iopub.status.idle":"2022-07-11T17:23:28.621657Z","shell.execute_reply.started":"2022-07-11T17:23:28.614121Z","shell.execute_reply":"2022-07-11T17:23:28.620728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TitanicFull[TitanicFull['Embarked']=='C']['Pclass'].mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:28.622645Z","iopub.execute_input":"2022-07-11T17:23:28.623328Z","iopub.status.idle":"2022-07-11T17:23:28.636608Z","shell.execute_reply.started":"2022-07-11T17:23:28.623298Z","shell.execute_reply":"2022-07-11T17:23:28.635244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TitanicFull[TitanicFull['Embarked']=='Q']['Pclass'].mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:28.638697Z","iopub.execute_input":"2022-07-11T17:23:28.639231Z","iopub.status.idle":"2022-07-11T17:23:28.651885Z","shell.execute_reply.started":"2022-07-11T17:23:28.639169Z","shell.execute_reply":"2022-07-11T17:23:28.651029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TitanicFull[TitanicFull['Embarked'].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:28.652824Z","iopub.execute_input":"2022-07-11T17:23:28.653418Z","iopub.status.idle":"2022-07-11T17:23:28.672170Z","shell.execute_reply.started":"2022-07-11T17:23:28.653386Z","shell.execute_reply":"2022-07-11T17:23:28.671349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TitanicFull['Embarked'].fillna('C', inplace =True)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:28.673330Z","iopub.execute_input":"2022-07-11T17:23:28.673785Z","iopub.status.idle":"2022-07-11T17:23:28.682592Z","shell.execute_reply.started":"2022-07-11T17:23:28.673755Z","shell.execute_reply":"2022-07-11T17:23:28.681598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TitanicFull.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:28.684344Z","iopub.execute_input":"2022-07-11T17:23:28.684958Z","iopub.status.idle":"2022-07-11T17:23:28.696974Z","shell.execute_reply.started":"2022-07-11T17:23:28.684915Z","shell.execute_reply":"2022-07-11T17:23:28.696279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotarDadosDeMortes(train,'Fare',False)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:28.698041Z","iopub.execute_input":"2022-07-11T17:23:28.698825Z","iopub.status.idle":"2022-07-11T17:23:29.439899Z","shell.execute_reply.started":"2022-07-11T17:23:28.698792Z","shell.execute_reply":"2022-07-11T17:23:29.438866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"O preço tambem está ligado a classe, portanto o dado faltante será preenchido com a media da classe onde ele se encontra","metadata":{}},{"cell_type":"code","source":"TitanicFull[TitanicFull['Fare'].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:29.441306Z","iopub.execute_input":"2022-07-11T17:23:29.441711Z","iopub.status.idle":"2022-07-11T17:23:29.456481Z","shell.execute_reply.started":"2022-07-11T17:23:29.441678Z","shell.execute_reply":"2022-07-11T17:23:29.455561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TitanicFull['Fare'].fillna( TitanicFull[TitanicFull['Pclass']==3]['Fare'].mean(), inplace=True )","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:29.457639Z","iopub.execute_input":"2022-07-11T17:23:29.458454Z","iopub.status.idle":"2022-07-11T17:23:29.468723Z","shell.execute_reply.started":"2022-07-11T17:23:29.458418Z","shell.execute_reply":"2022-07-11T17:23:29.467666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TitanicFull.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:29.470138Z","iopub.execute_input":"2022-07-11T17:23:29.470484Z","iopub.status.idle":"2022-07-11T17:23:29.483808Z","shell.execute_reply.started":"2022-07-11T17:23:29.470456Z","shell.execute_reply":"2022-07-11T17:23:29.482603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sib = irmãos \n#sp = esposo ou esposa\nplotarDadosDeMortes(TitanicFull,'SibSp')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:29.484872Z","iopub.execute_input":"2022-07-11T17:23:29.485415Z","iopub.status.idle":"2022-07-11T17:23:29.718646Z","shell.execute_reply.started":"2022-07-11T17:23:29.485378Z","shell.execute_reply":"2022-07-11T17:23:29.717903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TitanicFull['Cabin'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:29.719797Z","iopub.execute_input":"2022-07-11T17:23:29.720358Z","iopub.status.idle":"2022-07-11T17:23:29.726878Z","shell.execute_reply.started":"2022-07-11T17:23:29.720328Z","shell.execute_reply":"2022-07-11T17:23:29.725845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Parch \nplotarDadosDeMortes(TitanicFull,'Parch')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:29.728087Z","iopub.execute_input":"2022-07-11T17:23:29.728411Z","iopub.status.idle":"2022-07-11T17:23:29.950848Z","shell.execute_reply.started":"2022-07-11T17:23:29.728381Z","shell.execute_reply":"2022-07-11T17:23:29.950117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Tamanho da familia\nTitanicFull['FamilySize'] = TitanicFull['SibSp']+ TitanicFull['Parch']+1","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:29.956713Z","iopub.execute_input":"2022-07-11T17:23:29.957342Z","iopub.status.idle":"2022-07-11T17:23:29.962813Z","shell.execute_reply.started":"2022-07-11T17:23:29.957307Z","shell.execute_reply":"2022-07-11T17:23:29.961708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Porcentagem de sobreviventes em cada portão","metadata":{}},{"cell_type":"markdown","source":"## Extrair dados dos nomes","metadata":{}},{"cell_type":"markdown","source":"Os dados de nomes possuem um padrão, separados por\n* \"Sobrenome, Titulo.Nome \"\n*  Braund, Mr. Owen Harris","metadata":{}},{"cell_type":"code","source":"TitanicFull['Name']","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:29.964155Z","iopub.execute_input":"2022-07-11T17:23:29.964470Z","iopub.status.idle":"2022-07-11T17:23:29.978425Z","shell.execute_reply.started":"2022-07-11T17:23:29.964442Z","shell.execute_reply":"2022-07-11T17:23:29.977458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Adicionando a coluna de titulo\nTitanicFull['titulo']= TitanicFull['Name'].apply(lambda name: name.split(',')[1].split('.')[0].strip())","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:29.979897Z","iopub.execute_input":"2022-07-11T17:23:29.980267Z","iopub.status.idle":"2022-07-11T17:23:29.988696Z","shell.execute_reply.started":"2022-07-11T17:23:29.980230Z","shell.execute_reply":"2022-07-11T17:23:29.987876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotarDadosDeMortes(TitanicFull,'titulo')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:29.989892Z","iopub.execute_input":"2022-07-11T17:23:29.990380Z","iopub.status.idle":"2022-07-11T17:23:30.302954Z","shell.execute_reply.started":"2022-07-11T17:23:29.990351Z","shell.execute_reply":"2022-07-11T17:23:30.301892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TitanicFull","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:30.304373Z","iopub.execute_input":"2022-07-11T17:23:30.304642Z","iopub.status.idle":"2022-07-11T17:23:30.330376Z","shell.execute_reply.started":"2022-07-11T17:23:30.304617Z","shell.execute_reply":"2022-07-11T17:23:30.329396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def simplificarTitulo( tit ):\n    if (tit != 'Mr' ) and (tit != 'Miss' ) and (tit != 'Mrs' ) and(tit != 'Master' ):\n        return 'outros'\n    else:\n        return tit","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:30.332371Z","iopub.execute_input":"2022-07-11T17:23:30.332769Z","iopub.status.idle":"2022-07-11T17:23:30.337958Z","shell.execute_reply.started":"2022-07-11T17:23:30.332728Z","shell.execute_reply":"2022-07-11T17:23:30.336927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TitanicFull['titulo'] = TitanicFull['titulo'].map(simplificarTitulo)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:30.339765Z","iopub.execute_input":"2022-07-11T17:23:30.340189Z","iopub.status.idle":"2022-07-11T17:23:30.349328Z","shell.execute_reply.started":"2022-07-11T17:23:30.340149Z","shell.execute_reply":"2022-07-11T17:23:30.348347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotarDadosDeMortes(TitanicFull,'titulo')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:30.351673Z","iopub.execute_input":"2022-07-11T17:23:30.352615Z","iopub.status.idle":"2022-07-11T17:23:30.553237Z","shell.execute_reply.started":"2022-07-11T17:23:30.352471Z","shell.execute_reply":"2022-07-11T17:23:30.552240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Transformando os dados categóricos em numericos","metadata":{}},{"cell_type":"markdown","source":"Um dos metodos para trabalhar com dados categóricos é transformar cada dado categorico em uma coluna de 0 e 1 ","metadata":{}},{"cell_type":"code","source":"Pclass = pd.get_dummies(TitanicFull['Pclass'],prefix ='Pclass')\ntitulo = pd.get_dummies(TitanicFull['titulo'],prefix ='titulo')\nembarked = pd.get_dummies(TitanicFull['Embarked'],prefix ='Embarked')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:30.554674Z","iopub.execute_input":"2022-07-11T17:23:30.554958Z","iopub.status.idle":"2022-07-11T17:23:30.564437Z","shell.execute_reply.started":"2022-07-11T17:23:30.554932Z","shell.execute_reply":"2022-07-11T17:23:30.563625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embarked","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:30.565808Z","iopub.execute_input":"2022-07-11T17:23:30.566233Z","iopub.status.idle":"2022-07-11T17:23:30.580318Z","shell.execute_reply.started":"2022-07-11T17:23:30.566179Z","shell.execute_reply":"2022-07-11T17:23:30.579233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TitanicFull = pd.concat([TitanicFull,Pclass,titulo,embarked],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:30.581662Z","iopub.execute_input":"2022-07-11T17:23:30.581973Z","iopub.status.idle":"2022-07-11T17:23:30.593608Z","shell.execute_reply.started":"2022-07-11T17:23:30.581946Z","shell.execute_reply":"2022-07-11T17:23:30.592783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TitanicFull","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:30.596159Z","iopub.execute_input":"2022-07-11T17:23:30.597246Z","iopub.status.idle":"2022-07-11T17:23:30.625861Z","shell.execute_reply.started":"2022-07-11T17:23:30.597213Z","shell.execute_reply":"2022-07-11T17:23:30.624952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TitanicFull.corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:30.626797Z","iopub.execute_input":"2022-07-11T17:23:30.627455Z","iopub.status.idle":"2022-07-11T17:23:30.656225Z","shell.execute_reply.started":"2022-07-11T17:23:30.627425Z","shell.execute_reply":"2022-07-11T17:23:30.655239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:30.657638Z","iopub.execute_input":"2022-07-11T17:23:30.657906Z","iopub.status.idle":"2022-07-11T17:23:31.010953Z","shell.execute_reply.started":"2022-07-11T17:23:30.657881Z","shell.execute_reply":"2022-07-11T17:23:31.009890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Cria uma variavel que receberá os treinamentos \nmodelo = RandomForestClassifier(n_estimators=100,n_jobs=-1,random_state=0)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:31.012321Z","iopub.execute_input":"2022-07-11T17:23:31.013474Z","iopub.status.idle":"2022-07-11T17:23:31.017693Z","shell.execute_reply.started":"2022-07-11T17:23:31.013436Z","shell.execute_reply":"2022-07-11T17:23:31.016928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#verificanco quais informaçoes existem em Sexo\ntrain['Sex'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:31.018857Z","iopub.execute_input":"2022-07-11T17:23:31.019353Z","iopub.status.idle":"2022-07-11T17:23:31.035532Z","shell.execute_reply.started":"2022-07-11T17:23:31.019320Z","shell.execute_reply":"2022-07-11T17:23:31.034754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Não se deve ultilizar variaveis categorias no modelo, sendo necessario transformalas em quantitativas","metadata":{}},{"cell_type":"code","source":"# O comando .map aplica uma função nos meus dados linha a linha, deste modo\n# O que for homen = 0 e mulher = 1\n\n# METODO 1\n#train['Sex'].map({'female:1,'male':0})\n\n# METODO 2 \n#train['Sex'].replace(['male','female'],[0,2])\n\n#METODO 3\ndef transformar_sexo(valor):\n    if valor == 'female':\n        return 1\n    else:\n        return 0\n    \ntrain['Sex_binario'] = train['Sex'].map(transformar_sexo)   \ntest['Sex_binario'] = test['Sex'].map(transformar_sexo)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:31.036716Z","iopub.execute_input":"2022-07-11T17:23:31.037185Z","iopub.status.idle":"2022-07-11T17:23:31.048144Z","shell.execute_reply.started":"2022-07-11T17:23:31.037156Z","shell.execute_reply":"2022-07-11T17:23:31.047254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:31.049311Z","iopub.execute_input":"2022-07-11T17:23:31.049787Z","iopub.status.idle":"2022-07-11T17:23:31.070950Z","shell.execute_reply.started":"2022-07-11T17:23:31.049758Z","shell.execute_reply":"2022-07-11T17:23:31.070245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:31.072010Z","iopub.execute_input":"2022-07-11T17:23:31.072458Z","iopub.status.idle":"2022-07-11T17:23:31.084452Z","shell.execute_reply.started":"2022-07-11T17:23:31.072432Z","shell.execute_reply":"2022-07-11T17:23:31.083579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Esse modelo levará em conta x = (sexo e a idade) para treinar o modelo\n# e y sao os dados q desejamos prever\nx = train[['Sex_binario','Age']]\ny = train['Survived']","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:31.085697Z","iopub.execute_input":"2022-07-11T17:23:31.086122Z","iopub.status.idle":"2022-07-11T17:23:31.092783Z","shell.execute_reply.started":"2022-07-11T17:23:31.086095Z","shell.execute_reply":"2022-07-11T17:23:31.091842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:31.093931Z","iopub.execute_input":"2022-07-11T17:23:31.094805Z","iopub.status.idle":"2022-07-11T17:23:31.108110Z","shell.execute_reply.started":"2022-07-11T17:23:31.094761Z","shell.execute_reply":"2022-07-11T17:23:31.106792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#subistitui todos os dados faltantes por -1\nx = x.fillna(-1)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:31.109579Z","iopub.execute_input":"2022-07-11T17:23:31.110252Z","iopub.status.idle":"2022-07-11T17:23:31.115675Z","shell.execute_reply.started":"2022-07-11T17:23:31.110188Z","shell.execute_reply":"2022-07-11T17:23:31.114417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#treina o modelo\nmodelo.fit(x,y)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:31.117136Z","iopub.execute_input":"2022-07-11T17:23:31.118267Z","iopub.status.idle":"2022-07-11T17:23:31.368679Z","shell.execute_reply.started":"2022-07-11T17:23:31.118223Z","shell.execute_reply":"2022-07-11T17:23:31.367586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:31.370368Z","iopub.execute_input":"2022-07-11T17:23:31.370769Z","iopub.status.idle":"2022-07-11T17:23:31.393046Z","shell.execute_reply.started":"2022-07-11T17:23:31.370728Z","shell.execute_reply":"2022-07-11T17:23:31.391776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#É necessario aplicar o modelo nos dados de teste\nx_prev =  test[['Sex_binario','Age']]\nx_prev = x_prev.fillna(-1)\nx_prev.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:31.394312Z","iopub.execute_input":"2022-07-11T17:23:31.394904Z","iopub.status.idle":"2022-07-11T17:23:31.405937Z","shell.execute_reply.started":"2022-07-11T17:23:31.394865Z","shell.execute_reply":"2022-07-11T17:23:31.404900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p = modelo.predict(x_prev)\np","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:31.407361Z","iopub.execute_input":"2022-07-11T17:23:31.407861Z","iopub.status.idle":"2022-07-11T17:23:31.518962Z","shell.execute_reply.started":"2022-07-11T17:23:31.407830Z","shell.execute_reply":"2022-07-11T17:23:31.517638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(data=p)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:31.520723Z","iopub.execute_input":"2022-07-11T17:23:31.521150Z","iopub.status.idle":"2022-07-11T17:23:31.532503Z","shell.execute_reply.started":"2022-07-11T17:23:31.521109Z","shell.execute_reply":"2022-07-11T17:23:31.531601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:31.533883Z","iopub.execute_input":"2022-07-11T17:23:31.534169Z","iopub.status.idle":"2022-07-11T17:23:31.555946Z","shell.execute_reply.started":"2022-07-11T17:23:31.534143Z","shell.execute_reply":"2022-07-11T17:23:31.554922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Converte o array em serie\npd.Series(p).head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:31.557111Z","iopub.execute_input":"2022-07-11T17:23:31.557418Z","iopub.status.idle":"2022-07-11T17:23:31.569138Z","shell.execute_reply.started":"2022-07-11T17:23:31.557390Z","shell.execute_reply":"2022-07-11T17:23:31.568143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Enviar os arquivos para a competiçao do Kaggle \n\n# Declara o indice como o indice do passageiro\n# Name muda o nome da coluna\n\nsub = pd.Series(p,index=test['PassengerId'], name = 'Survived')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:31.570585Z","iopub.execute_input":"2022-07-11T17:23:31.571428Z","iopub.status.idle":"2022-07-11T17:23:31.579374Z","shell.execute_reply.started":"2022-07-11T17:23:31.571382Z","shell.execute_reply":"2022-07-11T17:23:31.578376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# o header serve para salvar a primeira linha como titulo\nsub.to_csv('./primeiro_modelo.csv',header=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:31.580833Z","iopub.execute_input":"2022-07-11T17:23:31.582497Z","iopub.status.idle":"2022-07-11T17:23:31.593003Z","shell.execute_reply.started":"2022-07-11T17:23:31.582443Z","shell.execute_reply":"2022-07-11T17:23:31.592111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv('./primeiro_modelo.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:31.594478Z","iopub.execute_input":"2022-07-11T17:23:31.595273Z","iopub.status.idle":"2022-07-11T17:23:31.609351Z","shell.execute_reply.started":"2022-07-11T17:23:31.595228Z","shell.execute_reply":"2022-07-11T17:23:31.608340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\n\n#biblioteca para separação dos dados em treinamento e test\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:31.610740Z","iopub.execute_input":"2022-07-11T17:23:31.611333Z","iopub.status.idle":"2022-07-11T17:23:31.620879Z","shell.execute_reply.started":"2022-07-11T17:23:31.611289Z","shell.execute_reply":"2022-07-11T17:23:31.620181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# random.seed(0) define que os numeros aleatorios serão sempre uma sequencia já definida\nnp.random.seed(0)\n\n# divide a variavel x em (x_treino e x_valid)\n#em seguida\n# divide a variavel y em (y_treino e y_valid)\n\n# \nx_treino,x_valid,y_treino,y_valid = train_test_split(x,y,test_size=0.5)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:31.621963Z","iopub.execute_input":"2022-07-11T17:23:31.622509Z","iopub.status.idle":"2022-07-11T17:23:31.634508Z","shell.execute_reply.started":"2022-07-11T17:23:31.622478Z","shell.execute_reply":"2022-07-11T17:23:31.633473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_treino.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:31.637381Z","iopub.execute_input":"2022-07-11T17:23:31.638258Z","iopub.status.idle":"2022-07-11T17:23:31.650279Z","shell.execute_reply.started":"2022-07-11T17:23:31.638176Z","shell.execute_reply":"2022-07-11T17:23:31.649136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_treino.shape,x_valid.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:31.651711Z","iopub.execute_input":"2022-07-11T17:23:31.652412Z","iopub.status.idle":"2022-07-11T17:23:31.664544Z","shell.execute_reply.started":"2022-07-11T17:23:31.652368Z","shell.execute_reply":"2022-07-11T17:23:31.663775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelo = RandomForestClassifier(n_estimators=100,n_jobs=-1,random_state=0)\nmodelo.fit(x_treino,y_treino)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:31.665472Z","iopub.execute_input":"2022-07-11T17:23:31.666355Z","iopub.status.idle":"2022-07-11T17:23:31.916297Z","shell.execute_reply.started":"2022-07-11T17:23:31.666323Z","shell.execute_reply":"2022-07-11T17:23:31.915502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p=modelo.predict(x_valid)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:31.917981Z","iopub.execute_input":"2022-07-11T17:23:31.918404Z","iopub.status.idle":"2022-07-11T17:23:32.028576Z","shell.execute_reply.started":"2022-07-11T17:23:31.918363Z","shell.execute_reply":"2022-07-11T17:23:32.027436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Acuracia do modelo\nnp.mean(y_valid == p)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.029795Z","iopub.execute_input":"2022-07-11T17:23:32.030100Z","iopub.status.idle":"2022-07-11T17:23:32.037694Z","shell.execute_reply.started":"2022-07-11T17:23:32.030073Z","shell.execute_reply":"2022-07-11T17:23:32.036437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Acuracia para todas as mulheres sobreviverem\np = (x_valid['Sex_binario']==1).astype(np.int64)\nnp.mean(y_valid==p)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.039373Z","iopub.execute_input":"2022-07-11T17:23:32.040036Z","iopub.status.idle":"2022-07-11T17:23:32.052868Z","shell.execute_reply.started":"2022-07-11T17:23:32.039981Z","shell.execute_reply":"2022-07-11T17:23:32.051570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Validação cruzada - metodos de reamostragem\n","metadata":{}},{"cell_type":"markdown","source":"# INICIO\n","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.054100Z","iopub.execute_input":"2022-07-11T17:23:32.054883Z","iopub.status.idle":"2022-07-11T17:23:32.064789Z","shell.execute_reply.started":"2022-07-11T17:23:32.054847Z","shell.execute_reply":"2022-07-11T17:23:32.063756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Importa os dados\ntrain = pd.read_csv(\"../input/titanic/train.csv\")\ntest = pd.read_csv(\"../input/titanic/test.csv\")\ngender_submission = pd.read_csv(\"../input/titanic/gender_submission.csv\")\nprimeiro_modelo = pd.read_csv(\"./primeiro_modelo.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.065880Z","iopub.execute_input":"2022-07-11T17:23:32.066683Z","iopub.status.idle":"2022-07-11T17:23:32.095295Z","shell.execute_reply.started":"2022-07-11T17:23:32.066640Z","shell.execute_reply":"2022-07-11T17:23:32.094252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#como os dados devem estar formatados para a submissão\ngender_submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.097093Z","iopub.execute_input":"2022-07-11T17:23:32.097633Z","iopub.status.idle":"2022-07-11T17:23:32.106175Z","shell.execute_reply.started":"2022-07-11T17:23:32.097587Z","shell.execute_reply":"2022-07-11T17:23:32.105466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.107071Z","iopub.execute_input":"2022-07-11T17:23:32.107614Z","iopub.status.idle":"2022-07-11T17:23:32.128684Z","shell.execute_reply.started":"2022-07-11T17:23:32.107582Z","shell.execute_reply":"2022-07-11T17:23:32.127947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.129611Z","iopub.execute_input":"2022-07-11T17:23:32.130376Z","iopub.status.idle":"2022-07-11T17:23:32.144161Z","shell.execute_reply.started":"2022-07-11T17:23:32.130345Z","shell.execute_reply":"2022-07-11T17:23:32.143341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.145758Z","iopub.execute_input":"2022-07-11T17:23:32.146385Z","iopub.status.idle":"2022-07-11T17:23:32.160351Z","shell.execute_reply.started":"2022-07-11T17:23:32.146334Z","shell.execute_reply":"2022-07-11T17:23:32.159268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.161976Z","iopub.execute_input":"2022-07-11T17:23:32.162414Z","iopub.status.idle":"2022-07-11T17:23:32.176251Z","shell.execute_reply.started":"2022-07-11T17:23:32.162362Z","shell.execute_reply":"2022-07-11T17:23:32.175052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Colocando survived na primeira coluna\ntrain = train[[ 'Survived','PassengerId', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp','Parch', 'Ticket', 'Fare', 'Cabin', 'Embarked']]","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.187022Z","iopub.execute_input":"2022-07-11T17:23:32.188533Z","iopub.status.idle":"2022-07-11T17:23:32.194323Z","shell.execute_reply.started":"2022-07-11T17:23:32.188475Z","shell.execute_reply":"2022-07-11T17:23:32.193433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.195482Z","iopub.execute_input":"2022-07-11T17:23:32.195907Z","iopub.status.idle":"2022-07-11T17:23:32.214635Z","shell.execute_reply.started":"2022-07-11T17:23:32.195880Z","shell.execute_reply":"2022-07-11T17:23:32.213646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#TRANSFORMANDO A COLUNA SEXO EM DADOS 1 e 0\ndef transformSexBool(sexo):\n    if sexo == 'male':\n        return 1\n    else:\n        return 0\ntrain['SexBool'] = train['Sex'].map(transformSexBool)\ntest['SexBool'] = test['Sex'].map(transformSexBool)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.215615Z","iopub.execute_input":"2022-07-11T17:23:32.216545Z","iopub.status.idle":"2022-07-11T17:23:32.223510Z","shell.execute_reply.started":"2022-07-11T17:23:32.216512Z","shell.execute_reply":"2022-07-11T17:23:32.222490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.224622Z","iopub.execute_input":"2022-07-11T17:23:32.225119Z","iopub.status.idle":"2022-07-11T17:23:32.249049Z","shell.execute_reply.started":"2022-07-11T17:23:32.225089Z","shell.execute_reply":"2022-07-11T17:23:32.247973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.groupby(\"Pclass\").count()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.250367Z","iopub.execute_input":"2022-07-11T17:23:32.251392Z","iopub.status.idle":"2022-07-11T17:23:32.267163Z","shell.execute_reply.started":"2022-07-11T17:23:32.251347Z","shell.execute_reply":"2022-07-11T17:23:32.266226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.groupby(\"Pclass\").sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.268378Z","iopub.execute_input":"2022-07-11T17:23:32.268673Z","iopub.status.idle":"2022-07-11T17:23:32.283184Z","shell.execute_reply.started":"2022-07-11T17:23:32.268646Z","shell.execute_reply":"2022-07-11T17:23:32.282343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.284263Z","iopub.execute_input":"2022-07-11T17:23:32.285103Z","iopub.status.idle":"2022-07-11T17:23:32.298924Z","shell.execute_reply.started":"2022-07-11T17:23:32.285073Z","shell.execute_reply":"2022-07-11T17:23:32.297871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Cabin'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.300169Z","iopub.execute_input":"2022-07-11T17:23:32.300580Z","iopub.status.idle":"2022-07-11T17:23:32.307971Z","shell.execute_reply.started":"2022-07-11T17:23:32.300547Z","shell.execute_reply":"2022-07-11T17:23:32.306769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transformando dados categoricos em numericos\nfrom sklearn.preprocessing import LabelEncoder\nlabel_cabin_encoder = LabelEncoder()\nlabel_cabin = label_cabin_encoder.fit_transform(train.Cabin)\ntrain['label_cabin'] = label_cabin\n\n\nlabel_Embarked_encoder = LabelEncoder()\nlabel_Embarked = label_Embarked_encoder.fit_transform(train.Embarked)\ntrain['label_Embarked'] = label_Embarked\n","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.309808Z","iopub.execute_input":"2022-07-11T17:23:32.310495Z","iopub.status.idle":"2022-07-11T17:23:32.323135Z","shell.execute_reply.started":"2022-07-11T17:23:32.310453Z","shell.execute_reply":"2022-07-11T17:23:32.322250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.sort_values(by='Pclass') #Crescente","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.324285Z","iopub.execute_input":"2022-07-11T17:23:32.324988Z","iopub.status.idle":"2022-07-11T17:23:32.354259Z","shell.execute_reply.started":"2022-07-11T17:23:32.324948Z","shell.execute_reply":"2022-07-11T17:23:32.353459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.355393Z","iopub.execute_input":"2022-07-11T17:23:32.356160Z","iopub.status.idle":"2022-07-11T17:23:32.375857Z","shell.execute_reply.started":"2022-07-11T17:23:32.356130Z","shell.execute_reply":"2022-07-11T17:23:32.374874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.377031Z","iopub.execute_input":"2022-07-11T17:23:32.377334Z","iopub.status.idle":"2022-07-11T17:23:32.388244Z","shell.execute_reply.started":"2022-07-11T17:23:32.377308Z","shell.execute_reply":"2022-07-11T17:23:32.387010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#quais dados sao faltantes dados faltantes \ntrain.loc[pd.isnull(train['Age'])]\ntest.loc[pd.isnull(test['Age'])]","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.389666Z","iopub.execute_input":"2022-07-11T17:23:32.390754Z","iopub.status.idle":"2022-07-11T17:23:32.416798Z","shell.execute_reply.started":"2022-07-11T17:23:32.390706Z","shell.execute_reply":"2022-07-11T17:23:32.415949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Subistituir dados faltantes pela media\ntrain['Age'].fillna(train['Age'].mean(), inplace=True)\ntest['Age'].fillna(test['Age'].mean(), inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.418124Z","iopub.execute_input":"2022-07-11T17:23:32.418706Z","iopub.status.idle":"2022-07-11T17:23:32.424622Z","shell.execute_reply.started":"2022-07-11T17:23:32.418674Z","shell.execute_reply":"2022-07-11T17:23:32.423623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Subistituir dados faltantes pela media\ntrain['Age'] = train['Age'].fillna( train['Age'].mean() )\ntrain.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.426100Z","iopub.execute_input":"2022-07-11T17:23:32.426402Z","iopub.status.idle":"2022-07-11T17:23:32.447111Z","shell.execute_reply.started":"2022-07-11T17:23:32.426376Z","shell.execute_reply":"2022-07-11T17:23:32.445352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.448152Z","iopub.execute_input":"2022-07-11T17:23:32.448844Z","iopub.status.idle":"2022-07-11T17:23:32.461123Z","shell.execute_reply.started":"2022-07-11T17:23:32.448813Z","shell.execute_reply":"2022-07-11T17:23:32.460297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"treino_base = train[['PassengerId', 'Pclass', 'Age', 'SibSp','Parch', 'Fare', 'SexBool']]\nvalid_base = train['Survived']","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.462280Z","iopub.execute_input":"2022-07-11T17:23:32.462770Z","iopub.status.idle":"2022-07-11T17:23:32.469519Z","shell.execute_reply.started":"2022-07-11T17:23:32.462739Z","shell.execute_reply":"2022-07-11T17:23:32.468511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.470879Z","iopub.execute_input":"2022-07-11T17:23:32.471184Z","iopub.status.idle":"2022-07-11T17:23:32.481938Z","shell.execute_reply.started":"2022-07-11T17:23:32.471158Z","shell.execute_reply":"2022-07-11T17:23:32.480950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Divide os dados em em 70% de um lado 30% do outro onde existe \n#e cada uma dessas separações são divididas em treino e validação\nx_treino, x_valid, y_treino, y_valid = train_test_split( treino_base, valid_base, test_size=0.7)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.483366Z","iopub.execute_input":"2022-07-11T17:23:32.483643Z","iopub.status.idle":"2022-07-11T17:23:32.494727Z","shell.execute_reply.started":"2022-07-11T17:23:32.483617Z","shell.execute_reply":"2022-07-11T17:23:32.493821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_treino.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.495816Z","iopub.execute_input":"2022-07-11T17:23:32.496573Z","iopub.status.idle":"2022-07-11T17:23:32.506667Z","shell.execute_reply.started":"2022-07-11T17:23:32.496543Z","shell.execute_reply":"2022-07-11T17:23:32.505744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.507601Z","iopub.execute_input":"2022-07-11T17:23:32.508290Z","iopub.status.idle":"2022-07-11T17:23:32.516083Z","shell.execute_reply.started":"2022-07-11T17:23:32.508253Z","shell.execute_reply":"2022-07-11T17:23:32.515370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#declara uma variavel para recever os parametros da arvore\narvore_titanic = DecisionTreeClassifier(criterion='entropy')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.516977Z","iopub.execute_input":"2022-07-11T17:23:32.517731Z","iopub.status.idle":"2022-07-11T17:23:32.527093Z","shell.execute_reply.started":"2022-07-11T17:23:32.517699Z","shell.execute_reply":"2022-07-11T17:23:32.526076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arvore_titanic.fit(x_treino,y_treino)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.528376Z","iopub.execute_input":"2022-07-11T17:23:32.528747Z","iopub.status.idle":"2022-07-11T17:23:32.545705Z","shell.execute_reply.started":"2022-07-11T17:23:32.528717Z","shell.execute_reply":"2022-07-11T17:23:32.544597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"previsoes = arvore_titanic.predict(x_valid)\nprevisoes","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.546747Z","iopub.execute_input":"2022-07-11T17:23:32.547550Z","iopub.status.idle":"2022-07-11T17:23:32.559580Z","shell.execute_reply.started":"2022-07-11T17:23:32.547518Z","shell.execute_reply":"2022-07-11T17:23:32.558526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_valid","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.560669Z","iopub.execute_input":"2022-07-11T17:23:32.561404Z","iopub.status.idle":"2022-07-11T17:23:32.570766Z","shell.execute_reply.started":"2022-07-11T17:23:32.561361Z","shell.execute_reply":"2022-07-11T17:23:32.569719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(previsoes,y_valid)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.572262Z","iopub.execute_input":"2022-07-11T17:23:32.572874Z","iopub.status.idle":"2022-07-11T17:23:32.585239Z","shell.execute_reply.started":"2022-07-11T17:23:32.572831Z","shell.execute_reply":"2022-07-11T17:23:32.584065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# APLICANDO NO MODELO REAL","metadata":{}},{"cell_type":"code","source":"#TRANSFORMANDO A COLUNA SEXO EM DADOS 1 e 0\ndef transformSexBool(sexo):\n    if sexo == 'male':\n        return 1\n    else:\n        return 0\ntrain['SexBool'] = train['Sex'].map(transformSexBool)\ntest['SexBool'] = test['Sex'].map(transformSexBool)\n\n\n#Subistituir dados faltantes pela media\ntrain['Age'].fillna(train['Age'].mean(), inplace=True)\ntest['Age'].fillna(test['Age'].mean(), inplace=True)\ntest['Fare'].fillna(test['Fare'].mean(), inplace=True)\n\n#separa as classes que o estudo deseja ser feito\ntreino_base_treinamento = train[['PassengerId', 'Pclass', 'Age', 'SibSp','Parch', 'Fare', 'SexBool']]\ntest_base_previsao =  test[['PassengerId', 'Pclass', 'Age', 'SibSp','Parch', 'Fare', 'SexBool']]\n\nvalid_base_treinamento = train['Survived']\n\n#aplica a arvore\nfrom sklearn.ensemble import RandomForestClassifier\narvore_titanic = RandomForestClassifier(criterion='entropy')\narvore_titanic.fit(treino_base_treinamento,valid_base_treinamento)\n\n#KNN\nfrom sklearn.neighbors import KNeighborsClassifier\nknn_titanic = KNeighborsClassifier(n_neighbors=100, metric='minkowski', p = 100)\nknn_titanic.fit(treino_base_treinamento,valid_base_treinamento)\n\n#deepLearn\nfrom sklearn.neural_network import MLPClassifier\nredeNeural_Titanic = MLPClassifier(max_iter=1500, tol=0.00000100,\n                                   solver = 'adam', activation = 'relu',\n                                   hidden_layer_sizes = (200,200))\nredeNeural_Titanic.fit(treino_base_treinamento,valid_base_treinamento)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:32.586786Z","iopub.execute_input":"2022-07-11T17:23:32.587483Z","iopub.status.idle":"2022-07-11T17:23:33.519315Z","shell.execute_reply.started":"2022-07-11T17:23:32.587440Z","shell.execute_reply":"2022-07-11T17:23:33.518102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Verifica a acuracia dos modelos\nprevisoes_arvore = arvore_titanic.predict(test_base_previsao)\nprevisoes_knn = knn_titanic.predict(test_base_previsao)\nprevisoes_redeNeural = redeNeural_Titanic.predict(test_base_previsao)\n\nfrom sklearn.metrics import accuracy_score\nprint(accuracy_score(previsoes_arvore,gender_submission['Survived']))\nprint(accuracy_score(previsoes_knn,gender_submission['Survived']))\nprint(accuracy_score(previsoes_redeNeural,gender_submission['Survived']))","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:33.524707Z","iopub.execute_input":"2022-07-11T17:23:33.525553Z","iopub.status.idle":"2022-07-11T17:23:33.643479Z","shell.execute_reply.started":"2022-07-11T17:23:33.525498Z","shell.execute_reply":"2022-07-11T17:23:33.642275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"previsoes","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:33.644988Z","iopub.execute_input":"2022-07-11T17:23:33.645741Z","iopub.status.idle":"2022-07-11T17:23:33.656748Z","shell.execute_reply.started":"2022-07-11T17:23:33.645691Z","shell.execute_reply":"2022-07-11T17:23:33.655414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gender_submission","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:33.658529Z","iopub.execute_input":"2022-07-11T17:23:33.659257Z","iopub.status.idle":"2022-07-11T17:23:33.674710Z","shell.execute_reply.started":"2022-07-11T17:23:33.659208Z","shell.execute_reply":"2022-07-11T17:23:33.673219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#pd.DataFrame(previsoes,index=gender_submission['PassengerId'])\nmodelo_arvore = pd.DataFrame(data=previsoes_arvore,index=gender_submission['PassengerId'], columns=['Survived'])\nmodelo_arvore.to_csv ('./modelo_arvore.csv')\n\nmodelo_knn = pd.DataFrame(data=previsoes_knn,index=gender_submission['PassengerId'], columns=['Survived'])\nmodelo_knn.to_csv ('./modelo_knn.csv')\n\nmodelo_redeNeural = pd.DataFrame(data=previsoes_redeNeural,index=gender_submission['PassengerId'], columns=['Survived'])\nmodelo_redeNeural.to_csv ('./modelo_redeNeural.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:33.676524Z","iopub.execute_input":"2022-07-11T17:23:33.677282Z","iopub.status.idle":"2022-07-11T17:23:33.694886Z","shell.execute_reply.started":"2022-07-11T17:23:33.677232Z","shell.execute_reply":"2022-07-11T17:23:33.693532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelo_knn","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:23:33.696928Z","iopub.execute_input":"2022-07-11T17:23:33.697849Z","iopub.status.idle":"2022-07-11T17:23:33.711950Z","shell.execute_reply.started":"2022-07-11T17:23:33.697796Z","shell.execute_reply":"2022-07-11T17:23:33.710747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}