"""
**Loading Libraries**
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sb
import missingno as msno
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.model_selection import train_test_split
from xgboost import XGBClassifier
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_classif
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix,accuracy_score,classification_report
import warnings
warnings.filterwarnings('ignore')
x = pd.read_csv('../input/titanic/train.csv')
x
x.describe().T.style.bar(subset=['mean'], color='#A34843').background_gradient(subset=['std'], cmap='cividis_r').background_gradient(subset=['50%'], cmap='cividis')
x.isnull().sum()
mean = x['Age'].mean()
x['Age'] = x['Age'].fillna(mean)
x.isnull().sum()
x.Embarked.describe()
x.Embarked.fillna('S',inplace=True)
x.Cabin.describe()
x.Cabin.fillna('G6',inplace=True)
x.isnull().sum()
"""
**EDA**
"""
msno.matrix(x)
sb.pairplot(x,hue="Survived", palette='flare')
sb.countplot(data = x, x="Sex", palette=["#A939A1","#43C591"], edgecolor="black", lw=3)
plt.figure(figsize=(12,8))
sb.heatmap(x.corr(), cmap='vlag_r', annot=True, linewidth=3)
"""
Here we see "Fare" feature has most negative correlation
"""
"""
**Preprocessing**
"""
y =x["Survived"] 
x = x.drop(['Survived'], axis=1)
le = LabelEncoder()
x.Sex = le.fit_transform(x.Sex)
x.Name = le.fit_transform(x.Name)
x.Embarked = le.fit_transform(x.Embarked)
x.Cabin = le.fit_transform(x.Cabin)
x.Ticket = le.fit_transform(x.Ticket)
fit_feat = SelectKBest(score_func=f_classif)
fit_feat.fit(x,y)
score = pd.DataFrame(fit_feat.scores_, columns=['Score Values'])
score
x
x = x.drop(['PassengerId','Name','Age','SibSp','Parch'], axis=1)
xtrain, xtest, ytrain, ytest = train_test_split(x, y, train_size=.66, random_state=17)
sc = StandardScaler()
xtrain = sc.fit_transform(xtrain)
xtest = sc.fit_transform(xtest)
xgb = XGBClassifier()
xgb.fit(xtrain,ytrain)
"""
**Modeling**
"""
rfc = RandomForestClassifier(n_estimators=400)
rfc.fit(xtrain,ytrain)
ypred = rfc.predict(xtest)
accuracy_score(ypred, ytest)
sb.heatmap(confusion_matrix(ypred,ytest),annot=True, cmap='binary')
print(classification_report(ypred,ytest))
plt.bar(ypred, ytest, color='y')
plt.xlabel("Predicted Value")
plt.ylabel("Tested Value")
plt.title("Accuracy Line")
"""
**Please consider an upvote, if you like this kernel**
"""