{"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":"#importing libraries....\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:37:58.047907Z","iopub.execute_input":"2022-07-31T15:37:58.048329Z","iopub.status.idle":"2022-07-31T15:37:58.067396Z","shell.execute_reply.started":"2022-07-31T15:37:58.048289Z","shell.execute_reply":"2022-07-31T15:37:58.065914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/titanic/train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:37:58.068920Z","iopub.execute_input":"2022-07-31T15:37:58.069626Z","iopub.status.idle":"2022-07-31T15:37:58.096833Z","shell.execute_reply.started":"2022-07-31T15:37:58.069592Z","shell.execute_reply":"2022-07-31T15:37:58.095587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:37:58.146094Z","iopub.execute_input":"2022-07-31T15:37:58.146519Z","iopub.status.idle":"2022-07-31T15:37:58.161409Z","shell.execute_reply.started":"2022-07-31T15:37:58.146483Z","shell.execute_reply":"2022-07-31T15:37:58.160567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:37:58.162874Z","iopub.execute_input":"2022-07-31T15:37:58.163531Z","iopub.status.idle":"2022-07-31T15:37:58.177807Z","shell.execute_reply.started":"2022-07-31T15:37:58.163499Z","shell.execute_reply":"2022-07-31T15:37:58.176351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Now, we have to clean our data**","metadata":{}},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"code","source":"# We will drop column name cabin because it contains several null values\n\ndf = df.drop(columns='Cabin',axis =1)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:37:58.187564Z","iopub.execute_input":"2022-07-31T15:37:58.188218Z","iopub.status.idle":"2022-07-31T15:37:58.193878Z","shell.execute_reply.started":"2022-07-31T15:37:58.188140Z","shell.execute_reply":"2022-07-31T15:37:58.192717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# We are taking mean of age to fill NaN value present in age column.\n\ndf['Age'].fillna(df['Age'].mean(),inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:37:58.196028Z","iopub.execute_input":"2022-07-31T15:37:58.196387Z","iopub.status.idle":"2022-07-31T15:37:58.208623Z","shell.execute_reply.started":"2022-07-31T15:37:58.196335Z","shell.execute_reply":"2022-07-31T15:37:58.207367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# In embarked column there are two Nan values alnd all data present in this column is categorical data,so i am filling it with mode values.\ndf['Embarked'].mode()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:37:58.210644Z","iopub.execute_input":"2022-07-31T15:37:58.211106Z","iopub.status.idle":"2022-07-31T15:37:58.228988Z","shell.execute_reply.started":"2022-07-31T15:37:58.211063Z","shell.execute_reply":"2022-07-31T15:37:58.227943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Embarked'].fillna(df['Age'].mode()[0],inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:37:58.242614Z","iopub.execute_input":"2022-07-31T15:37:58.243388Z","iopub.status.idle":"2022-07-31T15:37:58.249618Z","shell.execute_reply.started":"2022-07-31T15:37:58.243324Z","shell.execute_reply":"2022-07-31T15:37:58.248650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:37:58.251781Z","iopub.execute_input":"2022-07-31T15:37:58.252288Z","iopub.status.idle":"2022-07-31T15:37:58.266589Z","shell.execute_reply.started":"2022-07-31T15:37:58.252258Z","shell.execute_reply":"2022-07-31T15:37:58.265534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:37:58.267758Z","iopub.execute_input":"2022-07-31T15:37:58.268793Z","iopub.status.idle":"2022-07-31T15:37:58.304683Z","shell.execute_reply.started":"2022-07-31T15:37:58.268757Z","shell.execute_reply":"2022-07-31T15:37:58.303665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:37:58.306526Z","iopub.execute_input":"2022-07-31T15:37:58.307019Z","iopub.status.idle":"2022-07-31T15:37:58.324573Z","shell.execute_reply.started":"2022-07-31T15:37:58.306989Z","shell.execute_reply":"2022-07-31T15:37:58.322984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Visualiztion","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15,6))\nsns.countplot(data = df,x= 'Sex',hue = 'Survived')","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:37:58.325732Z","iopub.execute_input":"2022-07-31T15:37:58.326702Z","iopub.status.idle":"2022-07-31T15:37:58.534289Z","shell.execute_reply.started":"2022-07-31T15:37:58.326657Z","shell.execute_reply":"2022-07-31T15:37:58.533346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Sex'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:37:58.535569Z","iopub.execute_input":"2022-07-31T15:37:58.536065Z","iopub.status.idle":"2022-07-31T15:37:58.545353Z","shell.execute_reply.started":"2022-07-31T15:37:58.536035Z","shell.execute_reply":"2022-07-31T15:37:58.543920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,6))\nsns.countplot(data = df,x= 'Pclass',hue = 'Survived')","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:37:58.549318Z","iopub.execute_input":"2022-07-31T15:37:58.550064Z","iopub.status.idle":"2022-07-31T15:37:58.768927Z","shell.execute_reply.started":"2022-07-31T15:37:58.550024Z","shell.execute_reply":"2022-07-31T15:37:58.767666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Pclass'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:37:58.770493Z","iopub.execute_input":"2022-07-31T15:37:58.770820Z","iopub.status.idle":"2022-07-31T15:37:58.779907Z","shell.execute_reply.started":"2022-07-31T15:37:58.770792Z","shell.execute_reply":"2022-07-31T15:37:58.778811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,6))\nsns.countplot(data = df,x= 'Parch',hue = 'Survived')","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:37:58.781506Z","iopub.execute_input":"2022-07-31T15:37:58.782535Z","iopub.status.idle":"2022-07-31T15:37:59.245835Z","shell.execute_reply.started":"2022-07-31T15:37:58.782497Z","shell.execute_reply":"2022-07-31T15:37:59.244640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,6))\nsns.countplot(data = df,x= 'SibSp',hue = 'Survived')","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:37:59.247790Z","iopub.execute_input":"2022-07-31T15:37:59.248654Z","iopub.status.idle":"2022-07-31T15:37:59.517561Z","shell.execute_reply.started":"2022-07-31T15:37:59.248617Z","shell.execute_reply":"2022-07-31T15:37:59.516438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,6))\nsns.countplot(data = df,x= 'Embarked',hue = 'Survived')","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:37:59.519084Z","iopub.execute_input":"2022-07-31T15:37:59.519457Z","iopub.status.idle":"2022-07-31T15:37:59.762212Z","shell.execute_reply.started":"2022-07-31T15:37:59.519425Z","shell.execute_reply":"2022-07-31T15:37:59.760224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.pairplot(df,hue = 'Survived')","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:37:59.763736Z","iopub.execute_input":"2022-07-31T15:37:59.764047Z","iopub.status.idle":"2022-07-31T15:38:10.325371Z","shell.execute_reply.started":"2022-07-31T15:37:59.764019Z","shell.execute_reply":"2022-07-31T15:38:10.323865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# As we saw in countplot between **Sex** and **Survived** we got to know that Survivors depends upon Sex.\n# So, we apply labelling .. \ndf.replace({'Sex':{'female':0,'male': 1}},inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:38:10.326796Z","iopub.execute_input":"2022-07-31T15:38:10.327138Z","iopub.status.idle":"2022-07-31T15:38:10.335773Z","shell.execute_reply.started":"2022-07-31T15:38:10.327108Z","shell.execute_reply":"2022-07-31T15:38:10.334603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# As we saw in countplot between **Embarked** and **Survived** we got to know that Survivors depends upon Embarked.\n# So, we apply labelling.. \ndf.replace({'Embarked':{'S':0,'C': 1,'Q':2}},inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:38:10.337550Z","iopub.execute_input":"2022-07-31T15:38:10.337914Z","iopub.status.idle":"2022-07-31T15:38:10.350278Z","shell.execute_reply.started":"2022-07-31T15:38:10.337882Z","shell.execute_reply":"2022-07-31T15:38:10.349414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:38:10.354515Z","iopub.execute_input":"2022-07-31T15:38:10.355436Z","iopub.status.idle":"2022-07-31T15:38:10.374293Z","shell.execute_reply.started":"2022-07-31T15:38:10.355392Z","shell.execute_reply":"2022-07-31T15:38:10.372829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This shows the correlation amoung all parameters..\n\nplt.figure(figsize=(15,6))\nsns.heatmap(df.corr(),annot = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:38:10.375784Z","iopub.execute_input":"2022-07-31T15:38:10.376795Z","iopub.status.idle":"2022-07-31T15:38:10.996089Z","shell.execute_reply.started":"2022-07-31T15:38:10.376757Z","shell.execute_reply":"2022-07-31T15:38:10.994860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df[['Pclass','Sex','Age','SibSp','Parch','Embarked','Fare']]\ny = df['Survived']","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:38:10.997916Z","iopub.execute_input":"2022-07-31T15:38:10.998653Z","iopub.status.idle":"2022-07-31T15:38:11.005605Z","shell.execute_reply.started":"2022-07-31T15:38:10.998609Z","shell.execute_reply":"2022-07-31T15:38:11.004448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Logistic Regression","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:38:11.007234Z","iopub.execute_input":"2022-07-31T15:38:11.007711Z","iopub.status.idle":"2022-07-31T15:38:11.018037Z","shell.execute_reply.started":"2022-07-31T15:38:11.007666Z","shell.execute_reply":"2022-07-31T15:38:11.016902Z"},"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.33, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:38:11.019696Z","iopub.execute_input":"2022-07-31T15:38:11.020165Z","iopub.status.idle":"2022-07-31T15:38:11.032054Z","shell.execute_reply.started":"2022-07-31T15:38:11.020110Z","shell.execute_reply":"2022-07-31T15:38:11.030991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\n\nlm = LogisticRegression()\nlm.fit(X_train,y_train)\n\npredictions = lm.predict(X_test)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:38:11.033348Z","iopub.execute_input":"2022-07-31T15:38:11.034537Z","iopub.status.idle":"2022-07-31T15:38:11.076813Z","shell.execute_reply.started":"2022-07-31T15:38:11.034491Z","shell.execute_reply":"2022-07-31T15:38:11.075451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report,confusion_matrix,accuracy_score","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:38:11.078416Z","iopub.execute_input":"2022-07-31T15:38:11.079027Z","iopub.status.idle":"2022-07-31T15:38:11.084144Z","shell.execute_reply.started":"2022-07-31T15:38:11.078990Z","shell.execute_reply":"2022-07-31T15:38:11.083375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(accuracy_score(y_test,predictions))","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:38:11.085535Z","iopub.execute_input":"2022-07-31T15:38:11.085870Z","iopub.status.idle":"2022-07-31T15:38:11.095943Z","shell.execute_reply.started":"2022-07-31T15:38:11.085840Z","shell.execute_reply":"2022-07-31T15:38:11.094988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test,predictions))","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:38:11.097190Z","iopub.execute_input":"2022-07-31T15:38:11.097708Z","iopub.status.idle":"2022-07-31T15:38:11.112268Z","shell.execute_reply.started":"2022-07-31T15:38:11.097676Z","shell.execute_reply":"2022-07-31T15:38:11.111094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Decision Tree","metadata":{}},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:38:11.113335Z","iopub.execute_input":"2022-07-31T15:38:11.114509Z","iopub.status.idle":"2022-07-31T15:38:11.120970Z","shell.execute_reply.started":"2022-07-31T15:38:11.114464Z","shell.execute_reply":"2022-07-31T15:38:11.120052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dt = DecisionTreeClassifier(criterion='entropy')","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:38:11.122375Z","iopub.execute_input":"2022-07-31T15:38:11.124747Z","iopub.status.idle":"2022-07-31T15:38:11.130384Z","shell.execute_reply.started":"2022-07-31T15:38:11.124710Z","shell.execute_reply":"2022-07-31T15:38:11.129573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dt.fit(X_train,y_train)\nprediction = dt.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:38:11.131245Z","iopub.execute_input":"2022-07-31T15:38:11.131550Z","iopub.status.idle":"2022-07-31T15:38:11.148421Z","shell.execute_reply.started":"2022-07-31T15:38:11.131524Z","shell.execute_reply":"2022-07-31T15:38:11.147460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(accuracy_score(y_test,prediction))","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:38:11.149960Z","iopub.execute_input":"2022-07-31T15:38:11.150582Z","iopub.status.idle":"2022-07-31T15:38:11.159164Z","shell.execute_reply.started":"2022-07-31T15:38:11.150550Z","shell.execute_reply":"2022-07-31T15:38:11.158096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test,prediction))","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:38:11.160410Z","iopub.execute_input":"2022-07-31T15:38:11.161037Z","iopub.status.idle":"2022-07-31T15:38:11.172354Z","shell.execute_reply.started":"2022-07-31T15:38:11.161002Z","shell.execute_reply":"2022-07-31T15:38:11.171190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nsc = StandardScaler()\nX_train = sc.fit_transform(X_train) \nX_test = sc.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:38:11.174802Z","iopub.execute_input":"2022-07-31T15:38:11.175582Z","iopub.status.idle":"2022-07-31T15:38:11.187334Z","shell.execute_reply.started":"2022-07-31T15:38:11.175547Z","shell.execute_reply":"2022-07-31T15:38:11.186156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Appling all ML classification algorithms..","metadata":{}},{"cell_type":"code","source":"from sklearn.discriminant_analysis import LinearDiscriminantAnalysis\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.naive_bayes import GaussianNB\nfrom sklearn.svm import SVC\n\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.model_selection import StratifiedKFold","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:38:11.188866Z","iopub.execute_input":"2022-07-31T15:38:11.189712Z","iopub.status.idle":"2022-07-31T15:38:11.196412Z","shell.execute_reply.started":"2022-07-31T15:38:11.189679Z","shell.execute_reply":"2022-07-31T15:38:11.195225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models = []\nmodels.append(('LR', LogisticRegression(solver='liblinear', multi_class='ovr')))\nmodels.append(('LDA', LinearDiscriminantAnalysis(solver='svd',shrinkage=None,priors=None,n_components=None,store_covariance=False)))\nmodels.append(('KNN', KNeighborsClassifier(n_neighbors=3,weights='uniform',algorithm='auto',leaf_size=30,p=2,metric='minkowski')))\nmodels.append(('CART', DecisionTreeClassifier()))\nmodels.append(('NB', GaussianNB()))\nmodels.append(('SVM', SVC( )))","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:38:11.199154Z","iopub.execute_input":"2022-07-31T15:38:11.199677Z","iopub.status.idle":"2022-07-31T15:38:11.208812Z","shell.execute_reply.started":"2022-07-31T15:38:11.199599Z","shell.execute_reply":"2022-07-31T15:38:11.207636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Results of all the algo.","metadata":{}},{"cell_type":"code","source":"results = []\nnames = []\nres = []\nfor name, model in models:\n    kfold = StratifiedKFold(n_splits=10, random_state=None)\n    cv_results = cross_val_score(model, X_train, y_train, cv=kfold, scoring='accuracy')\n    results.append(cv_results)\n    names.append(name)\n    res.append(cv_results.mean())\n    print('%s: %f' % (name, cv_results.mean()))","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:38:11.212354Z","iopub.execute_input":"2022-07-31T15:38:11.213220Z","iopub.status.idle":"2022-07-31T15:38:11.512721Z","shell.execute_reply.started":"2022-07-31T15:38:11.213169Z","shell.execute_reply":"2022-07-31T15:38:11.511453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}