{"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-11T19:22:36.203503Z","iopub.execute_input":"2022-07-11T19:22:36.204187Z","iopub.status.idle":"2022-07-11T19:22:36.244486Z","shell.execute_reply.started":"2022-07-11T19:22:36.204043Z","shell.execute_reply":"2022-07-11T19:22:36.243636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/titanic/train.csv')\ntdf = pd.read_csv('/kaggle/input/titanic/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T19:22:37.733028Z","iopub.execute_input":"2022-07-11T19:22:37.733895Z","iopub.status.idle":"2022-07-11T19:22:37.765352Z","shell.execute_reply.started":"2022-07-11T19:22:37.733844Z","shell.execute_reply":"2022-07-11T19:22:37.764161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nfrom sklearn import metrics\nfrom sklearn.preprocessing import StandardScaler as Norm\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-07-11T19:22:40.039589Z","iopub.execute_input":"2022-07-11T19:22:40.040431Z","iopub.status.idle":"2022-07-11T19:22:40.772381Z","shell.execute_reply.started":"2022-07-11T19:22:40.040372Z","shell.execute_reply":"2022-07-11T19:22:40.770882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-11T19:22:41.177752Z","iopub.execute_input":"2022-07-11T19:22:41.178130Z","iopub.status.idle":"2022-07-11T19:22:41.189893Z","shell.execute_reply.started":"2022-07-11T19:22:41.178101Z","shell.execute_reply":"2022-07-11T19:22:41.188603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df[['Survived', 'Pclass', 'Sex', 'Age', 'SibSp','Parch' , 'Fare', 'Embarked']]\ndf","metadata":{"execution":{"iopub.status.busy":"2022-07-11T19:22:42.315897Z","iopub.execute_input":"2022-07-11T19:22:42.317002Z","iopub.status.idle":"2022-07-11T19:22:42.356391Z","shell.execute_reply.started":"2022-07-11T19:22:42.316957Z","shell.execute_reply":"2022-07-11T19:22:42.354840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.dropna(inplace = True)\ndf.reset_index(inplace = True)\ndf.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-11T19:22:45.616624Z","iopub.execute_input":"2022-07-11T19:22:45.617037Z","iopub.status.idle":"2022-07-11T19:22:45.632016Z","shell.execute_reply.started":"2022-07-11T19:22:45.616998Z","shell.execute_reply":"2022-07-11T19:22:45.630900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.loc[df['Sex']=='male','Sex'] = 0\ndf.loc[df['Sex']=='female','Sex'] = 1","metadata":{"execution":{"iopub.status.busy":"2022-07-11T19:22:48.851174Z","iopub.execute_input":"2022-07-11T19:22:48.852167Z","iopub.status.idle":"2022-07-11T19:22:48.863713Z","shell.execute_reply.started":"2022-07-11T19:22:48.852131Z","shell.execute_reply":"2022-07-11T19:22:48.862754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.loc[df['Embarked']=='C','Embarked'] = 0\ndf.loc[df['Embarked']=='Q','Embarked'] = 1\ndf.loc[df['Embarked']=='S','Embarked'] = 2","metadata":{"execution":{"iopub.status.busy":"2022-07-11T19:22:50.006737Z","iopub.execute_input":"2022-07-11T19:22:50.007158Z","iopub.status.idle":"2022-07-11T19:22:50.020291Z","shell.execute_reply.started":"2022-07-11T19:22:50.007125Z","shell.execute_reply":"2022-07-11T19:22:50.019268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-11T19:22:51.058324Z","iopub.execute_input":"2022-07-11T19:22:51.058927Z","iopub.status.idle":"2022-07-11T19:22:51.066772Z","shell.execute_reply.started":"2022-07-11T19:22:51.058893Z","shell.execute_reply":"2022-07-11T19:22:51.065350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df[['Pclass', 'Sex', 'Age', 'SibSp','Parch', 'Fare', 'Embarked']].values\ny = df[['Survived']].values","metadata":{"execution":{"iopub.status.busy":"2022-07-11T19:22:57.340215Z","iopub.execute_input":"2022-07-11T19:22:57.340990Z","iopub.status.idle":"2022-07-11T19:22:57.351483Z","shell.execute_reply.started":"2022-07-11T19:22:57.340945Z","shell.execute_reply":"2022-07-11T19:22:57.350079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainx , testx, trainy , testy = train_test_split(X , y , test_size = 0.2 , random_state=5)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T19:22:59.805720Z","iopub.execute_input":"2022-07-11T19:22:59.807234Z","iopub.status.idle":"2022-07-11T19:22:59.814084Z","shell.execute_reply.started":"2022-07-11T19:22:59.807171Z","shell.execute_reply":"2022-07-11T19:22:59.812686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainx.shape , testx.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-11T19:23:01.338923Z","iopub.execute_input":"2022-07-11T19:23:01.340182Z","iopub.status.idle":"2022-07-11T19:23:01.347764Z","shell.execute_reply.started":"2022-07-11T19:23:01.340131Z","shell.execute_reply":"2022-07-11T19:23:01.346779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = []\nfor k in range(1,10):\n    knn = KNeighborsClassifier(n_neighbors = k).fit(trainx , trainy)\n    predict = knn.predict(testx)\n    f1 = np.average(metrics.f1_score(testy , predict , average = None))\n    score.append(f1)\nscore","metadata":{"execution":{"iopub.status.busy":"2022-07-11T19:23:04.328676Z","iopub.execute_input":"2022-07-11T19:23:04.329501Z","iopub.status.idle":"2022-07-11T19:23:04.419184Z","shell.execute_reply.started":"2022-07-11T19:23:04.329462Z","shell.execute_reply":"2022-07-11T19:23:04.417683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tdf.loc[tdf['Sex']=='male','Sex'] = 0\ntdf.loc[tdf['Sex']=='female','Sex'] = 1\ntdf.loc[tdf['Embarked']=='C','Embarked'] = 0\ntdf.loc[tdf['Embarked']=='Q','Embarked'] = 1\ntdf.loc[tdf['Embarked']=='S','Embarked'] = 2\ntdf.fillna(value=0 , inplace=True)\ntdf","metadata":{"execution":{"iopub.status.busy":"2022-07-11T19:23:07.814542Z","iopub.execute_input":"2022-07-11T19:23:07.814990Z","iopub.status.idle":"2022-07-11T19:23:07.851240Z","shell.execute_reply.started":"2022-07-11T19:23:07.814956Z","shell.execute_reply":"2022-07-11T19:23:07.849975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"XTEST = tdf[['Pclass', 'Sex', 'Age', 'SibSp','Parch', 'Fare', 'Embarked']].values","metadata":{"execution":{"iopub.status.busy":"2022-07-11T19:23:10.605146Z","iopub.execute_input":"2022-07-11T19:23:10.606030Z","iopub.status.idle":"2022-07-11T19:23:10.616855Z","shell.execute_reply.started":"2022-07-11T19:23:10.605991Z","shell.execute_reply":"2022-07-11T19:23:10.615603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"    KNN = KNeighborsClassifier(n_neighbors = 9).fit(trainx , trainy)\n    PRE = knn.predict(XTEST)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T19:23:14.387991Z","iopub.execute_input":"2022-07-11T19:23:14.388801Z","iopub.status.idle":"2022-07-11T19:23:14.413434Z","shell.execute_reply.started":"2022-07-11T19:23:14.388739Z","shell.execute_reply":"2022-07-11T19:23:14.412230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PRE","metadata":{"execution":{"iopub.status.busy":"2022-07-11T19:23:17.945430Z","iopub.execute_input":"2022-07-11T19:23:17.946106Z","iopub.status.idle":"2022-07-11T19:23:17.952941Z","shell.execute_reply.started":"2022-07-11T19:23:17.946071Z","shell.execute_reply":"2022-07-11T19:23:17.951988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m = pd.DataFrame({'PassengerId': tdf['PassengerId'].values , 'Survived' :PRE})\nm","metadata":{"execution":{"iopub.status.busy":"2022-07-11T19:27:34.648481Z","iopub.execute_input":"2022-07-11T19:27:34.649200Z","iopub.status.idle":"2022-07-11T19:27:34.663290Z","shell.execute_reply.started":"2022-07-11T19:27:34.649164Z","shell.execute_reply":"2022-07-11T19:27:34.661425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m.to_csv('D:\\\\.a.TitanicAlive.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T19:39:17.949328Z","iopub.execute_input":"2022-07-11T19:39:17.950423Z","iopub.status.idle":"2022-07-11T19:39:17.959126Z","shell.execute_reply.started":"2022-07-11T19:39:17.950360Z","shell.execute_reply":"2022-07-11T19:39:17.957870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}