{"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)\nimport matplotlib.pyplot as plt\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-25T17:54:06.669185Z","iopub.execute_input":"2022-07-25T17:54:06.669746Z","iopub.status.idle":"2022-07-25T17:54:06.708447Z","shell.execute_reply.started":"2022-07-25T17:54:06.669629Z","shell.execute_reply":"2022-07-25T17:54:06.707275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"space_ship_data=pd.read_csv('/kaggle/input/spaceship-titanic/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:07.085269Z","iopub.execute_input":"2022-07-25T17:54:07.086281Z","iopub.status.idle":"2022-07-25T17:54:07.143607Z","shell.execute_reply.started":"2022-07-25T17:54:07.086234Z","shell.execute_reply":"2022-07-25T17:54:07.142794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"space_ship_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:07.530105Z","iopub.execute_input":"2022-07-25T17:54:07.530614Z","iopub.status.idle":"2022-07-25T17:54:07.564523Z","shell.execute_reply.started":"2022-07-25T17:54:07.530568Z","shell.execute_reply":"2022-07-25T17:54:07.563363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" # **data quality check and cleaning**\n>  **checking null values**\n\n>  **checking duplicated values**\n","metadata":{}},{"cell_type":"code","source":"space_ship_data.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:08.005771Z","iopub.execute_input":"2022-07-25T17:54:08.006896Z","iopub.status.idle":"2022-07-25T17:54:08.027149Z","shell.execute_reply.started":"2022-07-25T17:54:08.006722Z","shell.execute_reply":"2022-07-25T17:54:08.026037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"space_ship_data[space_ship_data.duplicated(subset=None, keep='first')==True]","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:08.412549Z","iopub.execute_input":"2022-07-25T17:54:08.413256Z","iopub.status.idle":"2022-07-25T17:54:08.452207Z","shell.execute_reply.started":"2022-07-25T17:54:08.413219Z","shell.execute_reply":"2022-07-25T17:54:08.451209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"space_ship_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:08.800288Z","iopub.execute_input":"2022-07-25T17:54:08.801146Z","iopub.status.idle":"2022-07-25T17:54:08.826755Z","shell.execute_reply.started":"2022-07-25T17:54:08.801110Z","shell.execute_reply":"2022-07-25T17:54:08.825593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"space_ship_data=space_ship_data.dropna()\nspace_ship_data['CryoSleep'] = space_ship_data['CryoSleep'].astype('int')\nspace_ship_data['VIP'] = space_ship_data['VIP'].astype('int')\nspace_ship_data.describe(include='all')","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:09.335293Z","iopub.execute_input":"2022-07-25T17:54:09.336155Z","iopub.status.idle":"2022-07-25T17:54:09.415941Z","shell.execute_reply.started":"2022-07-25T17:54:09.336117Z","shell.execute_reply":"2022-07-25T17:54:09.414814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"space_ship_data['Transported']","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:09.737264Z","iopub.execute_input":"2022-07-25T17:54:09.738231Z","iopub.status.idle":"2022-07-25T17:54:09.746491Z","shell.execute_reply.started":"2022-07-25T17:54:09.738189Z","shell.execute_reply":"2022-07-25T17:54:09.745472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr=space_ship_data.corr(method='spearman')\ncorr.style.background_gradient(cmap='Blues')","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:10.114387Z","iopub.execute_input":"2022-07-25T17:54:10.115399Z","iopub.status.idle":"2022-07-25T17:54:10.221110Z","shell.execute_reply.started":"2022-07-25T17:54:10.115359Z","shell.execute_reply":"2022-07-25T17:54:10.220034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr['Transported'][abs(corr['Transported'])>=0.2]","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:10.516363Z","iopub.execute_input":"2022-07-25T17:54:10.516758Z","iopub.status.idle":"2022-07-25T17:54:10.525726Z","shell.execute_reply.started":"2022-07-25T17:54:10.516727Z","shell.execute_reply":"2022-07-25T17:54:10.524568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import preprocessing\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import train_test_split\nlabel_encoder = preprocessing.LabelEncoder()\ndef prepForTrain(df):\n    \n    train=df[['CryoSleep','RoomService','ShoppingMall','Spa','VRDeck']].to_numpy()\n    if 'Transported' not in df.columns:\n        return train\n    else:\n        \n        labels=label_encoder.fit_transform(df['Transported'])\n        return train,labels\n    ","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:10.689390Z","iopub.execute_input":"2022-07-25T17:54:10.689767Z","iopub.status.idle":"2022-07-25T17:54:11.312334Z","shell.execute_reply.started":"2022-07-25T17:54:10.689728Z","shell.execute_reply":"2022-07-25T17:54:11.311306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train,labels=prepForTrain(space_ship_data)\nlabels","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:11.314454Z","iopub.execute_input":"2022-07-25T17:54:11.315066Z","iopub.status.idle":"2022-07-25T17:54:11.323963Z","shell.execute_reply.started":"2022-07-25T17:54:11.315018Z","shell.execute_reply":"2022-07-25T17:54:11.323198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train,X_test,y_train,y_test= train_test_split(train,labels, test_size = 0.3)\ny_train","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:12.052505Z","iopub.execute_input":"2022-07-25T17:54:12.053123Z","iopub.status.idle":"2022-07-25T17:54:12.060395Z","shell.execute_reply.started":"2022-07-25T17:54:12.053087Z","shell.execute_reply":"2022-07-25T17:54:12.059504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **we are going to try different models**","metadata":{}},{"cell_type":"markdown","source":"# 1 - logistic regression","metadata":{}},{"cell_type":"code","source":"logreg = LogisticRegression()\nlogreg.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:12.435462Z","iopub.execute_input":"2022-07-25T17:54:12.436183Z","iopub.status.idle":"2022-07-25T17:54:12.506418Z","shell.execute_reply.started":"2022-07-25T17:54:12.436145Z","shell.execute_reply":"2022-07-25T17:54:12.505069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_pred = logreg.predict(X_train) \ny_pred=logreg.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:12.509096Z","iopub.execute_input":"2022-07-25T17:54:12.509926Z","iopub.status.idle":"2022-07-25T17:54:12.518924Z","shell.execute_reply.started":"2022-07-25T17:54:12.509871Z","shell.execute_reply":"2022-07-25T17:54:12.517621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import metrics\nprint(\"Accuracy:\",metrics.accuracy_score(y_train, y_train_pred))\nprint(\"Precision:\",metrics.precision_score(y_train, y_train_pred))\nprint(\"Recall:\",metrics.recall_score(y_train, y_train_pred))\nprint(\"val Accuracy:\",metrics.accuracy_score(y_test, y_pred))\nprint(\"val Precision:\",metrics.precision_score(y_test, y_pred))\nprint(\"val Recall:\",metrics.recall_score(y_test, y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:12.576815Z","iopub.execute_input":"2022-07-25T17:54:12.577406Z","iopub.status.idle":"2022-07-25T17:54:12.616799Z","shell.execute_reply.started":"2022-07-25T17:54:12.577357Z","shell.execute_reply":"2022-07-25T17:54:12.615478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2- KNN","metadata":{}},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nknn = KNeighborsClassifier(n_neighbors=4)\nknn.fit(X_train, y_train)\ny_train_pred = knn.predict(X_train) \ny_pred=knn.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:12.623871Z","iopub.execute_input":"2022-07-25T17:54:12.627347Z","iopub.status.idle":"2022-07-25T17:54:12.999913Z","shell.execute_reply.started":"2022-07-25T17:54:12.627281Z","shell.execute_reply":"2022-07-25T17:54:12.998941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Accuracy:\",metrics.accuracy_score(y_train, y_train_pred))\nprint(\"Precision:\",metrics.precision_score(y_train, y_train_pred))\nprint(\"Recall:\",metrics.recall_score(y_train, y_train_pred))\nprint(\"val Accuracy:\",metrics.accuracy_score(y_test, y_pred))\nprint(\"val Precision:\",metrics.precision_score(y_test, y_pred))\nprint(\"val Recall:\",metrics.recall_score(y_test, y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:13.001446Z","iopub.execute_input":"2022-07-25T17:54:13.002004Z","iopub.status.idle":"2022-07-25T17:54:13.022227Z","shell.execute_reply.started":"2022-07-25T17:54:13.001932Z","shell.execute_reply":"2022-07-25T17:54:13.021293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3- simple neural net","metadata":{}},{"cell_type":"code","source":"from keras.layers import Dense ,Input\nfrom keras import Model\nimport keras\ninput=Input(shape=(5,))\ndnn=Dense(100,'relu')(input)\noutput=Dense(1,'sigmoid')(dnn)\nmodel=Model(input,output)\nmodel.compile(optimizer='adam',loss=keras.losses.BinaryCrossentropy(),metrics=[keras.metrics.BinaryAccuracy(),keras.metrics.FalseNegatives()])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:13.024037Z","iopub.execute_input":"2022-07-25T17:54:13.024785Z","iopub.status.idle":"2022-07-25T17:54:19.145116Z","shell.execute_reply.started":"2022-07-25T17:54:13.024752Z","shell.execute_reply":"2022-07-25T17:54:19.144021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train, y_train,batch_size=32,epochs=20)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:19.146405Z","iopub.execute_input":"2022-07-25T17:54:19.146701Z","iopub.status.idle":"2022-07-25T17:54:24.945794Z","shell.execute_reply.started":"2022-07-25T17:54:19.146674Z","shell.execute_reply":"2022-07-25T17:54:24.944640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(X_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:24.948216Z","iopub.execute_input":"2022-07-25T17:54:24.948563Z","iopub.status.idle":"2022-07-25T17:54:25.377560Z","shell.execute_reply.started":"2022-07-25T17:54:24.948531Z","shell.execute_reply":"2022-07-25T17:54:25.376706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"space_ship_data_test=pd.read_csv('/kaggle/input/spaceship-titanic/test.csv')\nspace_ship_data_test[space_ship_data_test.isnull()==True]","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:25.378765Z","iopub.execute_input":"2022-07-25T17:54:25.379746Z","iopub.status.idle":"2022-07-25T17:54:25.436440Z","shell.execute_reply.started":"2022-07-25T17:54:25.379711Z","shell.execute_reply":"2022-07-25T17:54:25.435243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"space_ship_data_test.fillna( 0, inplace = True)\nspace_ship_data_test['CryoSleep'] = space_ship_data_test['CryoSleep'].astype('int')\nspace_ship_data_test['VIP'] = space_ship_data_test['VIP'].astype('int')\ntest=prepForTrain(space_ship_data_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:25.437878Z","iopub.execute_input":"2022-07-25T17:54:25.438239Z","iopub.status.idle":"2022-07-25T17:54:25.454337Z","shell.execute_reply.started":"2022-07-25T17:54:25.438209Z","shell.execute_reply":"2022-07-25T17:54:25.452913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# since KNN got the highest score we are going to use is for our submission","metadata":{}},{"cell_type":"code","source":"y_testpred=knn.predict(test)\nlast_pred = np.array (y_testpred, dtype = bool) \nlast_pred","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:25.456154Z","iopub.execute_input":"2022-07-25T17:54:25.456624Z","iopub.status.idle":"2022-07-25T17:54:25.670857Z","shell.execute_reply.started":"2022-07-25T17:54:25.456575Z","shell.execute_reply":"2022-07-25T17:54:25.669759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nsample=pd.DataFrame()\nsample['PassengerId']=space_ship_data_test['PassengerId']\nsample['Transported']=last_pred\n\nsample.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:25.672264Z","iopub.execute_input":"2022-07-25T17:54:25.672586Z","iopub.status.idle":"2022-07-25T17:54:25.690639Z","shell.execute_reply.started":"2022-07-25T17:54:25.672557Z","shell.execute_reply":"2022-07-25T17:54:25.689749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.size","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:25.693201Z","iopub.execute_input":"2022-07-25T17:54:25.693710Z","iopub.status.idle":"2022-07-25T17:54:25.699848Z","shell.execute_reply.started":"2022-07-25T17:54:25.693677Z","shell.execute_reply":"2022-07-25T17:54:25.698814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"space_ship_data_test.size","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:25.701129Z","iopub.execute_input":"2022-07-25T17:54:25.701453Z","iopub.status.idle":"2022-07-25T17:54:25.711065Z","shell.execute_reply.started":"2022-07-25T17:54:25.701424Z","shell.execute_reply":"2022-07-25T17:54:25.710139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_test=pd.read_csv('../input/spaceship-titanic/sample_submission.csv')\ndata_test.head(-5)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:25.712283Z","iopub.execute_input":"2022-07-25T17:54:25.712689Z","iopub.status.idle":"2022-07-25T17:54:25.736625Z","shell.execute_reply.started":"2022-07-25T17:54:25.712658Z","shell.execute_reply":"2022-07-25T17:54:25.735485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.head(-5)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:54:25.738035Z","iopub.execute_input":"2022-07-25T17:54:25.738769Z","iopub.status.idle":"2022-07-25T17:54:25.752702Z","shell.execute_reply.started":"2022-07-25T17:54:25.738730Z","shell.execute_reply":"2022-07-25T17:54:25.751350Z"},"trusted":true},"execution_count":null,"outputs":[]}]}