{"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":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:11.598374Z","iopub.execute_input":"2022-07-29T04:03:11.598916Z","iopub.status.idle":"2022-07-29T04:03:12.777144Z","shell.execute_reply.started":"2022-07-29T04:03:11.598803Z","shell.execute_reply":"2022-07-29T04:03:12.775720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:12.779739Z","iopub.execute_input":"2022-07-29T04:03:12.780203Z","iopub.status.idle":"2022-07-29T04:03:12.789560Z","shell.execute_reply.started":"2022-07-29T04:03:12.780160Z","shell.execute_reply":"2022-07-29T04:03:12.788098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1. Load the training data, how many numerical features do you have?","metadata":{}},{"cell_type":"code","source":"df=pd.read_csv('../input/spaceship-titanic/train.csv')\ndf.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:12.791094Z","iopub.execute_input":"2022-07-29T04:03:12.791885Z","iopub.status.idle":"2022-07-29T04:03:12.889487Z","shell.execute_reply.started":"2022-07-29T04:03:12.791836Z","shell.execute_reply":"2022-07-29T04:03:12.888412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.tail(5)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:12.892291Z","iopub.execute_input":"2022-07-29T04:03:12.892690Z","iopub.status.idle":"2022-07-29T04:03:12.915121Z","shell.execute_reply.started":"2022-07-29T04:03:12.892657Z","shell.execute_reply":"2022-07-29T04:03:12.914003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info() #or df.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:12.917132Z","iopub.execute_input":"2022-07-29T04:03:12.917862Z","iopub.status.idle":"2022-07-29T04:03:12.957377Z","shell.execute_reply.started":"2022-07-29T04:03:12.917824Z","shell.execute_reply":"2022-07-29T04:03:12.956405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. How many observations do you have in a training set?","metadata":{}},{"cell_type":"code","source":"df.shape[0]","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:12.959185Z","iopub.execute_input":"2022-07-29T04:03:12.959991Z","iopub.status.idle":"2022-07-29T04:03:12.966299Z","shell.execute_reply.started":"2022-07-29T04:03:12.959943Z","shell.execute_reply":"2022-07-29T04:03:12.965340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3. Select Age groups with a prevalent number of Transported passengers","metadata":{}},{"cell_type":"code","source":"def func(x):\n    if x<=20:\n        return '0-20'\n    if 20<x<=40:\n        return '20-40'\n    if  40<x<60:\n        return '40-60'\n    if x>60:\n        return '60-'\ndf['Age_grp']=df['Age'].apply(func)    ","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:12.967841Z","iopub.execute_input":"2022-07-29T04:03:12.968897Z","iopub.status.idle":"2022-07-29T04:03:12.991618Z","shell.execute_reply.started":"2022-07-29T04:03:12.968859Z","shell.execute_reply":"2022-07-29T04:03:12.990605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Age_grp'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:12.993307Z","iopub.execute_input":"2022-07-29T04:03:12.994612Z","iopub.status.idle":"2022-07-29T04:03:13.005712Z","shell.execute_reply.started":"2022-07-29T04:03:12.994567Z","shell.execute_reply":"2022-07-29T04:03:13.004759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='Age_grp',data=df)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:13.007283Z","iopub.execute_input":"2022-07-29T04:03:13.007929Z","iopub.status.idle":"2022-07-29T04:03:13.218558Z","shell.execute_reply.started":"2022-07-29T04:03:13.007896Z","shell.execute_reply":"2022-07-29T04:03:13.217701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4. Which HomePlanet has the highest percentage of Transported passengers?","metadata":{}},{"cell_type":"code","source":"df.groupby('HomePlanet')['Transported'].mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:13.221971Z","iopub.execute_input":"2022-07-29T04:03:13.222708Z","iopub.status.idle":"2022-07-29T04:03:13.234079Z","shell.execute_reply.started":"2022-07-29T04:03:13.222671Z","shell.execute_reply":"2022-07-29T04:03:13.233028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 5. Does CryoSleep increase the chances of being Transported?","metadata":{}},{"cell_type":"code","source":"df.groupby('CryoSleep')['Transported'].mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:13.235638Z","iopub.execute_input":"2022-07-29T04:03:13.236432Z","iopub.status.idle":"2022-07-29T04:03:13.248340Z","shell.execute_reply.started":"2022-07-29T04:03:13.236390Z","shell.execute_reply":"2022-07-29T04:03:13.247463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 6. What is one-hot-encoding?","metadata":{}},{"cell_type":"code","source":"df_target=pd.get_dummies(df.HomePlanet)\ndf_target.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:13.251221Z","iopub.execute_input":"2022-07-29T04:03:13.251608Z","iopub.status.idle":"2022-07-29T04:03:13.264793Z","shell.execute_reply.started":"2022-07-29T04:03:13.251568Z","shell.execute_reply":"2022-07-29T04:03:13.263708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_target.tail(5)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:13.266555Z","iopub.execute_input":"2022-07-29T04:03:13.267302Z","iopub.status.idle":"2022-07-29T04:03:13.278390Z","shell.execute_reply.started":"2022-07-29T04:03:13.267268Z","shell.execute_reply":"2022-07-29T04:03:13.276990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_target1=pd.get_dummies(df.Destination)\ndf_target1.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:13.280040Z","iopub.execute_input":"2022-07-29T04:03:13.281179Z","iopub.status.idle":"2022-07-29T04:03:13.303429Z","shell.execute_reply.started":"2022-07-29T04:03:13.281132Z","shell.execute_reply":"2022-07-29T04:03:13.301940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_target1.tail(5)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:13.305104Z","iopub.execute_input":"2022-07-29T04:03:13.305727Z","iopub.status.idle":"2022-07-29T04:03:13.316055Z","shell.execute_reply.started":"2022-07-29T04:03:13.305694Z","shell.execute_reply":"2022-07-29T04:03:13.314848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 7. Construct a simple pipeline.","metadata":{}},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:13.317580Z","iopub.execute_input":"2022-07-29T04:03:13.318174Z","iopub.status.idle":"2022-07-29T04:03:13.328374Z","shell.execute_reply.started":"2022-07-29T04:03:13.318141Z","shell.execute_reply":"2022-07-29T04:03:13.327483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=df.drop(['Name','PassengerId','Cabin','Transported', 'Age_grp'],axis=1)\ny_train=df['Transported']\nnumeric_features=[   'Age', 'RoomService', 'FoodCourt', 'ShoppingMall', 'Spa', 'VRDeck']\ncategorical_features=[ 'HomePlanet', 'CryoSleep','Destination','VIP']\n","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:13.329905Z","iopub.execute_input":"2022-07-29T04:03:13.330265Z","iopub.status.idle":"2022-07-29T04:03:13.346308Z","shell.execute_reply.started":"2022-07-29T04:03:13.330234Z","shell.execute_reply":"2022-07-29T04:03:13.344561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:13.348259Z","iopub.execute_input":"2022-07-29T04:03:13.349186Z","iopub.status.idle":"2022-07-29T04:03:13.371930Z","shell.execute_reply.started":"2022-07-29T04:03:13.349136Z","shell.execute_reply":"2022-07-29T04:03:13.370806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:13.373253Z","iopub.execute_input":"2022-07-29T04:03:13.373706Z","iopub.status.idle":"2022-07-29T04:03:13.392311Z","shell.execute_reply.started":"2022-07-29T04:03:13.373673Z","shell.execute_reply":"2022-07-29T04:03:13.390380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"we need to fill missing column values in categorical features with mode and in numerical with 0 or mean\n","metadata":{}},{"cell_type":"code","source":"\nfor feature in categorical_features:\n    m=x_train[feature].mode().values[0]\n    x_train[feature]=x_train[feature].fillna(str(m))\n    x_train[feature]=x_train[feature].astype(str)\n    ","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:13.395889Z","iopub.execute_input":"2022-07-29T04:03:13.396431Z","iopub.status.idle":"2022-07-29T04:03:13.432583Z","shell.execute_reply.started":"2022-07-29T04:03:13.396383Z","shell.execute_reply":"2022-07-29T04:03:13.431262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in numeric_features:\n    if col=='Age':\n        x_train[col]=x_train[col].fillna(x_train[col].mean())\n    else:\n        x_train[col]=x_train[col].fillna(0)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:13.434790Z","iopub.execute_input":"2022-07-29T04:03:13.435270Z","iopub.status.idle":"2022-07-29T04:03:13.448305Z","shell.execute_reply.started":"2022-07-29T04:03:13.435227Z","shell.execute_reply":"2022-07-29T04:03:13.446867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:13.453954Z","iopub.execute_input":"2022-07-29T04:03:13.455046Z","iopub.status.idle":"2022-07-29T04:03:13.478863Z","shell.execute_reply.started":"2022-07-29T04:03:13.454996Z","shell.execute_reply":"2022-07-29T04:03:13.477602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"let us standardize numerical features\n","metadata":{}},{"cell_type":"code","source":"mean=x_train[numeric_features].mean(axis=0)\nstd=x_train[numeric_features].std(axis=0)\nx_train[numeric_features]=(x_train[numeric_features]-mean)/std","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:13.479972Z","iopub.execute_input":"2022-07-29T04:03:13.480602Z","iopub.status.idle":"2022-07-29T04:03:13.498124Z","shell.execute_reply.started":"2022-07-29T04:03:13.480557Z","shell.execute_reply":"2022-07-29T04:03:13.497112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:13.534076Z","iopub.execute_input":"2022-07-29T04:03:13.534998Z","iopub.status.idle":"2022-07-29T04:03:13.545499Z","shell.execute_reply.started":"2022-07-29T04:03:13.534942Z","shell.execute_reply":"2022-07-29T04:03:13.543866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:13.548049Z","iopub.execute_input":"2022-07-29T04:03:13.549288Z","iopub.status.idle":"2022-07-29T04:03:13.572999Z","shell.execute_reply.started":"2022-07-29T04:03:13.549234Z","shell.execute_reply":"2022-07-29T04:03:13.572065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"we have no null values now let us move to dealing with categorical data","metadata":{}},{"cell_type":"code","source":"d_col=pd.get_dummies(x_train[categorical_features])\nx_train=x_train.drop(categorical_features,axis=1)\nx_train=pd.merge(x_train,d_col,right_index=True,left_index=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:13.574047Z","iopub.execute_input":"2022-07-29T04:03:13.574977Z","iopub.status.idle":"2022-07-29T04:03:13.597946Z","shell.execute_reply.started":"2022-07-29T04:03:13.574945Z","shell.execute_reply":"2022-07-29T04:03:13.596542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:13.600475Z","iopub.execute_input":"2022-07-29T04:03:13.601849Z","iopub.status.idle":"2022-07-29T04:03:13.623809Z","shell.execute_reply.started":"2022-07-29T04:03:13.601793Z","shell.execute_reply":"2022-07-29T04:03:13.622590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:13.625510Z","iopub.execute_input":"2022-07-29T04:03:13.625990Z","iopub.status.idle":"2022-07-29T04:03:13.634575Z","shell.execute_reply.started":"2022-07-29T04:03:13.625942Z","shell.execute_reply":"2022-07-29T04:03:13.633349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:13.636119Z","iopub.execute_input":"2022-07-29T04:03:13.637415Z","iopub.status.idle":"2022-07-29T04:03:13.647839Z","shell.execute_reply.started":"2022-07-29T04:03:13.637366Z","shell.execute_reply":"2022-07-29T04:03:13.646463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"we have 16 columns with 8693 samples","metadata":{}},{"cell_type":"code","source":"from sklearn import tree\nclf=tree.DecisionTreeClassifier()\nmodel=clf.fit(x_train,y_train)\naccuracy=model.score(x_train,y_train)\nprint(accuracy)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:13.704427Z","iopub.execute_input":"2022-07-29T04:03:13.704818Z","iopub.status.idle":"2022-07-29T04:03:14.176181Z","shell.execute_reply.started":"2022-07-29T04:03:13.704790Z","shell.execute_reply":"2022-07-29T04:03:14.174965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n=y_train.shape[0]\ny_pred=model.predict(x_train)\nplt.scatter(range(n),y_train,color='green',label='train labels')\nplt.scatter(range(n),y_pred,color='red',label='predicted labels')\nplt.legend()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:14.178865Z","iopub.execute_input":"2022-07-29T04:03:14.179386Z","iopub.status.idle":"2022-07-29T04:03:14.954749Z","shell.execute_reply.started":"2022-07-29T04:03:14.179351Z","shell.execute_reply":"2022-07-29T04:03:14.953295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(y_train==y_pred).sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:14.956126Z","iopub.execute_input":"2022-07-29T04:03:14.956468Z","iopub.status.idle":"2022-07-29T04:03:14.964404Z","shell.execute_reply.started":"2022-07-29T04:03:14.956435Z","shell.execute_reply":"2022-07-29T04:03:14.963544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## predicting on test data\ndf_test=pd.read_csv('../input/spaceship-titanic/test.csv')\ndf_test.head(4)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:14.965881Z","iopub.execute_input":"2022-07-29T04:03:14.966470Z","iopub.status.idle":"2022-07-29T04:03:15.019688Z","shell.execute_reply.started":"2022-07-29T04:03:14.966434Z","shell.execute_reply":"2022-07-29T04:03:15.018849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.tail(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:15.022270Z","iopub.execute_input":"2022-07-29T04:03:15.022868Z","iopub.status.idle":"2022-07-29T04:03:15.043217Z","shell.execute_reply.started":"2022-07-29T04:03:15.022832Z","shell.execute_reply":"2022-07-29T04:03:15.041995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test[\"CryoSleep\"].mode()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:15.045079Z","iopub.execute_input":"2022-07-29T04:03:15.045834Z","iopub.status.idle":"2022-07-29T04:03:15.057366Z","shell.execute_reply.started":"2022-07-29T04:03:15.045784Z","shell.execute_reply":"2022-07-29T04:03:15.056163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Treat missing values","metadata":{}},{"cell_type":"code","source":"df_test['Age']=df_test['Age'].fillna(df_test[\"Age\"].mean())\ndf_test=df_test.drop(['PassengerId','Name','Cabin'],axis=1)\ndf_test[numeric_features]=df[numeric_features].fillna(0)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:15.059089Z","iopub.execute_input":"2022-07-29T04:03:15.060010Z","iopub.status.idle":"2022-07-29T04:03:15.076940Z","shell.execute_reply.started":"2022-07-29T04:03:15.059964Z","shell.execute_reply":"2022-07-29T04:03:15.075423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean=df_test[numeric_features].mean(axis=0)\nstd=df_test[numeric_features].std(axis=0)\ndf_test[numeric_features]=(df_test[numeric_features]-mean)/std","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:15.079503Z","iopub.execute_input":"2022-07-29T04:03:15.080608Z","iopub.status.idle":"2022-07-29T04:03:15.096686Z","shell.execute_reply.started":"2022-07-29T04:03:15.080555Z","shell.execute_reply":"2022-07-29T04:03:15.095745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:15.098217Z","iopub.execute_input":"2022-07-29T04:03:15.099215Z","iopub.status.idle":"2022-07-29T04:03:15.115584Z","shell.execute_reply.started":"2022-07-29T04:03:15.099167Z","shell.execute_reply":"2022-07-29T04:03:15.114149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in categorical_features:\n    m=df_test[col].mode().values[0]\n    df_test[col]=df_test[col].fillna(str(m))\n    df_test[col]=df_test[col].astype(str)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:15.117448Z","iopub.execute_input":"2022-07-29T04:03:15.118289Z","iopub.status.idle":"2022-07-29T04:03:15.139436Z","shell.execute_reply.started":"2022-07-29T04:03:15.118234Z","shell.execute_reply":"2022-07-29T04:03:15.138624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:15.140853Z","iopub.execute_input":"2022-07-29T04:03:15.141305Z","iopub.status.idle":"2022-07-29T04:03:15.158096Z","shell.execute_reply.started":"2022-07-29T04:03:15.141257Z","shell.execute_reply":"2022-07-29T04:03:15.156962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"lets convert categorical to numeric","metadata":{}},{"cell_type":"code","source":"du_col=pd.get_dummies(df_test[categorical_features])\ndf_test=df_test.drop(categorical_features,axis=1)\nx_test=pd.merge(df_test,du_col,right_index=True,left_index=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:15.159869Z","iopub.execute_input":"2022-07-29T04:03:15.160642Z","iopub.status.idle":"2022-07-29T04:03:15.181107Z","shell.execute_reply.started":"2022-07-29T04:03:15.160592Z","shell.execute_reply":"2022-07-29T04:03:15.179862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:15.185290Z","iopub.execute_input":"2022-07-29T04:03:15.185684Z","iopub.status.idle":"2022-07-29T04:03:15.205123Z","shell.execute_reply.started":"2022-07-29T04:03:15.185650Z","shell.execute_reply":"2022-07-29T04:03:15.203660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:15.208938Z","iopub.execute_input":"2022-07-29T04:03:15.209510Z","iopub.status.idle":"2022-07-29T04:03:15.220447Z","shell.execute_reply.started":"2022-07-29T04:03:15.209477Z","shell.execute_reply":"2022-07-29T04:03:15.218874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:15.222753Z","iopub.execute_input":"2022-07-29T04:03:15.223386Z","iopub.status.idle":"2022-07-29T04:03:15.241883Z","shell.execute_reply.started":"2022-07-29T04:03:15.223314Z","shell.execute_reply":"2022-07-29T04:03:15.240179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t=pd.read_csv('../input/spaceship-titanic/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:15.243676Z","iopub.execute_input":"2022-07-29T04:03:15.244711Z","iopub.status.idle":"2022-07-29T04:03:15.280432Z","shell.execute_reply.started":"2022-07-29T04:03:15.244675Z","shell.execute_reply":"2022-07-29T04:03:15.276642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df=pd.DataFrame()\nnew_df['PassengerId']=df_t['PassengerId']\nnew_df['Transported']=y","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:15.284813Z","iopub.execute_input":"2022-07-29T04:03:15.285607Z","iopub.status.idle":"2022-07-29T04:03:15.298139Z","shell.execute_reply.started":"2022-07-29T04:03:15.285555Z","shell.execute_reply":"2022-07-29T04:03:15.296996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:15.300128Z","iopub.execute_input":"2022-07-29T04:03:15.301127Z","iopub.status.idle":"2022-07-29T04:03:15.315311Z","shell.execute_reply.started":"2022-07-29T04:03:15.301082Z","shell.execute_reply":"2022-07-29T04:03:15.314360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:03:15.316680Z","iopub.execute_input":"2022-07-29T04:03:15.317031Z","iopub.status.idle":"2022-07-29T04:03:15.331013Z","shell.execute_reply.started":"2022-07-29T04:03:15.317000Z","shell.execute_reply":"2022-07-29T04:03:15.329862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nmodel1=KNeighborsClassifier(7)\nmodel1.fit(x_train[0:5000],y_train[0:5000])","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:13:43.865559Z","iopub.execute_input":"2022-07-29T04:13:43.866072Z","iopub.status.idle":"2022-07-29T04:13:43.879352Z","shell.execute_reply.started":"2022-07-29T04:13:43.866018Z","shell.execute_reply":"2022-07-29T04:13:43.877738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred1=model1.predict(x_train[5000:])\nn=len(y_train[5000:])\nplt.scatter(range(n),y_pred1,label='prediction',color='blue')\nplt.scatter(range(n),y_train[5000:],label='train_labels',color='green')\nplt.legend()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:15:55.857584Z","iopub.execute_input":"2022-07-29T04:15:55.858551Z","iopub.status.idle":"2022-07-29T04:15:56.829172Z","shell.execute_reply.started":"2022-07-29T04:15:55.858492Z","shell.execute_reply":"2022-07-29T04:15:56.827919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(y_pred1==y_train[5000:]).sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:17:07.171880Z","iopub.execute_input":"2022-07-29T04:17:07.172315Z","iopub.status.idle":"2022-07-29T04:17:07.181364Z","shell.execute_reply.started":"2022-07-29T04:17:07.172284Z","shell.execute_reply":"2022-07-29T04:17:07.180160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_y=model1.predict(x_test)\nn_df=pd.DataFrame()\nn_df['PassengerId']=df_t['PassengerId']\nn_df['Transported']=n_y\n","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:21:01.329567Z","iopub.execute_input":"2022-07-29T04:21:01.329963Z","iopub.status.idle":"2022-07-29T04:21:01.978722Z","shell.execute_reply.started":"2022-07-29T04:21:01.329930Z","shell.execute_reply":"2022-07-29T04:21:01.977663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_df","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:21:09.108185Z","iopub.execute_input":"2022-07-29T04:21:09.108581Z","iopub.status.idle":"2022-07-29T04:21:09.122789Z","shell.execute_reply.started":"2022-07-29T04:21:09.108540Z","shell.execute_reply":"2022-07-29T04:21:09.121469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_df.to_csv('kn_sub.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-29T04:21:37.407710Z","iopub.execute_input":"2022-07-29T04:21:37.408116Z","iopub.status.idle":"2022-07-29T04:21:37.429352Z","shell.execute_reply.started":"2022-07-29T04:21:37.408083Z","shell.execute_reply":"2022-07-29T04:21:37.428202Z"},"trusted":true},"execution_count":null,"outputs":[]}]}