text_html_output_1.png text_plain_output_1.png
from sklearn import linear_model

from sklearn import preprocessing

from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier

from sklearn.metrics import mean_absolute_error

from sklearn.model_selection import train_test_split

import pandas

import re

TRAIN_PATH = '../input/train.csv'
TEST_PATH = '../input/test.csv'
train = pandas.read_csv(TRAIN_PATH)
test = pandas.read_csv(TEST_PATH)

train.isnull().any()

test.isnull().any()

def deriveTitles(s):
    title = re.search('(?:\\S )(?P<title>\\w*)', s).group('title')
    if title == 'Mr':
        return 'adult'
    elif title == 'Don':
        return 'gentry'
    elif title == 'Dona':
        return 'gentry'
    elif title == 'Miss':
        return 'miss'
    elif title == 'Col':
        return 'military'
    elif title == 'Rev':
        return 'other'
    elif title == 'Lady':
        return 'gentry'
    elif title == 'Master':
        return 'child'
    elif title == 'Mme':
        return 'adult'
    elif title == 'Captain':
        return 'military'
    elif title == 'Dr':
        return 'other'
    elif title == 'Mrs':
        return 'adult'
    elif title == 'Sir':
        return 'gentry'
    elif title == 'Jonkheer':
        return 'gentry'
    elif title == 'Mlle':
        return 'miss'
    elif title == 'Major':
        return 'military'
    elif title == 'Ms':
        return 'miss'
    elif title == 'the Countess':
        return 'gentry'
    else:
        return 'other'
train['title'] = train.Name.apply(deriveTitles)
test['title'] = test.Name.apply(deriveTitles)
le = preprocessing.LabelEncoder()
titles = ['adult', 'gentry', 'miss', 'military', 'other', 'child']
le.fit(titles)
train['encodedTitle'] = le.transform(train['title']).astype('int')
test['encodedTitle'] = le.transform(test['title']).astype('int')

train.Embarked.fillna(value='S', inplace=True)

combined = pandas.concat([train, test])
combined.ParChCategories = combined.Parch > 2

combined = combined.assign(SibSpGroup1=combined['SibSp'] < 2)
combined = combined.assign(SibSpGroup2=combined['SibSp'].between(2, 3, inclusive=True))
combined = combined.assign(SibSpGroup3=combined['SibSp'] > 2)
combined = combined.assign(ParChGT2=combined['Parch'] > 2)

age_train, age_validation = train_test_split(combined[combined.Age.notnull()], test_size=0.2)
age_learn = combined[combined.Age.isnull()]

age_rf = RandomForestRegressor()
age_rf.fit(age_train[['Pclass', 'encodedTitle', 'SibSpGroup1', 'SibSpGroup2', 'SibSpGroup3', 'ParChGT2']], age_train['Age'])
age_validation = age_validation.assign(rf_age=age_rf.predict(age_validation[['Pclass', 'encodedTitle', 'SibSpGroup1', 'SibSpGroup2', 'SibSpGroup3', 'ParChGT2']]))
mean_absolute_error(age_validation['Age'], age_validation['rf_age'], sample_weight=None, multioutput='uniform_average')

age_encoder = preprocessing.OneHotEncoder().fit(combined[['Pclass', 'encodedTitle', 'SibSpGroup1', 'SibSpGroup2', 'SibSpGroup3', 'ParChGT2']])
age_training_encoded = age_encoder.transform(age_train[['Pclass', 'encodedTitle', 'SibSpGroup1', 'SibSpGroup2', 'SibSpGroup3', 'ParChGT2']])
age_validation_encoded = age_encoder.transform(age_validation[['Pclass', 'encodedTitle', 'SibSpGroup1', 'SibSpGroup2', 'SibSpGroup3', 'ParChGT2']])
age_model = linear_model.RidgeCV(alphas=[0.1, 0.2, 0.3, 0.4, 0.5])
age_estimator = age_model.fit(age_training_encoded, age_train['Age'])
linear_age_predictions = age_estimator.predict(age_validation_encoded)
mean_absolute_error(age_validation['Age'], linear_age_predictions, sample_weight=None, multioutput='uniform_average')