image_output_1.png
from collections import Counter

import numpy as np

import pandas as pd

import seaborn as sns

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import string
import os
from collections import Counter
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier, GradientBoostingClassifier, ExtraTreesClassifier, VotingClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV, cross_val_score, StratifiedKFold, learning_curve
from sklearn.preprocessing import StandardScaler, LabelEncoder, OneHotEncoder
from sklearn.pipeline import Pipeline
sns.set(style='white', context='notebook', palette='magma')

markdown_data = pd.read_csv('../input/titanic/train.csv')
final_approval_data = pd.read_csv('../input/titanic/test.csv')
passenger_id_final = final_approval_data['PassengerId']

def detect_outliers(df, n, features):
    """
    Takes a dataframe df of features and returns a list of the indices
    corresponding to the observations containing more than n outliers according
    to the Tukey method.
    """
    outlier_indices = []
    for col in features:
        Q1 = np.percentile(df[col], 25)
        Q3 = np.percentile(df[col], 75)
        IQR = Q3 - Q1
        outlier_step = 1.5 * IQR
        outlier_list_col = df[(df[col] < Q1 - outlier_step) | (df[col] > Q3 + outlier_step)].index
        outlier_indices.extend(outlier_list_col)
    outlier_indices = Counter(outlier_indices)
    multiple_outliers = list((k for k, v in outlier_indices.items() if v > n))
    return multiple_outliers
Outliers_to_drop = detect_outliers(markdown_data, 2, ['Age', 'SibSp', 'Parch', 'Fare'])
markdown_data.loc[Outliers_to_drop]

markdown_data = markdown_data.drop(Outliers_to_drop).reset_index(drop=True)

sns.heatmap(markdown_data.corr(), annot=True)