{"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-11T17:39:45.563132Z","iopub.execute_input":"2022-07-11T17:39:45.563586Z","iopub.status.idle":"2022-07-11T17:39:45.574153Z","shell.execute_reply.started":"2022-07-11T17:39:45.563547Z","shell.execute_reply":"2022-07-11T17:39:45.572760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as nm\nimport matplotlib as plt","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:45.650190Z","iopub.execute_input":"2022-07-11T17:39:45.650623Z","iopub.status.idle":"2022-07-11T17:39:45.655577Z","shell.execute_reply.started":"2022-07-11T17:39:45.650589Z","shell.execute_reply":"2022-07-11T17:39:45.654481Z"},"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-11T17:39:45.657069Z","iopub.execute_input":"2022-07-11T17:39:45.658012Z","iopub.status.idle":"2022-07-11T17:39:45.682092Z","shell.execute_reply.started":"2022-07-11T17:39:45.657962Z","shell.execute_reply":"2022-07-11T17:39:45.680911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:45.741959Z","iopub.execute_input":"2022-07-11T17:39:45.742352Z","iopub.status.idle":"2022-07-11T17:39:45.756406Z","shell.execute_reply.started":"2022-07-11T17:39:45.742324Z","shell.execute_reply":"2022-07-11T17:39:45.755476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:45.870846Z","iopub.execute_input":"2022-07-11T17:39:45.871301Z","iopub.status.idle":"2022-07-11T17:39:45.878086Z","shell.execute_reply.started":"2022-07-11T17:39:45.871263Z","shell.execute_reply":"2022-07-11T17:39:45.877306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=train.Survived\ntrain=train.drop(['Survived'],axis='columns')\ndatas=pd.concat([train,test],axis=0)\ndatas.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:45.879453Z","iopub.execute_input":"2022-07-11T17:39:45.880489Z","iopub.status.idle":"2022-07-11T17:39:45.900721Z","shell.execute_reply.started":"2022-07-11T17:39:45.880453Z","shell.execute_reply":"2022-07-11T17:39:45.899525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"per=test.PassengerId","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:45.950093Z","iopub.execute_input":"2022-07-11T17:39:45.951209Z","iopub.status.idle":"2022-07-11T17:39:45.956119Z","shell.execute_reply.started":"2022-07-11T17:39:45.951166Z","shell.execute_reply":"2022-07-11T17:39:45.955210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nan_cols = [i for i in datas.columns if datas[i].isnull().any()]\nnan_cols","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:45.957686Z","iopub.execute_input":"2022-07-11T17:39:45.958276Z","iopub.status.idle":"2022-07-11T17:39:45.978459Z","shell.execute_reply.started":"2022-07-11T17:39:45.958234Z","shell.execute_reply":"2022-07-11T17:39:45.977615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols_to_fill_zero1 = ['Fare','Age']\ndatas[cols_to_fill_zero1]=datas[cols_to_fill_zero1].fillna(datas[cols_to_fill_zero1].median())\ndatas['Cabin']=datas['Cabin'].fillna('none')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:46.070038Z","iopub.execute_input":"2022-07-11T17:39:46.071178Z","iopub.status.idle":"2022-07-11T17:39:46.080424Z","shell.execute_reply.started":"2022-07-11T17:39:46.071132Z","shell.execute_reply":"2022-07-11T17:39:46.079514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datas['Embarked'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:46.082550Z","iopub.execute_input":"2022-07-11T17:39:46.083228Z","iopub.status.idle":"2022-07-11T17:39:46.093236Z","shell.execute_reply.started":"2022-07-11T17:39:46.083185Z","shell.execute_reply":"2022-07-11T17:39:46.091870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datas['Embarked']=datas['Embarked'].fillna('S')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:46.132252Z","iopub.execute_input":"2022-07-11T17:39:46.133022Z","iopub.status.idle":"2022-07-11T17:39:46.138283Z","shell.execute_reply.started":"2022-07-11T17:39:46.132978Z","shell.execute_reply":"2022-07-11T17:39:46.137469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datas.tail()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:46.270865Z","iopub.execute_input":"2022-07-11T17:39:46.271551Z","iopub.status.idle":"2022-07-11T17:39:46.287292Z","shell.execute_reply.started":"2022-07-11T17:39:46.271512Z","shell.execute_reply":"2022-07-11T17:39:46.285848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datas=datas.drop(['Name','Ticket','PassengerId'],axis='columns')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:46.289271Z","iopub.execute_input":"2022-07-11T17:39:46.290603Z","iopub.status.idle":"2022-07-11T17:39:46.300072Z","shell.execute_reply.started":"2022-07-11T17:39:46.290561Z","shell.execute_reply":"2022-07-11T17:39:46.299157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = pd.get_dummies(datas, drop_first=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:46.310182Z","iopub.execute_input":"2022-07-11T17:39:46.311444Z","iopub.status.idle":"2022-07-11T17:39:46.325543Z","shell.execute_reply.started":"2022-07-11T17:39:46.311393Z","shell.execute_reply":"2022-07-11T17:39:46.324477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.tail()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:46.470277Z","iopub.execute_input":"2022-07-11T17:39:46.470988Z","iopub.status.idle":"2022-07-11T17:39:46.492144Z","shell.execute_reply.started":"2022-07-11T17:39:46.470950Z","shell.execute_reply":"2022-07-11T17:39:46.490898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=dataset.iloc[:891,:]\ntest=dataset.iloc[891:,:]","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:46.494133Z","iopub.execute_input":"2022-07-11T17:39:46.494451Z","iopub.status.idle":"2022-07-11T17:39:46.498695Z","shell.execute_reply.started":"2022-07-11T17:39:46.494423Z","shell.execute_reply":"2022-07-11T17:39:46.497951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:46.570329Z","iopub.execute_input":"2022-07-11T17:39:46.570808Z","iopub.status.idle":"2022-07-11T17:39:46.577928Z","shell.execute_reply.started":"2022-07-11T17:39:46.570754Z","shell.execute_reply":"2022-07-11T17:39:46.577055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ntrain_X, test_X, train_y, test_y = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:46.579669Z","iopub.execute_input":"2022-07-11T17:39:46.580248Z","iopub.status.idle":"2022-07-11T17:39:46.593559Z","shell.execute_reply.started":"2022-07-11T17:39:46.580203Z","shell.execute_reply":"2022-07-11T17:39:46.592614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import StratifiedKFold\nfolds = StratifiedKFold(n_splits=5)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:46.670148Z","iopub.execute_input":"2022-07-11T17:39:46.670809Z","iopub.status.idle":"2022-07-11T17:39:46.675381Z","shell.execute_reply.started":"2022-07-11T17:39:46.670749Z","shell.execute_reply":"2022-07-11T17:39:46.674398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import cross_val_score\n","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:46.710834Z","iopub.execute_input":"2022-07-11T17:39:46.711543Z","iopub.status.idle":"2022-07-11T17:39:46.716611Z","shell.execute_reply.started":"2022-07-11T17:39:46.711500Z","shell.execute_reply":"2022-07-11T17:39:46.715386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.svm import SVC\nmodel = SVC()\nmodel.fit(train_X,train_y)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:46.779727Z","iopub.execute_input":"2022-07-11T17:39:46.780408Z","iopub.status.idle":"2022-07-11T17:39:46.833321Z","shell.execute_reply.started":"2022-07-11T17:39:46.780367Z","shell.execute_reply":"2022-07-11T17:39:46.832332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.score(test_X,test_y)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:47.000474Z","iopub.execute_input":"2022-07-11T17:39:47.001270Z","iopub.status.idle":"2022-07-11T17:39:47.035601Z","shell.execute_reply.started":"2022-07-11T17:39:47.001213Z","shell.execute_reply":"2022-07-11T17:39:47.034726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cross_val_score(model,X, y)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:47.037538Z","iopub.execute_input":"2022-07-11T17:39:47.038102Z","iopub.status.idle":"2022-07-11T17:39:47.428664Z","shell.execute_reply.started":"2022-07-11T17:39:47.038065Z","shell.execute_reply":"2022-07-11T17:39:47.427306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel_C = SVC(C=45,coef0=1)\nmodel_C.fit(train_X,train_y)\nmodel_C.score(test_X,test_y)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:47.430564Z","iopub.execute_input":"2022-07-11T17:39:47.430942Z","iopub.status.idle":"2022-07-11T17:39:47.511608Z","shell.execute_reply.started":"2022-07-11T17:39:47.430909Z","shell.execute_reply":"2022-07-11T17:39:47.510770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cross_val_score(model_C,X, y)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:47.514293Z","iopub.execute_input":"2022-07-11T17:39:47.515107Z","iopub.status.idle":"2022-07-11T17:39:47.870339Z","shell.execute_reply.started":"2022-07-11T17:39:47.515070Z","shell.execute_reply":"2022-07-11T17:39:47.869163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nmodel_1=LogisticRegression()\nmodel_1.fit(train_X,train_y)\nmodel_1.score(test_X,test_y)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:47.872601Z","iopub.execute_input":"2022-07-11T17:39:47.872958Z","iopub.status.idle":"2022-07-11T17:39:47.955224Z","shell.execute_reply.started":"2022-07-11T17:39:47.872927Z","shell.execute_reply":"2022-07-11T17:39:47.953866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cross_val_score(model_1,X, y)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:47.956959Z","iopub.execute_input":"2022-07-11T17:39:47.958178Z","iopub.status.idle":"2022-07-11T17:39:48.366533Z","shell.execute_reply.started":"2022-07-11T17:39:47.958122Z","shell.execute_reply":"2022-07-11T17:39:48.365306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nmodl = RandomForestClassifier(n_estimators=35)\nmodl.fit(train_X,train_y)\nmodl.score(test_X,test_y)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:48.368266Z","iopub.execute_input":"2022-07-11T17:39:48.369052Z","iopub.status.idle":"2022-07-11T17:39:48.516474Z","shell.execute_reply.started":"2022-07-11T17:39:48.368998Z","shell.execute_reply":"2022-07-11T17:39:48.515728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cross_val_score(modl,X, y)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:48.517460Z","iopub.execute_input":"2022-07-11T17:39:48.517925Z","iopub.status.idle":"2022-07-11T17:39:48.966486Z","shell.execute_reply.started":"2022-07-11T17:39:48.517896Z","shell.execute_reply":"2022-07-11T17:39:48.965226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.svm import LinearSVC\n\nmodel_2 = LinearSVC(loss='hinge',dual=True)\nmodel_2.fit(train_X, train_y)\nmodel_2.score(test_X,test_y)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:48.968780Z","iopub.execute_input":"2022-07-11T17:39:48.969611Z","iopub.status.idle":"2022-07-11T17:39:49.025330Z","shell.execute_reply.started":"2022-07-11T17:39:48.969560Z","shell.execute_reply":"2022-07-11T17:39:49.024095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cross_val_score(model_2,X, y)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:49.027002Z","iopub.execute_input":"2022-07-11T17:39:49.028113Z","iopub.status.idle":"2022-07-11T17:39:49.411146Z","shell.execute_reply.started":"2022-07-11T17:39:49.028059Z","shell.execute_reply":"2022-07-11T17:39:49.409957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Using Ensemble ","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import GradientBoostingClassifier #For Regression\ncl = GradientBoostingClassifier(n_estimators=100, learning_rate=0.5, max_depth=3)\ncl.fit(train_X,train_y)\ncl.score(test_X,test_y)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:49.414895Z","iopub.execute_input":"2022-07-11T17:39:49.416839Z","iopub.status.idle":"2022-07-11T17:39:49.650326Z","shell.execute_reply.started":"2022-07-11T17:39:49.416767Z","shell.execute_reply":"2022-07-11T17:39:49.649060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cross_val_score(cl,X, y)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:49.653717Z","iopub.execute_input":"2022-07-11T17:39:49.654097Z","iopub.status.idle":"2022-07-11T17:39:50.575812Z","shell.execute_reply.started":"2022-07-11T17:39:49.654067Z","shell.execute_reply":"2022-07-11T17:39:50.574633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Making Final submission CSV file","metadata":{}},{"cell_type":"code","source":"predicted=cl.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:50.577084Z","iopub.execute_input":"2022-07-11T17:39:50.577446Z","iopub.status.idle":"2022-07-11T17:39:50.589570Z","shell.execute_reply.started":"2022-07-11T17:39:50.577413Z","shell.execute_reply":"2022-07-11T17:39:50.588392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = pd.DataFrame(predicted,columns=['Survived']).to_csv('submissio.csv',index=False)\nresult=pd.read_csv('submissio.csv')\nresult=pd.concat([per,result],axis='columns')\n#result=result.drop([1459],axis=0)\nresult.tail()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:50.591453Z","iopub.execute_input":"2022-07-11T17:39:50.592164Z","iopub.status.idle":"2022-07-11T17:39:50.611336Z","shell.execute_reply.started":"2022-07-11T17:39:50.592113Z","shell.execute_reply":"2022-07-11T17:39:50.609675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = pd.DataFrame(result, columns=['PassengerId','Survived']).to_csv('submission.csv',index=False)\nresults=pd.read_csv('submission.csv')\nresults.tail()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T17:39:50.613959Z","iopub.execute_input":"2022-07-11T17:39:50.614583Z","iopub.status.idle":"2022-07-11T17:39:50.630954Z","shell.execute_reply.started":"2022-07-11T17:39:50.614543Z","shell.execute_reply":"2022-07-11T17:39:50.629923Z"},"trusted":true},"execution_count":null,"outputs":[]}]}