application_vnd.jupyter.stderr_output_1.png
from sklearn.cross_validation import KFold

from sklearn.metrics import log_loss

from sklearn.preprocessing import LabelEncoder

import numpy as np

import pandas as pd

import pandas as pd

import xgboost as xgb

phone = pd.read_csv('../input/phone_brand_device_model.csv', encoding='utf-8')

gatrain = pd.read_csv('../input/gender_age_train.csv')
gatest = pd.read_csv('../input/gender_age_test.csv')

dup = phone.groupby('device_id').size()
dup = dup[dup > 1]
dup.shape

dup = phone.loc[phone.device_id.isin(dup.index)]
first = dup.groupby('device_id').first()
last = dup.groupby('device_id').last()

phone = phone.drop_duplicates('device_id', keep='first')

lebrand = LabelEncoder().fit(phone.phone_brand)
phone['brand'] = lebrand.transform(phone.phone_brand)
m = phone.phone_brand.str.cat(phone.device_model)
lemodel = LabelEncoder().fit(m)
phone['model'] = lemodel.transform(m)
phone['old_model'] = LabelEncoder().fit_transform(phone.device_model)

train = gatrain.merge(phone)

train['y'] = LabelEncoder().fit_transform(train['group'])

params = {'objective': 'multi:softprob', 'num_class': 12, 'booster': 'gbtree', 'max_depth': 8, 'eval_metric': 'mlogloss', 'eta': 0.02, 'silent': 1, 'alpha': 3}

def encode_cat(Xtrain, Xtest):
    model_age = Xtrain.groupby(['model'])['age'].agg('mean')
    brand_age = Xtrain.groupby(['brand'])['age'].agg('mean')
    Xtest['model_age'] = Xtest['model'].map(model_age)
    Xtrain['model_age'] = Xtrain['model'].map(model_age)
    Xtest['brand_age'] = Xtest['brand'].map(brand_age)
    Xtrain['brand_age'] = Xtrain['brand'].map(brand_age)
    return (Xtrain[['brand', 'model', 'old_model']], Xtest[['brand', 'model', 'old_model']])

y = train['y']
kf = KFold(train.shape[0], n_folds=5, shuffle=True, random_state=1024)
pred = np.zeros((train.shape[0], 12))
for itrain, itest in kf:
    Xtrain = train.ix[itrain,]
    Xtest = train.ix[itest,]
    ytrain, ytest = (y[itrain], y[itest])
    Xtrain, Xtest = encode_cat(Xtrain, Xtest)
    dtrain = xgb.DMatrix(Xtrain, label=ytrain)
    dvalid = xgb.DMatrix(Xtest, label=ytest)
    watchlist = [(dtrain, 'train'), (dvalid, 'eval')]
    gbm = xgb.train(params, dtrain, 600, evals=watchlist, early_stopping_rounds=25, verbose_eval=20)
    temp_pred = gbm.predict(dvalid)
    pred[itest, :] = temp_pred

log_loss(train['y'].values.tolist(), pred)