{"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-26T19:34:25.355917Z","iopub.execute_input":"2022-07-26T19:34:25.356516Z","iopub.status.idle":"2022-07-26T19:34:25.371871Z","shell.execute_reply.started":"2022-07-26T19:34:25.356470Z","shell.execute_reply":"2022-07-26T19:34:25.370641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/titanic/train.csv')\ntest = pd.read_csv('/kaggle/input/titanic/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:25.374424Z","iopub.execute_input":"2022-07-26T19:34:25.375264Z","iopub.status.idle":"2022-07-26T19:34:25.392507Z","shell.execute_reply.started":"2022-07-26T19:34:25.375225Z","shell.execute_reply":"2022-07-26T19:34:25.391661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Let us check the dimensions of the train and test datasets","metadata":{}},{"cell_type":"code","source":"print(train.shape)\nprint(test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:25.393901Z","iopub.execute_input":"2022-07-26T19:34:25.394257Z","iopub.status.idle":"2022-07-26T19:34:25.399812Z","shell.execute_reply.started":"2022-07-26T19:34:25.394222Z","shell.execute_reply":"2022-07-26T19:34:25.398600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# let us check the columns in both datasets","metadata":{}},{"cell_type":"code","source":"print(\"Train dataset contains the following columns : \\n\",train.columns)\nprint(\"Test dataset contains the following columns : \\n\",test.columns)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:25.403008Z","iopub.execute_input":"2022-07-26T19:34:25.403447Z","iopub.status.idle":"2022-07-26T19:34:25.412069Z","shell.execute_reply.started":"2022-07-26T19:34:25.403410Z","shell.execute_reply":"2022-07-26T19:34:25.410891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# let us now check the data types of each column in the train dataset","metadata":{}},{"cell_type":"code","source":"print(train.dtypes)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:25.413704Z","iopub.execute_input":"2022-07-26T19:34:25.414898Z","iopub.status.idle":"2022-07-26T19:34:25.422330Z","shell.execute_reply.started":"2022-07-26T19:34:25.414862Z","shell.execute_reply":"2022-07-26T19:34:25.420868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# let's see statistics of the train data","metadata":{}},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:25.423628Z","iopub.execute_input":"2022-07-26T19:34:25.424115Z","iopub.status.idle":"2022-07-26T19:34:25.454911Z","shell.execute_reply.started":"2022-07-26T19:34:25.424078Z","shell.execute_reply":"2022-07-26T19:34:25.453623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# let us check for descriptive data","metadata":{}},{"cell_type":"code","source":"train.describe(include = 'object')","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:25.456542Z","iopub.execute_input":"2022-07-26T19:34:25.456883Z","iopub.status.idle":"2022-07-26T19:34:25.479593Z","shell.execute_reply.started":"2022-07-26T19:34:25.456849Z","shell.execute_reply":"2022-07-26T19:34:25.478734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas_profiling as pp","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:25.481778Z","iopub.execute_input":"2022-07-26T19:34:25.482373Z","iopub.status.idle":"2022-07-26T19:34:25.486780Z","shell.execute_reply.started":"2022-07-26T19:34:25.482336Z","shell.execute_reply":"2022-07-26T19:34:25.485467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.drop(['Name','PassengerId','Embarked','Ticket','Cabin','Parch'],axis = 1)\ntest_passengerIds = test['PassengerId']\ntest = test.drop(['Name','PassengerId','Embarked','Ticket','Cabin','Parch'],axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:25.488236Z","iopub.execute_input":"2022-07-26T19:34:25.488603Z","iopub.status.idle":"2022-07-26T19:34:25.498363Z","shell.execute_reply.started":"2022-07-26T19:34:25.488563Z","shell.execute_reply":"2022-07-26T19:34:25.497129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.get_dummies(train,columns = ['Sex'])\ntest = pd.get_dummies(test,columns = ['Sex'])","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:25.554066Z","iopub.execute_input":"2022-07-26T19:34:25.554452Z","iopub.status.idle":"2022-07-26T19:34:25.568587Z","shell.execute_reply.started":"2022-07-26T19:34:25.554416Z","shell.execute_reply":"2022-07-26T19:34:25.567468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# let us check for any Null values","metadata":{}},{"cell_type":"code","source":"def randommissingdata(df2):\n    df = df2.copy()\n    for col in df.columns:\n        data = df[col]\n        mask = data.isnull()\n        samples = random.choices(data[~mask].values , k = mask.sum())\n        data[mask] = samples\n    return df","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:25.571718Z","iopub.execute_input":"2022-07-26T19:34:25.571960Z","iopub.status.idle":"2022-07-26T19:34:25.579720Z","shell.execute_reply.started":"2022-07-26T19:34:25.571937Z","shell.execute_reply":"2022-07-26T19:34:25.578261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\ntrain = randommissingdata(train)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:25.581267Z","iopub.execute_input":"2022-07-26T19:34:25.581803Z","iopub.status.idle":"2022-07-26T19:34:25.604244Z","shell.execute_reply.started":"2022-07-26T19:34:25.581766Z","shell.execute_reply":"2022-07-26T19:34:25.603033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Age']","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:25.605619Z","iopub.execute_input":"2022-07-26T19:34:25.606139Z","iopub.status.idle":"2022-07-26T19:34:25.615122Z","shell.execute_reply.started":"2022-07-26T19:34:25.606105Z","shell.execute_reply":"2022-07-26T19:34:25.613955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.profile_report()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:25.618770Z","iopub.execute_input":"2022-07-26T19:34:25.619064Z","iopub.status.idle":"2022-07-26T19:34:33.639723Z","shell.execute_reply.started":"2022-07-26T19:34:25.619034Z","shell.execute_reply":"2022-07-26T19:34:33.638800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = randommissingdata(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:33.641349Z","iopub.execute_input":"2022-07-26T19:34:33.641907Z","iopub.status.idle":"2022-07-26T19:34:33.666822Z","shell.execute_reply.started":"2022-07-26T19:34:33.641869Z","shell.execute_reply":"2022-07-26T19:34:33.665965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = train.drop(columns = 'Survived',axis = 1).to_numpy()\ny = train['Survived'].to_numpy()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:33.668539Z","iopub.execute_input":"2022-07-26T19:34:33.669193Z","iopub.status.idle":"2022-07-26T19:34:33.675316Z","shell.execute_reply.started":"2022-07-26T19:34:33.669154Z","shell.execute_reply":"2022-07-26T19:34:33.674317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nsc = StandardScaler()\nx = sc.fit_transform(x)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:33.677143Z","iopub.execute_input":"2022-07-26T19:34:33.677881Z","iopub.status.idle":"2022-07-26T19:34:33.684612Z","shell.execute_reply.started":"2022-07-26T19:34:33.677845Z","shell.execute_reply":"2022-07-26T19:34:33.683407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" import tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:33.686859Z","iopub.execute_input":"2022-07-26T19:34:33.687706Z","iopub.status.idle":"2022-07-26T19:34:33.692173Z","shell.execute_reply.started":"2022-07-26T19:34:33.687670Z","shell.execute_reply":"2022-07-26T19:34:33.691237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    tf.keras.layers.Dense(9,activation  = 'relu'),\n    tf.keras.layers.Dense(20,activation  = 'relu'),\n    tf.keras.layers.Dense(30,activation  = 'relu'),\n    tf.keras.layers.Dense(50,activation  = 'relu'),\n    tf.keras.layers.Dense(70,activation  = 'relu'),\n    tf.keras.layers.Dense(90,activation  = 'relu'),\n    tf.keras.layers.Dense(2,activation  = 'softmax')\n    \n])","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:33.694534Z","iopub.execute_input":"2022-07-26T19:34:33.695598Z","iopub.status.idle":"2022-07-26T19:34:33.717492Z","shell.execute_reply.started":"2022-07-26T19:34:33.695558Z","shell.execute_reply":"2022-07-26T19:34:33.716531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss = tf.keras.losses.BinaryCrossentropy(),\n               optimizer = tf.keras.optimizers.Adam(learning_rate = 0.0001),\n               metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:33.719000Z","iopub.execute_input":"2022-07-26T19:34:33.719625Z","iopub.status.idle":"2022-07-26T19:34:33.732880Z","shell.execute_reply.started":"2022-07-26T19:34:33.719591Z","shell.execute_reply":"2022-07-26T19:34:33.731461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x,tf.one_hot(y,depth = 2),\n                   epochs = 250,\n                   verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:33.734659Z","iopub.execute_input":"2022-07-26T19:34:33.735314Z","iopub.status.idle":"2022-07-26T19:34:55.080434Z","shell.execute_reply.started":"2022-07-26T19:34:33.735278Z","shell.execute_reply":"2022-07-26T19:34:55.079517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import plot_model\nplot_model(model,show_shapes = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:55.082104Z","iopub.execute_input":"2022-07-26T19:34:55.082462Z","iopub.status.idle":"2022-07-26T19:34:55.231089Z","shell.execute_reply.started":"2022-07-26T19:34:55.082425Z","shell.execute_reply":"2022-07-26T19:34:55.229948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = sc.fit_transform(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:55.235797Z","iopub.execute_input":"2022-07-26T19:34:55.236223Z","iopub.status.idle":"2022-07-26T19:34:55.248228Z","shell.execute_reply.started":"2022-07-26T19:34:55.236175Z","shell.execute_reply":"2022-07-26T19:34:55.247311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(test).argmax(axis = 1)\ny_pred.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:55.250187Z","iopub.execute_input":"2022-07-26T19:34:55.250877Z","iopub.status.idle":"2022-07-26T19:34:55.358825Z","shell.execute_reply.started":"2022-07-26T19:34:55.250834Z","shell.execute_reply":"2022-07-26T19:34:55.357935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'PassengerId': test_passengerIds,'Survived': y_pred})\nsubmission.to_csv('submission.csv',index = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:55.360389Z","iopub.execute_input":"2022-07-26T19:34:55.360741Z","iopub.status.idle":"2022-07-26T19:34:55.368900Z","shell.execute_reply.started":"2022-07-26T19:34:55.360706Z","shell.execute_reply":"2022-07-26T19:34:55.368000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checker = pd.read_csv(\"/kaggle/input/titanic/gender_submission.csv\")\nchecker","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:55.370581Z","iopub.execute_input":"2022-07-26T19:34:55.371257Z","iopub.status.idle":"2022-07-26T19:34:55.389699Z","shell.execute_reply.started":"2022-07-26T19:34:55.371211Z","shell.execute_reply":"2022-07-26T19:34:55.388675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:55.390937Z","iopub.execute_input":"2022-07-26T19:34:55.391285Z","iopub.status.idle":"2022-07-26T19:34:55.404800Z","shell.execute_reply.started":"2022-07-26T19:34:55.391259Z","shell.execute_reply":"2022-07-26T19:34:55.404003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.compare(checker).count()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:34:55.405938Z","iopub.execute_input":"2022-07-26T19:34:55.406828Z","iopub.status.idle":"2022-07-26T19:34:55.424495Z","shell.execute_reply.started":"2022-07-26T19:34:55.406790Z","shell.execute_reply":"2022-07-26T19:34:55.423640Z"},"trusted":true},"execution_count":null,"outputs":[]}]}