<jupyter_start><jupyter_text>Import packages<jupyter_code>from sklearn.preprocessing import Normalizer, QuantileTransformer
from sklearn.model_selection import train_test_split, RandomizedSearchCV
from sklearn.ensemble import BaggingClassifier
from sklearn.metrics import classification_report
from time import time
import pandas as pd<jupyter_output><empty_output><jupyter_text>Helper function to get and process the data<jupyter_code>def dataget(train_path, test_path):

    train_data = pd.read_csv(train_path)
    test_data = pd.read_csv(test_path)
    #Join the train and test data to cleanse and enhance the data
    df = train_data.append(test_data, ignore_index=True)
    Titles_Dictionary = {
                        "Capt":         "Officer",
                        "Col":          "Officer",
                        "Major":        "Officer",
                        "Jonkheer":     "Royalty",
                        "Don":          "Royalty",
                        "Sir":          "Royalty",
                        "Dr":           "Officer",
                        "Rev":          "Officer",
                        "the Countess": "Royalty",
                        "Dona":         "Royalty",
                        "Mme":          "Mrs",
                        "Mlle":         "Miss",
                        "Ms":           "Mrs",
                        "Mr":           "Mr",
                        "Mrs":          "Mrs",
                        "Miss":         "Miss",
                        "Master":       "Master",
                        "Lady":         "Royalty"
                        }
    ## Extract Title and map to the Titles from each Name
    df['Title'] = df['Name'].apply(lambda x: Titles_Dictionary[x.split(',')[1].split('.')[0].strip()])
    ## Fill missing Embarked with 'C'
    df['Embarked'].fillna('C', inplace=True)
    ## Note down the Imputed Ages
    df['Imputed'] = df['Age'].isnull().astype('uint8')
    columns = ['Age','Fare']
    groups = ['Title', 'Embarked']
    ## Fill null Ages with the mean Age based on Title, Embarked
    df[columns] = df.groupby(groups)[columns].transform(lambda x: x.fillna(x.mean()))
    ## Convert to categorical data
    categories = ['Title', 'Sex', 'Pclass', 'SibSp', 'Parch', 'Embarked']
    df[categories] = df[categories].apply(lambda x: x.astype('category'))
    df = df.drop(columns=['Cabin', 'Name', 'Ticket'])
    #df = df.drop(columns=['Title', 'SibSp', 'Imputed', 'Pclass', 'Parch', 'Embarked', 'Fare'])
    df = df.round(2)
    original = df.copy()
    df = pd.get_dummies(df, drop_first=True)
    test_data = df[df.Survived.isnull()].copy()
    test_data = test_data.drop(columns=['Survived'])
    train_data = df.dropna().copy()
    train_data['Survived'] = train_data['Survived'].astype('uint8')
    train_data = train_data.drop(columns=['PassengerId'])

    return original, train_data, test_data
train_path = 'train.csv'
test_path = 'test.csv'
original, train_data, test_data = dataget(train_path, test_path)
df = original.copy()
df.drop(columns=['PassengerId'], inplace=True)
#df.dropna(inplace=True)
print(df.head(10))
print(df.describe())
print(df.info())
search = True
test_size = 0.2
normalizer = Normalizer(norm='l1')
df = train_data
train = df.dropna()
y_train = train['Survived'].values
x_train = train.drop(columns=['Survived']).values
quantile_transformer = QuantileTransformer(output_distribution='normal')
X_train = normalizer.fit_transform(x_train)
#X_train = quantile_transformer.fit_transform(x_train)
clf = BaggingClassifier()
param_dist = {"n_estimators": list(range(1,100)),
              "max_samples": [element / 100 for element in range(50,101)],
              "max_features": [element / 100 for element in range(50,101)],
              "bootstrap": [True, False]
             }
n_iter_search = 500

if search:
    random_search = RandomizedSearchCV(clf, param_distributions=param_dist, n_jobs =-1,
                                       n_iter=n_iter_search, cv=10, scoring='accuracy', random_state=42)

    start = time()
    random_search.fit(X_train, y_train)

    print("RandomizedSearchCV took %.2f seconds for %d candidates"
          " parameter settings." % ((time() - start), n_iter_search))

    print(random_search.best_score_)
    print(random_search.best_params_)
#http://scikit-learn.org/stable/auto_examples/preprocessing/plot_all_scaling.html#sphx-glr-auto-examples-preprocessing-plot-all-scaling-py
#params = {'n_estimators': 13, 'max_depth': 12, 'criterion': 'entropy', 'bootstrap': True} quantile
params = {'n_estimators': 92, 'max_samples': 0.58, 'max_features': 0.91, 'bootstrap': True, 'random_state':42}
clf = BaggingClassifier(**params)
clf.fit(X_train, y_train)
y_true, y_pred = y_train, clf.predict(X_train)
print(classification_report(y_true, y_pred))
df = test_data
resultdf = pd.DataFrame(data=df['PassengerId'])
df = df.drop(columns=['PassengerId'])
#test_x = quantile_transformer.transform(df)
test_x = normalizer.transform(df)
predictions = clf.predict(test_x)
resultdf['Survived'] = predictions.astype(int)

resultdf.to_csv('submission.csv', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14057942834921253, 'fraction_numerical': 0.010305269298075053, 'mean_word_length': 2.882264150943396, 'pattern_counts': {'":': 22, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '165985', 'license': 'MIT', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1617, 'n_words': 326, 'path': 'Kaggle/Titanic/Bagging Classifier.ipynb', 'repo_name': 'M-e-r-c-u-r-y/Machine-Learning', 'size': '5143'}