{"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 numpy as np\nfrom numpy import mean\nfrom numpy import std\nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set()\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.metrics import accuracy_score,classification_report,confusion_matrix\nfrom sklearn.linear_model import LogisticRegression\nimport warnings\nwarnings.filterwarnings('ignore')\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-16T14:35:59.333322Z","iopub.execute_input":"2022-07-16T14:35:59.333752Z","iopub.status.idle":"2022-07-16T14:35:59.349978Z","shell.execute_reply.started":"2022-07-16T14:35:59.333716Z","shell.execute_reply":"2022-07-16T14:35:59.348756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/titanic/train.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:35:59.411112Z","iopub.execute_input":"2022-07-16T14:35:59.411904Z","iopub.status.idle":"2022-07-16T14:35:59.437114Z","shell.execute_reply.started":"2022-07-16T14:35:59.411862Z","shell.execute_reply":"2022-07-16T14:35:59.435915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del train['PassengerId'],train['Name'],train['Ticket'],train['Cabin']","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:35:59.465349Z","iopub.execute_input":"2022-07-16T14:35:59.465837Z","iopub.status.idle":"2022-07-16T14:35:59.473414Z","shell.execute_reply.started":"2022-07-16T14:35:59.465798Z","shell.execute_reply":"2022-07-16T14:35:59.472392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.distplot(train.Age)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:35:59.529474Z","iopub.execute_input":"2022-07-16T14:35:59.530742Z","iopub.status.idle":"2022-07-16T14:35:59.775141Z","shell.execute_reply.started":"2022-07-16T14:35:59.530664Z","shell.execute_reply":"2022-07-16T14:35:59.774218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe(include='all')","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:35:59.776640Z","iopub.execute_input":"2022-07-16T14:35:59.777215Z","iopub.status.idle":"2022-07-16T14:35:59.815634Z","shell.execute_reply.started":"2022-07-16T14:35:59.777178Z","shell.execute_reply":"2022-07-16T14:35:59.814625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:35:59.816961Z","iopub.execute_input":"2022-07-16T14:35:59.817492Z","iopub.status.idle":"2022-07-16T14:35:59.827414Z","shell.execute_reply.started":"2022-07-16T14:35:59.817452Z","shell.execute_reply":"2022-07-16T14:35:59.826639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.Age.fillna(train.Age.mean(),inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:35:59.829390Z","iopub.execute_input":"2022-07-16T14:35:59.830333Z","iopub.status.idle":"2022-07-16T14:35:59.835472Z","shell.execute_reply.started":"2022-07-16T14:35:59.830298Z","shell.execute_reply":"2022-07-16T14:35:59.834622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.Embarked.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:35:59.836903Z","iopub.execute_input":"2022-07-16T14:35:59.837421Z","iopub.status.idle":"2022-07-16T14:35:59.849847Z","shell.execute_reply.started":"2022-07-16T14:35:59.837388Z","shell.execute_reply":"2022-07-16T14:35:59.848766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.Embarked = train.Embarked.replace(np.NaN,'S')","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:35:59.890855Z","iopub.execute_input":"2022-07-16T14:35:59.891628Z","iopub.status.idle":"2022-07-16T14:35:59.898613Z","shell.execute_reply.started":"2022-07-16T14:35:59.891586Z","shell.execute_reply":"2022-07-16T14:35:59.897756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:35:59.974051Z","iopub.execute_input":"2022-07-16T14:35:59.975061Z","iopub.status.idle":"2022-07-16T14:35:59.986774Z","shell.execute_reply.started":"2022-07-16T14:35:59.975010Z","shell.execute_reply":"2022-07-16T14:35:59.985573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:00.046992Z","iopub.execute_input":"2022-07-16T14:36:00.047825Z","iopub.status.idle":"2022-07-16T14:36:00.055767Z","shell.execute_reply.started":"2022-07-16T14:36:00.047788Z","shell.execute_reply":"2022-07-16T14:36:00.054774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.Sex = train.Sex.map({'male':1,'female':0})","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:00.139792Z","iopub.execute_input":"2022-07-16T14:36:00.140200Z","iopub.status.idle":"2022-07-16T14:36:00.147663Z","shell.execute_reply.started":"2022-07-16T14:36:00.140169Z","shell.execute_reply":"2022-07-16T14:36:00.146288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.get_dummies(train,columns=['Embarked'],drop_first=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:00.232219Z","iopub.execute_input":"2022-07-16T14:36:00.233361Z","iopub.status.idle":"2022-07-16T14:36:00.244331Z","shell.execute_reply.started":"2022-07-16T14:36:00.233306Z","shell.execute_reply":"2022-07-16T14:36:00.243357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:00.311473Z","iopub.execute_input":"2022-07-16T14:36:00.312609Z","iopub.status.idle":"2022-07-16T14:36:00.326599Z","shell.execute_reply.started":"2022-07-16T14:36:00.312565Z","shell.execute_reply":"2022-07-16T14:36:00.325612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train.iloc[:,1:].values\ny = train.iloc[:,0].values\n","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:00.364743Z","iopub.execute_input":"2022-07-16T14:36:00.365883Z","iopub.status.idle":"2022-07-16T14:36:00.372401Z","shell.execute_reply.started":"2022-07-16T14:36:00.365838Z","shell.execute_reply":"2022-07-16T14:36:00.371099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc = StandardScaler()\nX = sc.fit_transform(X)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:00.420112Z","iopub.execute_input":"2022-07-16T14:36:00.420900Z","iopub.status.idle":"2022-07-16T14:36:00.426281Z","shell.execute_reply.started":"2022-07-16T14:36:00.420860Z","shell.execute_reply":"2022-07-16T14:36:00.425444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rfc = RandomForestClassifier()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:00.472350Z","iopub.execute_input":"2022-07-16T14:36:00.473283Z","iopub.status.idle":"2022-07-16T14:36:00.477451Z","shell.execute_reply.started":"2022-07-16T14:36:00.473244Z","shell.execute_reply":"2022-07-16T14:36:00.476472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:00.526294Z","iopub.execute_input":"2022-07-16T14:36:00.527302Z","iopub.status.idle":"2022-07-16T14:36:00.532876Z","shell.execute_reply.started":"2022-07-16T14:36:00.527258Z","shell.execute_reply":"2022-07-16T14:36:00.532003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_params = ({'n_estimators':[100,200,300,400],'criterion':['gini', 'entropy', 'log_loss']})\ngrid_search = GridSearchCV(estimator=rfc,scoring='accuracy',\n             param_grid=grid_params)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:00.599936Z","iopub.execute_input":"2022-07-16T14:36:00.601032Z","iopub.status.idle":"2022-07-16T14:36:00.607654Z","shell.execute_reply.started":"2022-07-16T14:36:00.600977Z","shell.execute_reply":"2022-07-16T14:36:00.606860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_search.fit(X_train,y_train)\ngrid_search.best_params_","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:00.680207Z","iopub.execute_input":"2022-07-16T14:36:00.680921Z","iopub.status.idle":"2022-07-16T14:36:25.063889Z","shell.execute_reply.started":"2022-07-16T14:36:00.680882Z","shell.execute_reply":"2022-07-16T14:36:25.062830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_search.score(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:25.065581Z","iopub.execute_input":"2022-07-16T14:36:25.065904Z","iopub.status.idle":"2022-07-16T14:36:25.146325Z","shell.execute_reply.started":"2022-07-16T14:36:25.065875Z","shell.execute_reply":"2022-07-16T14:36:25.145173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rfc = RandomForestClassifier(criterion= 'entropy', n_estimators= 400)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:25.147888Z","iopub.execute_input":"2022-07-16T14:36:25.148568Z","iopub.status.idle":"2022-07-16T14:36:25.154233Z","shell.execute_reply.started":"2022-07-16T14:36:25.148525Z","shell.execute_reply":"2022-07-16T14:36:25.153022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rfc.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:25.156528Z","iopub.execute_input":"2022-07-16T14:36:25.157298Z","iopub.status.idle":"2022-07-16T14:36:26.034592Z","shell.execute_reply.started":"2022-07-16T14:36:25.157264Z","shell.execute_reply":"2022-07-16T14:36:26.033445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = rfc.predict(X_test)\naccuracy_score(prediction,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:26.036671Z","iopub.execute_input":"2022-07-16T14:36:26.037471Z","iopub.status.idle":"2022-07-16T14:36:26.108708Z","shell.execute_reply.started":"2022-07-16T14:36:26.037419Z","shell.execute_reply":"2022-07-16T14:36:26.107122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(prediction,y_test)\ncm","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:26.110725Z","iopub.execute_input":"2022-07-16T14:36:26.111299Z","iopub.status.idle":"2022-07-16T14:36:26.121225Z","shell.execute_reply.started":"2022-07-16T14:36:26.111247Z","shell.execute_reply":"2022-07-16T14:36:26.120021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(prediction,y_test))","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:26.122599Z","iopub.execute_input":"2022-07-16T14:36:26.123239Z","iopub.status.idle":"2022-07-16T14:36:26.133805Z","shell.execute_reply.started":"2022-07-16T14:36:26.123207Z","shell.execute_reply":"2022-07-16T14:36:26.132665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('../input/titanic/test.csv')\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:26.135055Z","iopub.execute_input":"2022-07-16T14:36:26.135412Z","iopub.status.idle":"2022-07-16T14:36:26.159541Z","shell.execute_reply.started":"2022-07-16T14:36:26.135378Z","shell.execute_reply":"2022-07-16T14:36:26.158699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del test['Name'],test['Ticket'],test['Cabin']","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:26.160829Z","iopub.execute_input":"2022-07-16T14:36:26.161313Z","iopub.status.idle":"2022-07-16T14:36:26.168538Z","shell.execute_reply.started":"2022-07-16T14:36:26.161279Z","shell.execute_reply":"2022-07-16T14:36:26.167482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:26.173153Z","iopub.execute_input":"2022-07-16T14:36:26.173483Z","iopub.status.idle":"2022-07-16T14:36:26.184105Z","shell.execute_reply.started":"2022-07-16T14:36:26.173453Z","shell.execute_reply":"2022-07-16T14:36:26.182762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.Age.fillna(test.Age.mean(),inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:26.186093Z","iopub.execute_input":"2022-07-16T14:36:26.186945Z","iopub.status.idle":"2022-07-16T14:36:26.194272Z","shell.execute_reply.started":"2022-07-16T14:36:26.186898Z","shell.execute_reply":"2022-07-16T14:36:26.193191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['Fare'].fillna(test.Fare.mean(),inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:26.195516Z","iopub.execute_input":"2022-07-16T14:36:26.196586Z","iopub.status.idle":"2022-07-16T14:36:26.206039Z","shell.execute_reply.started":"2022-07-16T14:36:26.196537Z","shell.execute_reply":"2022-07-16T14:36:26.205196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:26.207225Z","iopub.execute_input":"2022-07-16T14:36:26.207985Z","iopub.status.idle":"2022-07-16T14:36:26.220151Z","shell.execute_reply.started":"2022-07-16T14:36:26.207948Z","shell.execute_reply":"2022-07-16T14:36:26.219228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.Sex = test.Sex.map({'male':1,'female':0})","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:26.221291Z","iopub.execute_input":"2022-07-16T14:36:26.222559Z","iopub.status.idle":"2022-07-16T14:36:26.228792Z","shell.execute_reply.started":"2022-07-16T14:36:26.222523Z","shell.execute_reply":"2022-07-16T14:36:26.227938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.get_dummies(test,columns=['Embarked'],drop_first=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:26.230011Z","iopub.execute_input":"2022-07-16T14:36:26.230481Z","iopub.status.idle":"2022-07-16T14:36:26.244323Z","shell.execute_reply.started":"2022-07-16T14:36:26.230451Z","shell.execute_reply":"2022-07-16T14:36:26.243351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:26.245574Z","iopub.execute_input":"2022-07-16T14:36:26.246106Z","iopub.status.idle":"2022-07-16T14:36:26.263310Z","shell.execute_reply.started":"2022-07-16T14:36:26.246074Z","shell.execute_reply":"2022-07-16T14:36:26.262136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = test.iloc[:,1:].values\ninputs = sc.transform(inputs)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:39.073135Z","iopub.execute_input":"2022-07-16T14:36:39.073635Z","iopub.status.idle":"2022-07-16T14:36:39.083796Z","shell.execute_reply.started":"2022-07-16T14:36:39.073592Z","shell.execute_reply":"2022-07-16T14:36:39.082052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_prediction = rfc.predict(inputs)\nfinal_prediction = pd.DataFrame(final_prediction)\nfinal_prediction.columns=['Survived']\nfinal_prediction","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:36:40.751785Z","iopub.execute_input":"2022-07-16T14:36:40.752155Z","iopub.status.idle":"2022-07-16T14:36:40.844898Z","shell.execute_reply.started":"2022-07-16T14:36:40.752124Z","shell.execute_reply":"2022-07-16T14:36:40.843759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id = pd.DataFrame(test.PassengerId)\nid.columns = ['PassengerId']\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:37:51.434742Z","iopub.execute_input":"2022-07-16T14:37:51.435947Z","iopub.status.idle":"2022-07-16T14:37:51.451371Z","shell.execute_reply.started":"2022-07-16T14:37:51.435892Z","shell.execute_reply":"2022-07-16T14:37:51.450112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.concat([id,final_prediction],axis=1)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:43:39.186936Z","iopub.execute_input":"2022-07-16T14:43:39.187285Z","iopub.status.idle":"2022-07-16T14:43:39.203994Z","shell.execute_reply.started":"2022-07-16T14:43:39.187254Z","shell.execute_reply":"2022-07-16T14:43:39.202500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T14:43:54.457858Z","iopub.execute_input":"2022-07-16T14:43:54.458216Z","iopub.status.idle":"2022-07-16T14:43:54.465429Z","shell.execute_reply.started":"2022-07-16T14:43:54.458186Z","shell.execute_reply":"2022-07-16T14:43:54.463958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}