text_html_output_1.png
from sklearn.ensemble import RandomForestClassifier

from sklearn.model_selection import cross_val_score, train_test_split

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

data = pd.read_csv('../input/titanic/train.csv')
data_test = pd.read_csv('../input/titanic/test.csv')

X = data[['Pclass', 'Age', 'Sex', 'SibSp', 'Parch', 'Fare', 'Embarked', 'Cabin']]
y = data.Survived

X.isnull().sum()

def impute_age(X):
    mc_age = X[(X.Age <= 18) & (X.Sex == 'male')].Age.mean()
    fc_age = X[(X.Age <= 18) & (X.Sex == 'female')].Age.mean()
    ma_age = X[(X.Age > 18) & (X.Sex == 'male')].Age.mean()
    fa_age = X[(X.Age > 18) & (X.Sex == 'female')].Age.mean()
    for idx, row in X.iterrows():
        if not pd.isna(row.Age):
            continue
        if row.SibSp >= 3:
            if row.Sex == 'male':
                X.loc[idx, 'Age'] = mc_age
            if row.Sex == 'female':
                X.loc[idx, 'Age'] = fc_age
        else:
            if row.Sex == 'male':
                X.loc[idx, 'Age'] = ma_age
            if row.Sex == 'female':
                X.loc[idx, 'Age'] = fa_age

def impute_embarked(X):
    most_common = X.Embarked.mode()
    X.Embarked = [most_common if pd.isna(x) else x for x in X.Embarked]

def impute_cabins(X):
    for a in 'ABCDEFG':
        X[a] = [0 for _ in range(X.shape[0])]
        for i, cabins in enumerate(X.Cabin):
            if pd.isna(cabins):
                continue
            cabins = cabins.split()
            for cabin in cabins:
                if cabin == 'T':
                    continue
                X.loc[i, cabins[0][:1]] = int(cabin[1:] or 0)
    X.drop('Cabin', axis=1, inplace=True)

X.isnull().sum()

def optimize_model(ne, md, trials):
    for i in range(round(ne['low'] / ne['step']), round(ne['high'] / ne['step'])):
        for j in range(round(md['low'] / md['step']), round(md['high'] / md['step'])):
            print(i)
            maes = []
            for k in range(trials):
                X_train, y_train, X_test, y_test = train_test_split(X, y, train_size=0.7, shuffle=True)
                model = RandomForestClassifier(n_estimators=i * ne['step'], max_depth=j * md['step'])
                model.fit(X_train, y_train)
                prediction = model.predict(X_test)
                maes.push(mean_abolute_error(y_test, prediction))
            print(maes)
            print(f"average: {sum(maes) / trials}\nne: {i * ne['step']}\tmd: {j * md['step']}")
optimize_model({'low': 50, 'high': 1000, 'step': 50}, {'low': 50, 'high': 300, 'step': 50}, trials=3)