{"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-09T03:22:54.784308Z","iopub.execute_input":"2022-08-09T03:22:54.784689Z","iopub.status.idle":"2022-08-09T03:22:54.793345Z","shell.execute_reply.started":"2022-08-09T03:22:54.784658Z","shell.execute_reply":"2022-08-09T03:22:54.792248Z"},"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-09T03:23:02.257895Z","iopub.execute_input":"2022-08-09T03:23:02.259165Z","iopub.status.idle":"2022-08-09T03:23:02.283937Z","shell.execute_reply.started":"2022-08-09T03:23:02.259113Z","shell.execute_reply":"2022-08-09T03:23:02.283029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-08-09T03:23:09.595237Z","iopub.execute_input":"2022-08-09T03:23:09.595967Z","iopub.status.idle":"2022-08-09T03:23:09.631583Z","shell.execute_reply.started":"2022-08-09T03:23:09.595921Z","shell.execute_reply":"2022-08-09T03:23:09.630580Z"},"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-09T03:23:16.039840Z","iopub.execute_input":"2022-08-09T03:23:16.041175Z","iopub.status.idle":"2022-08-09T03:23:16.057357Z","shell.execute_reply.started":"2022-08-09T03:23:16.041119Z","shell.execute_reply":"2022-08-09T03:23:16.055901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-08-09T03:23:19.435484Z","iopub.execute_input":"2022-08-09T03:23:19.436213Z","iopub.status.idle":"2022-08-09T03:23:19.445109Z","shell.execute_reply.started":"2022-08-09T03:23:19.436176Z","shell.execute_reply":"2022-08-09T03:23:19.444051Z"},"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-09T03:23:23.806714Z","iopub.execute_input":"2022-08-09T03:23:23.807979Z","iopub.status.idle":"2022-08-09T03:23:23.814276Z","shell.execute_reply.started":"2022-08-09T03:23:23.807935Z","shell.execute_reply":"2022-08-09T03:23:23.812947Z"},"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-09T03:23:28.035265Z","iopub.execute_input":"2022-08-09T03:23:28.035671Z","iopub.status.idle":"2022-08-09T03:23:28.619755Z","shell.execute_reply.started":"2022-08-09T03:23:28.035638Z","shell.execute_reply":"2022-08-09T03:23:28.618497Z"},"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-09T03:23:33.163515Z","iopub.execute_input":"2022-08-09T03:23:33.163953Z","iopub.status.idle":"2022-08-09T03:23:33.328147Z","shell.execute_reply.started":"2022-08-09T03:23:33.163917Z","shell.execute_reply":"2022-08-09T03:23:33.327137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.tree import ExtraTreeClassifier\nmodelo = ExtraTreeClassifier(random_state=1)\nmodelo.fit(train_X,train_y)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T03:23:36.743519Z","iopub.execute_input":"2022-08-09T03:23:36.743933Z","iopub.status.idle":"2022-08-09T03:23:36.755010Z","shell.execute_reply.started":"2022-08-09T03:23:36.743899Z","shell.execute_reply":"2022-08-09T03:23:36.754162Z"},"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-09T03:23:40.513110Z","iopub.execute_input":"2022-08-09T03:23:40.513533Z","iopub.status.idle":"2022-08-09T03:23:40.521009Z","shell.execute_reply.started":"2022-08-09T03:23:40.513498Z","shell.execute_reply":"2022-08-09T03:23:40.520158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_y","metadata":{"execution":{"iopub.status.busy":"2022-08-09T03:23:46.186444Z","iopub.execute_input":"2022-08-09T03:23:46.186863Z","iopub.status.idle":"2022-08-09T03:23:46.195458Z","shell.execute_reply.started":"2022-08-09T03:23:46.186826Z","shell.execute_reply":"2022-08-09T03:23:46.194450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicoes","metadata":{"execution":{"iopub.status.busy":"2022-08-09T03:23:50.058560Z","iopub.execute_input":"2022-08-09T03:23:50.059669Z","iopub.status.idle":"2022-08-09T03:23:50.067610Z","shell.execute_reply.started":"2022-08-09T03:23:50.059623Z","shell.execute_reply":"2022-08-09T03:23:50.066417Z"},"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-09T03:23:57.437413Z","iopub.execute_input":"2022-08-09T03:23:57.438593Z","iopub.status.idle":"2022-08-09T03:23:57.445588Z","shell.execute_reply.started":"2022-08-09T03:23:57.438551Z","shell.execute_reply":"2022-08-09T03:23:57.444701Z"},"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-07-28T13:00:11.486496Z","iopub.execute_input":"2022-07-28T13:00:11.487918Z","iopub.status.idle":"2022-07-28T13:00:11.500451Z","shell.execute_reply.started":"2022-07-28T13:00:11.487863Z","shell.execute_reply":"2022-07-28T13:00:11.499296Z"},"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-09T03:25:15.419019Z","iopub.execute_input":"2022-08-09T03:25:15.419466Z","iopub.status.idle":"2022-08-09T03:25:15.447257Z","shell.execute_reply.started":"2022-08-09T03:25:15.419430Z","shell.execute_reply":"2022-08-09T03:25:15.446010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = dados_teste[colunas]\ntest","metadata":{"execution":{"iopub.status.busy":"2022-08-09T03:24:12.884216Z","iopub.execute_input":"2022-08-09T03:24:12.885399Z","iopub.status.idle":"2022-08-09T03:24:12.900471Z","shell.execute_reply.started":"2022-08-09T03:24:12.885356Z","shell.execute_reply":"2022-08-09T03:24:12.899310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T03:26:04.692512Z","iopub.execute_input":"2022-08-09T03:26:04.692927Z","iopub.status.idle":"2022-08-09T03:26:04.704126Z","shell.execute_reply.started":"2022-08-09T03:26:04.692893Z","shell.execute_reply":"2022-08-09T03:26:04.702923Z"},"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-09T03:26:10.118843Z","iopub.execute_input":"2022-08-09T03:26:10.119297Z","iopub.status.idle":"2022-08-09T03:26:10.127374Z","shell.execute_reply.started":"2022-08-09T03:26:10.119260Z","shell.execute_reply":"2022-08-09T03:26:10.126029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T03:26:13.797226Z","iopub.execute_input":"2022-08-09T03:26:13.797663Z","iopub.status.idle":"2022-08-09T03:26:13.807761Z","shell.execute_reply.started":"2022-08-09T03:26:13.797629Z","shell.execute_reply":"2022-08-09T03:26:13.806698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicoes = modelo.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T03:26:17.216795Z","iopub.execute_input":"2022-08-09T03:26:17.217249Z","iopub.status.idle":"2022-08-09T03:26:17.225499Z","shell.execute_reply.started":"2022-08-09T03:26:17.217209Z","shell.execute_reply":"2022-08-09T03:26:17.224350Z"},"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-09T03:26:20.907634Z","iopub.execute_input":"2022-08-09T03:26:20.908060Z","iopub.status.idle":"2022-08-09T03:26:20.917716Z","shell.execute_reply.started":"2022-08-09T03:26:20.908025Z","shell.execute_reply":"2022-08-09T03:26:20.916717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Pré Processamento - Colunas Selecionadas\ncolunas_selecionadas = ['Survived', 'Sex', 'Age',\n       'ticket', 'Cabin','Embarked']","metadata":{"execution":{"iopub.status.busy":"2022-08-09T03:26:30.349248Z","iopub.execute_input":"2022-08-09T03:26:30.349668Z","iopub.status.idle":"2022-08-09T03:26:30.354768Z","shell.execute_reply.started":"2022-08-09T03:26:30.349632Z","shell.execute_reply":"2022-08-09T03:26:30.353840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Criando novo DF a partir das colunas selecionadas\nnovo_df = dados_teste.filter(items=colunas_selecionadas)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T03:26:36.567316Z","iopub.execute_input":"2022-08-09T03:26:36.568619Z","iopub.status.idle":"2022-08-09T03:26:36.577364Z","shell.execute_reply.started":"2022-08-09T03:26:36.568569Z","shell.execute_reply":"2022-08-09T03:26:36.576366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"novo_df","metadata":{"execution":{"iopub.status.busy":"2022-08-09T03:26:40.771932Z","iopub.execute_input":"2022-08-09T03:26:40.772404Z","iopub.status.idle":"2022-08-09T03:26:40.791901Z","shell.execute_reply.started":"2022-08-09T03:26:40.772364Z","shell.execute_reply":"2022-08-09T03:26:40.790402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#calcula a mediana das idades\nmediana_score = novo_df['Age'].median()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T03:26:45.510695Z","iopub.execute_input":"2022-08-09T03:26:45.511149Z","iopub.status.idle":"2022-08-09T03:26:45.517870Z","shell.execute_reply.started":"2022-08-09T03:26:45.511105Z","shell.execute_reply":"2022-08-09T03:26:45.516663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#substitui os valores nulos com valor da mediana\nnovo_df['Age'].fillna(mediana_score,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T03:26:49.856588Z","iopub.execute_input":"2022-08-09T03:26:49.857846Z","iopub.status.idle":"2022-08-09T03:26:49.863250Z","shell.execute_reply.started":"2022-08-09T03:26:49.857794Z","shell.execute_reply":"2022-08-09T03:26:49.862308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"novo_df['Age'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T03:26:53.310288Z","iopub.execute_input":"2022-08-09T03:26:53.310892Z","iopub.status.idle":"2022-08-09T03:26:53.317205Z","shell.execute_reply.started":"2022-08-09T03:26:53.310859Z","shell.execute_reply":"2022-08-09T03:26:53.316375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"novo_df['Cabin'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T03:26:56.682320Z","iopub.execute_input":"2022-08-09T03:26:56.683044Z","iopub.status.idle":"2022-08-09T03:26:56.695390Z","shell.execute_reply.started":"2022-08-09T03:26:56.683005Z","shell.execute_reply":"2022-08-09T03:26:56.694231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#substitui os valores nulos pelo valor indicado\nnovo_df.Cabin.fillna('0',inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T03:28:45.997610Z","iopub.execute_input":"2022-08-09T03:28:45.998066Z","iopub.status.idle":"2022-08-09T03:28:46.004061Z","shell.execute_reply.started":"2022-08-09T03:28:45.998009Z","shell.execute_reply":"2022-08-09T03:28:46.003098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#visualiza as duplicatas\nnovo_df[novo_df.duplicated(keep=False)].sort_values('Cabin')","metadata":{"execution":{"iopub.status.busy":"2022-08-09T03:31:33.529238Z","iopub.execute_input":"2022-08-09T03:31:33.529638Z","iopub.status.idle":"2022-08-09T03:31:33.553559Z","shell.execute_reply.started":"2022-08-09T03:31:33.529607Z","shell.execute_reply":"2022-08-09T03:31:33.552300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"novo_df.drop_duplicates(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T03:31:55.416592Z","iopub.execute_input":"2022-08-09T03:31:55.417266Z","iopub.status.idle":"2022-08-09T03:31:55.423547Z","shell.execute_reply.started":"2022-08-09T03:31:55.417229Z","shell.execute_reply":"2022-08-09T03:31:55.422626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-08-09T03:35:50.605589Z","iopub.execute_input":"2022-08-09T03:35:50.606027Z","iopub.status.idle":"2022-08-09T03:35:50.629486Z","shell.execute_reply.started":"2022-08-09T03:35:50.605990Z","shell.execute_reply":"2022-08-09T03:35:50.628140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}}]}