{"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-07-25T16:19:14.049498Z","iopub.execute_input":"2022-07-25T16:19:14.049914Z","iopub.status.idle":"2022-07-25T16:19:14.060681Z","shell.execute_reply.started":"2022-07-25T16:19:14.049882Z","shell.execute_reply":"2022-07-25T16:19:14.059416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.063019Z","iopub.execute_input":"2022-07-25T16:19:14.064142Z","iopub.status.idle":"2022-07-25T16:19:14.068356Z","shell.execute_reply.started":"2022-07-25T16:19:14.064095Z","shell.execute_reply":"2022-07-25T16:19:14.067492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('../input/titanic/train.csv')\ndf_test = pd.read_csv('../input/titanic/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.071082Z","iopub.execute_input":"2022-07-25T16:19:14.071975Z","iopub.status.idle":"2022-07-25T16:19:14.091159Z","shell.execute_reply.started":"2022-07-25T16:19:14.071896Z","shell.execute_reply":"2022-07-25T16:19:14.090289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('trainset:',df_train.shape)\nprint('testset:',df_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.093288Z","iopub.execute_input":"2022-07-25T16:19:14.094014Z","iopub.status.idle":"2022-07-25T16:19:14.100299Z","shell.execute_reply.started":"2022-07-25T16:19:14.093971Z","shell.execute_reply":"2022-07-25T16:19:14.099200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.102233Z","iopub.execute_input":"2022-07-25T16:19:14.102939Z","iopub.status.idle":"2022-07-25T16:19:14.129531Z","shell.execute_reply.started":"2022-07-25T16:19:14.102895Z","shell.execute_reply":"2022-07-25T16:19:14.128729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.set_index('PassengerId',inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.131441Z","iopub.execute_input":"2022-07-25T16:19:14.131982Z","iopub.status.idle":"2022-07-25T16:19:14.137017Z","shell.execute_reply.started":"2022-07-25T16:19:14.131950Z","shell.execute_reply":"2022-07-25T16:19:14.135967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train=df_train.drop(['Ticket','Fare','Cabin','Embarked','Name'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.138456Z","iopub.execute_input":"2022-07-25T16:19:14.139214Z","iopub.status.idle":"2022-07-25T16:19:14.149302Z","shell.execute_reply.started":"2022-07-25T16:19:14.139179Z","shell.execute_reply":"2022-07-25T16:19:14.148484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Age'].fillna(value=df_train['Age'].mean(), inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.150851Z","iopub.execute_input":"2022-07-25T16:19:14.152091Z","iopub.status.idle":"2022-07-25T16:19:14.160084Z","shell.execute_reply.started":"2022-07-25T16:19:14.152047Z","shell.execute_reply":"2022-07-25T16:19:14.159075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.161412Z","iopub.execute_input":"2022-07-25T16:19:14.161803Z","iopub.status.idle":"2022-07-25T16:19:14.180803Z","shell.execute_reply.started":"2022-07-25T16:19:14.161771Z","shell.execute_reply":"2022-07-25T16:19:14.179631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.208048Z","iopub.execute_input":"2022-07-25T16:19:14.208701Z","iopub.status.idle":"2022-07-25T16:19:14.236240Z","shell.execute_reply.started":"2022-07-25T16:19:14.208668Z","shell.execute_reply":"2022-07-25T16:19:14.235401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data = df_train,x='Survived',hue='Sex')","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.237809Z","iopub.execute_input":"2022-07-25T16:19:14.238858Z","iopub.status.idle":"2022-07-25T16:19:14.377517Z","shell.execute_reply.started":"2022-07-25T16:19:14.238823Z","shell.execute_reply":"2022-07-25T16:19:14.376298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data = df_train,x='Survived',hue='Pclass')","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.379374Z","iopub.execute_input":"2022-07-25T16:19:14.380384Z","iopub.status.idle":"2022-07-25T16:19:14.538892Z","shell.execute_reply.started":"2022-07-25T16:19:14.380352Z","shell.execute_reply":"2022-07-25T16:19:14.537749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data = df_train,x='Parch',hue='Survived')","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.542834Z","iopub.execute_input":"2022-07-25T16:19:14.543474Z","iopub.status.idle":"2022-07-25T16:19:14.724244Z","shell.execute_reply.started":"2022-07-25T16:19:14.543418Z","shell.execute_reply":"2022-07-25T16:19:14.723031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.729396Z","iopub.execute_input":"2022-07-25T16:19:14.730023Z","iopub.status.idle":"2022-07-25T16:19:14.744332Z","shell.execute_reply.started":"2022-07-25T16:19:14.729978Z","shell.execute_reply":"2022-07-25T16:19:14.743055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import preprocessing\n  \nlabel_encoder = preprocessing.LabelEncoder()\n  \ndf_train['Sex']= label_encoder.fit_transform(df_train['Sex'])\n  ","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.746246Z","iopub.execute_input":"2022-07-25T16:19:14.747200Z","iopub.status.idle":"2022-07-25T16:19:14.755340Z","shell.execute_reply.started":"2022-07-25T16:19:14.747156Z","shell.execute_reply":"2022-07-25T16:19:14.754303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.756746Z","iopub.execute_input":"2022-07-25T16:19:14.757675Z","iopub.status.idle":"2022-07-25T16:19:14.773709Z","shell.execute_reply.started":"2022-07-25T16:19:14.757643Z","shell.execute_reply":"2022-07-25T16:19:14.772500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = df_train.drop(['Survived'],axis=1)\ny_train= df_train['Survived']","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.775238Z","iopub.execute_input":"2022-07-25T16:19:14.776354Z","iopub.status.idle":"2022-07-25T16:19:14.784994Z","shell.execute_reply.started":"2022-07-25T16:19:14.776311Z","shell.execute_reply":"2022-07-25T16:19:14.783975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.786043Z","iopub.execute_input":"2022-07-25T16:19:14.786920Z","iopub.status.idle":"2022-07-25T16:19:14.807152Z","shell.execute_reply.started":"2022-07-25T16:19:14.786886Z","shell.execute_reply":"2022-07-25T16:19:14.805929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.808755Z","iopub.execute_input":"2022-07-25T16:19:14.809298Z","iopub.status.idle":"2022-07-25T16:19:14.824539Z","shell.execute_reply.started":"2022-07-25T16:19:14.809254Z","shell.execute_reply":"2022-07-25T16:19:14.823787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.825671Z","iopub.execute_input":"2022-07-25T16:19:14.826104Z","iopub.status.idle":"2022-07-25T16:19:14.829805Z","shell.execute_reply.started":"2022-07-25T16:19:14.826076Z","shell.execute_reply":"2022-07-25T16:19:14.828961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Lr = LogisticRegression()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.830987Z","iopub.execute_input":"2022-07-25T16:19:14.831481Z","iopub.status.idle":"2022-07-25T16:19:14.840892Z","shell.execute_reply.started":"2022-07-25T16:19:14.831452Z","shell.execute_reply":"2022-07-25T16:19:14.840014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Lr.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.842164Z","iopub.execute_input":"2022-07-25T16:19:14.842701Z","iopub.status.idle":"2022-07-25T16:19:14.870415Z","shell.execute_reply.started":"2022-07-25T16:19:14.842662Z","shell.execute_reply":"2022-07-25T16:19:14.869632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test dataset","metadata":{}},{"cell_type":"code","source":"# df_test.set_index('PassengerId',inplace=True)\ndf_test=df_test.drop(['Ticket','Fare','Cabin','Embarked','Name'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.874224Z","iopub.execute_input":"2022-07-25T16:19:14.874724Z","iopub.status.idle":"2022-07-25T16:19:14.880354Z","shell.execute_reply.started":"2022-07-25T16:19:14.874694Z","shell.execute_reply":"2022-07-25T16:19:14.879180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.881672Z","iopub.execute_input":"2022-07-25T16:19:14.882439Z","iopub.status.idle":"2022-07-25T16:19:14.900389Z","shell.execute_reply.started":"2022-07-25T16:19:14.882354Z","shell.execute_reply":"2022-07-25T16:19:14.899620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test['Sex']= label_encoder.fit_transform(df_test['Sex'])","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.902181Z","iopub.execute_input":"2022-07-25T16:19:14.902883Z","iopub.status.idle":"2022-07-25T16:19:14.909545Z","shell.execute_reply.started":"2022-07-25T16:19:14.902840Z","shell.execute_reply":"2022-07-25T16:19:14.908360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.911317Z","iopub.execute_input":"2022-07-25T16:19:14.912634Z","iopub.status.idle":"2022-07-25T16:19:14.928861Z","shell.execute_reply.started":"2022-07-25T16:19:14.912505Z","shell.execute_reply":"2022-07-25T16:19:14.927863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test['Age'].fillna(value=df_test['Age'].mean(), inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.930485Z","iopub.execute_input":"2022-07-25T16:19:14.931748Z","iopub.status.idle":"2022-07-25T16:19:14.938572Z","shell.execute_reply.started":"2022-07-25T16:19:14.931705Z","shell.execute_reply":"2022-07-25T16:19:14.937448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.set_index('PassengerId',inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.940217Z","iopub.execute_input":"2022-07-25T16:19:14.941339Z","iopub.status.idle":"2022-07-25T16:19:14.950123Z","shell.execute_reply.started":"2022-07-25T16:19:14.941302Z","shell.execute_reply":"2022-07-25T16:19:14.949280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = Lr.predict(df_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.951446Z","iopub.execute_input":"2022-07-25T16:19:14.952402Z","iopub.status.idle":"2022-07-25T16:19:14.964811Z","shell.execute_reply.started":"2022-07-25T16:19:14.952370Z","shell.execute_reply":"2022-07-25T16:19:14.963786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.966068Z","iopub.execute_input":"2022-07-25T16:19:14.966581Z","iopub.status.idle":"2022-07-25T16:19:14.978047Z","shell.execute_reply.started":"2022-07-25T16:19:14.966530Z","shell.execute_reply":"2022-07-25T16:19:14.976937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_true = pd.read_csv('../input/titanic/gender_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.979354Z","iopub.execute_input":"2022-07-25T16:19:14.980416Z","iopub.status.idle":"2022-07-25T16:19:14.990459Z","shell.execute_reply.started":"2022-07-25T16:19:14.980373Z","shell.execute_reply":"2022-07-25T16:19:14.989589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_true=y_true['Survived']","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.991756Z","iopub.execute_input":"2022-07-25T16:19:14.992361Z","iopub.status.idle":"2022-07-25T16:19:14.997838Z","shell.execute_reply.started":"2022-07-25T16:19:14.992326Z","shell.execute_reply":"2022-07-25T16:19:14.996850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\ncon1=confusion_matrix(y_true, y_pred)\ncon1.ravel()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:14.999045Z","iopub.execute_input":"2022-07-25T16:19:14.999788Z","iopub.status.idle":"2022-07-25T16:19:15.010713Z","shell.execute_reply.started":"2022-07-25T16:19:14.999757Z","shell.execute_reply":"2022-07-25T16:19:15.009468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy = accuracy_score(y_pred, y_true)\naccuracy","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:15.012539Z","iopub.execute_input":"2022-07-25T16:19:15.013377Z","iopub.status.idle":"2022-07-25T16:19:15.024663Z","shell.execute_reply.started":"2022-07-25T16:19:15.013334Z","shell.execute_reply":"2022-07-25T16:19:15.023400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:15.026270Z","iopub.execute_input":"2022-07-25T16:19:15.026649Z","iopub.status.idle":"2022-07-25T16:19:15.036719Z","shell.execute_reply.started":"2022-07-25T16:19:15.026618Z","shell.execute_reply":"2022-07-25T16:19:15.035626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = df_test","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:15.038296Z","iopub.execute_input":"2022-07-25T16:19:15.038750Z","iopub.status.idle":"2022-07-25T16:19:15.045853Z","shell.execute_reply.started":"2022-07-25T16:19:15.038709Z","shell.execute_reply":"2022-07-25T16:19:15.044762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output['Survived'] = y_pred\nOutput = output.drop(['Pclass','Sex','Age','SibSp','Parch'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:15.047329Z","iopub.execute_input":"2022-07-25T16:19:15.047842Z","iopub.status.idle":"2022-07-25T16:19:15.057484Z","shell.execute_reply.started":"2022-07-25T16:19:15.047810Z","shell.execute_reply":"2022-07-25T16:19:15.056611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Output","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:15.058769Z","iopub.execute_input":"2022-07-25T16:19:15.059243Z","iopub.status.idle":"2022-07-25T16:19:15.073728Z","shell.execute_reply.started":"2022-07-25T16:19:15.059206Z","shell.execute_reply":"2022-07-25T16:19:15.072946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Output.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-25T16:19:15.074945Z","iopub.execute_input":"2022-07-25T16:19:15.075431Z","iopub.status.idle":"2022-07-25T16:19:15.086177Z","shell.execute_reply.started":"2022-07-25T16:19:15.075401Z","shell.execute_reply":"2022-07-25T16:19:15.085069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}