"""
# import Libraries
"""
import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
"""
# import DataSource
"""
train= pd.read_csv('../input/titanic/train.csv')
test= pd.read_csv('../input/titanic/test.csv')
train.head()
train.info
train.shape
train['Survived'].value_counts()
"""
# visualization
"""
sns.countplot(data=train,x='Survived')
sns.boxplot(data=train,x='Survived',y='Age')
"""
# Preprocessing
"""
(train.isnull().sum()/len(train))[(train.isnull().sum()/len(train))>0]
train[train['Embarked'].isnull()]
train=train.dropna(axis=0,subset=['Embarked'])
train[train['Embarked'].isnull()]
train.drop(['Name','PassengerId','Ticket','Fare'],axis=1 , inplace=  True)
train.drop('Embarked', axis=1, inplace=True)
train.drop("Cabin",axis=1 , inplace=  True)

train.head()
 train["Age"].fillna(train["Age"].mean(),inplace=True)
train.isnull().sum()
train.loc[train['Sex']=='male','Sex']=1
train.loc[train['Sex']=='female','Sex']=0
"""
# Feature/Label
"""
X=train.drop('Survived' ,axis=1)
y= train['Survived']
"""
# Test&Train Split
"""
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=101)
"""
# Scaling
"""
from sklearn.preprocessing import StandardScaler

X_train
scaler=StandardScaler()
scaler.fit(X_train)
scaler_X_Train=scaler.transform(X_train)
scaler_X_Test=scaler.transform(X_test)
"""
# Create Model
"""
from sklearn.neighbors import KNeighborsClassifier
model=KNeighborsClassifier(n_neighbors=1)
model.fit(scaler_X_Train,y_train)
"""
# Prediction
"""
y_pred=model.predict(scaler_X_Test)
"""
# Evaluation
"""
from sklearn.metrics import accuracy_score,confusion_matrix,classification_report,plot_confusion_matrix,precision_recall_curve,precision_recall_fscore_support,plot_roc_curve
accuracy_score(y_test,y_pred)
confusion_matrix(y_test,y_pred)
plot_confusion_matrix(model,scaler_X_Test,y_test)
print(classification_report(y_test,y_pred))
"""
# Test Error Rate
"""
test_error_rate=[]
for k in range(1,30):
    model=KNeighborsClassifier(n_neighbors=k)
    model.fit(scaler_X_Train,y_train)
    y_pred=model.predict(scaler_X_Test)
    test_error=1-accuracy_score(y_test,y_pred)
    test_error_rate.append(test_error)
    
    
test_error_rate
plt.figure()
plt.plot(range(1,30),test_error_rate,label='Test Error')